blob: 33b8676e7457e1cb663e6dc3efee456d2529cba1 [file] [log] [blame]
Chris Lattner226efd32010-11-23 19:19:34 +00001//===--- 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 Langmuirc8130a72014-02-20 21:59:23 +000015#include "clang/Basic/VirtualFileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000016#include "llvm/Support/Path.h"
Chris Lattnerf77e11b2010-11-23 21:53:56 +000017
Chris Lattner226efd32010-11-23 19:19:34 +000018using namespace clang;
19
David Blaikie68e081d2011-12-20 02:48:34 +000020void FileSystemStatCache::anchor() { }
21
Ben Langmuirc8130a72014-02-20 21:59:23 +000022static void copyStatusToFileData(const vfs::Status &Status,
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000023 FileData &Data) {
Ben Langmuird066d4c2014-02-28 21:16:07 +000024 Data.Name = Status.getName();
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000025 Data.Size = Status.getSize();
26 Data.ModTime = Status.getLastModificationTime().toEpochTime();
27 Data.UniqueID = Status.getUniqueID();
Ben Langmuirc8130a72014-02-20 21:59:23 +000028 Data.IsDirectory = Status.isDirectory();
29 Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000030 Data.InPCH = false;
Ben Langmuir5de00f32014-05-23 18:15:47 +000031 Data.IsVFSMapped = Status.IsVFSMapped;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000032}
33
Chris Lattnerdd278432010-11-23 21:17:56 +000034/// FileSystemStatCache::get - Get the 'stat' information for the specified
Chris Lattner4fc8fb02010-12-02 04:27:29 +000035/// path, using the cache to accelerate it if possible. This returns true if
Chris Lattnerdd278432010-11-23 21:17:56 +000036/// the path does not exist or false if it exists.
37///
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +000038/// 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 Lattnerdd278432010-11-23 21:17:56 +000040/// 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 Amini0df59d82016-10-11 07:31:29 +000043bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +000044 std::unique_ptr<vfs::File> *F,
45 FileSystemStatCache *Cache, vfs::FileSystem &FS) {
Chris Lattnerdd278432010-11-23 21:17:56 +000046 LookupResult R;
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +000047 bool isForDir = !isFile;
Chris Lattner5ea7d072010-11-23 22:32:37 +000048
49 // If we have a cache, use it to resolve the stat query.
Chris Lattnerdd278432010-11-23 21:17:56 +000050 if (Cache)
Ben Langmuirc8130a72014-02-20 21:59:23 +000051 R = Cache->getStat(Path, Data, isFile, F, FS);
52 else if (isForDir || !F) {
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +000053 // 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 Langmuirc8130a72014-02-20 21:59:23 +000055 llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
56 if (!Status) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000057 R = CacheMissing;
58 } else {
59 R = CacheExists;
Ben Langmuirc8130a72014-02-20 21:59:23 +000060 copyStatusToFileData(*Status, Data);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000061 }
Chris Lattner5ea7d072010-11-23 22:32:37 +000062 } 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 Kramera8857962014-10-26 22:44:13 +000070 auto OwnedFile = FS.openFileForRead(Path);
Rafael Espindola16125fb2013-07-16 19:44:23 +000071
Benjamin Kramera8857962014-10-26 22:44:13 +000072 if (!OwnedFile) {
Chris Lattner5ea7d072010-11-23 22:32:37 +000073 // 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 Kramera8857962014-10-26 22:44:13 +000079 llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status();
Ben Langmuirc8130a72014-02-20 21:59:23 +000080 if (Status) {
Chris Lattner5ea7d072010-11-23 22:32:37 +000081 R = CacheExists;
Ben Langmuirc8130a72014-02-20 21:59:23 +000082 copyStatusToFileData(*Status, Data);
Benjamin Kramera8857962014-10-26 22:44:13 +000083 *F = std::move(*OwnedFile);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000084 } else {
Chris Lattner5ea7d072010-11-23 22:32:37 +000085 // fstat rarely fails. If it does, claim the initial open didn't
86 // succeed.
87 R = CacheMissing;
Craig Topperf1186c52014-05-08 06:41:40 +000088 *F = nullptr;
Chris Lattner5ea7d072010-11-23 22:32:37 +000089 }
90 }
91 }
Chris Lattnerdd278432010-11-23 21:17:56 +000092
Chris Lattnerf77e11b2010-11-23 21:53:56 +000093 // If the path doesn't exist, return failure.
Chris Lattnerdd278432010-11-23 21:17:56 +000094 if (R == CacheMissing) return true;
95
Chris Lattnerf77e11b2010-11-23 21:53:56 +000096 // If the path exists, make sure that its "directoryness" matches the clients
97 // demands.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000098 if (Data.IsDirectory != isForDir) {
Chris Lattnerf77e11b2010-11-23 21:53:56 +000099 // If not, close the file if opened.
David Blaikie326ffb32014-07-08 15:46:02 +0000100 if (F)
Craig Topperf1186c52014-05-08 06:41:40 +0000101 *F = nullptr;
Chris Lattnerf77e11b2010-11-23 21:53:56 +0000102
103 return true;
104 }
105
106 return false;
Chris Lattnerdd278432010-11-23 21:17:56 +0000107}
108
Chris Lattner226efd32010-11-23 19:19:34 +0000109MemorizeStatCalls::LookupResult
Mehdi Amini0df59d82016-10-11 07:31:29 +0000110MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +0000111 std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
Ben Langmuirc8130a72014-02-20 21:59:23 +0000112 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000113
Chris Lattner226efd32010-11-23 19:19:34 +0000114 // 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 Lattner8f0583d2010-11-23 20:05:15 +0000118 if (Result == CacheMissing)
Chris Lattner226efd32010-11-23 19:19:34 +0000119 return Result;
120
121 // Cache file 'stat' results and directories with absolutely paths.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000122 if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
123 StatCalls[Path] = Data;
124
Chris Lattner226efd32010-11-23 19:19:34 +0000125 return Result;
126}