1//===--- 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"
15#include "clang/Basic/VirtualFileSystem.h"
16#include "llvm/Support/Path.h"
17
18using namespace clang;
19
20void FileSystemStatCache::anchor() { }
21
22static void copyStatusToFileData(const vfs::Status &Status,
23                                 FileData &Data) {
24  Data.Name = Status.getName();
25  Data.Size = Status.getSize();
26  Data.ModTime = Status.getLastModificationTime().toEpochTime();
27  Data.UniqueID = Status.getUniqueID();
28  Data.IsDirectory = Status.isDirectory();
29  Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
30  Data.InPCH = false;
31  Data.IsVFSMapped = Status.IsVFSMapped;
32}
33
34/// FileSystemStatCache::get - Get the 'stat' information for the specified
35/// path, using the cache to accelerate it if possible.  This returns true if
36/// the path does not exist or false if it exists.
37///
38/// 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
40/// 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.
43bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
44                              std::unique_ptr<vfs::File> *F,
45                              FileSystemStatCache *Cache, vfs::FileSystem &FS) {
46  LookupResult R;
47  bool isForDir = !isFile;
48
49  // If we have a cache, use it to resolve the stat query.
50  if (Cache)
51    R = Cache->getStat(Path, Data, isFile, F, FS);
52  else if (isForDir || !F) {
53    // If this is a directory or a file descriptor is not needed and we have
54    // no cache, just go to the file system.
55    llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
56    if (!Status) {
57      R = CacheMissing;
58    } else {
59      R = CacheExists;
60      copyStatusToFileData(*Status, Data);
61    }
62  } 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.
70    auto OwnedFile = FS.openFileForRead(Path);
71
72    if (!OwnedFile) {
73      // 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.
79      llvm::ErrorOr<vfs::Status> Status = (*OwnedFile)->status();
80      if (Status) {
81        R = CacheExists;
82        copyStatusToFileData(*Status, Data);
83        *F = std::move(*OwnedFile);
84      } else {
85        // fstat rarely fails.  If it does, claim the initial open didn't
86        // succeed.
87        R = CacheMissing;
88        *F = nullptr;
89      }
90    }
91  }
92
93  // If the path doesn't exist, return failure.
94  if (R == CacheMissing) return true;
95
96  // If the path exists, make sure that its "directoryness" matches the clients
97  // demands.
98  if (Data.IsDirectory != isForDir) {
99    // If not, close the file if opened.
100    if (F)
101      *F = nullptr;
102
103    return true;
104  }
105
106  return false;
107}
108
109MemorizeStatCalls::LookupResult
110MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
111                           std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
112  LookupResult Result = statChained(Path, Data, isFile, F, FS);
113
114  // 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).
118  if (Result == CacheMissing)
119    return Result;
120
121  // Cache file 'stat' results and directories with absolutely paths.
122  if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
123    StatCalls[Path] = Data;
124
125  return Result;
126}
127