summaryrefslogtreecommitdiff
path: root/lib/Support/Windows/Path.inc
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2013-07-19 15:02:03 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2013-07-19 15:02:03 +0000
commitb1a003f37725a31e5e744c46112b628c5e0aeb8a (patch)
tree836521091143142749d41ea9cb7a14c3d62e3a21 /lib/Support/Windows/Path.inc
parentc9c9825c93791e0b1c0055e1d47ad2e6a703931e (diff)
downloadllvm-b1a003f37725a31e5e744c46112b628c5e0aeb8a.tar.gz
llvm-b1a003f37725a31e5e744c46112b628c5e0aeb8a.tar.bz2
llvm-b1a003f37725a31e5e744c46112b628c5e0aeb8a.tar.xz
Split openFileForWrite into windows and unix versions.
It is similar to 186511, but for creating files for writing. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186679 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows/Path.inc')
-rw-r--r--lib/Support/Windows/Path.inc54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/Support/Windows/Path.inc b/lib/Support/Windows/Path.inc
index dff89c74b1..1be7433871 100644
--- a/lib/Support/Windows/Path.inc
+++ b/lib/Support/Windows/Path.inc
@@ -1071,6 +1071,60 @@ error_code openFileForRead(const Twine &Name, int &ResultFD) {
return error_code::success();
}
+error_code openFileForWrite(const Twine &Name, int &ResultFD,
+ sys::fs::OpenFlags Flags, unsigned Mode) {
+ // Verify that we don't have both "append" and "excl".
+ assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
+ "Cannot specify both 'excl' and 'append' file creation flags!");
+
+ SmallString<128> PathStorage;
+ SmallVector<wchar_t, 128> PathUTF16;
+
+ if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
+ PathUTF16))
+ return EC;
+
+ DWORD CreationDisposition;
+ if (Flags & F_Excl)
+ CreationDisposition = CREATE_NEW;
+ else if (Flags & F_Append)
+ CreationDisposition = OPEN_ALWAYS;
+ else
+ CreationDisposition = CREATE_ALWAYS;
+
+ HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
+ CreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
+
+ if (H == INVALID_HANDLE_VALUE) {
+ error_code EC = windows_error(::GetLastError());
+ // Provide a better error message when trying to open directories.
+ // This only runs if we failed to open the file, so there is probably
+ // no performances issues.
+ if (EC != windows_error::access_denied)
+ return EC;
+ if (is_directory(Name))
+ return error_code(errc::is_a_directory, posix_category());
+ return EC;
+ }
+
+ int OpenFlags = 0;
+ if (Flags & F_Append)
+ OpenFlags |= _O_APPEND;
+
+ if (!(Flags & F_Binary))
+ OpenFlags |= _O_TEXT;
+
+ int FD = ::_open_osfhandle(intptr_t(H), OpenFlags);
+ if (FD == -1) {
+ ::CloseHandle(H);
+ return windows_error::invalid_handle;
+ }
+
+ ResultFD = FD;
+ return error_code::success();
+}
+
} // end namespace fs
} // end namespace sys
} // end namespace llvm