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