110e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
210e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//
310e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//                     The LLVM Compiler Infrastructure
410e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//
510e286aa8d39fb51a21412850265d9dae74613eeChris Lattner// This file is distributed under the University of Illinois Open Source
610e286aa8d39fb51a21412850265d9dae74613eeChris Lattner// License. See LICENSE.TXT for details.
710e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//
810e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//===----------------------------------------------------------------------===//
910e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//
1010e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//  This file defines the FileSystemStatCache interface.
1110e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//
1210e286aa8d39fb51a21412850265d9dae74613eeChris Lattner//===----------------------------------------------------------------------===//
1310e286aa8d39fb51a21412850265d9dae74613eeChris Lattner
1410e286aa8d39fb51a21412850265d9dae74613eeChris Lattner#include "clang/Basic/FileSystemStatCache.h"
15651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Basic/VirtualFileSystem.h"
1603013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
17b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner
18b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner// FIXME: This is terrible, we need this for ::close.
19b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner#if !defined(_MSC_VER) && !defined(__MINGW32__)
20b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner#include <unistd.h>
21b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner#include <sys/uio.h>
22b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner#else
23b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner#include <io.h>
24b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner#endif
2510e286aa8d39fb51a21412850265d9dae74613eeChris Lattnerusing namespace clang;
2610e286aa8d39fb51a21412850265d9dae74613eeChris Lattner
2772f6130a17742e9c9078b4b60ad8f7ac2c690a3bChris Lattner#if defined(_MSC_VER)
280d4739abf60372abdbafabd8ed59acc5d01729b5Francois Pichet#define S_ISDIR(s) ((_S_IFDIR & s) !=0)
2972f6130a17742e9c9078b4b60ad8f7ac2c690a3bChris Lattner#endif
3072f6130a17742e9c9078b4b60ad8f7ac2c690a3bChris Lattner
3199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid FileSystemStatCache::anchor() { }
3299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
33651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstatic void copyStatusToFileData(const vfs::Status &Status,
340fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola                                 FileData &Data) {
35651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Data.Name = Status.getName();
360fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  Data.Size = Status.getSize();
370fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  Data.ModTime = Status.getLastModificationTime().toEpochTime();
380fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  Data.UniqueID = Status.getUniqueID();
39651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Data.IsDirectory = Status.isDirectory();
40651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Data.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
410fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  Data.InPCH = false;
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Data.IsVFSMapped = Status.IsVFSMapped;
430fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola}
440fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola
45898a061f69e1145bf89a987c08203132b9922a3cChris Lattner/// FileSystemStatCache::get - Get the 'stat' information for the specified
46c0f31fd08537b65ad92db8ce860747e3402a07e6Chris Lattner/// path, using the cache to accelerate it if possible.  This returns true if
47898a061f69e1145bf89a987c08203132b9922a3cChris Lattner/// the path does not exist or false if it exists.
48898a061f69e1145bf89a987c08203132b9922a3cChris Lattner///
49e5d30e3b403539b10aaa52f03875a2243bf88904Argyrios Kyrtzidis/// If isFile is true, then this lookup should only return success for files
50e5d30e3b403539b10aaa52f03875a2243bf88904Argyrios Kyrtzidis/// (not directories).  If it is false this lookup should only return
51898a061f69e1145bf89a987c08203132b9922a3cChris Lattner/// success for directories (not files).  On a successful file lookup, the
52898a061f69e1145bf89a987c08203132b9922a3cChris Lattner/// implementation can optionally fill in FileDescriptor with a valid
53898a061f69e1145bf89a987c08203132b9922a3cChris Lattner/// descriptor and the client guarantees that it will close it.
540fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindolabool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
55ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                              std::unique_ptr<vfs::File> *F,
56ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                              FileSystemStatCache *Cache, vfs::FileSystem &FS) {
57898a061f69e1145bf89a987c08203132b9922a3cChris Lattner  LookupResult R;
58e5d30e3b403539b10aaa52f03875a2243bf88904Argyrios Kyrtzidis  bool isForDir = !isFile;
595cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner
605cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner  // If we have a cache, use it to resolve the stat query.
61898a061f69e1145bf89a987c08203132b9922a3cChris Lattner  if (Cache)
62651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    R = Cache->getStat(Path, Data, isFile, F, FS);
63651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  else if (isForDir || !F) {
64e5d30e3b403539b10aaa52f03875a2243bf88904Argyrios Kyrtzidis    // If this is a directory or a file descriptor is not needed and we have
65e5d30e3b403539b10aaa52f03875a2243bf88904Argyrios Kyrtzidis    // no cache, just go to the file system.
66651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::ErrorOr<vfs::Status> Status = FS.status(Path);
67651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (!Status) {
680fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      R = CacheMissing;
690fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    } else {
700fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      R = CacheExists;
71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      copyStatusToFileData(*Status, Data);
720fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    }
735cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner  } else {
745cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    // Otherwise, we have to go to the filesystem.  We can always just use
755cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    // 'stat' here, but (for files) the client is asking whether the file exists
765cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    // because it wants to turn around and *open* it.  It is more efficient to
775cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    // do "open+fstat" on success than it is to do "stat+open".
785cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    //
795cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    // Because of this, check to see if the file exists with 'open'.  If the
805cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    // open succeeds, use fstat to get the stat info.
81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    std::unique_ptr<vfs::File> OwnedFile;
82ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    std::error_code EC = FS.openFileForRead(Path, OwnedFile);
83d965f95daa97097c8ddc5e1165ceae585a888719Rafael Espindola
84d965f95daa97097c8ddc5e1165ceae585a888719Rafael Espindola    if (EC) {
855cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner      // If the open fails, our "stat" fails.
865cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner      R = CacheMissing;
875cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    } else {
885cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner      // Otherwise, the open succeeded.  Do an fstat to get the information
895cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner      // about the file.  We'll end up returning the open file descriptor to the
905cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner      // client to do what they please with it.
91651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      llvm::ErrorOr<vfs::Status> Status = OwnedFile->status();
92651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (Status) {
935cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner        R = CacheExists;
94651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        copyStatusToFileData(*Status, Data);
95ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        *F = std::move(OwnedFile);
960fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola      } else {
975cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner        // fstat rarely fails.  If it does, claim the initial open didn't
985cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner        // succeed.
995cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner        R = CacheMissing;
1006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        *F = nullptr;
1015cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner      }
1025cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner    }
1035cc1c738b0c51af55cbfe7672c284c19f8e30eb2Chris Lattner  }
104898a061f69e1145bf89a987c08203132b9922a3cChris Lattner
105b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner  // If the path doesn't exist, return failure.
106898a061f69e1145bf89a987c08203132b9922a3cChris Lattner  if (R == CacheMissing) return true;
107898a061f69e1145bf89a987c08203132b9922a3cChris Lattner
108b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner  // If the path exists, make sure that its "directoryness" matches the clients
109b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner  // demands.
1100fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  if (Data.IsDirectory != isForDir) {
111b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner    // If not, close the file if opened.
112ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    if (F)
1136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      *F = nullptr;
114b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner
115b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner    return true;
116b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner  }
117b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner
118b2b93c12d768cfdeb21909f734b04ccb82d75da2Chris Lattner  return false;
119898a061f69e1145bf89a987c08203132b9922a3cChris Lattner}
120898a061f69e1145bf89a987c08203132b9922a3cChris Lattner
12110e286aa8d39fb51a21412850265d9dae74613eeChris LattnerMemorizeStatCalls::LookupResult
1220fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael EspindolaMemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
123ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                           std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  LookupResult Result = statChained(Path, Data, isFile, F, FS);
1250fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola
12610e286aa8d39fb51a21412850265d9dae74613eeChris Lattner  // Do not cache failed stats, it is easy to construct common inconsistent
12710e286aa8d39fb51a21412850265d9dae74613eeChris Lattner  // situations if we do, and they are not important for PCH performance (which
12810e286aa8d39fb51a21412850265d9dae74613eeChris Lattner  // currently only needs the stats to construct the initial FileManager
12910e286aa8d39fb51a21412850265d9dae74613eeChris Lattner  // entries).
130d6f611198089b78e32d3a15fe8bc986204aee1aaChris Lattner  if (Result == CacheMissing)
13110e286aa8d39fb51a21412850265d9dae74613eeChris Lattner    return Result;
13210e286aa8d39fb51a21412850265d9dae74613eeChris Lattner
13310e286aa8d39fb51a21412850265d9dae74613eeChris Lattner  // Cache file 'stat' results and directories with absolutely paths.
1340fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola  if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
1350fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola    StatCalls[Path] = Data;
1360fda0f75f053ad9afdb2bb8d4ea72c5e6a49d657Rafael Espindola
13710e286aa8d39fb51a21412850265d9dae74613eeChris Lattner  return Result;
13810e286aa8d39fb51a21412850265d9dae74613eeChris Lattner}
139