diff options
author | Tor Egge <Tor.Egge@online.no> | 2022-06-24 13:45:01 +0200 |
---|---|---|
committer | Tor Egge <Tor.Egge@online.no> | 2022-06-24 13:45:01 +0200 |
commit | 54741a65578269abf9cd515f4246320162e8713e (patch) | |
tree | b65d1c1ee87ec0b5c6bec0b8dfeaa8c572a75f3f /fastos | |
parent | a9249c4629131fa2bcf3c6e68793d455af812e93 (diff) |
Remove unused code for creating / removing directories and
for recursive directory traversal.
Diffstat (limited to 'fastos')
-rw-r--r-- | fastos/src/tests/filetest.cpp | 195 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/file.cpp | 78 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/file.h | 47 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_file.cpp | 18 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_file.h | 2 |
5 files changed, 12 insertions, 328 deletions
diff --git a/fastos/src/tests/filetest.cpp b/fastos/src/tests/filetest.cpp index a3470810e2e..88e81c2bbaf 100644 --- a/fastos/src/tests/filetest.cpp +++ b/fastos/src/tests/filetest.cpp @@ -5,6 +5,7 @@ #include <memory> #include <cassert> #include <sys/mman.h> +#include <filesystem> namespace { @@ -46,176 +47,6 @@ public: const std::string woFilename = "generated/writeonlytest.txt"; const std::string rwFilename = "generated/readwritetest.txt"; - void DirectoryTest() - { - TestHeader ("Directory management (remove & empty) test"); - - const char *dirName = "tmpTestDir"; - char file1[1024]; - char file2[1024]; - char file3[1024]; - char file4[1024]; - char file5[1024]; - char subdir1[512]; - char subdir2[512]; - sprintf(file1, "%s%sfile1", dirName, FastOS_File::GetPathSeparator()); - sprintf(file2, "%s%sfile2", dirName, FastOS_File::GetPathSeparator()); - sprintf(file3, "%s%sfile2", dirName, FastOS_File::GetPathSeparator()); - sprintf(subdir1, "%s%sdir1", dirName, FastOS_File::GetPathSeparator()); - sprintf(subdir2, "%s%sdir2", dirName, FastOS_File::GetPathSeparator()); - sprintf(file4, "%s%sfile4", subdir2, FastOS_File::GetPathSeparator()); - sprintf(file5, "%s%sfile5", subdir2, FastOS_File::GetPathSeparator()); - - FastOS_StatInfo statInfo; - - bool success = false; - - // Don't run at all if the directory already exists - assert(!FastOS_File::Stat(dirName, &statInfo)); - - FastOS_File::MakeDirectory(dirName); - - // Verify that we succeed with an empty directory - FastOS_File::EmptyDirectory(dirName); - success = FastOS_File::Stat(dirName, &statInfo); - Progress(success, "Removing empty directory."); - - // Verify that we can empty a directory with files in it - createFile(file1); - createFile(file2); - createFile(file3); - FastOS_File::EmptyDirectory(dirName); - success = - !FastOS_File::Stat(file1, &statInfo) && - !FastOS_File::Stat(file2, &statInfo) && - !FastOS_File::Stat(file3, &statInfo) && - FastOS_File::Stat(dirName, &statInfo); - Progress(success, "Deleting dir with files in it."); - - // Verify that we can empty a directory with files and directories in it - createFile(file1); - createFile(file2); - createFile(file3); - FastOS_File::MakeDirectory(subdir1); - FastOS_File::MakeDirectory(subdir2); - createFile(file4); - createFile(file5); - FastOS_File::EmptyDirectory(dirName); - success = FastOS_File::Stat(dirName, &statInfo) && - !FastOS_File::Stat(file1, &statInfo) && - !FastOS_File::Stat(file2, &statInfo) && - !FastOS_File::Stat(file3, &statInfo) && - !FastOS_File::Stat(file4, &statInfo) && - !FastOS_File::Stat(file5, &statInfo) && - !FastOS_File::Stat(subdir1, &statInfo) && - !FastOS_File::Stat(subdir2, &statInfo); - Progress(success, "Emptying directory with files and folders in it."); - - // Verify that we don't empty the directory if we find a file to keep - createFile(file1); - createFile(file2); - createFile(file3); - FastOS_File::MakeDirectory(subdir1); - FastOS_File::MakeDirectory(subdir2); - createFile(file4); - createFile(file5); - FastOS_File::EmptyDirectory(dirName, "file1"); - success = FastOS_File::Stat(dirName, &statInfo); - Progress(success, "Emptying dir with keepfile in it."); - // Verify that all but the file to keep are removed - success = FastOS_File::Stat(file1, &statInfo) && - !FastOS_File::Stat(file2, &statInfo) && - !FastOS_File::Stat(file3, &statInfo) && - !FastOS_File::Stat(file4, &statInfo) && - !FastOS_File::Stat(file5, &statInfo) && - !FastOS_File::Stat(subdir1, &statInfo) && - !FastOS_File::Stat(subdir2, &statInfo); - Progress(success, "Looking for keepfile."); - - // Verify that we don't empty the sub-directory if we find a file to keep - createFile(file1); - createFile(file2); - createFile(file3); - FastOS_File::MakeDirectory(subdir1); - FastOS_File::MakeDirectory(subdir2); - createFile(file4); - createFile(file5); - FastOS_File::EmptyDirectory(dirName, "file4"); - success = FastOS_File::Stat(dirName, &statInfo); - Progress(success, "Emptying file with nested keepfile."); - // Verify that all but the file to keep are removed - success = !FastOS_File::Stat(file1, &statInfo) && - !FastOS_File::Stat(file2, &statInfo) && - !FastOS_File::Stat(file3, &statInfo) && - FastOS_File::Stat(file4, &statInfo) && - !FastOS_File::Stat(file5, &statInfo) && - !FastOS_File::Stat(subdir1, &statInfo) && - FastOS_File::Stat(subdir2, &statInfo); - // Progress(success, "Looking for nested keepfile."); // Unsupported for now. - - - FastOS_File::EmptyAndRemoveDirectory(dirName); - - FastOS_File::MakeDirectory(dirName); - - // Verify that we can remove an empty directory - FastOS_File::EmptyAndRemoveDirectory(dirName); - success = !FastOS_File::Stat(dirName, &statInfo); - Progress(success, "Deleting empty directory."); - - // Verify that we can remove a directory with files in it - FastOS_File::MakeDirectory(dirName); - createFile(file1); - createFile(file2); - createFile(file3); - FastOS_File::EmptyAndRemoveDirectory(dirName); - success = !FastOS_File::Stat(dirName, &statInfo); - Progress(success, "Deleting a directory with files in it."); - - // Verify that we can remove a directory with files and directories in it - FastOS_File::MakeDirectory(dirName); - createFile(file1); - createFile(file2); - createFile(file3); - FastOS_File::MakeDirectory(subdir1); - FastOS_File::MakeDirectory(subdir2); - createFile(file4); - createFile(file5); - FastOS_File::EmptyAndRemoveDirectory(dirName); - success = !FastOS_File::Stat(dirName, &statInfo); - Progress(success, "Deleting directory with files and directories in it."); - - } - - void MoveFileTest() { - TestHeader ("Moving files (across volumes too) test"); - - const char *dirName = "tmpTestDir"; - char file1[1024]; - char file2[1024]; - char file3[1024]; - sprintf(file1, "%s%sfile1", dirName, FastOS_File::GetPathSeparator()); - sprintf(file2, "%s%sfile2", dirName, FastOS_File::GetPathSeparator()); - sprintf(file3, "%stmp%sfile3", FastOS_File::GetPathSeparator(), - FastOS_File::GetPathSeparator()); - - FastOS_File::MakeDirectory(dirName); - createFile(file1); - - FastOS_StatInfo statInfo; - // Move file to new name in same dir. - FastOS_File::MoveFile(file1, file2); - Progress(FastOS_File::Stat(file2, &statInfo), "Moving one within a directory."); - - // Move file to /tmp. - FastOS_File::MoveFile(file2, file3); - Progress(FastOS_File::Stat(file3, &statInfo), "Moving to /tmp/"); - - // Clean up - FastOS_File::Delete(file3); - FastOS_File::EmptyAndRemoveDirectory(dirName); - } - void GetCurrentDirTest () { TestHeader ("Get Current Directory Test"); @@ -252,7 +83,7 @@ public: int i; const int bufSize = 1000; - FastOS_File::MakeDirectory("generated"); + std::filesystem::create_directory(std::filesystem::path("generated")); FastOS_File file("generated/memorymaptest"); bool rc = file.OpenReadWrite(); @@ -294,7 +125,7 @@ public: } delete [] buffer; } - FastOS_File::EmptyAndRemoveDirectory("generated"); + std::filesystem::remove_all(std::filesystem::path("generated")); PrintSeparator(); } @@ -305,7 +136,7 @@ public: int i; const int bufSize = 40000; - FastOS_File::MakeDirectory("generated"); + std::filesystem::create_directory(std::filesystem::path("generated")); FastOS_File file("generated/diotest"); bool rc = file.OpenWriteOnly(); @@ -429,7 +260,7 @@ public: delete [] buffer; } - FastOS_File::EmptyAndRemoveDirectory("generated"); + std::filesystem::remove_all(std::filesystem::path("generated")); PrintSeparator(); } @@ -483,7 +314,7 @@ public: void WriteOnlyTest () { TestHeader("Write-Only Test"); - FastOS_File::MakeDirectory("generated"); + std::filesystem::create_directory(std::filesystem::path("generated")); FastOS_File *myFile = new FastOS_File(woFilename.c_str()); @@ -541,14 +372,14 @@ public: Progress(deleteResult, "Delete file '%s'.", woFilename.c_str()); delete(myFile); - FastOS_File::EmptyAndRemoveDirectory("generated"); + std::filesystem::remove_all(std::filesystem::path("generated")); PrintSeparator(); } void ReadWriteTest () { TestHeader("Read/Write Test"); - FastOS_File::MakeDirectory("generated"); + std::filesystem::create_directory(std::filesystem::path("generated")); FastOS_File *myFile = new FastOS_File(rwFilename.c_str()); @@ -637,7 +468,7 @@ public: Progress(deleteResult, "Delete file '%s'.", rwFilename.c_str()); delete(myFile); - FastOS_File::EmptyAndRemoveDirectory("generated"); + std::filesystem::remove_all(std::filesystem::path("generated")); PrintSeparator(); } @@ -731,8 +562,8 @@ public: sprintf(file4, "%s%sfile4", dirName, FastOS_File::GetPathSeparator()); sprintf(file5, "%s%sfile5", dirName, FastOS_File::GetPathSeparator()); - FastOS_File::EmptyAndRemoveDirectory(dirName); - FastOS_File::MakeDirectory(dirName); + 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. @@ -789,7 +620,7 @@ public: "Size of copied file should be %u bytes.", sizeOfFile5); - FastOS_File::EmptyAndRemoveDirectory("./tmpDir"); + std::filesystem::remove_all(std::filesystem::path("tmpDir")); PrintSeparator(); } @@ -798,8 +629,6 @@ public: printf("This test should be run in the 'tests' directory.\n\n"); printf("grep for the string '%s' to detect failures.\n\n", failString); - DirectoryTest(); - MoveFileTest(); CopyFileTest(); GetCurrentDirTest(); DirectIOTest(); diff --git a/fastos/src/vespa/fastos/file.cpp b/fastos/src/vespa/fastos/file.cpp index ca109d831a8..2857f45e3de 100644 --- a/fastos/src/vespa/fastos/file.cpp +++ b/fastos/src/vespa/fastos/file.cpp @@ -260,84 +260,6 @@ FastOS_FileInterface::CopyFile( const char *src, const char *dst ) } -bool -FastOS_FileInterface::MoveFile(const char* src, const char* dst) -{ - bool rc = FastOS_File::Rename(src, dst); - if (!rc) { - // Try copy and remove. - if (CopyFile(src, dst)) { - rc = FastOS_File::Delete(src); - } - } - return rc; -} - - -void -FastOS_FileInterface::EmptyDirectory( const char *dir, - const char *keepFile /* = nullptr */ ) -{ - FastOS_StatInfo statInfo; - if (!FastOS_File::Stat(dir, &statInfo)) - return; // Fail if the directory does not exist - FastOS_DirectoryScan dirScan( dir ); - - while (dirScan.ReadNext()) { - if (strcmp(dirScan.GetName(), ".") != 0 && - strcmp(dirScan.GetName(), "..") != 0 && - (keepFile == nullptr || strcmp(dirScan.GetName(), keepFile) != 0)) - { - std::string name = dir; - name += GetPathSeparator(); - name += dirScan.GetName(); - if (dirScan.IsDirectory()) { - EmptyAndRemoveDirectory(name.c_str()); - } else { - if ( ! FastOS_File::Delete(name.c_str()) ) { - std::ostringstream os; - os << "Failed deleting file '" << name << "' due to " << getLastErrorString(); - throw std::runtime_error(os.str().c_str()); - } - } - } - } -} - - -void -FastOS_FileInterface::EmptyAndRemoveDirectory(const char *dir) -{ - EmptyDirectory(dir); - FastOS_File::RemoveDirectory(dir); -} - -void -FastOS_FileInterface::MakeDirIfNotPresentOrExit(const char *name) -{ - FastOS_StatInfo statInfo; - - if (FastOS_File::Stat(name, &statInfo)) { - if (statInfo._isDirectory) - return; - - fprintf(stderr, "%s is not a directory\n", name); - std::_Exit(1); - } - - if (statInfo._error != FastOS_StatInfo::FileNotFound) { - std::error_code ec(errno, std::system_category()); - fprintf(stderr, "Could not stat %s: %s\n", name, ec.message().c_str()); - std::_Exit(1); - } - - if (!FastOS_File::MakeDirectory(name)) { - std::error_code ec(errno, std::system_category()); - fprintf(stderr, "Could not mkdir(\"%s\", 0775): %s\n", name, ec.message().c_str()); - std::_Exit(1); - } -} - void FastOS_FileInterface::SetFileName(const char *filename) { diff --git a/fastos/src/vespa/fastos/file.h b/fastos/src/vespa/fastos/file.h index b5e1add3529..b52b7e787fa 100644 --- a/fastos/src/vespa/fastos/file.h +++ b/fastos/src/vespa/fastos/file.h @@ -109,53 +109,6 @@ public: static bool CopyFile( const char *src, const char *dst ); /** - * Move a file from src to dst. Has the same semantics as - * FastOS_File::Rename, except that it works across different - * volumes / disks as well (Via copy and remove). - * - * @author Terje Loken - * @return success / failure - * @param src a 'const char *' value with the file to move from - * @param dst a 'const char *' value with the name of the resulting filename - */ - static bool MoveFile( const char *src, const char *dst); - - /** - * Remove a directory, even if it is non-empty. Missing directory does not cause error. - * - * @author Terje Loken - * @throws std::runtime_error if there are errors. - * @param dir a 'const char *' valuem, with the path to the directory we - * want to remove. - */ - static void EmptyAndRemoveDirectory(const char *dir); - - /** - * Empty a directory. Delete all files and directories within the - * dir, with the exception of files matching a specific name - * (optional). The exception does not apply files in - * subdirectories. - * - * @author Terje Loken - * @throws std::runtime_error if there are errors. - * @param dir a 'const char *' value with the directory to empty. - * @param keepFile a 'const char *' value. If supplied, leave files with - * this name alone. - */ - static void EmptyDirectory( const char *dir, const char *keepFile = nullptr); - - /** - * Make a directory (special compatibility version) - * Succeed if the directory already exists. A stat is performed - * to check the directory before attempting to create the - * directory. - * If the procedure fails, an error is printed to stderr and - * the program exits. - * @param name Name of directory - */ - static void MakeDirIfNotPresentOrExit(const char *name); - - /** * Return path separator string. This will yield "/" on UNIX systems. * @return pointer to path separator character string */ diff --git a/fastos/src/vespa/fastos/unix_file.cpp b/fastos/src/vespa/fastos/unix_file.cpp index 39e31c87702..71a9f6e6faa 100644 --- a/fastos/src/vespa/fastos/unix_file.cpp +++ b/fastos/src/vespa/fastos/unix_file.cpp @@ -128,24 +128,6 @@ int FastOS_UNIX_File::GetMaximumPathLength(const char *pathName) return pathconf(pathName, _PC_PATH_MAX); } -bool -FastOS_UNIX_File::MakeDirectory (const char *name) -{ - return (mkdir(name, 0775) == 0); -} - - -void -FastOS_UNIX_File::RemoveDirectory (const char *name) -{ - if ((rmdir(name) != 0) && (ERR_ENOENT != GetLastError())) { - std::ostringstream os; - os << "Remove of directory '" << name << "' failed with error :'" << getLastErrorString() << "'"; - throw std::runtime_error(os.str()); - } -} - - std::string FastOS_UNIX_File::getCurrentDirectory(void) { diff --git a/fastos/src/vespa/fastos/unix_file.h b/fastos/src/vespa/fastos/unix_file.h index 70a8db5036e..368d3c5aca5 100644 --- a/fastos/src/vespa/fastos/unix_file.h +++ b/fastos/src/vespa/fastos/unix_file.h @@ -39,8 +39,6 @@ public: } static bool Stat(const char *filename, FastOS_StatInfo *statInfo); - static bool MakeDirectory(const char *name); - static void RemoveDirectory(const char *name); static std::string getCurrentDirectory(); |