aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-06-24 21:02:02 +0200
committerGitHub <noreply@github.com>2022-06-24 21:02:02 +0200
commit70de558627fec9a2b89567d4357dc026503a0ae8 (patch)
tree76918de77a7bdcf7fa08eeca30f3532cd682a619
parent4a53c233ad4f4d6ce2b8afa0873d9596c9c13e99 (diff)
parentd22e15a6b577c3a716bede6c1af71c0b10abc087 (diff)
Merge pull request #23235 from vespa-engine/toregge/use-std-filesystem-copy-filev8.8.35
Use std::filesystem::copy_file. Remove FastOS_FileInterface::CopyFile.
-rw-r--r--fastos/src/tests/filetest.cpp111
-rw-r--r--fastos/src/vespa/fastos/file.cpp53
-rw-r--r--fastos/src/vespa/fastos/file.h10
-rw-r--r--searchcore/src/vespa/searchcorespi/index/indexwriteutilities.cpp7
4 files changed, 6 insertions, 175 deletions
diff --git a/fastos/src/tests/filetest.cpp b/fastos/src/tests/filetest.cpp
index 88e81c2bbaf..d0f8bbfd98b 100644
--- a/fastos/src/tests/filetest.cpp
+++ b/fastos/src/tests/filetest.cpp
@@ -7,38 +7,6 @@
#include <sys/mman.h>
#include <filesystem>
-namespace {
-
-// Create the named file, and write it's filename into it.
-// Return true on success
-bool createFile(const char* fileName) {
- FastOS_StatInfo statInfo;
- FastOS_File cf(fileName);
- return ( cf.OpenWriteOnly() &&
- cf.CheckedWrite(fileName, strlen(fileName)) &&
- cf.Close() &&
- FastOS_File::Stat(fileName, &statInfo) &&
- statInfo._isRegular );
-}
-
-bool createFile(const char* fileName,
- const unsigned int size) {
- FastOS_File cf(fileName);
- bool success = false;
- if (cf.OpenWriteOnlyTruncate()) {
- auto buf = std::make_unique<char[]>(size); // Could be dangerous..
- if (buf) {
- memset(buf.get(), 0, size); // dont write uninitialized bytes since valgrind will complain
- if (cf.CheckedWrite(buf.get(), size)) {
- success = true;
- }
- }
- }
- return success;
-}
-} // namespace <unnamed>
-
-
class FileTest : public BaseTest
{
public:
@@ -546,90 +514,11 @@ public:
PrintSeparator();
}
- void CopyFileTest ()
- {
- FastOS_StatInfo statInfo;
- TestHeader("CopyFile Test");
- const char *dirName = "tmpDir";
- char file1[1024];
- char file2[1024];
- char file3[1024];
- char file4[1024];
- char file5[1024];
- sprintf(file1, "%s%sfile1", dirName, FastOS_File::GetPathSeparator());
- sprintf(file2, "%s%sfile2", dirName, FastOS_File::GetPathSeparator());
- sprintf(file3, "%s%sfile3", dirName, FastOS_File::GetPathSeparator());
- sprintf(file4, "%s%sfile4", dirName, FastOS_File::GetPathSeparator());
- sprintf(file5, "%s%sfile5", dirName, FastOS_File::GetPathSeparator());
-
- std::filesystem::remove_all(std::filesystem::path(dirName));
- std::filesystem::create_directory(std::filesystem::path(dirName));
- printf("Creating files to copy. Some of them are quite large...\n\n");
- createFile(file1);
- createFile(file3, 20*1024*1024); // 20MB file.
- createFile(file4, 1024*1024); // 1MB file, i.e. exact size of buffer.
- createFile(file5, 1024*1024 + 100); // 1.001MB file
-
- FastOS_File::Stat(file4, &statInfo);
- unsigned int sizeOfFile4 = statInfo._size;
-
- FastOS_File::Stat(file5, &statInfo);
- unsigned int sizeOfFile5 = statInfo._size;
-
- // Tests start here.
- bool copyOK = FastOS_File::CopyFile(file1, file2);
- Progress(copyOK,
- "File copy from %s to %s.", file1, file2);
-
- FastOS_File::Delete(file2);
- copyOK = FastOS_File::CopyFile(file3, file2);
- Progress(copyOK,
- "File copy from %s to %s.", file3, file2);
- FastOS_File::Stat(file2, &statInfo);
- Progress(statInfo._size == 20*1024*1024,
- "Size of copied file is 20MB.");
-
- copyOK = FastOS_File::CopyFile(file3, file3);
- Progress(!copyOK,
- "File copy onto itself should fail.");
-
- FastOS_File::Delete(file1);
- copyOK = FastOS_File::CopyFile(file1, file2);
- Progress(!copyOK,
- "File copy of a missing file should fail.");
-
- copyOK = FastOS_File::CopyFile(file4, file2);
- Progress(copyOK,
- "Copying a smaller file onto a larger one.");
- FastOS_File::Stat(file2, &statInfo);
- Progress(statInfo._size == sizeOfFile4,
- "Size of copied file should be %u bytes.", sizeOfFile4);
-
- copyOK = FastOS_File::CopyFile(file4, file1);
- Progress(copyOK,
- "Copying a file with exact size of buffer.");
- FastOS_File::Stat(file1, &statInfo);
- Progress(statInfo._size == sizeOfFile4,
- "Size of copied file should be %u bytes.", sizeOfFile4);
-
- copyOK = FastOS_File::CopyFile(file5, file1);
- Progress(copyOK,
- "Copying a file with size %u bytes.", sizeOfFile5);
- FastOS_File::Stat(file1, &statInfo);
- Progress(statInfo._size == sizeOfFile5,
- "Size of copied file should be %u bytes.", sizeOfFile5);
-
-
- std::filesystem::remove_all(std::filesystem::path("tmpDir"));
- PrintSeparator();
- }
-
int Main () override
{
printf("This test should be run in the 'tests' directory.\n\n");
printf("grep for the string '%s' to detect failures.\n\n", failString);
- CopyFileTest();
GetCurrentDirTest();
DirectIOTest();
MaxLengthTest();
diff --git a/fastos/src/vespa/fastos/file.cpp b/fastos/src/vespa/fastos/file.cpp
index 2857f45e3de..fdbacb570b4 100644
--- a/fastos/src/vespa/fastos/file.cpp
+++ b/fastos/src/vespa/fastos/file.cpp
@@ -207,59 +207,6 @@ FastOS_FileInterface::IsMemoryMapped() const
return false;
}
-bool
-FastOS_FileInterface::CopyFile( const char *src, const char *dst )
-{
- FastOS_File s, d;
- FastOS_StatInfo statInfo;
- bool success = false;
-
- if ( src != nullptr &&
- dst != nullptr &&
- strcmp(src, dst) != 0 &&
- FastOS_File::Stat( src, &statInfo )) {
-
- if ( s.OpenReadOnly( src ) && d.OpenWriteOnlyTruncate( dst ) ) {
-
- unsigned int bufSize = 1024*1024;
- int64_t bufSizeBound = statInfo._size;
- if (bufSizeBound < 1)
- bufSizeBound = 1;
- if (bufSizeBound < static_cast<int64_t>(bufSize))
- bufSize = static_cast<unsigned int>(bufSizeBound);
- char *tmpBuf = new char[ bufSize ];
-
- if ( tmpBuf != nullptr ) {
- int64_t copied = 0;
- success = true;
- do {
- unsigned int readBytes = s.Read( tmpBuf, bufSize );
- if (readBytes > 0) {
- ssize_t written = d.Write2(tmpBuf, readBytes);
- if ( written != readBytes) {
- success = false;
- }
- copied += readBytes;
- } else {
- // Could not read from src.
- success = false;
- }
- } while (copied < statInfo._size && success);
-
- delete [] tmpBuf;
- } // else out of memory ?
-
- bool close_ok = s.Close();
- assert(close_ok);
- close_ok = d.Close();
- assert(close_ok);
- } // else Could not open source or destination file.
- } // else Source file does not exist, or input args are invalid.
-
- return success;
-}
-
-
void
FastOS_FileInterface::SetFileName(const char *filename)
{
diff --git a/fastos/src/vespa/fastos/file.h b/fastos/src/vespa/fastos/file.h
index b52b7e787fa..34b28b7cc31 100644
--- a/fastos/src/vespa/fastos/file.h
+++ b/fastos/src/vespa/fastos/file.h
@@ -99,16 +99,6 @@ public:
void setFAdviseOptions(int options) { _fAdviseOptions = options; }
/**
- * Copy a single file. Will overwrite destination if it already exists.
- *
- * @author Sveinar Rasmussen
- * @return success/failure
- * @param src a 'const char *' value with the file to copy from
- * @param dst a 'const char *' value with the name of the resulting copy
- */
- static bool CopyFile( const char *src, const char *dst );
-
- /**
* Return path separator string. This will yield "/" on UNIX systems.
* @return pointer to path separator character string
*/
diff --git a/searchcore/src/vespa/searchcorespi/index/indexwriteutilities.cpp b/searchcore/src/vespa/searchcorespi/index/indexwriteutilities.cpp
index cc2575f74d2..54d2cf6e1f5 100644
--- a/searchcore/src/vespa/searchcorespi/index/indexwriteutilities.cpp
+++ b/searchcore/src/vespa/searchcorespi/index/indexwriteutilities.cpp
@@ -7,7 +7,9 @@
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/util/exceptions.h>
+#include <filesystem>
#include <sstream>
+#include <system_error>
#include <unistd.h>
#include <vespa/log/log.h>
@@ -80,7 +82,10 @@ IndexWriteUtilities::copySerialNumFile(const vespalib::string &sourceDir,
vespalib::string source = IndexDiskLayout::getSerialNumFileName(sourceDir);
vespalib::string dest = IndexDiskLayout::getSerialNumFileName(destDir);
vespalib::string tmpDest = dest + ".tmp";
- if (!FastOS_FileInterface::CopyFile(source.c_str(), tmpDest.c_str())) {
+ std::error_code ec;
+
+ std::filesystem::copy_file(std::filesystem::path(source), std::filesystem::path(tmpDest), ec);
+ if (ec) {
LOG(error, "Unable to copy file '%s'", source.c_str());
return false;
}