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"
15226efd35SChris Lattner #include "llvm/System/Path.h"
16226efd35SChris Lattner using namespace clang;
17226efd35SChris Lattner 
18ea61b32cSChris Lattner #if defined(_MSC_VER)
19ea61b32cSChris Lattner #define S_ISDIR(s) (_S_IFDIR & s)
20ea61b32cSChris Lattner #endif
21ea61b32cSChris Lattner 
22*dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified
23*dd278430SChris Lattner /// path, using the cache to accellerate it if possible.  This returns true if
24*dd278430SChris Lattner /// the path does not exist or false if it exists.
25*dd278430SChris Lattner ///
26*dd278430SChris Lattner /// If FileDescriptor is non-null, then this lookup should only return success
27*dd278430SChris Lattner /// for files (not directories).  If it is null this lookup should only return
28*dd278430SChris Lattner /// success for directories (not files).  On a successful file lookup, the
29*dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid
30*dd278430SChris Lattner /// descriptor and the client guarantees that it will close it.
31*dd278430SChris Lattner bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
32*dd278430SChris Lattner                               int *FileDescriptor, FileSystemStatCache *Cache) {
33*dd278430SChris Lattner   LookupResult R;
34*dd278430SChris Lattner 
35*dd278430SChris Lattner   if (Cache)
36*dd278430SChris Lattner     R = Cache->getStat(Path, StatBuf, FileDescriptor);
37*dd278430SChris Lattner   else
38*dd278430SChris Lattner     R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
39*dd278430SChris Lattner 
40*dd278430SChris Lattner   if (R == CacheMissing) return true;
41*dd278430SChris Lattner 
42*dd278430SChris Lattner   bool isForDir = FileDescriptor == 0;
43*dd278430SChris Lattner   return S_ISDIR(StatBuf.st_mode) != isForDir;
44*dd278430SChris Lattner }
45*dd278430SChris Lattner 
46*dd278430SChris Lattner 
47226efd35SChris Lattner MemorizeStatCalls::LookupResult
48*dd278430SChris Lattner MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
49*dd278430SChris Lattner                            int *FileDescriptor) {
50*dd278430SChris Lattner   LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
51226efd35SChris Lattner 
52226efd35SChris Lattner   // Do not cache failed stats, it is easy to construct common inconsistent
53226efd35SChris Lattner   // situations if we do, and they are not important for PCH performance (which
54226efd35SChris Lattner   // currently only needs the stats to construct the initial FileManager
55226efd35SChris Lattner   // entries).
568f0583daSChris Lattner   if (Result == CacheMissing)
57226efd35SChris Lattner     return Result;
58226efd35SChris Lattner 
59226efd35SChris Lattner   // Cache file 'stat' results and directories with absolutely paths.
60226efd35SChris Lattner   if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
612a6fa47bSChris Lattner     StatCalls[Path] = StatBuf;
62226efd35SChris Lattner 
63226efd35SChris Lattner   return Result;
64226efd35SChris Lattner }
65