Skip to content

Commit 65d2f2d

Browse files
authored
Merge pull request #8411 from benlangmuir/redirecting-fs-path-changes-stable
[stable/20230725][llvm][vfs] Preserve paths for fallback/fallthrough in RedirectingFileSystem
2 parents 38b55e1 + bde84e9 commit 65d2f2d

File tree

3 files changed

+127
-60
lines changed

3 files changed

+127
-60
lines changed

llvm/include/llvm/Support/VirtualFileSystem.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,12 +1001,12 @@ class RedirectingFileSystem
10011001
/// Canonicalize path by removing ".", "..", "./", components. This is
10021002
/// a VFS request, do not bother about symlinks in the path components
10031003
/// but canonicalize in order to perform the correct entry search.
1004-
std::error_code makeCanonical(SmallVectorImpl<char> &Path) const;
1004+
std::error_code makeCanonicalForLookup(SmallVectorImpl<char> &Path) const;
10051005

10061006
/// Get the File status, or error, from the underlying external file system.
10071007
/// This returns the status with the originally requested name, while looking
1008-
/// up the entry using the canonical path.
1009-
ErrorOr<Status> getExternalStatus(const Twine &CanonicalPath,
1008+
/// up the entry using a potentially different path.
1009+
ErrorOr<Status> getExternalStatus(const Twine &LookupPath,
10101010
const Twine &OriginalPath) const;
10111011

10121012
/// Make \a Path an absolute path.
@@ -1094,7 +1094,7 @@ class RedirectingFileSystem
10941094
llvm::SmallVectorImpl<Entry *> &Entries) const;
10951095

10961096
/// Get the status for a path with the provided \c LookupResult.
1097-
ErrorOr<Status> status(const Twine &CanonicalPath, const Twine &OriginalPath,
1097+
ErrorOr<Status> status(const Twine &LookupPath, const Twine &OriginalPath,
10981098
const LookupResult &Result);
10991099

11001100
public:

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ std::error_code RedirectingFileSystem::isLocal(const Twine &Path_,
13731373
SmallString<256> Path;
13741374
Path_.toVector(Path);
13751375

1376-
if (std::error_code EC = makeCanonical(Path))
1376+
if (makeAbsolute(Path))
13771377
return {};
13781378

13791379
return ExternalFS->isLocal(Path, Result);
@@ -1440,7 +1440,7 @@ directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir,
14401440
SmallString<256> Path;
14411441
Dir.toVector(Path);
14421442

1443-
EC = makeCanonical(Path);
1443+
EC = makeAbsolute(Path);
14441444
if (EC)
14451445
return {};
14461446

@@ -2290,8 +2290,8 @@ void RedirectingFileSystem::LookupResult::getPath(
22902290
llvm::sys::path::append(Result, E->getName());
22912291
}
22922292

2293-
std::error_code
2294-
RedirectingFileSystem::makeCanonical(SmallVectorImpl<char> &Path) const {
2293+
std::error_code RedirectingFileSystem::makeCanonicalForLookup(
2294+
SmallVectorImpl<char> &Path) const {
22952295
if (std::error_code EC = makeAbsolute(Path))
22962296
return EC;
22972297

@@ -2306,12 +2306,16 @@ RedirectingFileSystem::makeCanonical(SmallVectorImpl<char> &Path) const {
23062306

23072307
ErrorOr<RedirectingFileSystem::LookupResult>
23082308
RedirectingFileSystem::lookupPath(StringRef Path) const {
2309+
llvm::SmallString<128> CanonicalPath(Path);
2310+
if (std::error_code EC = makeCanonicalForLookup(CanonicalPath))
2311+
return EC;
2312+
23092313
// RedirectOnly means the VFS is always used.
23102314
if (UsageTrackingActive && Redirection == RedirectKind::RedirectOnly)
23112315
HasBeenUsed = true;
23122316

2313-
sys::path::const_iterator Start = sys::path::begin(Path);
2314-
sys::path::const_iterator End = sys::path::end(Path);
2317+
sys::path::const_iterator Start = sys::path::begin(CanonicalPath);
2318+
sys::path::const_iterator End = sys::path::end(CanonicalPath);
23152319
llvm::SmallVector<Entry *, 32> Entries;
23162320
for (const auto &Root : Roots) {
23172321
ErrorOr<RedirectingFileSystem::LookupResult> Result =
@@ -2388,14 +2392,14 @@ static Status getRedirectedFileStatus(const Twine &OriginalPath,
23882392
}
23892393

23902394
ErrorOr<Status> RedirectingFileSystem::status(
2391-
const Twine &CanonicalPath, const Twine &OriginalPath,
2395+
const Twine &LookupPath, const Twine &OriginalPath,
23922396
const RedirectingFileSystem::LookupResult &Result) {
23932397
if (std::optional<StringRef> ExtRedirect = Result.getExternalRedirect()) {
2394-
SmallString<256> CanonicalRemappedPath((*ExtRedirect).str());
2395-
if (std::error_code EC = makeCanonical(CanonicalRemappedPath))
2398+
SmallString<256> RemappedPath((*ExtRedirect).str());
2399+
if (std::error_code EC = makeAbsolute(RemappedPath))
23962400
return EC;
23972401

2398-
ErrorOr<Status> S = ExternalFS->status(CanonicalRemappedPath);
2402+
ErrorOr<Status> S = ExternalFS->status(RemappedPath);
23992403
if (!S)
24002404
return S;
24012405
S = Status::copyWithNewName(*S, *ExtRedirect);
@@ -2405,13 +2409,13 @@ ErrorOr<Status> RedirectingFileSystem::status(
24052409
}
24062410

24072411
auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(Result.E);
2408-
return Status::copyWithNewName(DE->getStatus(), CanonicalPath);
2412+
return Status::copyWithNewName(DE->getStatus(), LookupPath);
24092413
}
24102414

24112415
ErrorOr<Status>
2412-
RedirectingFileSystem::getExternalStatus(const Twine &CanonicalPath,
2416+
RedirectingFileSystem::getExternalStatus(const Twine &LookupPath,
24132417
const Twine &OriginalPath) const {
2414-
auto Result = ExternalFS->status(CanonicalPath);
2418+
auto Result = ExternalFS->status(LookupPath);
24152419

24162420
// The path has been mapped by some nested VFS, don't override it with the
24172421
// original path.
@@ -2421,65 +2425,63 @@ RedirectingFileSystem::getExternalStatus(const Twine &CanonicalPath,
24212425
}
24222426

24232427
ErrorOr<Status> RedirectingFileSystem::status(const Twine &OriginalPath) {
2424-
SmallString<256> CanonicalPath;
2425-
OriginalPath.toVector(CanonicalPath);
2428+
SmallString<256> Path;
2429+
OriginalPath.toVector(Path);
24262430

2427-
if (std::error_code EC = makeCanonical(CanonicalPath))
2431+
if (std::error_code EC = makeAbsolute(Path))
24282432
return EC;
24292433

24302434
if (Redirection == RedirectKind::Fallback) {
24312435
// Attempt to find the original file first, only falling back to the
24322436
// mapped file if that fails.
2433-
ErrorOr<Status> S = getExternalStatus(CanonicalPath, OriginalPath);
2437+
ErrorOr<Status> S = getExternalStatus(Path, OriginalPath);
24342438
if (S)
24352439
return S;
24362440
}
24372441

2438-
ErrorOr<RedirectingFileSystem::LookupResult> Result =
2439-
lookupPath(CanonicalPath);
2442+
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
24402443
if (!Result) {
24412444
// Was not able to map file, fallthrough to using the original path if
24422445
// that was the specified redirection type.
24432446
if (Redirection == RedirectKind::Fallthrough &&
24442447
isFileNotFound(Result.getError()))
2445-
return getExternalStatus(CanonicalPath, OriginalPath);
2448+
return getExternalStatus(Path, OriginalPath);
24462449
return Result.getError();
24472450
}
24482451

2449-
ErrorOr<Status> S = status(CanonicalPath, OriginalPath, *Result);
2452+
ErrorOr<Status> S = status(Path, OriginalPath, *Result);
24502453
if (!S && Redirection == RedirectKind::Fallthrough &&
24512454
isFileNotFound(S.getError(), Result->E)) {
24522455
// Mapped the file but it wasn't found in the underlying filesystem,
24532456
// fallthrough to using the original path if that was the specified
24542457
// redirection type.
2455-
return getExternalStatus(CanonicalPath, OriginalPath);
2458+
return getExternalStatus(Path, OriginalPath);
24562459
}
24572460

24582461
return S;
24592462
}
24602463

24612464
bool RedirectingFileSystem::exists(const Twine &OriginalPath) {
2462-
SmallString<256> CanonicalPath;
2463-
OriginalPath.toVector(CanonicalPath);
2465+
SmallString<256> Path;
2466+
OriginalPath.toVector(Path);
24642467

2465-
if (makeCanonical(CanonicalPath))
2468+
if (makeAbsolute(Path))
24662469
return false;
24672470

24682471
if (Redirection == RedirectKind::Fallback) {
24692472
// Attempt to find the original file first, only falling back to the
24702473
// mapped file if that fails.
2471-
if (ExternalFS->exists(CanonicalPath))
2474+
if (ExternalFS->exists(Path))
24722475
return true;
24732476
}
24742477

2475-
ErrorOr<RedirectingFileSystem::LookupResult> Result =
2476-
lookupPath(CanonicalPath);
2478+
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
24772479
if (!Result) {
24782480
// Was not able to map file, fallthrough to using the original path if
24792481
// that was the specified redirection type.
24802482
if (Redirection == RedirectKind::Fallthrough &&
24812483
isFileNotFound(Result.getError()))
2482-
return ExternalFS->exists(CanonicalPath);
2484+
return ExternalFS->exists(Path);
24832485
return false;
24842486
}
24852487

@@ -2489,18 +2491,18 @@ bool RedirectingFileSystem::exists(const Twine &OriginalPath) {
24892491
return true;
24902492
}
24912493

2492-
SmallString<256> CanonicalRemappedPath((*ExtRedirect).str());
2493-
if (makeCanonical(CanonicalRemappedPath))
2494+
SmallString<256> RemappedPath((*ExtRedirect).str());
2495+
if (makeAbsolute(RemappedPath))
24942496
return false;
24952497

2496-
if (ExternalFS->exists(CanonicalRemappedPath))
2498+
if (ExternalFS->exists(RemappedPath))
24972499
return true;
24982500

24992501
if (Redirection == RedirectKind::Fallthrough) {
25002502
// Mapped the file but it wasn't found in the underlying filesystem,
25012503
// fallthrough to using the original path if that was the specified
25022504
// redirection type.
2503-
return ExternalFS->exists(CanonicalPath);
2505+
return ExternalFS->exists(Path);
25042506
}
25052507

25062508
return false;
@@ -2549,53 +2551,49 @@ File::getWithPath(ErrorOr<std::unique_ptr<File>> Result, const Twine &P) {
25492551

25502552
ErrorOr<std::unique_ptr<File>>
25512553
RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) {
2552-
SmallString<256> CanonicalPath;
2553-
OriginalPath.toVector(CanonicalPath);
2554+
SmallString<256> Path;
2555+
OriginalPath.toVector(Path);
25542556

2555-
if (std::error_code EC = makeCanonical(CanonicalPath))
2557+
if (std::error_code EC = makeAbsolute(Path))
25562558
return EC;
25572559

25582560
if (Redirection == RedirectKind::Fallback) {
25592561
// Attempt to find the original file first, only falling back to the
25602562
// mapped file if that fails.
2561-
auto F = File::getWithPath(ExternalFS->openFileForRead(CanonicalPath),
2562-
OriginalPath);
2563+
auto F = File::getWithPath(ExternalFS->openFileForRead(Path), OriginalPath);
25632564
if (F)
25642565
return F;
25652566
}
25662567

2567-
ErrorOr<RedirectingFileSystem::LookupResult> Result =
2568-
lookupPath(CanonicalPath);
2568+
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
25692569
if (!Result) {
25702570
// Was not able to map file, fallthrough to using the original path if
25712571
// that was the specified redirection type.
25722572
if (Redirection == RedirectKind::Fallthrough &&
25732573
isFileNotFound(Result.getError()))
2574-
return File::getWithPath(ExternalFS->openFileForRead(CanonicalPath),
2575-
OriginalPath);
2574+
return File::getWithPath(ExternalFS->openFileForRead(Path), OriginalPath);
25762575
return Result.getError();
25772576
}
25782577

25792578
if (!Result->getExternalRedirect()) // FIXME: errc::not_a_file?
25802579
return make_error_code(llvm::errc::invalid_argument);
25812580

25822581
StringRef ExtRedirect = *Result->getExternalRedirect();
2583-
SmallString<256> CanonicalRemappedPath(ExtRedirect.str());
2584-
if (std::error_code EC = makeCanonical(CanonicalRemappedPath))
2582+
SmallString<256> RemappedPath(ExtRedirect.str());
2583+
if (std::error_code EC = makeAbsolute(RemappedPath))
25852584
return EC;
25862585

25872586
auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
25882587

2589-
auto ExternalFile = File::getWithPath(
2590-
ExternalFS->openFileForRead(CanonicalRemappedPath), ExtRedirect);
2588+
auto ExternalFile =
2589+
File::getWithPath(ExternalFS->openFileForRead(RemappedPath), ExtRedirect);
25912590
if (!ExternalFile) {
25922591
if (Redirection == RedirectKind::Fallthrough &&
25932592
isFileNotFound(ExternalFile.getError(), Result->E)) {
25942593
// Mapped the file but it wasn't found in the underlying filesystem,
25952594
// fallthrough to using the original path if that was the specified
25962595
// redirection type.
2597-
return File::getWithPath(ExternalFS->openFileForRead(CanonicalPath),
2598-
OriginalPath);
2596+
return File::getWithPath(ExternalFS->openFileForRead(Path), OriginalPath);
25992597
}
26002598
return ExternalFile;
26012599
}
@@ -2615,28 +2613,27 @@ RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) {
26152613
std::error_code
26162614
RedirectingFileSystem::getRealPath(const Twine &OriginalPath,
26172615
SmallVectorImpl<char> &Output) const {
2618-
SmallString<256> CanonicalPath;
2619-
OriginalPath.toVector(CanonicalPath);
2616+
SmallString<256> Path;
2617+
OriginalPath.toVector(Path);
26202618

2621-
if (std::error_code EC = makeCanonical(CanonicalPath))
2619+
if (std::error_code EC = makeAbsolute(Path))
26222620
return EC;
26232621

26242622
if (Redirection == RedirectKind::Fallback) {
26252623
// Attempt to find the original file first, only falling back to the
26262624
// mapped file if that fails.
2627-
std::error_code EC = ExternalFS->getRealPath(CanonicalPath, Output);
2625+
std::error_code EC = ExternalFS->getRealPath(Path, Output);
26282626
if (!EC)
26292627
return EC;
26302628
}
26312629

2632-
ErrorOr<RedirectingFileSystem::LookupResult> Result =
2633-
lookupPath(CanonicalPath);
2630+
ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
26342631
if (!Result) {
26352632
// Was not able to map file, fallthrough to using the original path if
26362633
// that was the specified redirection type.
26372634
if (Redirection == RedirectKind::Fallthrough &&
26382635
isFileNotFound(Result.getError()))
2639-
return ExternalFS->getRealPath(CanonicalPath, Output);
2636+
return ExternalFS->getRealPath(Path, Output);
26402637
return Result.getError();
26412638
}
26422639

@@ -2649,7 +2646,7 @@ RedirectingFileSystem::getRealPath(const Twine &OriginalPath,
26492646
// Mapped the file but it wasn't found in the underlying filesystem,
26502647
// fallthrough to using the original path if that was the specified
26512648
// redirection type.
2652-
return ExternalFS->getRealPath(CanonicalPath, Output);
2649+
return ExternalFS->getRealPath(Path, Output);
26532650
}
26542651
return P;
26552652
}

0 commit comments

Comments
 (0)