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