summaryrefslogtreecommitdiff
path: root/lib/Support
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2014-02-23 13:56:14 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2014-02-23 13:56:14 +0000
commit589d6377251d39e5f4d866bcb495bf6e547b4372 (patch)
treeecfc6cb9f76c144d99f1389ca266ecff177af386 /lib/Support
parente4e42f7ff8c99fafeec556ee4b4d685b5896e9c8 (diff)
downloadllvm-589d6377251d39e5f4d866bcb495bf6e547b4372.tar.gz
llvm-589d6377251d39e5f4d866bcb495bf6e547b4372.tar.bz2
llvm-589d6377251d39e5f4d866bcb495bf6e547b4372.tar.xz
Simplify remove, create_directory and create_directories.
Before this patch they would take an boolean argument to say if the path already existed. This was redundant with the returned error_code which is able to represent that. This allowed for callers to incorrectly check only the existed flag instead of first checking the error code. Instead, pass in a boolean flag to say if the previous (non-)existence should be an error or not. Callers of the of the old simple versions are not affected. They still ignore the previous (non-)existence as they did before. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201979 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r--lib/Support/Path.cpp6
-rw-r--r--lib/Support/Unix/Path.inc28
-rw-r--r--lib/Support/Windows/Path.inc52
3 files changed, 34 insertions, 52 deletions
diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp
index b21846c548..535ff00bae 100644
--- a/lib/Support/Path.cpp
+++ b/lib/Support/Path.cpp
@@ -751,12 +751,12 @@ error_code make_absolute(SmallVectorImpl<char> &path) {
"occurred above!");
}
-error_code create_directories(const Twine &Path, bool &Existed) {
+error_code create_directories(const Twine &Path, bool IgnoreExisting) {
SmallString<128> PathStorage;
StringRef P = Path.toStringRef(PathStorage);
// Be optimistic and try to create the directory
- error_code EC = create_directory(P, Existed);
+ error_code EC = create_directory(P, IgnoreExisting);
// If we succeeded, or had any error other than the parent not existing, just
// return it.
if (EC != errc::no_such_file_or_directory)
@@ -771,7 +771,7 @@ error_code create_directories(const Twine &Path, bool &Existed) {
if ((EC = create_directories(Parent)))
return EC;
- return create_directory(P, Existed);
+ return create_directory(P, IgnoreExisting);
}
bool exists(file_status status) {
diff --git a/lib/Support/Unix/Path.inc b/lib/Support/Unix/Path.inc
index 20e9642897..83d2b4284a 100644
--- a/lib/Support/Unix/Path.inc
+++ b/lib/Support/Unix/Path.inc
@@ -164,12 +164,11 @@ retry_random_path:
}
case FS_Dir: {
- bool Existed;
- error_code EC = sys::fs::create_directory(ResultPath.begin(), Existed);
- if (EC)
+ if (error_code EC = sys::fs::create_directory(ResultPath.begin(), false)) {
+ if (EC == errc::file_exists)
+ goto retry_random_path;
return EC;
- if (Existed)
- goto retry_random_path;
+ }
return error_code::success();
}
}
@@ -333,16 +332,14 @@ error_code current_path(SmallVectorImpl<char> &result) {
return error_code::success();
}
-error_code create_directory(const Twine &path, bool &existed) {
+error_code create_directory(const Twine &path, bool IgnoreExisting) {
SmallString<128> path_storage;
StringRef p = path.toNullTerminatedStringRef(path_storage);
if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
- if (errno != errc::file_exists)
+ if (errno != errc::file_exists || !IgnoreExisting)
return error_code(errno, system_category());
- existed = true;
- } else
- existed = false;
+ }
return error_code::success();
}
@@ -360,15 +357,14 @@ error_code create_hard_link(const Twine &to, const Twine &from) {
return error_code::success();
}
-error_code remove(const Twine &path, bool &existed) {
+error_code remove(const Twine &path, bool IgnoreNonExisting) {
SmallString<128> path_storage;
StringRef p = path.toNullTerminatedStringRef(path_storage);
struct stat buf;
if (stat(p.begin(), &buf) != 0) {
- if (errno != errc::no_such_file_or_directory)
+ if (errno != errc::no_such_file_or_directory || !IgnoreNonExisting)
return error_code(errno, system_category());
- existed = false;
return error_code::success();
}
@@ -381,11 +377,9 @@ error_code remove(const Twine &path, bool &existed) {
return make_error_code(errc::operation_not_permitted);
if (::remove(p.begin()) == -1) {
- if (errno != errc::no_such_file_or_directory)
+ if (errno != errc::no_such_file_or_directory || !IgnoreNonExisting)
return error_code(errno, system_category());
- existed = false;
- } else
- existed = true;
+ }
return error_code::success();
}
diff --git a/lib/Support/Windows/Path.inc b/lib/Support/Windows/Path.inc
index fa7d18a711..f3c7ba1a24 100644
--- a/lib/Support/Windows/Path.inc
+++ b/lib/Support/Windows/Path.inc
@@ -264,7 +264,7 @@ error_code current_path(SmallVectorImpl<char> &result) {
return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
}
-error_code create_directory(const Twine &path, bool &existed) {
+error_code create_directory(const Twine &path, bool IgnoreExisting) {
SmallString<128> path_storage;
SmallVector<wchar_t, 128> path_utf16;
@@ -274,12 +274,9 @@ error_code create_directory(const Twine &path, bool &existed) {
if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
error_code ec = windows_error(::GetLastError());
- if (ec == windows_error::already_exists)
- existed = true;
- else
+ if (ec != windows_error::already_exists || !IgnoreExisting)
return ec;
- } else
- existed = false;
+ }
return error_code::success();
}
@@ -303,43 +300,34 @@ error_code create_hard_link(const Twine &to, const Twine &from) {
return error_code::success();
}
-error_code remove(const Twine &path, bool &existed) {
+error_code remove(const Twine &path, bool IgnoreNonExisting) {
SmallString<128> path_storage;
SmallVector<wchar_t, 128> path_utf16;
- file_status st;
- error_code EC = status(path, st);
- if (EC) {
- if (EC == windows_error::file_not_found ||
- EC == windows_error::path_not_found) {
- existed = false;
- return error_code::success();
- }
- return EC;
+ file_status ST;
+ if (error_code EC = status(path, ST)) {
+ if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
+ return EC;
+ return error_code::success();
}
if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
path_utf16))
return ec;
- if (st.type() == file_type::directory_file) {
+ if (ST.type() == file_type::directory_file) {
if (!::RemoveDirectoryW(c_str(path_utf16))) {
- error_code ec = windows_error(::GetLastError());
- if (ec != windows_error::file_not_found)
- return ec;
- existed = false;
- } else
- existed = true;
- } else {
- if (!::DeleteFileW(c_str(path_utf16))) {
- error_code ec = windows_error(::GetLastError());
- if (ec != windows_error::file_not_found)
- return ec;
- existed = false;
- } else
- existed = true;
+ error_code EC = windows_error(::GetLastError());
+ if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
+ return EC;
+ }
+ return error_code::success();
+ }
+ if (!::DeleteFileW(c_str(path_utf16))) {
+ error_code EC = windows_error(::GetLastError());
+ if (EC != errc::no_such_file_or_directory || !IgnoreNonExisting)
+ return EC;
}
-
return error_code::success();
}