1 //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the FileSystemStatCache interface.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/FileSystemStatCache.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/Path.h"
18 // FIXME: This is terrible, we need this for ::close.
19 #if !defined(_MSC_VER) && !defined(__MINGW32__)
25 using namespace clang;
28 #define S_ISDIR(s) ((_S_IFDIR & s) !=0)
31 void FileSystemStatCache::anchor() { }
33 static void copyStatusToFileData(const llvm::sys::fs::file_status &Status,
35 Data.Size = Status.getSize();
36 Data.ModTime = Status.getLastModificationTime().toEpochTime();
37 Data.UniqueID = Status.getUniqueID();
38 Data.IsDirectory = is_directory(Status);
39 Data.IsNamedPipe = Status.type() == llvm::sys::fs::file_type::fifo_file;
43 /// FileSystemStatCache::get - Get the 'stat' information for the specified
44 /// path, using the cache to accelerate it if possible. This returns true if
45 /// the path does not exist or false if it exists.
47 /// If isFile is true, then this lookup should only return success for files
48 /// (not directories). If it is false this lookup should only return
49 /// success for directories (not files). On a successful file lookup, the
50 /// implementation can optionally fill in FileDescriptor with a valid
51 /// descriptor and the client guarantees that it will close it.
52 bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
53 int *FileDescriptor, FileSystemStatCache *Cache) {
55 bool isForDir = !isFile;
57 // If we have a cache, use it to resolve the stat query.
59 R = Cache->getStat(Path, Data, isFile, FileDescriptor);
60 else if (isForDir || !FileDescriptor) {
61 // If this is a directory or a file descriptor is not needed and we have
62 // no cache, just go to the file system.
63 llvm::sys::fs::file_status Status;
64 if (llvm::sys::fs::status(Path, Status)) {
68 copyStatusToFileData(Status, Data);
71 // Otherwise, we have to go to the filesystem. We can always just use
72 // 'stat' here, but (for files) the client is asking whether the file exists
73 // because it wants to turn around and *open* it. It is more efficient to
74 // do "open+fstat" on success than it is to do "stat+open".
76 // Because of this, check to see if the file exists with 'open'. If the
77 // open succeeds, use fstat to get the stat info.
78 llvm::error_code EC = llvm::sys::fs::openFileForRead(Path, *FileDescriptor);
81 // If the open fails, our "stat" fails.
84 // Otherwise, the open succeeded. Do an fstat to get the information
85 // about the file. We'll end up returning the open file descriptor to the
86 // client to do what they please with it.
87 llvm::sys::fs::file_status Status;
88 if (!llvm::sys::fs::status(*FileDescriptor, Status)) {
90 copyStatusToFileData(Status, Data);
92 // fstat rarely fails. If it does, claim the initial open didn't
95 ::close(*FileDescriptor);
101 // If the path doesn't exist, return failure.
102 if (R == CacheMissing) return true;
104 // If the path exists, make sure that its "directoryness" matches the clients
106 if (Data.IsDirectory != isForDir) {
107 // If not, close the file if opened.
108 if (FileDescriptor && *FileDescriptor != -1) {
109 ::close(*FileDescriptor);
110 *FileDescriptor = -1;
119 MemorizeStatCalls::LookupResult
120 MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
121 int *FileDescriptor) {
122 LookupResult Result = statChained(Path, Data, isFile, FileDescriptor);
124 // Do not cache failed stats, it is easy to construct common inconsistent
125 // situations if we do, and they are not important for PCH performance (which
126 // currently only needs the stats to construct the initial FileManager
128 if (Result == CacheMissing)
131 // Cache file 'stat' results and directories with absolutely paths.
132 if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
133 StatCalls[Path] = Data;