clang  6.0.0svn
FileSystemStatCache.cpp
Go to the documentation of this file.
1 //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the FileSystemStatCache interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
16 #include "llvm/Support/Path.h"
17 
18 using namespace clang;
19 
20 void FileSystemStatCache::anchor() { }
21 
22 static void copyStatusToFileData(const vfs::Status &Status,
23  FileData &Data) {
24  Data.Name = Status.getName();
25  Data.Size = Status.getSize();
26  Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
27  Data.UniqueID = Status.getUniqueID();
28  Data.IsDirectory = Status.isDirectory();
29  Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
30  Data.InPCH = false;
31  Data.IsVFSMapped = Status.IsVFSMapped;
32 }
33 
34 /// FileSystemStatCache::get - Get the 'stat' information for the specified
35 /// path, using the cache to accelerate it if possible. This returns true if
36 /// the path does not exist or false if it exists.
37 ///
38 /// If isFile is true, then this lookup should only return success for files
39 /// (not directories). If it is false this lookup should only return
40 /// success for directories (not files). On a successful file lookup, the
41 /// implementation can optionally fill in FileDescriptor with a valid
42 /// descriptor and the client guarantees that it will close it.
43 bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile,
44  std::unique_ptr<vfs::File> *F,
46  LookupResult R;
47  bool isForDir = !isFile;
48 
49  // If we have a cache, use it to resolve the stat query.
50  if (Cache)
51  R = Cache->getStat(Path, Data, isFile, F, FS);
52  else if (isForDir || !F) {
53  // If this is a directory or a file descriptor is not needed and we have
54  // no cache, just go to the file system.
55  llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
56  if (!Status) {
57  R = CacheMissing;
58  } else {
59  R = CacheExists;
60  copyStatusToFileData(*Status, Data);
61  }
62  } else {
63  // Otherwise, we have to go to the filesystem. We can always just use
64  // 'stat' here, but (for files) the client is asking whether the file exists
65  // because it wants to turn around and *open* it. It is more efficient to
66  // do "open+fstat" on success than it is to do "stat+open".
67  //
68  // Because of this, check to see if the file exists with 'open'. If the
69  // open succeeds, use fstat to get the stat info.
70  auto OwnedFile = FS.openFileForRead(Path);
71 
72  if (!OwnedFile) {
73  // If the open fails, our "stat" fails.
74  R = CacheMissing;
75  } else {
76  // Otherwise, the open succeeded. Do an fstat to get the information
77  // about the file. We'll end up returning the open file descriptor to the
78  // client to do what they please with it.
79  llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status();
80  if (Status) {
81  R = CacheExists;
82  copyStatusToFileData(*Status, Data);
83  *F = std::move(*OwnedFile);
84  } else {
85  // fstat rarely fails. If it does, claim the initial open didn't
86  // succeed.
87  R = CacheMissing;
88  *F = nullptr;
89  }
90  }
91  }
92 
93  // If the path doesn't exist, return failure.
94  if (R == CacheMissing) return true;
95 
96  // If the path exists, make sure that its "directoryness" matches the clients
97  // demands.
98  if (Data.IsDirectory != isForDir) {
99  // If not, close the file if opened.
100  if (F)
101  *F = nullptr;
102 
103  return true;
104  }
105 
106  return false;
107 }
108 
110 MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile,
111  std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
112  LookupResult Result = statChained(Path, Data, isFile, F, FS);
113 
114  // Do not cache failed stats, it is easy to construct common inconsistent
115  // situations if we do, and they are not important for PCH performance (which
116  // currently only needs the stats to construct the initial FileManager
117  // entries).
118  if (Result == CacheMissing)
119  return Result;
120 
121  // Cache file 'stat' results and directories with absolutely paths.
122  if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
123  StatCalls[Path] = Data;
124 
125  return Result;
126 }
Defines the FileSystemStatCache interface.
StringRef getName() const
Returns the name that should be used for this file or directory.
LookupResult statChained(StringRef Path, FileData &Data, bool isFile, std::unique_ptr< vfs::File > *F, vfs::FileSystem &FS)
We know the file exists and its cached stat data.
virtual llvm::ErrorOr< std::unique_ptr< File > > openFileForRead(const Twine &Path)=0
Get a File object for the file at Path, if one exists.
We know that the file doesn&#39;t exist.
The virtual file system interface.
virtual llvm::ErrorOr< Status > status(const Twine &Path)=0
Get the status of the entry at Path, if one exists.
Abstract interface for introducing a FileManager cache for &#39;stat&#39; system calls, which is used by prec...
The result of a status operation.
static bool get(StringRef Path, FileData &Data, bool isFile, std::unique_ptr< vfs::File > *F, FileSystemStatCache *Cache, vfs::FileSystem &FS)
Get the &#39;stat&#39; information for the specified path, using the cache to accelerate it if possible...
virtual LookupResult getStat(StringRef Path, FileData &Data, bool isFile, std::unique_ptr< vfs::File > *F, vfs::FileSystem &FS)=0
static void copyStatusToFileData(const vfs::Status &Status, FileData &Data)
The result type of a method or function.
llvm::sys::fs::file_type getType() const
llvm::sys::fs::UniqueID UniqueID
llvm::sys::TimePoint getLastModificationTime() const
Dataflow Directional Tag Classes.
uint64_t getSize() const
Defines the virtual file system interface vfs::FileSystem.
llvm::sys::fs::UniqueID getUniqueID() const
The type-property cache.
Definition: Type.cpp:3350
LookupResult getStat(StringRef Path, FileData &Data, bool isFile, std::unique_ptr< vfs::File > *F, vfs::FileSystem &FS) override