]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Basic/FileSystemStatCache.cpp
Merge clang trunk r338150 (just before the 7.0.0 branch point), and
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Basic / FileSystemStatCache.cpp
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
14 #include "clang/Basic/FileSystemStatCache.h"
15 #include "clang/Basic/VirtualFileSystem.h"
16 #include "llvm/Support/Chrono.h"
17 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/Path.h"
19 #include <utility>
20
21 using namespace clang;
22
23 void FileSystemStatCache::anchor() {}
24
25 static void copyStatusToFileData(const vfs::Status &Status,
26                                  FileData &Data) {
27   Data.Name = Status.getName();
28   Data.Size = Status.getSize();
29   Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
30   Data.UniqueID = Status.getUniqueID();
31   Data.IsDirectory = Status.isDirectory();
32   Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
33   Data.InPCH = false;
34   Data.IsVFSMapped = Status.IsVFSMapped;
35 }
36
37 /// FileSystemStatCache::get - Get the 'stat' information for the specified
38 /// path, using the cache to accelerate it if possible.  This returns true if
39 /// the path does not exist or false if it exists.
40 ///
41 /// If isFile is true, then this lookup should only return success for files
42 /// (not directories).  If it is false this lookup should only return
43 /// success for directories (not files).  On a successful file lookup, the
44 /// implementation can optionally fill in FileDescriptor with a valid
45 /// descriptor and the client guarantees that it will close it.
46 bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile,
47                               std::unique_ptr<vfs::File> *F,
48                               FileSystemStatCache *Cache, vfs::FileSystem &FS) {
49   LookupResult R;
50   bool isForDir = !isFile;
51
52   // If we have a cache, use it to resolve the stat query.
53   if (Cache)
54     R = Cache->getStat(Path, Data, isFile, F, FS);
55   else if (isForDir || !F) {
56     // If this is a directory or a file descriptor is not needed and we have
57     // no cache, just go to the file system.
58     llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
59     if (!Status) {
60       R = CacheMissing;
61     } else {
62       R = CacheExists;
63       copyStatusToFileData(*Status, Data);
64     }
65   } else {
66     // Otherwise, we have to go to the filesystem.  We can always just use
67     // 'stat' here, but (for files) the client is asking whether the file exists
68     // because it wants to turn around and *open* it.  It is more efficient to
69     // do "open+fstat" on success than it is to do "stat+open".
70     //
71     // Because of this, check to see if the file exists with 'open'.  If the
72     // open succeeds, use fstat to get the stat info.
73     auto OwnedFile = FS.openFileForRead(Path);
74
75     if (!OwnedFile) {
76       // If the open fails, our "stat" fails.
77       R = CacheMissing;
78     } else {
79       // Otherwise, the open succeeded.  Do an fstat to get the information
80       // about the file.  We'll end up returning the open file descriptor to the
81       // client to do what they please with it.
82       llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status();
83       if (Status) {
84         R = CacheExists;
85         copyStatusToFileData(*Status, Data);
86         *F = std::move(*OwnedFile);
87       } else {
88         // fstat rarely fails.  If it does, claim the initial open didn't
89         // succeed.
90         R = CacheMissing;
91         *F = nullptr;
92       }
93     }
94   }
95
96   // If the path doesn't exist, return failure.
97   if (R == CacheMissing) return true;
98
99   // If the path exists, make sure that its "directoryness" matches the clients
100   // demands.
101   if (Data.IsDirectory != isForDir) {
102     // If not, close the file if opened.
103     if (F)
104       *F = nullptr;
105
106     return true;
107   }
108
109   return false;
110 }
111
112 MemorizeStatCalls::LookupResult
113 MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile,
114                            std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
115   LookupResult Result = statChained(Path, Data, isFile, F, FS);
116
117   // Do not cache failed stats, it is easy to construct common inconsistent
118   // situations if we do, and they are not important for PCH performance (which
119   // currently only needs the stats to construct the initial FileManager
120   // entries).
121   if (Result == CacheMissing)
122     return Result;
123
124   // Cache file 'stat' results and directories with absolutely paths.
125   if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
126     StatCalls[Path] = Data;
127
128   return Result;
129 }