1226efd35SChris Lattner //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
2226efd35SChris Lattner //
3226efd35SChris Lattner //                     The LLVM Compiler Infrastructure
4226efd35SChris Lattner //
5226efd35SChris Lattner // This file is distributed under the University of Illinois Open Source
6226efd35SChris Lattner // License. See LICENSE.TXT for details.
7226efd35SChris Lattner //
8226efd35SChris Lattner //===----------------------------------------------------------------------===//
9226efd35SChris Lattner //
10226efd35SChris Lattner //  This file defines the FileSystemStatCache interface.
11226efd35SChris Lattner //
12226efd35SChris Lattner //===----------------------------------------------------------------------===//
13226efd35SChris Lattner 
14226efd35SChris Lattner #include "clang/Basic/FileSystemStatCache.h"
15*16125fb6SRafael Espindola #include "llvm/Support/FileSystem.h"
168aaf4995SMichael J. Spencer #include "llvm/Support/Path.h"
17f77e11baSChris Lattner 
18f77e11baSChris Lattner // FIXME: This is terrible, we need this for ::close.
19f77e11baSChris Lattner #if !defined(_MSC_VER) && !defined(__MINGW32__)
20f77e11baSChris Lattner #include <unistd.h>
21f77e11baSChris Lattner #include <sys/uio.h>
22f77e11baSChris Lattner #else
23f77e11baSChris Lattner #include <io.h>
24f77e11baSChris Lattner #endif
25226efd35SChris Lattner using namespace clang;
26226efd35SChris Lattner 
27ea61b32cSChris Lattner #if defined(_MSC_VER)
2831ee8bfbSFrancois Pichet #define S_ISDIR(s) ((_S_IFDIR & s) !=0)
29ea61b32cSChris Lattner #endif
30ea61b32cSChris Lattner 
3168e081d6SDavid Blaikie void FileSystemStatCache::anchor() { }
3268e081d6SDavid Blaikie 
33dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified
344fc8fb09SChris Lattner /// path, using the cache to accelerate it if possible.  This returns true if
35dd278430SChris Lattner /// the path does not exist or false if it exists.
36dd278430SChris Lattner ///
373b779379SArgyrios Kyrtzidis /// If isFile is true, then this lookup should only return success for files
383b779379SArgyrios Kyrtzidis /// (not directories).  If it is false this lookup should only return
39dd278430SChris Lattner /// success for directories (not files).  On a successful file lookup, the
40dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid
41dd278430SChris Lattner /// descriptor and the client guarantees that it will close it.
42dd278430SChris Lattner bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
433b779379SArgyrios Kyrtzidis                               bool isFile, int *FileDescriptor,
443b779379SArgyrios Kyrtzidis                               FileSystemStatCache *Cache) {
45dd278430SChris Lattner   LookupResult R;
463b779379SArgyrios Kyrtzidis   bool isForDir = !isFile;
47dd278430SChris Lattner 
485ea7d07dSChris Lattner   // If we have a cache, use it to resolve the stat query.
49dd278430SChris Lattner   if (Cache)
503b779379SArgyrios Kyrtzidis     R = Cache->getStat(Path, StatBuf, isFile, FileDescriptor);
513b779379SArgyrios Kyrtzidis   else if (isForDir || !FileDescriptor) {
523b779379SArgyrios Kyrtzidis     // If this is a directory or a file descriptor is not needed and we have
533b779379SArgyrios Kyrtzidis     // no cache, just go to the file system.
54dd278430SChris Lattner     R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
555ea7d07dSChris Lattner   } else {
565ea7d07dSChris Lattner     // Otherwise, we have to go to the filesystem.  We can always just use
575ea7d07dSChris Lattner     // 'stat' here, but (for files) the client is asking whether the file exists
585ea7d07dSChris Lattner     // because it wants to turn around and *open* it.  It is more efficient to
595ea7d07dSChris Lattner     // do "open+fstat" on success than it is to do "stat+open".
605ea7d07dSChris Lattner     //
615ea7d07dSChris Lattner     // Because of this, check to see if the file exists with 'open'.  If the
625ea7d07dSChris Lattner     // open succeeds, use fstat to get the stat info.
63*16125fb6SRafael Espindola     llvm::error_code EC = llvm::sys::fs::openFileForRead(Path, *FileDescriptor);
645ea7d07dSChris Lattner 
65*16125fb6SRafael Espindola     if (EC) {
665ea7d07dSChris Lattner       // If the open fails, our "stat" fails.
675ea7d07dSChris Lattner       R = CacheMissing;
685ea7d07dSChris Lattner     } else {
695ea7d07dSChris Lattner       // Otherwise, the open succeeded.  Do an fstat to get the information
705ea7d07dSChris Lattner       // about the file.  We'll end up returning the open file descriptor to the
715ea7d07dSChris Lattner       // client to do what they please with it.
725ea7d07dSChris Lattner       if (::fstat(*FileDescriptor, &StatBuf) == 0)
735ea7d07dSChris Lattner         R = CacheExists;
745ea7d07dSChris Lattner       else {
755ea7d07dSChris Lattner         // fstat rarely fails.  If it does, claim the initial open didn't
765ea7d07dSChris Lattner         // succeed.
775ea7d07dSChris Lattner         R = CacheMissing;
785ea7d07dSChris Lattner         ::close(*FileDescriptor);
795ea7d07dSChris Lattner         *FileDescriptor = -1;
805ea7d07dSChris Lattner       }
815ea7d07dSChris Lattner     }
825ea7d07dSChris Lattner   }
83dd278430SChris Lattner 
84f77e11baSChris Lattner   // If the path doesn't exist, return failure.
85dd278430SChris Lattner   if (R == CacheMissing) return true;
86dd278430SChris Lattner 
87f77e11baSChris Lattner   // If the path exists, make sure that its "directoryness" matches the clients
88f77e11baSChris Lattner   // demands.
89f77e11baSChris Lattner   if (S_ISDIR(StatBuf.st_mode) != isForDir) {
90f77e11baSChris Lattner     // If not, close the file if opened.
91f77e11baSChris Lattner     if (FileDescriptor && *FileDescriptor != -1) {
92f77e11baSChris Lattner       ::close(*FileDescriptor);
93f77e11baSChris Lattner       *FileDescriptor = -1;
94f77e11baSChris Lattner     }
95f77e11baSChris Lattner 
96f77e11baSChris Lattner     return true;
97f77e11baSChris Lattner   }
98f77e11baSChris Lattner 
99f77e11baSChris Lattner   return false;
100dd278430SChris Lattner }
101dd278430SChris Lattner 
102dd278430SChris Lattner 
103226efd35SChris Lattner MemorizeStatCalls::LookupResult
104dd278430SChris Lattner MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
1053b779379SArgyrios Kyrtzidis                            bool isFile, int *FileDescriptor) {
1063b779379SArgyrios Kyrtzidis   LookupResult Result = statChained(Path, StatBuf, isFile, FileDescriptor);
107226efd35SChris Lattner 
108226efd35SChris Lattner   // Do not cache failed stats, it is easy to construct common inconsistent
109226efd35SChris Lattner   // situations if we do, and they are not important for PCH performance (which
110226efd35SChris Lattner   // currently only needs the stats to construct the initial FileManager
111226efd35SChris Lattner   // entries).
1128f0583daSChris Lattner   if (Result == CacheMissing)
113226efd35SChris Lattner     return Result;
114226efd35SChris Lattner 
115226efd35SChris Lattner   // Cache file 'stat' results and directories with absolutely paths.
116f28df4cdSMichael J. Spencer   if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::path::is_absolute(Path))
1172a6fa47bSChris Lattner     StatCalls[Path] = StatBuf;
118226efd35SChris Lattner 
119226efd35SChris Lattner   return Result;
120226efd35SChris Lattner }
121