Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 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" |
Rafael Espindola | d965f95 | 2013-07-16 19:44:23 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Path.h" |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Chris Lattner | 72f6130 | 2010-11-23 20:07:39 +0000 | [diff] [blame] | 27 | #if defined(_MSC_VER) |
Francois Pichet | 0d4739ab | 2010-11-24 03:07:43 +0000 | [diff] [blame] | 28 | #define S_ISDIR(s) ((_S_IFDIR & s) !=0) |
Chris Lattner | 72f6130 | 2010-11-23 20:07:39 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 31 | void FileSystemStatCache::anchor() { } |
| 32 | |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 33 | static void copyStatusToFileData(const llvm::sys::fs::file_status &Status, |
| 34 | FileData &Data) { |
| 35 | Data.Size = Status.getSize(); |
| 36 | Data.ModTime = Status.getLastModificationTime().toEpochTime(); |
| 37 | Data.UniqueID = Status.getUniqueID(); |
| 38 | Data.IsDirectory = is_directory(Status); |
| 39 | Data.IsNamedPipe = Status.type() == llvm::sys::fs::file_type::fifo_file; |
| 40 | Data.InPCH = false; |
| 41 | } |
| 42 | |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 43 | /// FileSystemStatCache::get - Get the 'stat' information for the specified |
Chris Lattner | c0f31fd | 2010-12-02 04:27:29 +0000 | [diff] [blame] | 44 | /// path, using the cache to accelerate it if possible. This returns true if |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 45 | /// the path does not exist or false if it exists. |
| 46 | /// |
Argyrios Kyrtzidis | e5d30e3 | 2012-12-11 07:48:23 +0000 | [diff] [blame] | 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 |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 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. |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 52 | bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile, |
| 53 | int *FileDescriptor, FileSystemStatCache *Cache) { |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 54 | LookupResult R; |
Argyrios Kyrtzidis | e5d30e3 | 2012-12-11 07:48:23 +0000 | [diff] [blame] | 55 | bool isForDir = !isFile; |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 56 | |
| 57 | // If we have a cache, use it to resolve the stat query. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 58 | if (Cache) |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 59 | R = Cache->getStat(Path, Data, isFile, FileDescriptor); |
Argyrios Kyrtzidis | e5d30e3 | 2012-12-11 07:48:23 +0000 | [diff] [blame] | 60 | else if (isForDir || !FileDescriptor) { |
| 61 | // If this is a directory or a file descriptor is not needed and we have |
| 62 | // no cache, just go to the file system. |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 63 | llvm::sys::fs::file_status Status; |
| 64 | if (llvm::sys::fs::status(Path, Status)) { |
| 65 | R = CacheMissing; |
| 66 | } else { |
| 67 | R = CacheExists; |
| 68 | copyStatusToFileData(Status, Data); |
| 69 | } |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 70 | } else { |
| 71 | // Otherwise, we have to go to the filesystem. We can always just use |
| 72 | // 'stat' here, but (for files) the client is asking whether the file exists |
| 73 | // because it wants to turn around and *open* it. It is more efficient to |
| 74 | // do "open+fstat" on success than it is to do "stat+open". |
| 75 | // |
| 76 | // Because of this, check to see if the file exists with 'open'. If the |
| 77 | // open succeeds, use fstat to get the stat info. |
Rafael Espindola | d965f95 | 2013-07-16 19:44:23 +0000 | [diff] [blame] | 78 | llvm::error_code EC = llvm::sys::fs::openFileForRead(Path, *FileDescriptor); |
| 79 | |
| 80 | if (EC) { |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 81 | // If the open fails, our "stat" fails. |
| 82 | R = CacheMissing; |
| 83 | } else { |
| 84 | // Otherwise, the open succeeded. Do an fstat to get the information |
| 85 | // about the file. We'll end up returning the open file descriptor to the |
| 86 | // client to do what they please with it. |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 87 | llvm::sys::fs::file_status Status; |
| 88 | if (!llvm::sys::fs::status(*FileDescriptor, Status)) { |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 89 | R = CacheExists; |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 90 | copyStatusToFileData(Status, Data); |
| 91 | } else { |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 92 | // fstat rarely fails. If it does, claim the initial open didn't |
| 93 | // succeed. |
| 94 | R = CacheMissing; |
| 95 | ::close(*FileDescriptor); |
| 96 | *FileDescriptor = -1; |
| 97 | } |
| 98 | } |
| 99 | } |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 100 | |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 101 | // If the path doesn't exist, return failure. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 102 | if (R == CacheMissing) return true; |
| 103 | |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 104 | // If the path exists, make sure that its "directoryness" matches the clients |
| 105 | // demands. |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 106 | if (Data.IsDirectory != isForDir) { |
Chris Lattner | b2b93c1 | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 107 | // If not, close the file if opened. |
| 108 | if (FileDescriptor && *FileDescriptor != -1) { |
| 109 | ::close(*FileDescriptor); |
| 110 | *FileDescriptor = -1; |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | return false; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 119 | MemorizeStatCalls::LookupResult |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 120 | MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile, |
| 121 | int *FileDescriptor) { |
| 122 | LookupResult Result = statChained(Path, Data, isFile, FileDescriptor); |
| 123 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 124 | // Do not cache failed stats, it is easy to construct common inconsistent |
| 125 | // situations if we do, and they are not important for PCH performance (which |
| 126 | // currently only needs the stats to construct the initial FileManager |
| 127 | // entries). |
Chris Lattner | d6f6111 | 2010-11-23 20:05:15 +0000 | [diff] [blame] | 128 | if (Result == CacheMissing) |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 129 | return Result; |
| 130 | |
| 131 | // Cache file 'stat' results and directories with absolutely paths. |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 132 | if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path)) |
| 133 | StatCalls[Path] = Data; |
| 134 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 135 | return Result; |
| 136 | } |