//=== is guaranteed to work on *all* Windows variants.
//===----------------------------------------------------------------------===//
+#include "llvm/ADT/STLExtras.h"
#include "Windows.h"
#include <fcntl.h>
#include <io.h>
return error_code::success();
}
-error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
- // Get arguments.
- SmallString<128> from_storage;
- SmallString<128> to_storage;
- StringRef f = from.toStringRef(from_storage);
- StringRef t = to.toStringRef(to_storage);
-
- // Convert to utf-16.
- SmallVector<wchar_t, 128> wide_from;
- SmallVector<wchar_t, 128> wide_to;
- if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
- if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
-
- // Copy the file.
- BOOL res = ::CopyFileW(wide_from.begin(), wide_to.begin(),
- copt != copy_option::overwrite_if_exists);
-
- if (res == 0)
- return windows_error(::GetLastError());
-
- return error_code::success();
-}
-
error_code create_directory(const Twine &path, bool &existed) {
SmallString<128> path_storage;
SmallVector<wchar_t, 128> path_utf16;
return true;
// Then compare against the list of ancient reserved names
- for (size_t i = 0; i < sizeof(sReservedNames) / sizeof(const char *); ++i) {
+ for (size_t i = 0; i < array_lengthof(sReservedNames); ++i) {
if (path.equals_lower(sReservedNames[i]))
return true;
}
return false;
}
+static error_code getStatus(HANDLE FileHandle, file_status &Result) {
+ if (FileHandle == INVALID_HANDLE_VALUE)
+ goto handle_status_error;
+
+ switch (::GetFileType(FileHandle)) {
+ default:
+ llvm_unreachable("Don't know anything about this file type");
+ case FILE_TYPE_UNKNOWN: {
+ DWORD Err = ::GetLastError();
+ if (Err != NO_ERROR)
+ return windows_error(Err);
+ Result = file_status(file_type::type_unknown);
+ return error_code::success();
+ }
+ case FILE_TYPE_DISK:
+ break;
+ case FILE_TYPE_CHAR:
+ Result = file_status(file_type::character_file);
+ return error_code::success();
+ case FILE_TYPE_PIPE:
+ Result = file_status(file_type::fifo_file);
+ return error_code::success();
+ }
+
+ BY_HANDLE_FILE_INFORMATION Info;
+ if (!::GetFileInformationByHandle(FileHandle, &Info))
+ goto handle_status_error;
+
+ Result = file_status(
+ file_type::regular_file, Info.ftLastWriteTime.dwHighDateTime,
+ Info.ftLastWriteTime.dwLowDateTime, Info.dwVolumeSerialNumber,
+ Info.nFileSizeHigh, Info.nFileSizeLow, Info.nFileIndexHigh,
+ Info.nFileIndexLow);
+ return error_code::success();
+
+handle_status_error:
+ error_code EC = windows_error(::GetLastError());
+ if (EC == windows_error::file_not_found ||
+ EC == windows_error::path_not_found)
+ Result = file_status(file_type::file_not_found);
+ else if (EC == windows_error::sharing_violation)
+ Result = file_status(file_type::type_unknown);
+ else {
+ Result = file_status(file_type::status_error);
+ return EC;
+ }
+ return error_code::success();
+}
+
error_code status(const Twine &path, file_status &result) {
SmallString<128> path_storage;
SmallVector<wchar_t, 128> path_utf16;
DWORD attr = ::GetFileAttributesW(path_utf16.begin());
if (attr == INVALID_FILE_ATTRIBUTES)
- goto handle_status_error;
+ return getStatus(INVALID_HANDLE_VALUE, result);
// Handle reparse points.
if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
FILE_FLAG_BACKUP_SEMANTICS,
0));
if (!h)
- goto handle_status_error;
+ return getStatus(INVALID_HANDLE_VALUE, result);
}
if (attr & FILE_ATTRIBUTE_DIRECTORY)
result = file_status(file_type::directory_file);
else {
- result = file_status(file_type::regular_file);
ScopedFileHandle h(
::CreateFileW(path_utf16.begin(),
0, // Attributes only.
FILE_FLAG_BACKUP_SEMANTICS,
0));
if (!h)
- goto handle_status_error;
+ return getStatus(INVALID_HANDLE_VALUE, result);
BY_HANDLE_FILE_INFORMATION Info;
if (!::GetFileInformationByHandle(h, &Info))
- goto handle_status_error;
- result.FileIndexHigh = Info.nFileIndexHigh;
- result.FileIndexLow = Info.nFileIndexLow;
- result.FileSizeHigh = Info.nFileSizeHigh;
- result.FileSizeLow = Info.nFileSizeLow;
- result.LastWriteTimeHigh = Info.ftLastWriteTime.dwHighDateTime;
- result.LastWriteTimeLow = Info.ftLastWriteTime.dwLowDateTime;
- result.VolumeSerialNumber = Info.dwVolumeSerialNumber;
- }
+ return getStatus(INVALID_HANDLE_VALUE, result);
- return error_code::success();
-
-handle_status_error:
- error_code ec = windows_error(::GetLastError());
- if (ec == windows_error::file_not_found ||
- ec == windows_error::path_not_found)
- result = file_status(file_type::file_not_found);
- else if (ec == windows_error::sharing_violation)
- result = file_status(file_type::type_unknown);
- else {
- result = file_status(file_type::status_error);
- return ec;
+ return getStatus(h, result);
}
-
return error_code::success();
}
+error_code status(int FD, file_status &Result) {
+ HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
+ return getStatus(FileHandle, Result);
+}
+
error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
ULARGE_INTEGER UI;
UI.QuadPart = Time.toWin32Time();
case readonly: flprotect = PAGE_READONLY; break;
case readwrite: flprotect = PAGE_READWRITE; break;
case priv: flprotect = PAGE_WRITECOPY; break;
- default: llvm_unreachable("invalid mapping mode");
}
FileMappingHandle = ::CreateFileMapping(FileHandle,
case readonly: dwDesiredAccess = FILE_MAP_READ; break;
case readwrite: dwDesiredAccess = FILE_MAP_WRITE; break;
case priv: dwDesiredAccess = FILE_MAP_COPY; break;
- default: llvm_unreachable("invalid mapping mode");
}
Mapping = ::MapViewOfFile(FileMappingHandle,
dwDesiredAccess,
return windows_error::invalid_function;
}
+error_code openFileForRead(const Twine &Name, int &ResultFD) {
+ SmallString<128> PathStorage;
+ SmallVector<wchar_t, 128> PathUTF16;
+
+ if (error_code EC = UTF8ToUTF16(Name.toStringRef(PathStorage),
+ PathUTF16))
+ return EC;
+
+ HANDLE H = ::CreateFileW(PathUTF16.begin(), GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
+ OPEN_EXISTING, 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 FD = ::_open_osfhandle(intptr_t(H), 0);
+ if (FD == -1) {
+ ::CloseHandle(H);
+ return windows_error::invalid_handle;
+ }
+
+ ResultFD = FD;
+ 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