blob: 83e42bdb8483ef12b223093f86f89f851e972fd0 [file] [log] [blame]
Chris Lattner10e286a2010-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"
Stephen Hines651f13c2014-04-23 16:59:28 -070015#include "clang/Basic/VirtualFileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000016#include "llvm/Support/Path.h"
Chris Lattnerb2b93c12010-11-23 21:53:56 +000017
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 Lattner10e286a2010-11-23 19:19:34 +000025using namespace clang;
26
Chris Lattner72f61302010-11-23 20:07:39 +000027#if defined(_MSC_VER)
Francois Pichet0d4739ab2010-11-24 03:07:43 +000028#define S_ISDIR(s) ((_S_IFDIR & s) !=0)
Chris Lattner72f61302010-11-23 20:07:39 +000029#endif
30
David Blaikie99ba9e32011-12-20 02:48:34 +000031void FileSystemStatCache::anchor() { }
32
Stephen Hines651f13c2014-04-23 16:59:28 -070033static void copyStatusToFileData(const vfs::Status &Status,
Rafael Espindola0fda0f72013-08-01 21:42:11 +000034 FileData &Data) {
Stephen Hines651f13c2014-04-23 16:59:28 -070035 Data.Name = Status.getName();
Rafael Espindola0fda0f72013-08-01 21:42:11 +000036 Data.Size = Status.getSize();
37 Data.ModTime = Status.getLastModificationTime().toEpochTime();
38 Data.UniqueID = Status.getUniqueID();
Stephen Hines651f13c2014-04-23 16:59:28 -070039 Data.IsDirectory = Status.isDirectory();
40 Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
Rafael Espindola0fda0f72013-08-01 21:42:11 +000041 Data.InPCH = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070042 Data.IsVFSMapped = Status.IsVFSMapped;
Rafael Espindola0fda0f72013-08-01 21:42:11 +000043}
44
Chris Lattner898a0612010-11-23 21:17:56 +000045/// FileSystemStatCache::get - Get the 'stat' information for the specified
Chris Lattnerc0f31fd2010-12-02 04:27:29 +000046/// path, using the cache to accelerate it if possible. This returns true if
Chris Lattner898a0612010-11-23 21:17:56 +000047/// the path does not exist or false if it exists.
48///
Argyrios Kyrtzidise5d30e32012-12-11 07:48:23 +000049/// If isFile is true, then this lookup should only return success for files
50/// (not directories). If it is false this lookup should only return
Chris Lattner898a0612010-11-23 21:17:56 +000051/// success for directories (not files). On a successful file lookup, the
52/// implementation can optionally fill in FileDescriptor with a valid
53/// descriptor and the client guarantees that it will close it.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000054bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
Stephen Hinesc568f1e2014-07-21 00:47:37 -070055 std::unique_ptr<vfs::File> *F,
56 FileSystemStatCache *Cache, vfs::FileSystem &FS) {
Chris Lattner898a0612010-11-23 21:17:56 +000057 LookupResult R;
Argyrios Kyrtzidise5d30e32012-12-11 07:48:23 +000058 bool isForDir = !isFile;
Chris Lattner5cc1c732010-11-23 22:32:37 +000059
60 // If we have a cache, use it to resolve the stat query.
Chris Lattner898a0612010-11-23 21:17:56 +000061 if (Cache)
Stephen Hines651f13c2014-04-23 16:59:28 -070062 R = Cache->getStat(Path, Data, isFile, F, FS);
63 else if (isForDir || !F) {
Argyrios Kyrtzidise5d30e32012-12-11 07:48:23 +000064 // If this is a directory or a file descriptor is not needed and we have
65 // no cache, just go to the file system.
Stephen Hines651f13c2014-04-23 16:59:28 -070066 llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
67 if (!Status) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000068 R = CacheMissing;
69 } else {
70 R = CacheExists;
Stephen Hines651f13c2014-04-23 16:59:28 -070071 copyStatusToFileData(*Status, Data);
Rafael Espindola0fda0f72013-08-01 21:42:11 +000072 }
Chris Lattner5cc1c732010-11-23 22:32:37 +000073 } else {
74 // Otherwise, we have to go to the filesystem. We can always just use
75 // 'stat' here, but (for files) the client is asking whether the file exists
76 // because it wants to turn around and *open* it. It is more efficient to
77 // do "open+fstat" on success than it is to do "stat+open".
78 //
79 // Because of this, check to see if the file exists with 'open'. If the
80 // open succeeds, use fstat to get the stat info.
Stephen Hines176edba2014-12-01 14:53:08 -080081 auto OwnedFile = FS.openFileForRead(Path);
Rafael Espindolad965f952013-07-16 19:44:23 +000082
Stephen Hines176edba2014-12-01 14:53:08 -080083 if (!OwnedFile) {
Chris Lattner5cc1c732010-11-23 22:32:37 +000084 // If the open fails, our "stat" fails.
85 R = CacheMissing;
86 } else {
87 // Otherwise, the open succeeded. Do an fstat to get the information
88 // about the file. We'll end up returning the open file descriptor to the
89 // client to do what they please with it.
Stephen Hines176edba2014-12-01 14:53:08 -080090 llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status();
Stephen Hines651f13c2014-04-23 16:59:28 -070091 if (Status) {
Chris Lattner5cc1c732010-11-23 22:32:37 +000092 R = CacheExists;
Stephen Hines651f13c2014-04-23 16:59:28 -070093 copyStatusToFileData(*Status, Data);
Stephen Hines176edba2014-12-01 14:53:08 -080094 *F = std::move(*OwnedFile);
Rafael Espindola0fda0f72013-08-01 21:42:11 +000095 } else {
Chris Lattner5cc1c732010-11-23 22:32:37 +000096 // fstat rarely fails. If it does, claim the initial open didn't
97 // succeed.
98 R = CacheMissing;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070099 *F = nullptr;
Chris Lattner5cc1c732010-11-23 22:32:37 +0000100 }
101 }
102 }
Chris Lattner898a0612010-11-23 21:17:56 +0000103
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000104 // If the path doesn't exist, return failure.
Chris Lattner898a0612010-11-23 21:17:56 +0000105 if (R == CacheMissing) return true;
106
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000107 // If the path exists, make sure that its "directoryness" matches the clients
108 // demands.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000109 if (Data.IsDirectory != isForDir) {
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000110 // If not, close the file if opened.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700111 if (F)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700112 *F = nullptr;
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000113
114 return true;
115 }
116
117 return false;
Chris Lattner898a0612010-11-23 21:17:56 +0000118}
119
Chris Lattner10e286a2010-11-23 19:19:34 +0000120MemorizeStatCalls::LookupResult
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000121MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700122 std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700123 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000124
Chris Lattner10e286a2010-11-23 19:19:34 +0000125 // Do not cache failed stats, it is easy to construct common inconsistent
126 // situations if we do, and they are not important for PCH performance (which
127 // currently only needs the stats to construct the initial FileManager
128 // entries).
Chris Lattnerd6f61112010-11-23 20:05:15 +0000129 if (Result == CacheMissing)
Chris Lattner10e286a2010-11-23 19:19:34 +0000130 return Result;
131
132 // Cache file 'stat' results and directories with absolutely paths.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000133 if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
134 StatCalls[Path] = Data;
135
Chris Lattner10e286a2010-11-23 19:19:34 +0000136 return Result;
137}