Index: llvm/trunk/include/llvm/Support/FileSystem.h =================================================================== --- llvm/trunk/include/llvm/Support/FileSystem.h +++ llvm/trunk/include/llvm/Support/FileSystem.h @@ -817,9 +817,38 @@ return A; } +/// @brief Opens the file with the given name in a write-only or read-write +/// mode, returning its open file descriptor. If the file does not exist, it +/// is created. +/// +/// The caller is responsible for closing the file descriptor once they are +/// finished with it. +/// +/// @param Name The path of the file to open, relative or absolute. +/// @param ResultFD If the file could be opened successfully, its descriptor +/// is stored in this location. Otherwise, this is set to -1. +/// @param Flags Additional flags used to determine whether the file should be +/// opened in, for example, read-write or in write-only mode. +/// @param Mode The access permissions of the file, represented in octal. +/// @returns errc::success if \a Name has been opened, otherwise a +/// platform-specific error_code. std::error_code openFileForWrite(const Twine &Name, int &ResultFD, OpenFlags Flags, unsigned Mode = 0666); +/// @brief Opens the file with the given name in a read-only mode, returning +/// its open file descriptor. +/// +/// The caller is responsible for closing the file descriptor once they are +/// finished with it. +/// +/// @param Name The path of the file to open, relative or absolute. +/// @param ResultFD If the file could be opened successfully, its descriptor +/// is stored in this location. Otherwise, this is set to -1. +/// @param RealPath If nonnull, extra work is done to determine the real path +/// of the opened file, and that path is stored in this +/// location. +/// @returns errc::success if \a Name has been opened, otherwise a +/// platform-specific error_code. std::error_code openFileForRead(const Twine &Name, int &ResultFD, SmallVectorImpl *RealPath = nullptr); Index: llvm/trunk/lib/Support/Windows/Path.inc =================================================================== --- llvm/trunk/lib/Support/Windows/Path.inc +++ llvm/trunk/lib/Support/Windows/Path.inc @@ -1052,6 +1052,7 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD, SmallVectorImpl *RealPath) { + ResultFD = -1; SmallVector PathUTF16; if (std::error_code EC = widenPath(Name, PathUTF16)) @@ -1074,8 +1075,8 @@ return EC; } - int FD = ::_open_osfhandle(intptr_t(H), 0); - if (FD == -1) { + ResultFD = ::_open_osfhandle(intptr_t(H), 0); + if (ResultFD == -1) { ::CloseHandle(H); return mapWindowsError(ERROR_INVALID_HANDLE); } @@ -1084,7 +1085,6 @@ if (RealPath) realPathFromHandle(H, *RealPath); - ResultFD = FD; return std::error_code(); } @@ -1094,6 +1094,7 @@ assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) && "Cannot specify both 'excl' and 'append' file creation flags!"); + ResultFD = -1; SmallVector PathUTF16; if (std::error_code EC = widenPath(Name, PathUTF16)) @@ -1141,13 +1142,12 @@ if (Flags & F_Text) OpenFlags |= _O_TEXT; - int FD = ::_open_osfhandle(intptr_t(H), OpenFlags); - if (FD == -1) { + ResultFD = ::_open_osfhandle(intptr_t(H), OpenFlags); + if (ResultFD == -1) { ::CloseHandle(H); return mapWindowsError(ERROR_INVALID_HANDLE); } - ResultFD = FD; return std::error_code(); }