@@ -1016,7 +1016,6 @@ RedirectingFileSystem::RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
1016
1016
if (auto ExternalWorkingDirectory =
1017
1017
ExternalFS->getCurrentWorkingDirectory ()) {
1018
1018
WorkingDirectory = *ExternalWorkingDirectory;
1019
- ExternalFSValidWD = true ;
1020
1019
}
1021
1020
}
1022
1021
@@ -1075,12 +1074,6 @@ RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
1075
1074
if (!exists (Path))
1076
1075
return errc::no_such_file_or_directory;
1077
1076
1078
- // Always change the external FS but ignore its result.
1079
- if (ExternalFS) {
1080
- auto EC = ExternalFS->setCurrentWorkingDirectory (Path);
1081
- ExternalFSValidWD = !static_cast <bool >(EC);
1082
- }
1083
-
1084
1077
SmallString<128 > AbsolutePath;
1085
1078
Path.toVector (AbsolutePath);
1086
1079
if (std::error_code EC = makeAbsolute (AbsolutePath))
@@ -1089,8 +1082,14 @@ RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
1089
1082
return {};
1090
1083
}
1091
1084
1092
- std::error_code RedirectingFileSystem::isLocal (const Twine &Path ,
1085
+ std::error_code RedirectingFileSystem::isLocal (const Twine &Path_ ,
1093
1086
bool &Result) {
1087
+ SmallString<256 > Path;
1088
+ Path_.toVector (Path);
1089
+
1090
+ if (std::error_code EC = makeCanonical (Path))
1091
+ return {};
1092
+
1094
1093
return ExternalFS->isLocal (Path, Result);
1095
1094
}
1096
1095
@@ -1125,14 +1124,21 @@ std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path)
1125
1124
1126
1125
directory_iterator RedirectingFileSystem::dir_begin (const Twine &Dir,
1127
1126
std::error_code &EC) {
1128
- ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath (Dir);
1127
+ SmallString<256 > Path;
1128
+ Dir.toVector (Path);
1129
+
1130
+ EC = makeCanonical (Path);
1131
+ if (EC)
1132
+ return {};
1133
+
1134
+ ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath (Path);
1129
1135
if (!E) {
1130
1136
EC = E.getError ();
1131
1137
if (shouldUseExternalFS () && EC == errc::no_such_file_or_directory)
1132
- return ExternalFS->dir_begin (Dir , EC);
1138
+ return ExternalFS->dir_begin (Path , EC);
1133
1139
return {};
1134
1140
}
1135
- ErrorOr<Status> S = status (Dir , *E);
1141
+ ErrorOr<Status> S = status (Path , *E);
1136
1142
if (!S) {
1137
1143
EC = S.getError ();
1138
1144
return {};
@@ -1145,7 +1151,7 @@ directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir,
1145
1151
1146
1152
auto *D = cast<RedirectingFileSystem::RedirectingDirectoryEntry>(*E);
1147
1153
return directory_iterator (std::make_shared<VFSFromYamlDirIterImpl>(
1148
- Dir , D->contents_begin (), D->contents_end (),
1154
+ Path , D->contents_begin (), D->contents_end (),
1149
1155
/* IterateExternalFS=*/ shouldUseExternalFS (), *ExternalFS, EC));
1150
1156
}
1151
1157
@@ -1739,22 +1745,22 @@ std::unique_ptr<RedirectingFileSystem> RedirectingFileSystem::create(
1739
1745
return FS;
1740
1746
}
1741
1747
1742
- ErrorOr<RedirectingFileSystem::Entry *>
1743
- RedirectingFileSystem::lookupPath (const Twine &Path_) const {
1744
- SmallString<256 > Path;
1745
- Path_.toVector (Path);
1746
-
1747
- // Handle relative paths
1748
+ std::error_code
1749
+ RedirectingFileSystem::makeCanonical (SmallVectorImpl<char > &Path) const {
1748
1750
if (std::error_code EC = makeAbsolute (Path))
1749
1751
return EC;
1750
1752
1751
- // Canonicalize path by removing ".", "..", "./", components. This is
1752
- // a VFS request, do not bother about symlinks in the path components
1753
- // but canonicalize in order to perform the correct entry search.
1754
- Path = canonicalize (Path);
1755
- if (Path.empty ())
1753
+ llvm::SmallString<256 > CanonicalPath =
1754
+ canonicalize (StringRef (Path.data (), Path.size ()));
1755
+ if (CanonicalPath.empty ())
1756
1756
return make_error_code (llvm::errc::invalid_argument);
1757
1757
1758
+ Path.assign (CanonicalPath.begin (), CanonicalPath.end ());
1759
+ return {};
1760
+ }
1761
+
1762
+ ErrorOr<RedirectingFileSystem::Entry *>
1763
+ RedirectingFileSystem::lookupPath (StringRef Path) const {
1758
1764
sys::path::const_iterator Start = sys::path::begin (Path);
1759
1765
sys::path::const_iterator End = sys::path::end (Path);
1760
1766
for (const auto &Root : Roots) {
@@ -1829,7 +1835,13 @@ ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path,
1829
1835
}
1830
1836
}
1831
1837
1832
- ErrorOr<Status> RedirectingFileSystem::status (const Twine &Path) {
1838
+ ErrorOr<Status> RedirectingFileSystem::status (const Twine &Path_) {
1839
+ SmallString<256 > Path;
1840
+ Path_.toVector (Path);
1841
+
1842
+ if (std::error_code EC = makeCanonical (Path))
1843
+ return EC;
1844
+
1833
1845
ErrorOr<RedirectingFileSystem::Entry *> Result = lookupPath (Path);
1834
1846
if (!Result) {
1835
1847
if (shouldUseExternalFS () &&
@@ -1867,7 +1879,13 @@ class FileWithFixedStatus : public File {
1867
1879
} // namespace
1868
1880
1869
1881
ErrorOr<std::unique_ptr<File>>
1870
- RedirectingFileSystem::openFileForRead (const Twine &Path) {
1882
+ RedirectingFileSystem::openFileForRead (const Twine &Path_) {
1883
+ SmallString<256 > Path;
1884
+ Path_.toVector (Path);
1885
+
1886
+ if (std::error_code EC = makeCanonical (Path))
1887
+ return EC;
1888
+
1871
1889
ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath (Path);
1872
1890
if (!E) {
1873
1891
if (shouldUseExternalFS () &&
@@ -1897,8 +1915,14 @@ RedirectingFileSystem::openFileForRead(const Twine &Path) {
1897
1915
}
1898
1916
1899
1917
std::error_code
1900
- RedirectingFileSystem::getRealPath (const Twine &Path ,
1918
+ RedirectingFileSystem::getRealPath (const Twine &Path_ ,
1901
1919
SmallVectorImpl<char > &Output) const {
1920
+ SmallString<256 > Path;
1921
+ Path_.toVector (Path);
1922
+
1923
+ if (std::error_code EC = makeCanonical (Path))
1924
+ return EC;
1925
+
1902
1926
ErrorOr<RedirectingFileSystem::Entry *> Result = lookupPath (Path);
1903
1927
if (!Result) {
1904
1928
if (shouldUseExternalFS () &&
0 commit comments