summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Windows
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2018-06-07 23:25:13 +0000
committerZachary Turner <zturner@google.com>2018-06-07 23:25:13 +0000
commit9d2cfa6cccbe710f724f1a94cb65df9787c8f077 (patch)
treec1e7aa1f44776badff9d3d13717f929b644c5889 /llvm/lib/Support/Windows
parentf0f51755182f6935f44b5f247ad45e38c1130d21 (diff)
downloadbcm5719-llvm-9d2cfa6cccbe710f724f1a94cb65df9787c8f077.tar.gz
bcm5719-llvm-9d2cfa6cccbe710f724f1a94cb65df9787c8f077.zip
Expose a single global file open function.
This one allows much more flexibility than the standard openFileForRead / openFileForWrite functions. Since there is now just one "real" function that does the work, all other implementations simply delegate to this one. llvm-svn: 334246
Diffstat (limited to 'llvm/lib/Support/Windows')
-rw-r--r--llvm/lib/Support/Windows/Path.inc88
1 files changed, 37 insertions, 51 deletions
diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index a7e2a75dc0c..6017fcedc33 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -1102,11 +1102,12 @@ static DWORD nativeAccess(FileAccess Access, OpenFlags Flags) {
return Result;
}
-static Expected<file_t> nativeOpenFile(const Twine &Name, DWORD Disp,
- DWORD Access, DWORD Flags) {
+static std::error_code openNativeFileInternal(const Twine &Name,
+ file_t &ResultFile, DWORD Disp,
+ DWORD Access, DWORD Flags) {
SmallVector<wchar_t, 128> PathUTF16;
if (std::error_code EC = widenPath(Name, PathUTF16))
- return errorCodeToError(EC);
+ return EC;
HANDLE H =
::CreateFileW(PathUTF16.begin(), Access,
@@ -1119,16 +1120,18 @@ static Expected<file_t> nativeOpenFile(const Twine &Name, DWORD Disp,
// This only runs if we failed to open the file, so there is probably
// no performances issues.
if (LastError != ERROR_ACCESS_DENIED)
- return errorCodeToError(EC);
+ return EC;
if (is_directory(Name))
- return errorCodeToError(make_error_code(errc::is_a_directory));
- return errorCodeToError(EC);
+ return make_error_code(errc::is_a_directory);
+ return EC;
}
- return H;
+ ResultFile = H;
+ return std::error_code();
}
-static Expected<file_t> openFile(const Twine &Name, CreationDisposition Disp,
- FileAccess Access, OpenFlags Flags) {
+Expected<file_t> openNativeFile(const Twine &Name, CreationDisposition Disp,
+ FileAccess Access, OpenFlags Flags,
+ unsigned Mode) {
// Verify that we don't have both "append" and "excl".
assert((!(Disp == CD_CreateNew) || !(Flags & OF_Append)) &&
"Cannot specify both 'CreateNew' and 'Append' file creation flags!");
@@ -1137,18 +1140,34 @@ static Expected<file_t> openFile(const Twine &Name, CreationDisposition Disp,
DWORD NativeDisp = nativeDisposition(Disp, Flags);
DWORD NativeAccess = nativeAccess(Access, Flags);
- return nativeOpenFile(Name, NativeDisp, NativeAccess, NativeFlags);
+ file_t Result;
+ std::error_code EC = openNativeFileInternal(Name, Result, NativeDisp,
+ NativeAccess, NativeFlags);
+ if (EC)
+ return errorCodeToError(EC);
+ return Result;
+}
+
+std::error_code openFile(const Twine &Name, int &ResultFD,
+ CreationDisposition Disp, FileAccess Access,
+ OpenFlags Flags, unsigned int Mode) {
+ Expected<file_t> Result = openNativeFile(Name, Disp, Access, Flags);
+ if (!Result)
+ return errorToErrorCode(Result.takeError());
+
+ return nativeFileToFd(*Result, ResultFD, Flags);
}
static std::error_code directoryRealPath(const Twine &Name,
SmallVectorImpl<char> &RealPath) {
- Expected<file_t> EF = nativeOpenFile(Name, OPEN_EXISTING, GENERIC_READ,
- FILE_FLAG_BACKUP_SEMANTICS);
- if (!EF)
- return errorToErrorCode(EF.takeError());
+ file_t File;
+ std::error_code EC = openNativeFileInternal(
+ Name, File, OPEN_EXISTING, GENERIC_READ, FILE_FLAG_BACKUP_SEMANTICS);
+ if (EC)
+ return EC;
- std::error_code EC = realPathFromHandle(*EF, RealPath);
- ::CloseHandle(*EF);
+ EC = realPathFromHandle(File, RealPath);
+ ::CloseHandle(File);
return EC;
}
@@ -1161,8 +1180,8 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
SmallVectorImpl<char> *RealPath) {
-
- Expected<file_t> Result = openFile(Name, CD_OpenExisting, FA_Read, Flags);
+ Expected<file_t> Result =
+ openNativeFile(Name, CD_OpenExisting, FA_Read, Flags);
// Fetch the real name of the file, if the user asked
if (Result && RealPath)
@@ -1171,39 +1190,6 @@ Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
return std::move(Result);
}
-std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
- CreationDisposition Disp, OpenFlags Flags,
- unsigned Mode) {
- Expected<HANDLE> NativeFile = openNativeFileForWrite(Name, Disp, Flags, Mode);
- if (!NativeFile)
- return errorToErrorCode(NativeFile.takeError());
-
- return nativeFileToFd(std::move(NativeFile), ResultFD, Flags);
-}
-
-Expected<file_t> openNativeFileForWrite(const Twine &Name,
- CreationDisposition Disp,
- OpenFlags Flags, unsigned Mode) {
- return openFile(Name, Disp, FA_Write, Flags);
-}
-
-std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD,
- CreationDisposition Disp, OpenFlags Flags,
- unsigned Mode) {
- Expected<HANDLE> NativeFile =
- openNativeFileForReadWrite(Name, Disp, Flags, Mode);
- if (!NativeFile)
- return errorToErrorCode(NativeFile.takeError());
-
- return nativeFileToFd(std::move(NativeFile), ResultFD, Flags);
-}
-
-Expected<file_t> openNativeFileForReadWrite(const Twine &Name,
- CreationDisposition Disp,
- OpenFlags Flags, unsigned Mode) {
- return openFile(Name, Disp, FA_Write | FA_Read, Flags);
-}
-
void closeFile(file_t &F) {
::CloseHandle(F);
F = kInvalidFile;
OpenPOWER on IntegriCloud