2018-03-29 06:09:09 +08:00
|
|
|
//===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
|
2010-11-24 03:19:34 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-11-24 03:19:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the FileSystemStatCache interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/FileSystemStatCache.h"
|
2018-03-29 06:09:09 +08:00
|
|
|
#include "llvm/Support/Chrono.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2018-03-29 06:09:09 +08:00
|
|
|
#include <utility>
|
2010-11-24 05:53:56 +08:00
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2018-03-29 06:09:09 +08:00
|
|
|
void FileSystemStatCache::anchor() {}
|
2011-12-20 10:48:34 +08:00
|
|
|
|
2010-11-24 05:17:56 +08:00
|
|
|
/// FileSystemStatCache::get - Get the 'stat' information for the specified
|
2010-12-02 12:27:29 +08:00
|
|
|
/// path, using the cache to accelerate it if possible. This returns true if
|
2010-11-24 05:17:56 +08:00
|
|
|
/// the path does not exist or false if it exists.
|
|
|
|
///
|
2012-12-11 15:48:23 +08:00
|
|
|
/// If isFile is true, then this lookup should only return success for files
|
|
|
|
/// (not directories). If it is false this lookup should only return
|
2010-11-24 05:17:56 +08:00
|
|
|
/// success for directories (not files). On a successful file lookup, the
|
|
|
|
/// implementation can optionally fill in FileDescriptor with a valid
|
|
|
|
/// descriptor and the client guarantees that it will close it.
|
2019-04-17 01:34:26 +08:00
|
|
|
std::error_code
|
|
|
|
FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
|
|
|
|
bool isFile, std::unique_ptr<llvm::vfs::File> *F,
|
|
|
|
FileSystemStatCache *Cache,
|
|
|
|
llvm::vfs::FileSystem &FS) {
|
2012-12-11 15:48:23 +08:00
|
|
|
bool isForDir = !isFile;
|
2019-04-17 01:34:26 +08:00
|
|
|
std::error_code RetCode;
|
2010-11-24 06:32:37 +08:00
|
|
|
|
|
|
|
// If we have a cache, use it to resolve the stat query.
|
2010-11-24 05:17:56 +08:00
|
|
|
if (Cache)
|
2019-04-17 01:34:26 +08:00
|
|
|
RetCode = Cache->getStat(Path, Status, isFile, F, FS);
|
2014-02-21 05:59:23 +08:00
|
|
|
else if (isForDir || !F) {
|
2012-12-11 15:48:23 +08:00
|
|
|
// If this is a directory or a file descriptor is not needed and we have
|
|
|
|
// no cache, just go to the file system.
|
2019-03-05 10:27:12 +08:00
|
|
|
llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);
|
|
|
|
if (!StatusOrErr) {
|
2019-04-17 01:34:26 +08:00
|
|
|
RetCode = StatusOrErr.getError();
|
2013-08-02 05:42:11 +08:00
|
|
|
} else {
|
2019-03-05 10:27:12 +08:00
|
|
|
Status = *StatusOrErr;
|
2013-08-02 05:42:11 +08:00
|
|
|
}
|
2010-11-24 06:32:37 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, we have to go to the filesystem. We can always just use
|
|
|
|
// 'stat' here, but (for files) the client is asking whether the file exists
|
|
|
|
// because it wants to turn around and *open* it. It is more efficient to
|
|
|
|
// do "open+fstat" on success than it is to do "stat+open".
|
|
|
|
//
|
|
|
|
// Because of this, check to see if the file exists with 'open'. If the
|
|
|
|
// open succeeds, use fstat to get the stat info.
|
2014-10-27 06:44:13 +08:00
|
|
|
auto OwnedFile = FS.openFileForRead(Path);
|
2013-07-17 03:44:23 +08:00
|
|
|
|
2014-10-27 06:44:13 +08:00
|
|
|
if (!OwnedFile) {
|
2010-11-24 06:32:37 +08:00
|
|
|
// If the open fails, our "stat" fails.
|
2019-04-17 01:34:26 +08:00
|
|
|
RetCode = OwnedFile.getError();
|
2010-11-24 06:32:37 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, the open succeeded. Do an fstat to get the information
|
|
|
|
// about the file. We'll end up returning the open file descriptor to the
|
|
|
|
// client to do what they please with it.
|
2019-03-05 10:27:12 +08:00
|
|
|
llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
|
|
|
|
if (StatusOrErr) {
|
|
|
|
Status = *StatusOrErr;
|
2014-10-27 06:44:13 +08:00
|
|
|
*F = std::move(*OwnedFile);
|
2013-08-02 05:42:11 +08:00
|
|
|
} else {
|
2010-11-24 06:32:37 +08:00
|
|
|
// fstat rarely fails. If it does, claim the initial open didn't
|
|
|
|
// succeed.
|
2014-05-08 14:41:40 +08:00
|
|
|
*F = nullptr;
|
2019-04-17 01:34:26 +08:00
|
|
|
RetCode = StatusOrErr.getError();
|
2010-11-24 06:32:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-24 05:17:56 +08:00
|
|
|
|
2010-11-24 05:53:56 +08:00
|
|
|
// If the path doesn't exist, return failure.
|
2019-04-17 01:34:26 +08:00
|
|
|
if (RetCode)
|
|
|
|
return RetCode;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-11-24 05:53:56 +08:00
|
|
|
// If the path exists, make sure that its "directoryness" matches the clients
|
|
|
|
// demands.
|
2019-03-05 10:27:12 +08:00
|
|
|
if (Status.isDirectory() != isForDir) {
|
2010-11-24 05:53:56 +08:00
|
|
|
// If not, close the file if opened.
|
2014-07-08 23:46:02 +08:00
|
|
|
if (F)
|
2014-05-08 14:41:40 +08:00
|
|
|
*F = nullptr;
|
2019-04-17 01:34:26 +08:00
|
|
|
return std::make_error_code(
|
|
|
|
Status.isDirectory() ?
|
|
|
|
std::errc::is_a_directory : std::errc::not_a_directory);
|
2010-11-24 05:53:56 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2019-04-17 01:34:26 +08:00
|
|
|
return std::error_code();
|
2010-11-24 05:17:56 +08:00
|
|
|
}
|
|
|
|
|
2019-04-17 01:34:26 +08:00
|
|
|
std::error_code
|
2019-03-05 10:27:12 +08:00
|
|
|
MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
|
|
|
|
bool isFile,
|
2018-10-10 21:27:25 +08:00
|
|
|
std::unique_ptr<llvm::vfs::File> *F,
|
|
|
|
llvm::vfs::FileSystem &FS) {
|
2019-04-17 01:34:26 +08:00
|
|
|
auto err = get(Path, Status, isFile, F, nullptr, FS);
|
|
|
|
if (err) {
|
2018-12-22 03:33:09 +08:00
|
|
|
// Do not cache failed stats, it is easy to construct common inconsistent
|
|
|
|
// situations if we do, and they are not important for PCH performance
|
|
|
|
// (which currently only needs the stats to construct the initial
|
|
|
|
// FileManager entries).
|
2019-04-17 01:34:26 +08:00
|
|
|
return err;
|
2018-12-22 03:33:09 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
// Cache file 'stat' results and directories with absolutely paths.
|
2019-03-05 10:27:12 +08:00
|
|
|
if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
|
|
|
|
StatCalls[Path] = Status;
|
2013-08-02 05:42:11 +08:00
|
|
|
|
2019-04-17 01:34:26 +08:00
|
|
|
return std::error_code();
|
2010-11-24 03:19:34 +08:00
|
|
|
}
|