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/Path.h"
17 
18 // FIXME: This is terrible, we need this for ::close.
19 #if !defined(_MSC_VER) && !defined(__MINGW32__)
20 #include <unistd.h>
21 #include <sys/uio.h>
22 #else
23 #include <io.h>
24 #endif
25 using namespace clang;
26 
27 #if defined(_MSC_VER)
28 #define S_ISDIR(s) ((_S_IFDIR & s) !=0)
29 #endif
30 
31 void FileSystemStatCache::anchor() { }
32 
33 static void copyStatusToFileData(const vfs::Status &Status,
34                                  FileData &Data) {
35   Data.Size = Status.getSize();
36   Data.ModTime = Status.getLastModificationTime().toEpochTime();
37   Data.UniqueID = Status.getUniqueID();
38   Data.IsDirectory = Status.isDirectory();
39   Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
40   Data.InPCH = false;
41 }
42 
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.
46 ///
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                               vfs::File **F, FileSystemStatCache *Cache,
54                               vfs::FileSystem &FS) {
55   LookupResult R;
56   bool isForDir = !isFile;
57 
58   // If we have a cache, use it to resolve the stat query.
59   if (Cache)
60     R = Cache->getStat(Path, Data, isFile, F, FS);
61   else if (isForDir || !F) {
62     // If this is a directory or a file descriptor is not needed and we have
63     // no cache, just go to the file system.
64     llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
65     if (!Status) {
66       R = CacheMissing;
67     } else {
68       R = CacheExists;
69       copyStatusToFileData(*Status, Data);
70     }
71   } else {
72     // Otherwise, we have to go to the filesystem.  We can always just use
73     // 'stat' here, but (for files) the client is asking whether the file exists
74     // because it wants to turn around and *open* it.  It is more efficient to
75     // do "open+fstat" on success than it is to do "stat+open".
76     //
77     // Because of this, check to see if the file exists with 'open'.  If the
78     // open succeeds, use fstat to get the stat info.
79     llvm::OwningPtr<vfs::File> OwnedFile;
80     llvm::error_code EC = FS.openFileForRead(Path, OwnedFile);
81 
82     if (EC) {
83       // If the open fails, our "stat" fails.
84       R = CacheMissing;
85     } else {
86       // Otherwise, the open succeeded.  Do an fstat to get the information
87       // about the file.  We'll end up returning the open file descriptor to the
88       // client to do what they please with it.
89       llvm::ErrorOr<vfs::Status> Status = OwnedFile->status();
90       if (Status) {
91         R = CacheExists;
92         copyStatusToFileData(*Status, Data);
93         *F = OwnedFile.take();
94       } else {
95         // fstat rarely fails.  If it does, claim the initial open didn't
96         // succeed.
97         R = CacheMissing;
98         *F = 0;
99       }
100     }
101   }
102 
103   // If the path doesn't exist, return failure.
104   if (R == CacheMissing) return true;
105 
106   // If the path exists, make sure that its "directoryness" matches the clients
107   // demands.
108   if (Data.IsDirectory != isForDir) {
109     // If not, close the file if opened.
110     if (F && *F) {
111       (*F)->close();
112       *F = 0;
113     }
114 
115     return true;
116   }
117 
118   return false;
119 }
120 
121 MemorizeStatCalls::LookupResult
122 MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
123                            vfs::File **F, vfs::FileSystem &FS) {
124   LookupResult Result = statChained(Path, Data, isFile, F, FS);
125 
126   // Do not cache failed stats, it is easy to construct common inconsistent
127   // situations if we do, and they are not important for PCH performance (which
128   // currently only needs the stats to construct the initial FileManager
129   // entries).
130   if (Result == CacheMissing)
131     return Result;
132 
133   // Cache file 'stat' results and directories with absolutely paths.
134   if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
135     StatCalls[Path] = Data;
136 
137   return Result;
138 }
139