clang API Documentation
00001 //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the FileSystemStatCache interface. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Basic/FileSystemStatCache.h" 00015 #include "llvm/Support/Path.h" 00016 #include <fcntl.h> 00017 00018 // FIXME: This is terrible, we need this for ::close. 00019 #if !defined(_MSC_VER) && !defined(__MINGW32__) 00020 #include <unistd.h> 00021 #include <sys/uio.h> 00022 #else 00023 #include <io.h> 00024 #endif 00025 using namespace clang; 00026 00027 #if defined(_MSC_VER) 00028 #define S_ISDIR(s) ((_S_IFDIR & s) !=0) 00029 #endif 00030 00031 void FileSystemStatCache::anchor() { } 00032 00033 /// FileSystemStatCache::get - Get the 'stat' information for the specified 00034 /// path, using the cache to accelerate it if possible. This returns true if 00035 /// the path does not exist or false if it exists. 00036 /// 00037 /// If FileDescriptor is non-null, then this lookup should only return success 00038 /// for files (not directories). If it is null this lookup should only return 00039 /// success for directories (not files). On a successful file lookup, the 00040 /// implementation can optionally fill in FileDescriptor with a valid 00041 /// descriptor and the client guarantees that it will close it. 00042 bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf, 00043 int *FileDescriptor, FileSystemStatCache *Cache) { 00044 LookupResult R; 00045 bool isForDir = FileDescriptor == 0; 00046 00047 // If we have a cache, use it to resolve the stat query. 00048 if (Cache) 00049 R = Cache->getStat(Path, StatBuf, FileDescriptor); 00050 else if (isForDir) { 00051 // If this is a directory and we have no cache, just go to the file system. 00052 R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists; 00053 } else { 00054 // Otherwise, we have to go to the filesystem. We can always just use 00055 // 'stat' here, but (for files) the client is asking whether the file exists 00056 // because it wants to turn around and *open* it. It is more efficient to 00057 // do "open+fstat" on success than it is to do "stat+open". 00058 // 00059 // Because of this, check to see if the file exists with 'open'. If the 00060 // open succeeds, use fstat to get the stat info. 00061 int OpenFlags = O_RDONLY; 00062 #ifdef O_BINARY 00063 OpenFlags |= O_BINARY; // Open input file in binary mode on win32. 00064 #endif 00065 *FileDescriptor = ::open(Path, OpenFlags); 00066 00067 if (*FileDescriptor == -1) { 00068 // If the open fails, our "stat" fails. 00069 R = CacheMissing; 00070 } else { 00071 // Otherwise, the open succeeded. Do an fstat to get the information 00072 // about the file. We'll end up returning the open file descriptor to the 00073 // client to do what they please with it. 00074 if (::fstat(*FileDescriptor, &StatBuf) == 0) 00075 R = CacheExists; 00076 else { 00077 // fstat rarely fails. If it does, claim the initial open didn't 00078 // succeed. 00079 R = CacheMissing; 00080 ::close(*FileDescriptor); 00081 *FileDescriptor = -1; 00082 } 00083 } 00084 } 00085 00086 // If the path doesn't exist, return failure. 00087 if (R == CacheMissing) return true; 00088 00089 // If the path exists, make sure that its "directoryness" matches the clients 00090 // demands. 00091 if (S_ISDIR(StatBuf.st_mode) != isForDir) { 00092 // If not, close the file if opened. 00093 if (FileDescriptor && *FileDescriptor != -1) { 00094 ::close(*FileDescriptor); 00095 *FileDescriptor = -1; 00096 } 00097 00098 return true; 00099 } 00100 00101 return false; 00102 } 00103 00104 00105 MemorizeStatCalls::LookupResult 00106 MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf, 00107 int *FileDescriptor) { 00108 LookupResult Result = statChained(Path, StatBuf, FileDescriptor); 00109 00110 // Do not cache failed stats, it is easy to construct common inconsistent 00111 // situations if we do, and they are not important for PCH performance (which 00112 // currently only needs the stats to construct the initial FileManager 00113 // entries). 00114 if (Result == CacheMissing) 00115 return Result; 00116 00117 // Cache file 'stat' results and directories with absolutely paths. 00118 if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::path::is_absolute(Path)) 00119 StatCalls[Path] = StatBuf; 00120 00121 return Result; 00122 }