blob: 9d87999509eed8eac188f44473edcc03645ff3d5 [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;
42}
43
Chris Lattner898a0612010-11-23 21:17:56 +000044/// FileSystemStatCache::get - Get the 'stat' information for the specified
Chris Lattnerc0f31fd2010-12-02 04:27:29 +000045/// path, using the cache to accelerate it if possible. This returns true if
Chris Lattner898a0612010-11-23 21:17:56 +000046/// the path does not exist or false if it exists.
47///
Argyrios Kyrtzidise5d30e32012-12-11 07:48:23 +000048/// If isFile is true, then this lookup should only return success for files
49/// (not directories). If it is false this lookup should only return
Chris Lattner898a0612010-11-23 21:17:56 +000050/// success for directories (not files). On a successful file lookup, the
51/// implementation can optionally fill in FileDescriptor with a valid
52/// descriptor and the client guarantees that it will close it.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000053bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -070054 vfs::File **F, FileSystemStatCache *Cache,
55 vfs::FileSystem &FS) {
Chris Lattner898a0612010-11-23 21:17:56 +000056 LookupResult R;
Argyrios Kyrtzidise5d30e32012-12-11 07:48:23 +000057 bool isForDir = !isFile;
Chris Lattner5cc1c732010-11-23 22:32:37 +000058
59 // If we have a cache, use it to resolve the stat query.
Chris Lattner898a0612010-11-23 21:17:56 +000060 if (Cache)
Stephen Hines651f13c2014-04-23 16:59:28 -070061 R = Cache->getStat(Path, Data, isFile, F, FS);
62 else if (isForDir || !F) {
Argyrios Kyrtzidise5d30e32012-12-11 07:48:23 +000063 // If this is a directory or a file descriptor is not needed and we have
64 // no cache, just go to the file system.
Stephen Hines651f13c2014-04-23 16:59:28 -070065 llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
66 if (!Status) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000067 R = CacheMissing;
68 } else {
69 R = CacheExists;
Stephen Hines651f13c2014-04-23 16:59:28 -070070 copyStatusToFileData(*Status, Data);
Rafael Espindola0fda0f72013-08-01 21:42:11 +000071 }
Chris Lattner5cc1c732010-11-23 22:32:37 +000072 } else {
73 // Otherwise, we have to go to the filesystem. We can always just use
74 // 'stat' here, but (for files) the client is asking whether the file exists
75 // because it wants to turn around and *open* it. It is more efficient to
76 // do "open+fstat" on success than it is to do "stat+open".
77 //
78 // Because of this, check to see if the file exists with 'open'. If the
79 // open succeeds, use fstat to get the stat info.
Stephen Hines651f13c2014-04-23 16:59:28 -070080 std::unique_ptr<vfs::File> OwnedFile;
81 llvm::error_code EC = FS.openFileForRead(Path, OwnedFile);
Rafael Espindolad965f952013-07-16 19:44:23 +000082
83 if (EC) {
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 Hines651f13c2014-04-23 16:59:28 -070090 llvm::ErrorOr<vfs::Status> Status = OwnedFile->status();
91 if (Status) {
Chris Lattner5cc1c732010-11-23 22:32:37 +000092 R = CacheExists;
Stephen Hines651f13c2014-04-23 16:59:28 -070093 copyStatusToFileData(*Status, Data);
94 *F = OwnedFile.release();
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 Hines651f13c2014-04-23 16:59:28 -070099 *F = 0;
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 Hines651f13c2014-04-23 16:59:28 -0700111 if (F && *F) {
112 (*F)->close();
113 *F = 0;
Chris Lattnerb2b93c12010-11-23 21:53:56 +0000114 }
115
116 return true;
117 }
118
119 return false;
Chris Lattner898a0612010-11-23 21:17:56 +0000120}
121
Chris Lattner10e286a2010-11-23 19:19:34 +0000122MemorizeStatCalls::LookupResult
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000123MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -0700124 vfs::File **F, vfs::FileSystem &FS) {
125 LookupResult Result = statChained(Path, Data, isFile, F, FS);
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000126
Chris Lattner10e286a2010-11-23 19:19:34 +0000127 // Do not cache failed stats, it is easy to construct common inconsistent
128 // situations if we do, and they are not important for PCH performance (which
129 // currently only needs the stats to construct the initial FileManager
130 // entries).
Chris Lattnerd6f61112010-11-23 20:05:15 +0000131 if (Result == CacheMissing)
Chris Lattner10e286a2010-11-23 19:19:34 +0000132 return Result;
133
134 // Cache file 'stat' results and directories with absolutely paths.
Rafael Espindola0fda0f72013-08-01 21:42:11 +0000135 if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
136 StatCalls[Path] = Data;
137
Chris Lattner10e286a2010-11-23 19:19:34 +0000138 return Result;
139}