Chris Lattner | 226efd3 | 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" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 15 | #include "clang/Basic/VirtualFileSystem.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Path.h" |
Chris Lattner | f77e11b | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 20 | void FileSystemStatCache::anchor() { } |
| 21 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 22 | static void copyStatusToFileData(const vfs::Status &Status, |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 23 | FileData &Data) { |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 24 | Data.Name = Status.getName(); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 25 | Data.Size = Status.getSize(); |
| 26 | Data.ModTime = Status.getLastModificationTime().toEpochTime(); |
| 27 | Data.UniqueID = Status.getUniqueID(); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 28 | Data.IsDirectory = Status.isDirectory(); |
| 29 | Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 30 | Data.InPCH = false; |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 31 | Data.IsVFSMapped = Status.IsVFSMapped; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 34 | /// FileSystemStatCache::get - Get the 'stat' information for the specified |
Chris Lattner | 4fc8fb0 | 2010-12-02 04:27:29 +0000 | [diff] [blame] | 35 | /// path, using the cache to accelerate it if possible. This returns true if |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 36 | /// the path does not exist or false if it exists. |
| 37 | /// |
Argyrios Kyrtzidis | 3b77937 | 2012-12-11 07:48:23 +0000 | [diff] [blame] | 38 | /// If isFile is true, then this lookup should only return success for files |
| 39 | /// (not directories). If it is false this lookup should only return |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 40 | /// success for directories (not files). On a successful file lookup, the |
| 41 | /// implementation can optionally fill in FileDescriptor with a valid |
| 42 | /// descriptor and the client guarantees that it will close it. |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame^] | 43 | bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile, |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 44 | std::unique_ptr<vfs::File> *F, |
| 45 | FileSystemStatCache *Cache, vfs::FileSystem &FS) { |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 46 | LookupResult R; |
Argyrios Kyrtzidis | 3b77937 | 2012-12-11 07:48:23 +0000 | [diff] [blame] | 47 | bool isForDir = !isFile; |
Chris Lattner | 5ea7d07 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 48 | |
| 49 | // If we have a cache, use it to resolve the stat query. |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 50 | if (Cache) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 51 | R = Cache->getStat(Path, Data, isFile, F, FS); |
| 52 | else if (isForDir || !F) { |
Argyrios Kyrtzidis | 3b77937 | 2012-12-11 07:48:23 +0000 | [diff] [blame] | 53 | // If this is a directory or a file descriptor is not needed and we have |
| 54 | // no cache, just go to the file system. |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 55 | llvm::ErrorOr<vfs::Status> Status = FS.status(Path); |
| 56 | if (!Status) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 57 | R = CacheMissing; |
| 58 | } else { |
| 59 | R = CacheExists; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 60 | copyStatusToFileData(*Status, Data); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 5ea7d07 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 62 | } else { |
| 63 | // Otherwise, we have to go to the filesystem. We can always just use |
| 64 | // 'stat' here, but (for files) the client is asking whether the file exists |
| 65 | // because it wants to turn around and *open* it. It is more efficient to |
| 66 | // do "open+fstat" on success than it is to do "stat+open". |
| 67 | // |
| 68 | // Because of this, check to see if the file exists with 'open'. If the |
| 69 | // open succeeds, use fstat to get the stat info. |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 70 | auto OwnedFile = FS.openFileForRead(Path); |
Rafael Espindola | 16125fb | 2013-07-16 19:44:23 +0000 | [diff] [blame] | 71 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 72 | if (!OwnedFile) { |
Chris Lattner | 5ea7d07 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 73 | // If the open fails, our "stat" fails. |
| 74 | R = CacheMissing; |
| 75 | } else { |
| 76 | // Otherwise, the open succeeded. Do an fstat to get the information |
| 77 | // about the file. We'll end up returning the open file descriptor to the |
| 78 | // client to do what they please with it. |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 79 | llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status(); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 80 | if (Status) { |
Chris Lattner | 5ea7d07 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 81 | R = CacheExists; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 82 | copyStatusToFileData(*Status, Data); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 83 | *F = std::move(*OwnedFile); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 84 | } else { |
Chris Lattner | 5ea7d07 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 85 | // fstat rarely fails. If it does, claim the initial open didn't |
| 86 | // succeed. |
| 87 | R = CacheMissing; |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 88 | *F = nullptr; |
Chris Lattner | 5ea7d07 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 92 | |
Chris Lattner | f77e11b | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 93 | // If the path doesn't exist, return failure. |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 94 | if (R == CacheMissing) return true; |
| 95 | |
Chris Lattner | f77e11b | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 96 | // If the path exists, make sure that its "directoryness" matches the clients |
| 97 | // demands. |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 98 | if (Data.IsDirectory != isForDir) { |
Chris Lattner | f77e11b | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 99 | // If not, close the file if opened. |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 100 | if (F) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 101 | *F = nullptr; |
Chris Lattner | f77e11b | 2010-11-23 21:53:56 +0000 | [diff] [blame] | 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | return false; |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 109 | MemorizeStatCalls::LookupResult |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame^] | 110 | MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile, |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 111 | std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 112 | LookupResult Result = statChained(Path, Data, isFile, F, FS); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 114 | // Do not cache failed stats, it is easy to construct common inconsistent |
| 115 | // situations if we do, and they are not important for PCH performance (which |
| 116 | // currently only needs the stats to construct the initial FileManager |
| 117 | // entries). |
Chris Lattner | 8f0583d | 2010-11-23 20:05:15 +0000 | [diff] [blame] | 118 | if (Result == CacheMissing) |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 119 | return Result; |
| 120 | |
| 121 | // Cache file 'stat' results and directories with absolutely paths. |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 122 | if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path)) |
| 123 | StatCalls[Path] = Data; |
| 124 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 125 | return Result; |
| 126 | } |