From fba8fc209f6d00a0cc011cc93eee2c5c089d0381 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 11 Jul 2019 20:29:32 +0000 Subject: [PATCH] Open native file handles to avoid converting from FDs, NFC Follow up to r365588. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365820 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/LTO/Caching.cpp | 19 ++++++++++--------- lib/LTO/ThinLTOCodeGenerator.cpp | 14 ++++++-------- lib/XRay/InstrumentationMap.cpp | 15 +++++++++------ lib/XRay/Profile.cpp | 12 ++++++------ lib/XRay/Trace.cpp | 13 ++++++------- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/lib/LTO/Caching.cpp b/lib/LTO/Caching.cpp index 7b3fc020d6e..000ab91dba7 100644 --- a/lib/LTO/Caching.cpp +++ b/lib/LTO/Caching.cpp @@ -38,22 +38,23 @@ Expected lto::localCache(StringRef CacheDirectoryPath, SmallString<64> EntryPath; sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key); // First, see if we have a cache hit. - int FD; SmallString<64> ResultPath; - std::error_code EC = sys::fs::openFileForRead( - Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath); - if (!EC) { + Expected FDOrErr = sys::fs::openNativeFileForRead( + Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath); + std::error_code EC; + if (FDOrErr) { ErrorOr> MBOrErr = - MemoryBuffer::getOpenFile(sys::fs::convertFDToNativeFile(FD), - EntryPath, - /*FileSize*/ -1, - /*RequiresNullTerminator*/ false); - close(FD); + MemoryBuffer::getOpenFile(*FDOrErr, EntryPath, + /*FileSize=*/-1, + /*RequiresNullTerminator=*/false); + sys::fs::closeFile(*FDOrErr); if (MBOrErr) { AddBuffer(Task, std::move(*MBOrErr)); return AddStreamFn(); } EC = MBOrErr.getError(); + } else { + EC = errorToErrorCode(FDOrErr.takeError()); } // On Windows we can fail to open a cache file with a permission denied diff --git a/lib/LTO/ThinLTOCodeGenerator.cpp b/lib/LTO/ThinLTOCodeGenerator.cpp index 5c447a14b06..1c52218836c 100644 --- a/lib/LTO/ThinLTOCodeGenerator.cpp +++ b/lib/LTO/ThinLTOCodeGenerator.cpp @@ -349,16 +349,14 @@ public: ErrorOr> tryLoadingBuffer() { if (EntryPath.empty()) return std::error_code(); - int FD; SmallString<64> ResultPath; - std::error_code EC = sys::fs::openFileForRead( - Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath); - if (EC) - return EC; + Expected FDOrErr = sys::fs::openNativeFileForRead( + Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath); + if (!FDOrErr) + return errorToErrorCode(FDOrErr.takeError()); ErrorOr> MBOrErr = MemoryBuffer::getOpenFile( - sys::fs::convertFDToNativeFile(FD), EntryPath, - /*FileSize=*/-1, /*RequiresNullTerminator=*/false); - close(FD); + *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false); + sys::fs::closeFile(*FDOrErr); return MBOrErr; } diff --git a/lib/XRay/InstrumentationMap.cpp b/lib/XRay/InstrumentationMap.cpp index 5b90c5a7ebd..fe5e941f7ea 100644 --- a/lib/XRay/InstrumentationMap.cpp +++ b/lib/XRay/InstrumentationMap.cpp @@ -172,14 +172,14 @@ loadObj(StringRef Filename, object::OwningBinary &ObjFile, } static Error -loadYAML(int Fd, size_t FileSize, StringRef Filename, +loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds) { std::error_code EC; sys::fs::mapped_file_region MappedFile( - sys::fs::convertFDToNativeFile(Fd), - sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); + Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); + sys::fs::closeFile(Fd); if (EC) return make_error( Twine("Failed memory-mapping file '") + Filename + "'.", EC); @@ -215,9 +215,12 @@ llvm::xray::loadInstrumentationMap(StringRef Filename) { if (!ObjectFileOrError) { auto E = ObjectFileOrError.takeError(); // We try to load it as YAML if the ELF load didn't work. - int Fd; - if (sys::fs::openFileForRead(Filename, Fd)) + Expected FdOrErr = sys::fs::openNativeFileForRead(Filename); + if (!FdOrErr) { + // Report the ELF load error if YAML failed. + consumeError(FdOrErr.takeError()); return std::move(E); + } uint64_t FileSize; if (sys::fs::file_size(Filename, FileSize)) @@ -230,7 +233,7 @@ llvm::xray::loadInstrumentationMap(StringRef Filename) { // From this point on the errors will be only for the YAML parts, so we // consume the errors at this point. consumeError(std::move(E)); - if (auto E = loadYAML(Fd, FileSize, Filename, Map.Sleds, + if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds, Map.FunctionAddresses, Map.FunctionIds)) return std::move(E); } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds, diff --git a/lib/XRay/Profile.cpp b/lib/XRay/Profile.cpp index 9ba8eb1088d..e34b182f2e0 100644 --- a/lib/XRay/Profile.cpp +++ b/lib/XRay/Profile.cpp @@ -260,10 +260,9 @@ Profile mergeProfilesByStack(const Profile &L, const Profile &R) { } Expected loadProfile(StringRef Filename) { - int Fd; - if (auto EC = sys::fs::openFileForRead(Filename, Fd)) - return make_error( - Twine("Cannot read profile from '") + Filename + "'", EC); + Expected FdOrErr = sys::fs::openNativeFileForRead(Filename); + if (!FdOrErr) + return FdOrErr.takeError(); uint64_t FileSize; if (auto EC = sys::fs::file_size(Filename, FileSize)) @@ -272,8 +271,9 @@ Expected loadProfile(StringRef Filename) { std::error_code EC; sys::fs::mapped_file_region MappedFile( - sys::fs::convertFDToNativeFile(Fd), - sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); + *FdOrErr, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, + EC); + sys::fs::closeFile(*FdOrErr); if (EC) return make_error( Twine("Cannot mmap profile '") + Filename + "'", EC); diff --git a/lib/XRay/Trace.cpp b/lib/XRay/Trace.cpp index 0945c8e847e..b9b67c561c6 100644 --- a/lib/XRay/Trace.cpp +++ b/lib/XRay/Trace.cpp @@ -371,11 +371,9 @@ Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader, } // namespace Expected llvm::xray::loadTraceFile(StringRef Filename, bool Sort) { - int Fd; - if (auto EC = sys::fs::openFileForRead(Filename, Fd)) { - return make_error( - Twine("Cannot read log from '") + Filename + "'", EC); - } + Expected FdOrErr = sys::fs::openNativeFileForRead(Filename); + if (!FdOrErr) + return FdOrErr.takeError(); uint64_t FileSize; if (auto EC = sys::fs::file_size(Filename, FileSize)) { @@ -391,8 +389,9 @@ Expected llvm::xray::loadTraceFile(StringRef Filename, bool Sort) { // Map the opened file into memory and use a StringRef to access it later. std::error_code EC; sys::fs::mapped_file_region MappedFile( - sys::fs::convertFDToNativeFile(Fd), - sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); + *FdOrErr, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, + EC); + sys::fs::closeFile(*FdOrErr); if (EC) { return make_error( Twine("Cannot read log from '") + Filename + "'", EC); -- 2.40.0