summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@online.no>2022-06-01 19:12:45 +0200
committerTor Egge <Tor.Egge@online.no>2022-06-01 19:12:45 +0200
commit4a326e9d3c040b9670d0de3a08add5036df19796 (patch)
tree1f1c6d31bfc5f182376421a2838ae94f7e0b7876 /vespalib
parent2bb6689c54b39e4b11d3085c0c7a70179ece0347 (diff)
Convert vespalib::rmdir to a deprecated shim.
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/io/fileutil/fileutiltest.cpp19
-rw-r--r--vespalib/src/vespa/vespalib/io/fileutil.cpp49
-rw-r--r--vespalib/src/vespa/vespalib/io/fileutil.h5
3 files changed, 14 insertions, 59 deletions
diff --git a/vespalib/src/tests/io/fileutil/fileutiltest.cpp b/vespalib/src/tests/io/fileutil/fileutiltest.cpp
index 189bfef349b..51cf2055d33 100644
--- a/vespalib/src/tests/io/fileutil/fileutiltest.cpp
+++ b/vespalib/src/tests/io/fileutil/fileutiltest.cpp
@@ -1,9 +1,11 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/testkit/test_kit.h>
+#include <filesystem>
#include <iostream>
#include <vector>
#include <regex>
+#include <system_error>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/size_literals.h>
@@ -237,9 +239,9 @@ TEST("require that vespalib::mkdir and vespalib::rmdir works")
ASSERT_TRUE(mkdir("mydir/otherdir/evenmorestuff"));
rmdir("mydir");
TEST_FATAL("Should not work without recursive option set");
- } catch (IoException& e) {
+ } catch (std::filesystem::filesystem_error& e) {
//std::cerr << e.what() << "\n";
- EXPECT_EQUAL(IoException::DIRECTORY_HAVE_CONTENT, e.getType());
+ EXPECT_EQUAL(make_error_code(std::errc::directory_not_empty), e.code());
}
// Works with recursive option
{
@@ -247,19 +249,6 @@ TEST("require that vespalib::mkdir and vespalib::rmdir works")
ASSERT_TRUE(!fileExists("mydir"));
ASSERT_TRUE(!rmdir("mydir", true));
}
- // Doesn't work on file
- try{
- {
- File f("myfile");
- f.open(File::CREATE);
- f.write("foo", 3, 0);
- }
- rmdir("myfile");
- TEST_FATAL("Should have failed to run rmdir on file.");
- } catch (IoException& e) {
- //std::cerr << e.what() << "\n";
- EXPECT_EQUAL(IoException::ILLEGAL_PATH, e.getType());
- }
// mkdir works when a path component is a symlink which points to
// another directory and the final path component does not exist.
diff --git a/vespalib/src/vespa/vespalib/io/fileutil.cpp b/vespalib/src/vespa/vespalib/io/fileutil.cpp
index 9994e0d0d6f..2d28f07e600 100644
--- a/vespalib/src/vespa/vespalib/io/fileutil.cpp
+++ b/vespalib/src/vespa/vespalib/io/fileutil.cpp
@@ -5,9 +5,10 @@
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/util/stringfmt.h>
-#include <vespa/fastos/file.h>
#include <ostream>
#include <cassert>
+#include <filesystem>
+#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
@@ -534,50 +535,12 @@ chdir(const string & directory)
bool
rmdir(const string & directory, bool recursive)
{
- string dirname(directory);
- if (!dirname.empty() && *dirname.rbegin() == '/') {
- dirname.resize(dirname.size() - 1);
- }
- if (dirname.empty()) {
- LOG(debug, "rmdir(%s): Not allowing deletion of '/'.", directory.c_str());
- return false;
- }
+ std::filesystem::path path(directory);
if (recursive) {
- FastOS_DirectoryScan dir(dirname.c_str());
- while (dir.ReadNext()) {
- if (strcmp(dir.GetName(), "..") != 0 &&
- strcmp(dir.GetName(), ".") != 0)
- {
- string fullpath(dirname + "/" + dir.GetName());
- if (dir.IsDirectory()) {
- rmdir(fullpath, true);
- } else {
- if (::unlink(fullpath.c_str()) != 0) {
- asciistream ost;
- ost << "rmdir(" << fullpath
- << (recursive ? ", recursive" : "")
- << "): Failed, errno(" << errno << "): "
- << safeStrerror(errno);
- throw IoException(ost.str(),
- IoException::getErrorType(errno),
- VESPA_STRLOC);
- }
- }
- }
- }
- }
- if (::rmdir(dirname.c_str()) == 0) {
- LOG(debug, "rmdir(%s): Directory deleted.", directory.c_str());
- return true;
- }
- if (errno == ENOENT) {
- LOG(debug, "rmdir(%s): No directory to delete.", directory.c_str());
- return false;
+ return std::filesystem::remove_all(path) > 0;
+ } else {
+ return std::filesystem::remove(path);
}
- asciistream ost;
- ost << "rmdir(" << dirname << (recursive ? ", recursive" : "")
- << "): Failed, errno(" << errno << "): " << safeStrerror(errno);
- throw IoException(ost.str(), IoException::getErrorType(errno), VESPA_STRLOC);
}
FileInfo::UP
diff --git a/vespalib/src/vespa/vespalib/io/fileutil.h b/vespalib/src/vespa/vespalib/io/fileutil.h
index d9e1a00345f..4d2ce45358a 100644
--- a/vespalib/src/vespa/vespalib/io/fileutil.h
+++ b/vespalib/src/vespa/vespalib/io/fileutil.h
@@ -240,9 +240,12 @@ extern void chdir(const vespalib::string & directory);
* @param directory The directory name.
* @param recursive If set, remove all content of the directory to. If not
* set, fail if the directory is not empty.
- * @throw IoException If we failed to remove the directory.
+ * @throw std::filesystem::filesystem_error If we failed to remove the directory.
*
* @return True if directory existed, false if not.
+ *
+ * This function is deprecated. Use std::filesystem::remove or std::filesystem::remove_all
+ * instead.
*/
extern bool rmdir(const vespalib::string & directory, bool recursive = false);