2018-03-29 06:09:09 +08:00
|
|
|
//===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
|
2010-11-24 03:19:34 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
static void copyStatusToFileData(const llvm::vfs::Status &Status,
|
2013-08-02 05:42:11 +08:00
|
|
|
FileData &Data) {
|
2014-03-01 05:16:07 +08:00
|
|
|
Data.Name = Status.getName();
|
2013-08-02 05:42:11 +08:00
|
|
|
Data.Size = Status.getSize();
|
2016-11-09 18:52:22 +08:00
|
|
|
Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
|
2013-08-02 05:42:11 +08:00
|
|
|
Data.UniqueID = Status.getUniqueID();
|
2014-02-21 05:59:23 +08:00
|
|
|
Data.IsDirectory = Status.isDirectory();
|
|
|
|
Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
|
2013-08-02 05:42:11 +08:00
|
|
|
Data.InPCH = false;
|
2014-05-24 02:15:47 +08:00
|
|
|
Data.IsVFSMapped = Status.IsVFSMapped;
|
2013-08-02 05:42:11 +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.
|
2016-10-11 15:31:29 +08:00
|
|
|
bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile,
|
2018-10-10 21:27:25 +08:00
|
|
|
std::unique_ptr<llvm::vfs::File> *F,
|
|
|
|
FileSystemStatCache *Cache,
|
|
|
|
llvm::vfs::FileSystem &FS) {
|
2010-11-24 05:17:56 +08:00
|
|
|
LookupResult R;
|
2012-12-11 15:48:23 +08:00
|
|
|
bool isForDir = !isFile;
|
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)
|
2014-02-21 05:59:23 +08:00
|
|
|
R = Cache->getStat(Path, Data, isFile, F, FS);
|
|
|
|
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.
|
2018-10-10 21:27:25 +08:00
|
|
|
llvm::ErrorOr<llvm::vfs::Status> Status = FS.status(Path);
|
2014-02-21 05:59:23 +08:00
|
|
|
if (!Status) {
|
2013-08-02 05:42:11 +08:00
|
|
|
R = CacheMissing;
|
|
|
|
} else {
|
|
|
|
R = CacheExists;
|
2014-02-21 05:59:23 +08:00
|
|
|
copyStatusToFileData(*Status, Data);
|
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.
|
|
|
|
R = CacheMissing;
|
|
|
|
} 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.
|
2018-10-10 21:27:25 +08:00
|
|
|
llvm::ErrorOr<llvm::vfs::Status> Status = (*OwnedFile)->status();
|
2014-02-21 05:59:23 +08:00
|
|
|
if (Status) {
|
2010-11-24 06:32:37 +08:00
|
|
|
R = CacheExists;
|
2014-02-21 05:59:23 +08:00
|
|
|
copyStatusToFileData(*Status, Data);
|
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.
|
|
|
|
R = CacheMissing;
|
2014-05-08 14:41:40 +08:00
|
|
|
*F = nullptr;
|
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.
|
2010-11-24 05:17:56 +08:00
|
|
|
if (R == CacheMissing) return true;
|
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.
|
2013-08-02 05:42:11 +08:00
|
|
|
if (Data.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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-11-24 05:53:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-11-24 05:53:56 +08:00
|
|
|
return false;
|
2010-11-24 05:17:56 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
MemorizeStatCalls::LookupResult
|
2016-10-11 15:31:29 +08:00
|
|
|
MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile,
|
2018-10-10 21:27:25 +08:00
|
|
|
std::unique_ptr<llvm::vfs::File> *F,
|
|
|
|
llvm::vfs::FileSystem &FS) {
|
2014-02-21 05:59:23 +08:00
|
|
|
LookupResult Result = statChained(Path, Data, isFile, F, FS);
|
2013-08-02 05:42:11 +08:00
|
|
|
|
2010-11-24 03:19:34 +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).
|
2010-11-24 04:05:15 +08:00
|
|
|
if (Result == CacheMissing)
|
2010-11-24 03:19:34 +08:00
|
|
|
return Result;
|
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.
|
2013-08-02 05:42:11 +08:00
|
|
|
if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
|
|
|
|
StatCalls[Path] = Data;
|
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
return Result;
|
|
|
|
}
|