diff options
author | Ben Langmuir <blangmuir@apple.com> | 2023-04-25 11:04:06 -0700 |
---|---|---|
committer | Ben Langmuir <blangmuir@apple.com> | 2023-05-02 09:39:07 -0700 |
commit | 5437a4c5e90886c6688da90eaf70617d50e64895 (patch) | |
tree | e1cc1410611cac53bc7221e8fe1a69eb2f089158 /llvm/lib/Support/VirtualFileSystem.cpp | |
parent | 64888d437c9e0dd52adad9b26784369a4ea24b7f (diff) | |
download | llvm-5437a4c5e90886c6688da90eaf70617d50e64895.zip llvm-5437a4c5e90886c6688da90eaf70617d50e64895.tar.gz llvm-5437a4c5e90886c6688da90eaf70617d50e64895.tar.bz2 |
[llvm][vfs] Avoid silent fallback to process-wide working directory
In createPhysicalFileSystem, preserve the per-instance working
directory, even after the first call to getcwd fails.
rdar://108213753
Differential Revision: https://reviews.llvm.org/D149173
Diffstat (limited to 'llvm/lib/Support/VirtualFileSystem.cpp')
-rw-r--r-- | llvm/lib/Support/VirtualFileSystem.cpp | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp index a167e0a..bcf5389 100644 --- a/llvm/lib/Support/VirtualFileSystem.cpp +++ b/llvm/lib/Support/VirtualFileSystem.cpp @@ -257,12 +257,12 @@ public: explicit RealFileSystem(bool LinkCWDToProcess) { if (!LinkCWDToProcess) { SmallString<128> PWD, RealPWD; - if (llvm::sys::fs::current_path(PWD)) - return; // Awful, but nothing to do here. - if (llvm::sys::fs::real_path(PWD, RealPWD)) - WD = {PWD, PWD}; + if (std::error_code EC = llvm::sys::fs::current_path(PWD)) + WD = EC; + else if (llvm::sys::fs::real_path(PWD, RealPWD)) + WD = WorkingDirectory{PWD, PWD}; else - WD = {PWD, RealPWD}; + WD = WorkingDirectory{PWD, RealPWD}; } } @@ -284,10 +284,10 @@ private: // If this FS has its own working dir, use it to make Path absolute. // The returned twine is safe to use as long as both Storage and Path live. Twine adjustPath(const Twine &Path, SmallVectorImpl<char> &Storage) const { - if (!WD) + if (!WD || !*WD) return Path; Path.toVector(Storage); - sys::fs::make_absolute(WD->Resolved, Storage); + sys::fs::make_absolute(WD->get().Resolved, Storage); return Storage; } @@ -297,7 +297,7 @@ private: // The current working directory, with links resolved. (readlink .). SmallString<128> Resolved; }; - std::optional<WorkingDirectory> WD; + std::optional<llvm::ErrorOr<WorkingDirectory>> WD; }; } // namespace @@ -323,8 +323,10 @@ RealFileSystem::openFileForRead(const Twine &Name) { } llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const { + if (WD && *WD) + return std::string(WD->get().Specified.str()); if (WD) - return std::string(WD->Specified.str()); + return WD->getError(); SmallString<128> Dir; if (std::error_code EC = llvm::sys::fs::current_path(Dir)) @@ -345,7 +347,7 @@ std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) { return std::make_error_code(std::errc::not_a_directory); if (auto Err = llvm::sys::fs::real_path(Absolute, Resolved)) return Err; - WD = {Absolute, Resolved}; + WD = WorkingDirectory{Absolute, Resolved}; return std::error_code(); } |