blob: 0f16e94a05ec17464dfdbd077d12cd2733a6e752 [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 Hines651f13c2014-04-23 16:59:28 -070055 vfs::File **F, FileSystemStatCache *Cache,
56 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 Hines651f13c2014-04-23 16:59:28 -070081 std::unique_ptr<vfs::File> OwnedFile;
82 llvm::error_code EC = FS.openFileForRead(Path, OwnedFile);
Rafael Espindolad965f952013-07-16 19:44:23 +000083
84 if (EC) {
Chris Lattner5cc1c732010-11-23 22:32:37 +000085 // If the open fails, our "stat" fails.
86 R = CacheMissing;
87 } else {
88 // Otherwise, the open succeeded. Do an fstat to get the information
89 // about the file. We'll end up returning the open file descriptor to the
90 // client to do what they please with it.
Stephen Hines651f13c2014-04-23 16:59:28 -070091 llvm::ErrorOr<vfs::Status> Status = OwnedFile->status();
92 if (Status) {
Chris Lattner5cc1c732010-11-23 22:32:37 +000093 R = CacheExists;
Stephen Hines651f13c2014-04-23 16:59:28 -070094 copyStatusToFileData(*Status, Data);
95 *F = OwnedFile.release();
Rafael Espindola0fda0f72013-08-01 21:42:11 +000096 } else {
Chris Lattner5cc1c732010-11-23 22:32:37 +000097 // fstat rarely fails. If it does, claim the initial open didn't
98 // succeed.
99 R = CacheMissing;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700100 *F = nullptr;
Chris Lattner5cc1c732010-11-23 22:32:37 +0000101 }
102 }
103 }
Chris Lattner898a0612010-11-23 21:17:56 +0000104
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000105 // If the path doesn't exist, return failure.
Chris Lattner898a0612010-11-23 21:17:56 +0000106 if (R == CacheMissing) return true;
107
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000108 // If the path exists, make sure that its "directoryness" matches the clients
109 // demands.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000110 if (Data.IsDirectory != isForDir) {
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000111 // If not, close the file if opened.
Stephen Hines651f13c2014-04-23 16:59:28 -0700112 if (F && *F) {
113 (*F)->close();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700114 *F = nullptr;
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000115 }
116
117 return true;
118 }
119
120 return false;
Chris Lattner898a0612010-11-23 21:17:56 +0000121}
122
Chris Lattner10e286a2010-11-23 19:19:34 +0000123MemorizeStatCalls::LookupResult
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000124MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -0700125 vfs::File **F, vfs::FileSystem &FS) {
126 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000127
Chris Lattner10e286a2010-11-23 19:19:34 +0000128 // Do not cache failed stats, it is easy to construct common inconsistent
129 // situations if we do, and they are not important for PCH performance (which
130 // currently only needs the stats to construct the initial FileManager
131 // entries).
Chris Lattnerd6f61112010-11-23 20:05:15 +0000132 if (Result == CacheMissing)
Chris Lattner10e286a2010-11-23 19:19:34 +0000133 return Result;
134
135 // Cache file 'stat' results and directories with absolutely paths.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000136 if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
137 StatCalls[Path] = Data;
138
Chris Lattner10e286a2010-11-23 19:19:34 +0000139 return Result;
140}