Support/PathV2: Remove redundant calls to make_error_code.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120913 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Michael J. Spencer 2010-12-04 18:45:32 +00:00
parent fd6d53fbad
commit 9d425e7687
3 changed files with 101 additions and 102 deletions

View File

@ -293,28 +293,28 @@ error_code root_path(const StringRef &path, StringRef &result) {
if ((++pos != e) && is_separator((*pos)[0])) { if ((++pos != e) && is_separator((*pos)[0])) {
// {C:/,//net/}, so get the first two components. // {C:/,//net/}, so get the first two components.
result = StringRef(path.begin(), b->size() + pos->size()); result = StringRef(path.begin(), b->size() + pos->size());
return make_error_code(errc::success); return success;
} else { } else {
// just {C:,//net}, return the first component. // just {C:,//net}, return the first component.
result = *b; result = *b;
return make_error_code(errc::success); return success;
} }
} }
// POSIX style root directory. // POSIX style root directory.
if (is_separator((*b)[0])) { if (is_separator((*b)[0])) {
result = *b; result = *b;
return make_error_code(errc::success); return success;
} }
// No root_path. // No root_path.
result = StringRef(); result = StringRef();
return make_error_code(errc::success); return success;
} }
// No path :(. // No path :(.
result = StringRef(); result = StringRef();
return make_error_code(errc::success); return success;
} }
error_code root_name(const StringRef &path, StringRef &result) { error_code root_name(const StringRef &path, StringRef &result) {
@ -332,13 +332,13 @@ error_code root_name(const StringRef &path, StringRef &result) {
if (has_net || has_drive) { if (has_net || has_drive) {
// just {C:,//net}, return the first component. // just {C:,//net}, return the first component.
result = *b; result = *b;
return make_error_code(errc::success); return success;
} }
} }
// No path or no name. // No path or no name.
result = StringRef(); result = StringRef();
return make_error_code(errc::success); return success;
} }
error_code root_directory(const StringRef &path, StringRef &result) { error_code root_directory(const StringRef &path, StringRef &result) {
@ -358,26 +358,26 @@ error_code root_directory(const StringRef &path, StringRef &result) {
// {C:,//net}, skip to the next component. // {C:,//net}, skip to the next component.
(++pos != e) && is_separator((*pos)[0])) { (++pos != e) && is_separator((*pos)[0])) {
result = *pos; result = *pos;
return make_error_code(errc::success); return success;
} }
// POSIX style root directory. // POSIX style root directory.
if (!has_net && is_separator((*b)[0])) { if (!has_net && is_separator((*b)[0])) {
result = *b; result = *b;
return make_error_code(errc::success); return success;
} }
} }
// No path or no root. // No path or no root.
result = StringRef(); result = StringRef();
return make_error_code(errc::success); return success;
} }
error_code relative_path(const StringRef &path, StringRef &result) { error_code relative_path(const StringRef &path, StringRef &result) {
StringRef root; StringRef root;
if (error_code ec = root_path(path, root)) return ec; if (error_code ec = root_path(path, root)) return ec;
result = StringRef(path.begin() + root.size(), path.size() - root.size()); result = StringRef(path.begin() + root.size(), path.size() - root.size());
return make_error_code(errc::success); return success;
} }
error_code append(SmallVectorImpl<char> &path, const Twine &a, error_code append(SmallVectorImpl<char> &path, const Twine &a,
@ -421,7 +421,7 @@ error_code append(SmallVectorImpl<char> &path, const Twine &a,
path.append(i->begin(), i->end()); path.append(i->begin(), i->end());
} }
return make_error_code(errc::success); return success;
} }
error_code make_absolute(SmallVectorImpl<char> &path) { error_code make_absolute(SmallVectorImpl<char> &path) {
@ -433,7 +433,7 @@ error_code make_absolute(SmallVectorImpl<char> &path) {
// Already absolute. // Already absolute.
if (rootName && rootDirectory) if (rootName && rootDirectory)
return make_error_code(errc::success); return success;
// All of the following conditions will need the current directory. // All of the following conditions will need the current directory.
SmallString<128> current_dir; SmallString<128> current_dir;
@ -445,7 +445,7 @@ error_code make_absolute(SmallVectorImpl<char> &path) {
if (error_code ec = append(current_dir, p)) return ec; if (error_code ec = append(current_dir, p)) return ec;
// Set path to the result. // Set path to the result.
path.swap(current_dir); path.swap(current_dir);
return make_error_code(errc::success); return success;
} }
if (!rootName && rootDirectory) { if (!rootName && rootDirectory) {
@ -455,7 +455,7 @@ error_code make_absolute(SmallVectorImpl<char> &path) {
if (error_code ec = append(curDirRootName, p)) return ec; if (error_code ec = append(curDirRootName, p)) return ec;
// Set path to the result. // Set path to the result.
path.swap(curDirRootName); path.swap(curDirRootName);
return make_error_code(errc::success); return success;
} }
if (rootName && !rootDirectory) { if (rootName && !rootDirectory) {
@ -472,7 +472,7 @@ error_code make_absolute(SmallVectorImpl<char> &path) {
if (error_code ec = append(res, pRootName, bRootDirectory, if (error_code ec = append(res, pRootName, bRootDirectory,
bRelativePath, pRelativePath)) return ec; bRelativePath, pRelativePath)) return ec;
path.swap(res); path.swap(res);
return make_error_code(errc::success); return success;
} }
llvm_unreachable("All rootName and rootDirectory combinations should have " llvm_unreachable("All rootName and rootDirectory combinations should have "
@ -485,15 +485,15 @@ error_code parent_path(const StringRef &path, StringRef &result) {
result = StringRef(); result = StringRef();
else else
result = StringRef(path.data(), end_pos); result = StringRef(path.data(), end_pos);
return make_error_code(errc::success); return success;
} }
error_code remove_filename(SmallVectorImpl<char> &path) { error_code remove_filename(SmallVectorImpl<char> &path) {
size_t end_pos = parent_path_end(StringRef(path.begin(), path.size())); size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
if (end_pos == StringRef::npos) if (end_pos == StringRef::npos)
return make_error_code(errc::success); return success;
path.set_size(end_pos); path.set_size(end_pos);
return make_error_code(errc::success); return success;
} }
error_code replace_extension(SmallVectorImpl<char> &path, error_code replace_extension(SmallVectorImpl<char> &path,
@ -513,7 +513,7 @@ error_code replace_extension(SmallVectorImpl<char> &path,
// Append extension. // Append extension.
path.append(ext.begin(), ext.end()); path.append(ext.begin(), ext.end());
return make_error_code(errc::success); return success;
} }
error_code native(const Twine &path, SmallVectorImpl<char> &result) { error_code native(const Twine &path, SmallVectorImpl<char> &result) {
@ -535,12 +535,12 @@ error_code native(const Twine &path, SmallVectorImpl<char> &result) {
#else #else
path.toVector(result); path.toVector(result);
#endif #endif
return make_error_code(errc::success); return success;
} }
error_code filename(const StringRef &path, StringRef &result) { error_code filename(const StringRef &path, StringRef &result) {
result = *(--end(path)); result = *(--end(path));
return make_error_code(errc::success); return success;
} }
error_code stem(const StringRef &path, StringRef &result) { error_code stem(const StringRef &path, StringRef &result) {
@ -556,7 +556,7 @@ error_code stem(const StringRef &path, StringRef &result) {
else else
result = StringRef(fname.begin(), pos); result = StringRef(fname.begin(), pos);
return make_error_code(errc::success); return success;
} }
error_code extension(const StringRef &path, StringRef &result) { error_code extension(const StringRef &path, StringRef &result) {
@ -572,7 +572,7 @@ error_code extension(const StringRef &path, StringRef &result) {
else else
result = StringRef(fname.begin() + pos, fname.size() - pos); result = StringRef(fname.begin() + pos, fname.size() - pos);
return make_error_code(errc::success); return success;
} }
error_code has_root_name(const Twine &path, bool &result) { error_code has_root_name(const Twine &path, bool &result) {
@ -582,7 +582,7 @@ error_code has_root_name(const Twine &path, bool &result) {
if (error_code ec = root_name(p, p)) return ec; if (error_code ec = root_name(p, p)) return ec;
result = !p.empty(); result = !p.empty();
return make_error_code(errc::success); return success;
} }
error_code has_root_directory(const Twine &path, bool &result) { error_code has_root_directory(const Twine &path, bool &result) {
@ -592,7 +592,7 @@ error_code has_root_directory(const Twine &path, bool &result) {
if (error_code ec = root_directory(p, p)) return ec; if (error_code ec = root_directory(p, p)) return ec;
result = !p.empty(); result = !p.empty();
return make_error_code(errc::success); return success;
} }
error_code has_root_path(const Twine &path, bool &result) { error_code has_root_path(const Twine &path, bool &result) {
@ -602,7 +602,7 @@ error_code has_root_path(const Twine &path, bool &result) {
if (error_code ec = root_path(p, p)) return ec; if (error_code ec = root_path(p, p)) return ec;
result = !p.empty(); result = !p.empty();
return make_error_code(errc::success); return success;
} }
error_code has_filename(const Twine &path, bool &result) { error_code has_filename(const Twine &path, bool &result) {
@ -612,7 +612,7 @@ error_code has_filename(const Twine &path, bool &result) {
if (error_code ec = filename(p, p)) return ec; if (error_code ec = filename(p, p)) return ec;
result = !p.empty(); result = !p.empty();
return make_error_code(errc::success); return success;
} }
error_code has_parent_path(const Twine &path, bool &result) { error_code has_parent_path(const Twine &path, bool &result) {
@ -622,7 +622,7 @@ error_code has_parent_path(const Twine &path, bool &result) {
if (error_code ec = parent_path(p, p)) return ec; if (error_code ec = parent_path(p, p)) return ec;
result = !p.empty(); result = !p.empty();
return make_error_code(errc::success); return success;
} }
error_code has_stem(const Twine &path, bool &result) { error_code has_stem(const Twine &path, bool &result) {
@ -632,7 +632,7 @@ error_code has_stem(const Twine &path, bool &result) {
if (error_code ec = stem(p, p)) return ec; if (error_code ec = stem(p, p)) return ec;
result = !p.empty(); result = !p.empty();
return make_error_code(errc::success); return success;
} }
error_code has_extension(const Twine &path, bool &result) { error_code has_extension(const Twine &path, bool &result) {
@ -642,7 +642,7 @@ error_code has_extension(const Twine &path, bool &result) {
if (error_code ec = extension(p, p)) return ec; if (error_code ec = extension(p, p)) return ec;
result = !p.empty(); result = !p.empty();
return make_error_code(errc::success); return success;
} }
error_code is_absolute(const Twine &path, bool &result) { error_code is_absolute(const Twine &path, bool &result) {
@ -659,7 +659,7 @@ error_code is_absolute(const Twine &path, bool &result) {
#endif #endif
result = rootDir && rootName; result = rootDir && rootName;
return make_error_code(errc::success); return success;
} }
error_code is_relative(const Twine &path, bool &result) { error_code is_relative(const Twine &path, bool &result) {

View File

@ -63,7 +63,7 @@ namespace {
result.set_size(0); result.set_size(0);
StringRef d(dir); StringRef d(dir);
result.append(d.begin(), d.end()); result.append(d.begin(), d.end());
return make_error_code(errc::success); return success;
} }
} }
@ -80,7 +80,7 @@ error_code current_path(SmallVectorImpl<char> &result) {
return error_code(errno, system_category()); return error_code(errno, system_category());
result.set_size(strlen(result.data())); result.set_size(strlen(result.data()));
return make_error_code(errc::success); return success;
} }
} // end namespace path } // end namespace path
@ -143,7 +143,7 @@ error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
if (sz_read < 0) if (sz_read < 0)
return error_code(errno, system_category()); return error_code(errno, system_category());
return make_error_code(errc::success); return success;
} }
error_code create_directory(const Twine &path, bool &existed) { error_code create_directory(const Twine &path, bool &existed) {
@ -151,13 +151,13 @@ error_code create_directory(const Twine &path, bool &existed) {
StringRef p = path.toNullTerminatedStringRef(path_storage); StringRef p = path.toNullTerminatedStringRef(path_storage);
if (::mkdir(p.begin(), 0700) == -1) { if (::mkdir(p.begin(), 0700) == -1) {
if (errno != EEXIST) if (errno != errc::file_exists)
return error_code(errno, system_category()); return error_code(errno, system_category());
existed = true; existed = true;
} else } else
existed = false; existed = false;
return make_error_code(errc::success); return success;
} }
error_code create_hard_link(const Twine &to, const Twine &from) { error_code create_hard_link(const Twine &to, const Twine &from) {
@ -170,7 +170,7 @@ error_code create_hard_link(const Twine &to, const Twine &from) {
if (::link(t.begin(), f.begin()) == -1) if (::link(t.begin(), f.begin()) == -1)
return error_code(errno, system_category()); return error_code(errno, system_category());
return make_error_code(errc::success); return success;
} }
error_code create_symlink(const Twine &to, const Twine &from) { error_code create_symlink(const Twine &to, const Twine &from) {
@ -183,7 +183,7 @@ error_code create_symlink(const Twine &to, const Twine &from) {
if (::symlink(t.begin(), f.begin()) == -1) if (::symlink(t.begin(), f.begin()) == -1)
return error_code(errno, system_category()); return error_code(errno, system_category());
return make_error_code(errc::success); return success;
} }
error_code remove(const Twine &path, bool &existed) { error_code remove(const Twine &path, bool &existed) {
@ -191,13 +191,13 @@ error_code remove(const Twine &path, bool &existed) {
StringRef p = path.toNullTerminatedStringRef(path_storage); StringRef p = path.toNullTerminatedStringRef(path_storage);
if (::remove(p.begin()) == -1) { if (::remove(p.begin()) == -1) {
if (errno != ENOENT) if (errno != errc::no_such_file_or_directory)
return error_code(errno, system_category()); return error_code(errno, system_category());
existed = false; existed = false;
} else } else
existed = true; existed = true;
return make_error_code(errc::success); return success;
} }
error_code rename(const Twine &from, const Twine &to) { error_code rename(const Twine &from, const Twine &to) {
@ -210,7 +210,7 @@ error_code rename(const Twine &from, const Twine &to) {
if (::rename(f.begin(), t.begin()) == -1) if (::rename(f.begin(), t.begin()) == -1)
return error_code(errno, system_category()); return error_code(errno, system_category());
return make_error_code(errc::success); return success;
} }
error_code resize_file(const Twine &path, uint64_t size) { error_code resize_file(const Twine &path, uint64_t size) {
@ -220,7 +220,7 @@ error_code resize_file(const Twine &path, uint64_t size) {
if (::truncate(p.begin(), size) == -1) if (::truncate(p.begin(), size) == -1)
return error_code(errno, system_category()); return error_code(errno, system_category());
return make_error_code(errc::success); return success;
} }
error_code exists(const Twine &path, bool &result) { error_code exists(const Twine &path, bool &result) {
@ -229,13 +229,13 @@ error_code exists(const Twine &path, bool &result) {
struct stat status; struct stat status;
if (::stat(p.begin(), &status) == -1) { if (::stat(p.begin(), &status) == -1) {
if (errno != ENOENT) if (errno != errc::no_such_file_or_directory)
return error_code(errno, system_category()); return error_code(errno, system_category());
result = false; result = false;
} else } else
result = true; result = true;
return make_error_code(errc::success); return success;
} }
error_code equivalent(const Twine &A, const Twine &B, bool &result) { error_code equivalent(const Twine &A, const Twine &B, bool &result) {
@ -260,7 +260,7 @@ error_code equivalent(const Twine &A, const Twine &B, bool &result) {
stat_a.st_ino == stat_b.st_ino; stat_a.st_ino == stat_b.st_ino;
} }
return make_error_code(errc::success); return success;
} }
error_code file_size(const Twine &path, uint64_t &result) { error_code file_size(const Twine &path, uint64_t &result) {
@ -271,10 +271,10 @@ error_code file_size(const Twine &path, uint64_t &result) {
if (::stat(p.begin(), &status) == -1) if (::stat(p.begin(), &status) == -1)
return error_code(errno, system_category()); return error_code(errno, system_category());
if (!S_ISREG(status.st_mode)) if (!S_ISREG(status.st_mode))
return error_code(EPERM, system_category()); return make_error_code(errc::operation_not_permitted);
result = status.st_size; result = status.st_size;
return make_error_code(errc::success); return success;
} }
error_code status(const Twine &path, file_status &result) { error_code status(const Twine &path, file_status &result) {
@ -359,10 +359,10 @@ rety_open_create:
int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
if (RandomFD == -1) { if (RandomFD == -1) {
// If the file existed, try again, otherwise, error. // If the file existed, try again, otherwise, error.
if (errno == EEXIST) if (errno == errc::file_exists)
goto retry_random_path; goto retry_random_path;
// The path prefix doesn't exist. // The path prefix doesn't exist.
if (errno == ENOENT) { if (errno == errc::no_such_file_or_directory) {
StringRef p(RandomPath.begin(), RandomPath.size()); StringRef p(RandomPath.begin(), RandomPath.size());
SmallString<64> dir_to_create; SmallString<64> dir_to_create;
for (path::const_iterator i = path::begin(p), for (path::const_iterator i = path::begin(p),
@ -375,7 +375,7 @@ rety_open_create:
if (i->size() > 2 && (*i)[0] == '/' && if (i->size() > 2 && (*i)[0] == '/' &&
(*i)[1] == '/' && (*i)[1] == '/' &&
(*i)[2] != '/') (*i)[2] != '/')
return error_code(ENOENT, system_category()); return make_error_code(errc::no_such_file_or_directory);
if (::mkdir(dir_to_create.c_str(), 0700) == -1) if (::mkdir(dir_to_create.c_str(), 0700) == -1)
return error_code(errno, system_category()); return error_code(errno, system_category());
} }
@ -398,7 +398,7 @@ rety_open_create:
result_path.append(d.begin(), d.end()); result_path.append(d.begin(), d.end());
result_fd = RandomFD; result_fd = RandomFD;
return make_error_code(errc::success); return success;
} }
} // end namespace fs } // end namespace fs

View File

@ -48,7 +48,7 @@ namespace {
utf16.begin(), 0); utf16.begin(), 0);
if (len == 0) if (len == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
utf16.reserve(len + 1); utf16.reserve(len + 1);
utf16.set_size(len); utf16.set_size(len);
@ -58,13 +58,13 @@ namespace {
utf16.begin(), utf16.size()); utf16.begin(), utf16.size());
if (len == 0) if (len == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
// Make utf16 null terminated. // Make utf16 null terminated.
utf16.push_back(0); utf16.push_back(0);
utf16.pop_back(); utf16.pop_back();
return make_error_code(errc::success); return success;
} }
error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
@ -76,7 +76,7 @@ namespace {
NULL, NULL); NULL, NULL);
if (len == 0) if (len == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
utf8.reserve(len); utf8.reserve(len);
utf8.set_size(len); utf8.set_size(len);
@ -88,13 +88,13 @@ namespace {
NULL, NULL); NULL, NULL);
if (len == 0) if (len == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
// Make utf8 null terminated. // Make utf8 null terminated.
utf8.push_back(0); utf8.push_back(0);
utf8.pop_back(); utf8.pop_back();
return make_error_code(errc::success); return success;
} }
error_code TempDir(SmallVectorImpl<wchar_t> &result) { error_code TempDir(SmallVectorImpl<wchar_t> &result) {
@ -102,7 +102,7 @@ namespace {
DWORD len = ::GetTempPathW(result.capacity(), result.begin()); DWORD len = ::GetTempPathW(result.capacity(), result.begin());
if (len == 0) if (len == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
if (len > result.capacity()) { if (len > result.capacity()) {
result.reserve(len); result.reserve(len);
@ -110,7 +110,7 @@ namespace {
} }
result.set_size(len); result.set_size(len);
return make_error_code(errc::success); return success;
} }
struct AutoCryptoProvider { struct AutoCryptoProvider {
@ -136,7 +136,7 @@ retry_cur_dir:
// A zero return value indicates a failure other than insufficient space. // A zero return value indicates a failure other than insufficient space.
if (len == 0) if (len == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
// If there's insufficient space, the len returned is larger than the len // If there's insufficient space, the len returned is larger than the len
// given. // given.
@ -167,9 +167,9 @@ retry_cur_dir:
result.data(), result.size(), result.data(), result.size(),
NULL, NULL); NULL, NULL);
if (len == 0) if (len == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
return make_error_code(errc::success); return success;
} }
} // end namespace path } // end namespace path
@ -194,9 +194,9 @@ error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
copt != copy_option::overwrite_if_exists); copt != copy_option::overwrite_if_exists);
if (res == 0) if (res == 0)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
return make_error_code(errc::success); return success;
} }
error_code create_directory(const Twine &path, bool &existed) { error_code create_directory(const Twine &path, bool &existed) {
@ -208,15 +208,15 @@ error_code create_directory(const Twine &path, bool &existed) {
return ec; return ec;
if (!::CreateDirectoryW(path_utf16.begin(), NULL)) { if (!::CreateDirectoryW(path_utf16.begin(), NULL)) {
error_code ec = make_error_code(windows_error(::GetLastError())); error_code ec = windows_error(::GetLastError());
if (ec == make_error_code(windows_error::already_exists)) if (ec == windows_error::already_exists)
existed = true; existed = true;
else else
return ec; return ec;
} else } else
existed = false; existed = false;
return make_error_code(errc::success); return success;
} }
error_code create_hard_link(const Twine &to, const Twine &from) { error_code create_hard_link(const Twine &to, const Twine &from) {
@ -233,9 +233,9 @@ error_code create_hard_link(const Twine &to, const Twine &from) {
if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL)) if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
return make_error_code(errc::success); return success;
} }
error_code create_symlink(const Twine &to, const Twine &from) { error_code create_symlink(const Twine &to, const Twine &from) {
@ -256,9 +256,9 @@ error_code create_symlink(const Twine &to, const Twine &from) {
if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0)) if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
return make_error_code(errc::success); return success;
} }
error_code remove(const Twine &path, bool &existed) { error_code remove(const Twine &path, bool &existed) {
@ -270,14 +270,14 @@ error_code remove(const Twine &path, bool &existed) {
return ec; return ec;
if (!::DeleteFileW(path_utf16.begin())) { if (!::DeleteFileW(path_utf16.begin())) {
error_code ec = make_error_code(windows_error(::GetLastError())); error_code ec = windows_error(::GetLastError());
if (ec != make_error_code(windows_error::file_not_found)) if (ec != windows_error::file_not_found)
return ec; return ec;
existed = false; existed = false;
} else } else
existed = true; existed = true;
return make_error_code(errc::success); return success;
} }
error_code rename(const Twine &from, const Twine &to) { error_code rename(const Twine &from, const Twine &to) {
@ -294,9 +294,9 @@ error_code rename(const Twine &from, const Twine &to) {
if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
if (!::MoveFileW(wide_from.begin(), wide_to.begin())) if (!::MoveFileW(wide_from.begin(), wide_to.begin()))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
return make_error_code(errc::success); return success;
} }
error_code resize_file(const Twine &path, uint64_t size) { error_code resize_file(const Twine &path, uint64_t size) {
@ -332,13 +332,13 @@ error_code exists(const Twine &path, bool &result) {
if (attributes == INVALID_FILE_ATTRIBUTES) { if (attributes == INVALID_FILE_ATTRIBUTES) {
// See if the file didn't actually exist. // See if the file didn't actually exist.
error_code ec = make_error_code(windows_error(::GetLastError())); error_code ec = make_error_code(windows_error(::GetLastError()));
if (ec != error_code(windows_error::file_not_found) && if (ec != windows_error::file_not_found &&
ec != error_code(windows_error::path_not_found)) ec != windows_error::path_not_found)
return ec; return ec;
result = false; result = false;
} else } else
result = true; result = true;
return make_error_code(errc::success); return success;
} }
error_code equivalent(const Twine &A, const Twine &B, bool &result) { error_code equivalent(const Twine &A, const Twine &B, bool &result) {
@ -375,21 +375,21 @@ error_code equivalent(const Twine &A, const Twine &B, bool &result) {
// If both handles are invalid, it's an error. // If both handles are invalid, it's an error.
if (HandleA == INVALID_HANDLE_VALUE && if (HandleA == INVALID_HANDLE_VALUE &&
HandleB == INVALID_HANDLE_VALUE) HandleB == INVALID_HANDLE_VALUE)
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
// If only one is invalid, it's false. // If only one is invalid, it's false.
if (HandleA == INVALID_HANDLE_VALUE && if (HandleA == INVALID_HANDLE_VALUE &&
HandleB == INVALID_HANDLE_VALUE) { HandleB == INVALID_HANDLE_VALUE) {
result = false; result = false;
return make_error_code(errc::success); return success;
} }
// Get file information. // Get file information.
BY_HANDLE_FILE_INFORMATION InfoA, InfoB; BY_HANDLE_FILE_INFORMATION InfoA, InfoB;
if (!::GetFileInformationByHandle(HandleA, &InfoA)) if (!::GetFileInformationByHandle(HandleA, &InfoA))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
if (!::GetFileInformationByHandle(HandleB, &InfoB)) if (!::GetFileInformationByHandle(HandleB, &InfoB))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
// See if it's all the same. // See if it's all the same.
result = result =
@ -403,7 +403,7 @@ error_code equivalent(const Twine &A, const Twine &B, bool &result) {
InfoA.ftLastWriteTime.dwHighDateTime == InfoA.ftLastWriteTime.dwHighDateTime ==
InfoB.ftLastWriteTime.dwHighDateTime; InfoB.ftLastWriteTime.dwHighDateTime;
return make_error_code(errc::success); return success;
} }
error_code file_size(const Twine &path, uint64_t &result) { error_code file_size(const Twine &path, uint64_t &result) {
@ -418,13 +418,13 @@ error_code file_size(const Twine &path, uint64_t &result) {
if (!::GetFileAttributesExW(path_utf16.begin(), if (!::GetFileAttributesExW(path_utf16.begin(),
::GetFileExInfoStandard, ::GetFileExInfoStandard,
&FileData)) &FileData))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
result = result =
(uint64_t(FileData.nFileSizeHigh) << (sizeof(FileData.nFileSizeLow) * 8)) (uint64_t(FileData.nFileSizeHigh) << (sizeof(FileData.nFileSizeLow) * 8))
+ FileData.nFileSizeLow; + FileData.nFileSizeLow;
return make_error_code(errc::success); return success;
} }
error_code status(const Twine &path, file_status &result) { error_code status(const Twine &path, file_status &result) {
@ -504,13 +504,12 @@ error_code unique_file(const Twine &model, int &result_fd,
// Get a Crypto Provider for CryptGenRandom. // Get a Crypto Provider for CryptGenRandom.
AutoCryptoProvider CryptoProvider; AutoCryptoProvider CryptoProvider;
BOOL success = ::CryptAcquireContextW(&CryptoProvider.CryptoProvider, if (!::CryptAcquireContextW(&CryptoProvider.CryptoProvider,
NULL, NULL,
NULL, NULL,
PROV_RSA_FULL, PROV_RSA_FULL,
0); 0))
if (!success) return windows_error(::GetLastError());
return make_error_code(windows_error(::GetLastError()));
retry_random_path: retry_random_path:
random_path_utf16.set_size(0); random_path_utf16.set_size(0);
@ -520,7 +519,7 @@ retry_random_path:
if (*i == L'%') { if (*i == L'%') {
BYTE val = 0; BYTE val = 0;
if (!::CryptGenRandom(CryptoProvider, 1, &val)) if (!::CryptGenRandom(CryptoProvider, 1, &val))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
random_path_utf16.push_back("0123456789abcdef"[val & 15]); random_path_utf16.push_back("0123456789abcdef"[val & 15]);
} }
else else
@ -543,11 +542,11 @@ retry_create_file:
NULL); NULL);
if (TempFileHandle == INVALID_HANDLE_VALUE) { if (TempFileHandle == INVALID_HANDLE_VALUE) {
// If the file existed, try again, otherwise, error. // If the file existed, try again, otherwise, error.
error_code ec = make_error_code(windows_error(::GetLastError())); error_code ec = windows_error(::GetLastError());
if (ec == error_code(windows_error::file_exists)) if (ec == windows_error::file_exists)
goto retry_random_path; goto retry_random_path;
// Check for non-existing parent directories. // Check for non-existing parent directories.
if (ec == error_code(windows_error::path_not_found)) { if (ec == windows_error::path_not_found) {
// Create the directories using result_path as temp storage. // Create the directories using result_path as temp storage.
if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(), if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(),
random_path_utf16.size(), result_path)) random_path_utf16.size(), result_path))
@ -570,7 +569,7 @@ retry_create_file:
// Create the directory. // Create the directory.
if (!::CreateDirectoryW(dir_to_create_utf16.begin(), NULL)) if (!::CreateDirectoryW(dir_to_create_utf16.begin(), NULL))
return make_error_code(windows_error(::GetLastError())); return windows_error(::GetLastError());
} }
} }
goto retry_create_file; goto retry_create_file;
@ -593,11 +592,11 @@ retry_create_file:
::DeleteFileW(random_path_utf16.begin()); ::DeleteFileW(random_path_utf16.begin());
// MSDN doesn't say anything about _open_osfhandle setting errno or // MSDN doesn't say anything about _open_osfhandle setting errno or
// GetLastError(), so just return invalid_handle. // GetLastError(), so just return invalid_handle.
return make_error_code(windows_error::invalid_handle); return windows_error::invalid_handle;
} }
result_fd = fd; result_fd = fd;
return make_error_code(errc::success); return success;
} }
} // end namespace fs } // end namespace fs
} // end namespace sys } // end namespace sys