aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@online.no>2023-07-20 10:53:26 +0200
committerTor Egge <Tor.Egge@online.no>2023-07-20 10:53:26 +0200
commit40fe7f7d9d62ec24eb155a97a0a58f47a7ef91a0 (patch)
tree6540a5b9d76bf00f1e2c528d9a9d7962946c6833
parentcc7d61d8bab95d7b7225712e94758861596946ae (diff)
Remove vespalib::copy and vespalib::rename.
-rw-r--r--vespalib/src/tests/io/fileutil/fileutiltest.cpp127
-rw-r--r--vespalib/src/vespa/vespalib/io/fileutil.cpp91
-rw-r--r--vespalib/src/vespa/vespalib/io/fileutil.h30
3 files changed, 0 insertions, 248 deletions
diff --git a/vespalib/src/tests/io/fileutil/fileutiltest.cpp b/vespalib/src/tests/io/fileutil/fileutiltest.cpp
index 4eb700fd4ed..5a62e257032 100644
--- a/vespalib/src/tests/io/fileutil/fileutiltest.cpp
+++ b/vespalib/src/tests/io/fileutil/fileutiltest.cpp
@@ -235,133 +235,6 @@ TEST("require that vespalib::unlink works")
}
}
-TEST("require that vespalib::rename works")
-{
- std::filesystem::remove_all(std::filesystem::path("mydir"));
- File f("myfile");
- f.open(File::CREATE | File::TRUNC);
- f.write("Hello World!\n", 13, 0);
- f.close();
- // Renaming to non-existing dir doesn't work
- try{
- rename("myfile", "mydir/otherfile");
- TEST_FATAL("This shouldn't work when mydir doesn't exist");
- } catch (IoException& e) {
- //std::cerr << e.what() << "\n";
- EXPECT_EQUAL(IoException::NOT_FOUND, e.getType());
- }
- // Renaming to non-existing dir works if autocreating dirs
- {
- ASSERT_TRUE(rename("myfile", "mydir/otherfile", true, true));
- ASSERT_TRUE(!fileExists("myfile"));
- ASSERT_TRUE(fileExists("mydir/otherfile"));
-
- File f2("mydir/otherfile");
- f2.open(File::READONLY);
- std::vector<char> vec(20, ' ');
- size_t read = f2.read(&vec[0], 20, 0);
- EXPECT_EQUAL(13u, read);
- EXPECT_EQUAL(std::string("Hello World!\n"), std::string(&vec[0], 13));
- }
- // Renaming non-existing returns false
- ASSERT_TRUE(!rename("myfile", "mydir/otherfile", true));
- // Rename to overwrite works
- {
- f.open(File::CREATE | File::TRUNC);
- f.write("Bah\n", 4, 0);
- f.close();
- ASSERT_TRUE(rename("myfile", "mydir/otherfile", true, true));
-
- File f2("mydir/otherfile");
- f2.open(File::READONLY);
- std::vector<char> vec(20, ' ');
- size_t read = f2.read(&vec[0], 20, 0);
- EXPECT_EQUAL(4u, read);
- EXPECT_EQUAL(std::string("Bah\n"), std::string(&vec[0], 4));
- }
- // Overwriting directory fails (does not put inside dir)
- try{
- std::filesystem::create_directory(std::filesystem::path("mydir"));
- f.open(File::CREATE | File::TRUNC);
- f.write("Bah\n", 4, 0);
- f.close();
- ASSERT_TRUE(rename("myfile", "mydir"));
- } catch (IoException& e) {
- //std::cerr << e.what() << "\n";
- EXPECT_EQUAL(IoException::ILLEGAL_PATH, e.getType());
- }
- // Moving directory works
- {
- ASSERT_TRUE(isDirectory("mydir"));
- std::filesystem::remove_all(std::filesystem::path("myotherdir"));
- ASSERT_TRUE(rename("mydir", "myotherdir"));
- ASSERT_TRUE(isDirectory("myotherdir"));
- ASSERT_TRUE(!isDirectory("mydir"));
- ASSERT_TRUE(!rename("mydir", "myotherdir"));
- }
- // Overwriting directory fails
- try{
- File f2("mydir/yetanotherfile");
- f2.open(File::CREATE, true);
- f2.write("foo", 3, 0);
- f2.open(File::READONLY);
- f2.close();
- rename("mydir", "myotherdir");
- TEST_FATAL("Should fail trying to overwrite directory");
- } catch (IoException& e) {
- //std::cerr << e.what() << "\n";
- EXPECT_TRUE((IoException::DIRECTORY_HAVE_CONTENT == e.getType()) ||
- (IoException::ALREADY_EXISTS == e.getType()));
- }
-}
-
-TEST("require that vespalib::copy works")
-{
- std::filesystem::remove_all(std::filesystem::path("mydir"));
- File f("myfile");
- f.open(File::CREATE | File::TRUNC);
-
- MallocAutoPtr buffer = getAlignedBuffer(5000);
- memset(buffer.get(), 0, 5000);
- strncpy(static_cast<char*>(buffer.get()), "Hello World!\n", 14);
- f.write(buffer.get(), 4_Ki, 0);
- f.close();
- std::cerr << "Simple copy\n";
- // Simple copy works (4096b dividable file)
- copy("myfile", "targetfile");
- ASSERT_TRUE(system("diff myfile targetfile") == 0);
- std::cerr << "Overwriting\n";
- // Overwriting works (may not be able to use direct IO writing on all
- // systems, so will always use cached IO)
- {
- f.open(File::CREATE | File::TRUNC);
- f.write("Bah\n", 4, 0);
- f.close();
-
- ASSERT_TRUE(system("diff myfile targetfile > /dev/null") != 0);
- copy("myfile", "targetfile");
- ASSERT_TRUE(system("diff myfile targetfile > /dev/null") == 0);
- }
- // Fails if target is directory
- try{
- std::filesystem::create_directory(std::filesystem::path("mydir"));
- copy("myfile", "mydir");
- TEST_FATAL("Should fail trying to overwrite directory");
- } catch (IoException& e) {
- //std::cerr << e.what() << "\n";
- EXPECT_EQUAL(IoException::ILLEGAL_PATH, e.getType());
- }
- // Fails if source is directory
- try{
- std::filesystem::create_directory(std::filesystem::path("mydir"));
- copy("mydir", "myfile");
- TEST_FATAL("Should fail trying to copy directory");
- } catch (IoException& e) {
- //std::cerr << e.what() << "\n";
- EXPECT_EQUAL(IoException::ILLEGAL_PATH, e.getType());
- }
-}
-
TEST("require that copy constructor and assignment for vespalib::File works")
{
// Copy file not opened.
diff --git a/vespalib/src/vespa/vespalib/io/fileutil.cpp b/vespalib/src/vespa/vespalib/io/fileutil.cpp
index ff39e56f000..491e07e2491 100644
--- a/vespalib/src/vespa/vespalib/io/fileutil.cpp
+++ b/vespalib/src/vespa/vespalib/io/fileutil.cpp
@@ -523,103 +523,12 @@ unlink(const string & filename)
return true;
}
-bool
-rename(const string & frompath, const string & topath,
- bool copyDeleteBetweenFilesystems, bool createTargetDirectoryIfMissing)
-{
- LOG(spam, "rename(%s, %s): Renaming file%s.",
- frompath.c_str(), topath.c_str(),
- createTargetDirectoryIfMissing
- ? " recursively creating target directory if missing" : "");
- if (::rename(frompath.c_str(), topath.c_str()) != 0) {
- if (errno == ENOENT) {
- if (!fileExists(frompath)) return false;
- if (createTargetDirectoryIfMissing) {
- string::size_type pos = topath.rfind('/');
- if (pos != string::npos) {
- string path(topath.substr(0, pos));
- std::filesystem::create_directories(std::filesystem::path(path));
- LOG(debug, "rename(%s, %s): Created target directory. Calling recursively.",
- frompath.c_str(), topath.c_str());
- return rename(frompath, topath, copyDeleteBetweenFilesystems, false);
- }
- } else {
- asciistream ost;
- ost << "rename(" << frompath << ", " << topath
- << (copyDeleteBetweenFilesystems ? ", revert to copy" : "")
- << (createTargetDirectoryIfMissing
- ? ", create missing target" : "")
- << "): Failed, target path does not exist.";
- throw IoException(ost.str(), IoException::NOT_FOUND,
- VESPA_STRLOC);
- }
- } else if (errno == EXDEV && copyDeleteBetweenFilesystems) {
- if (!fileExists(frompath)) {
- LOG(debug, "rename(%s, %s): Renaming non-existing file across "
- "filesystems returned EXDEV rather than ENOENT.",
- frompath.c_str(), topath.c_str());
- return false;
- }
- LOG(debug, "rename(%s, %s): Cannot rename across filesystems. "
- "Copying and deleting instead.",
- frompath.c_str(), topath.c_str());
- copy(frompath, topath, createTargetDirectoryIfMissing);
- unlink(frompath);
- return true;
- }
- asciistream ost;
- ost << "rename(" << frompath << ", " << topath
- << (copyDeleteBetweenFilesystems ? ", revert to copy" : "")
- << (createTargetDirectoryIfMissing ? ", create missing target" : "")
- << "): Failed, errno(" << errno << "): " << safeStrerror(errno);
- throw IoException(ost.str(), IoException::getErrorType(errno),
- VESPA_STRLOC);
- }
- LOG(debug, "rename(%s, %s): Renamed.", frompath.c_str(), topath.c_str());
- return true;
-}
-
namespace {
- uint32_t bufferSize = 1_Mi;
uint32_t diskAlignmentSize = 4_Ki;
}
-void
-copy(const string & frompath, const string & topath,
- bool createTargetDirectoryIfMissing, bool useDirectIO)
-{
- // Get aligned buffer, so it works with direct IO
- LOG(spam, "copy(%s, %s): Copying file%s.",
- frompath.c_str(), topath.c_str(),
- createTargetDirectoryIfMissing
- ? " recursively creating target directory if missing" : "");
- MallocAutoPtr buffer(getAlignedBuffer(bufferSize));
-
- File source(frompath);
- File target(topath);
- source.open(File::READONLY | (useDirectIO ? File::DIRECTIO : 0));
- size_t sourceSize = source.getFileSize();
- if (useDirectIO && sourceSize % diskAlignmentSize != 0) {
- LOG(warning, "copy(%s, %s): Cannot use direct IO to write new file, "
- "as source file has size %zu, which is not "
- "dividable by the disk alignment size of %u.",
- frompath.c_str(), topath.c_str(), sourceSize, diskAlignmentSize);
- useDirectIO = false;
- }
- target.open(File::CREATE | File::TRUNC | (useDirectIO ? File::DIRECTIO : 0),
- createTargetDirectoryIfMissing);
- off_t offset = 0;
- for (;;) {
- size_t bytesRead = source.read(buffer.get(), bufferSize, offset);
- target.write(buffer.get(), bytesRead, offset);
- if (bytesRead < bufferSize) break;
- offset += bytesRead;
- }
- LOG(debug, "copy(%s, %s): Completed.", frompath.c_str(), topath.c_str());
-}
-
DirectoryList
listDirectory(const string & path)
{
diff --git a/vespalib/src/vespa/vespalib/io/fileutil.h b/vespalib/src/vespa/vespalib/io/fileutil.h
index 7d9e51532d0..faffc739720 100644
--- a/vespalib/src/vespa/vespalib/io/fileutil.h
+++ b/vespalib/src/vespa/vespalib/io/fileutil.h
@@ -334,36 +334,6 @@ extern vespalib::string readLink(const vespalib::string & path);
extern bool unlink(const vespalib::string & filename);
/**
- * Rename the file at frompath to topath.
- *
- * @param frompath old name of file.
- * @param topath new name of file.
- *
- * @param copyDeleteBetweenFilesystems whether a copy-and-delete
- * operation should be performed if rename crosses a file system
- * boundary, or not.
- *
- * @param createTargetDirectoryIfMissing whether the target directory
- * should be created if it's missing, or not.
- *
- * @throw IoException If we failed to rename the file.
- * @throw std::filesystem::filesystem_error If we failed to create a target directory
- * @return True if file was renamed, false if frompath did not exist.
- */
-extern bool rename(const vespalib::string & frompath,
- const vespalib::string & topath,
- bool copyDeleteBetweenFilesystems = true,
- bool createTargetDirectoryIfMissing = false);
-
-/**
- * Copies a file to a destination using Direct IO.
- */
-extern void copy(const vespalib::string & frompath,
- const vespalib::string & topath,
- bool createTargetDirectoryIfMissing = false,
- bool useDirectIO = true);
-
-/**
* List the contents of the given directory.
*/
using DirectoryList = std::vector<vespalib::string>;