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"
165ea7d07dSChris Lattner #include <fcntl.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)
28*31ee8bfbSFrancois Pichet #define S_ISDIR(s) ((_S_IFDIR & s) !=0)
29ea61b32cSChris Lattner #endif
30ea61b32cSChris Lattner 
31dd278430SChris Lattner /// FileSystemStatCache::get - Get the 'stat' information for the specified
32dd278430SChris Lattner /// path, using the cache to accellerate it if possible.  This returns true if
33dd278430SChris Lattner /// the path does not exist or false if it exists.
34dd278430SChris Lattner ///
35dd278430SChris Lattner /// If FileDescriptor is non-null, then this lookup should only return success
36dd278430SChris Lattner /// for files (not directories).  If it is null this lookup should only return
37dd278430SChris Lattner /// success for directories (not files).  On a successful file lookup, the
38dd278430SChris Lattner /// implementation can optionally fill in FileDescriptor with a valid
39dd278430SChris Lattner /// descriptor and the client guarantees that it will close it.
40dd278430SChris Lattner bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
41dd278430SChris Lattner                               int *FileDescriptor, FileSystemStatCache *Cache) {
42dd278430SChris Lattner   LookupResult R;
435ea7d07dSChris Lattner   bool isForDir = FileDescriptor == 0;
44dd278430SChris Lattner 
455ea7d07dSChris Lattner   // If we have a cache, use it to resolve the stat query.
46dd278430SChris Lattner   if (Cache)
47dd278430SChris Lattner     R = Cache->getStat(Path, StatBuf, FileDescriptor);
485ea7d07dSChris Lattner   else if (isForDir) {
495ea7d07dSChris Lattner     // If this is a directory and we have no cache, just go to the file system.
50dd278430SChris Lattner     R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
515ea7d07dSChris Lattner   } else {
525ea7d07dSChris Lattner     // Otherwise, we have to go to the filesystem.  We can always just use
535ea7d07dSChris Lattner     // 'stat' here, but (for files) the client is asking whether the file exists
545ea7d07dSChris Lattner     // because it wants to turn around and *open* it.  It is more efficient to
555ea7d07dSChris Lattner     // do "open+fstat" on success than it is to do "stat+open".
565ea7d07dSChris Lattner     //
575ea7d07dSChris Lattner     // Because of this, check to see if the file exists with 'open'.  If the
585ea7d07dSChris Lattner     // open succeeds, use fstat to get the stat info.
595ea7d07dSChris Lattner     int OpenFlags = O_RDONLY;
605ea7d07dSChris Lattner #ifdef O_BINARY
615ea7d07dSChris Lattner     OpenFlags |= O_BINARY;  // Open input file in binary mode on win32.
625ea7d07dSChris Lattner #endif
635ea7d07dSChris Lattner     *FileDescriptor = ::open(Path, OpenFlags);
645ea7d07dSChris Lattner 
655ea7d07dSChris Lattner     if (*FileDescriptor == -1) {
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,
105dd278430SChris Lattner                            int *FileDescriptor) {
106dd278430SChris Lattner   LookupResult Result = statChained(Path, StatBuf, 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.
116226efd35SChris Lattner   if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
1172a6fa47bSChris Lattner     StatCalls[Path] = StatBuf;
118226efd35SChris Lattner 
119226efd35SChris Lattner   return Result;
120226efd35SChris Lattner }
121