blob: ebee32670e0a5434a7dc839b0d3ab78755f79720 [file] [log] [blame]
Eugene Zelenko5a520112018-03-28 22:09:09 +00001//===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
Chris Lattner226efd32010-11-23 19:19:34 +00002//
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"
Eugene Zelenko5a520112018-03-28 22:09:09 +000016#include "llvm/Support/Chrono.h"
17#include "llvm/Support/ErrorOr.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000018#include "llvm/Support/Path.h"
Eugene Zelenko5a520112018-03-28 22:09:09 +000019#include <utility>
Chris Lattnerf77e11b2010-11-23 21:53:56 +000020
Chris Lattner226efd32010-11-23 19:19:34 +000021using namespace clang;
22
Eugene Zelenko5a520112018-03-28 22:09:09 +000023void FileSystemStatCache::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000024
Ben Langmuirc8130a72014-02-20 21:59:23 +000025static void copyStatusToFileData(const vfs::Status &Status,
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000026 FileData &Data) {
Ben Langmuird066d4c2014-02-28 21:16:07 +000027 Data.Name = Status.getName();
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000028 Data.Size = Status.getSize();
Pavel Labathac71c8e2016-11-09 10:52:22 +000029 Data.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000030 Data.UniqueID = Status.getUniqueID();
Ben Langmuirc8130a72014-02-20 21:59:23 +000031 Data.IsDirectory = Status.isDirectory();
32 Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000033 Data.InPCH = false;
Ben Langmuir5de00f32014-05-23 18:15:47 +000034 Data.IsVFSMapped = Status.IsVFSMapped;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000035}
36
Chris Lattnerdd278432010-11-23 21:17:56 +000037/// FileSystemStatCache::get - Get the 'stat' information for the specified
Chris Lattner4fc8fb02010-12-02 04:27:29 +000038/// path, using the cache to accelerate it if possible. This returns true if
Chris Lattnerdd278432010-11-23 21:17:56 +000039/// the path does not exist or false if it exists.
40///
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +000041/// 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 Lattnerdd278432010-11-23 21:17:56 +000043/// 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 Amini0df59d82016-10-11 07:31:29 +000046bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +000047 std::unique_ptr<vfs::File> *F,
48 FileSystemStatCache *Cache, vfs::FileSystem &FS) {
Chris Lattnerdd278432010-11-23 21:17:56 +000049 LookupResult R;
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +000050 bool isForDir = !isFile;
Chris Lattner5ea7d072010-11-23 22:32:37 +000051
52 // If we have a cache, use it to resolve the stat query.
Chris Lattnerdd278432010-11-23 21:17:56 +000053 if (Cache)
Ben Langmuirc8130a72014-02-20 21:59:23 +000054 R = Cache->getStat(Path, Data, isFile, F, FS);
55 else if (isForDir || !F) {
Argyrios Kyrtzidis3b779372012-12-11 07:48:23 +000056 // 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 Langmuirc8130a72014-02-20 21:59:23 +000058 llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
59 if (!Status) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000060 R = CacheMissing;
61 } else {
62 R = CacheExists;
Ben Langmuirc8130a72014-02-20 21:59:23 +000063 copyStatusToFileData(*Status, Data);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000064 }
Chris Lattner5ea7d072010-11-23 22:32:37 +000065 } 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 Kramera8857962014-10-26 22:44:13 +000073 auto OwnedFile = FS.openFileForRead(Path);
Rafael Espindola16125fb2013-07-16 19:44:23 +000074
Benjamin Kramera8857962014-10-26 22:44:13 +000075 if (!OwnedFile) {
Chris Lattner5ea7d072010-11-23 22:32:37 +000076 // 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 Kramera8857962014-10-26 22:44:13 +000082 llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status();
Ben Langmuirc8130a72014-02-20 21:59:23 +000083 if (Status) {
Chris Lattner5ea7d072010-11-23 22:32:37 +000084 R = CacheExists;
Ben Langmuirc8130a72014-02-20 21:59:23 +000085 copyStatusToFileData(*Status, Data);
Benjamin Kramera8857962014-10-26 22:44:13 +000086 *F = std::move(*OwnedFile);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000087 } else {
Chris Lattner5ea7d072010-11-23 22:32:37 +000088 // fstat rarely fails. If it does, claim the initial open didn't
89 // succeed.
90 R = CacheMissing;
Craig Topperf1186c52014-05-08 06:41:40 +000091 *F = nullptr;
Chris Lattner5ea7d072010-11-23 22:32:37 +000092 }
93 }
94 }
Chris Lattnerdd278432010-11-23 21:17:56 +000095
Chris Lattnerf77e11b2010-11-23 21:53:56 +000096 // If the path doesn't exist, return failure.
Chris Lattnerdd278432010-11-23 21:17:56 +000097 if (R == CacheMissing) return true;
98
Chris Lattnerf77e11b2010-11-23 21:53:56 +000099 // If the path exists, make sure that its "directoryness" matches the clients
100 // demands.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000101 if (Data.IsDirectory != isForDir) {
Chris Lattnerf77e11b2010-11-23 21:53:56 +0000102 // If not, close the file if opened.
David Blaikie326ffb32014-07-08 15:46:02 +0000103 if (F)
Craig Topperf1186c52014-05-08 06:41:40 +0000104 *F = nullptr;
Chris Lattnerf77e11b2010-11-23 21:53:56 +0000105
106 return true;
107 }
108
109 return false;
Chris Lattnerdd278432010-11-23 21:17:56 +0000110}
111
Chris Lattner226efd32010-11-23 19:19:34 +0000112MemorizeStatCalls::LookupResult
Mehdi Amini0df59d82016-10-11 07:31:29 +0000113MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile,
David Blaikie326ffb32014-07-08 15:46:02 +0000114 std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
Ben Langmuirc8130a72014-02-20 21:59:23 +0000115 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000116
Chris Lattner226efd32010-11-23 19:19:34 +0000117 // 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 Lattner8f0583d2010-11-23 20:05:15 +0000121 if (Result == CacheMissing)
Chris Lattner226efd32010-11-23 19:19:34 +0000122 return Result;
123
124 // Cache file 'stat' results and directories with absolutely paths.
Rafael Espindolaf8f91b82013-08-01 21:42:11 +0000125 if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
126 StatCalls[Path] = Data;
127
Chris Lattner226efd32010-11-23 19:19:34 +0000128 return Result;
129}