Home | History | Annotate | Line # | Download | only in Basic
      1  1.1  joerg //===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
      2  1.1  joerg //
      3  1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4  1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      5  1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6  1.1  joerg //
      7  1.1  joerg //===----------------------------------------------------------------------===//
      8  1.1  joerg //
      9  1.1  joerg //  This file defines the FileSystemStatCache interface.
     10  1.1  joerg //
     11  1.1  joerg //===----------------------------------------------------------------------===//
     12  1.1  joerg 
     13  1.1  joerg #include "clang/Basic/FileSystemStatCache.h"
     14  1.1  joerg #include "llvm/Support/Chrono.h"
     15  1.1  joerg #include "llvm/Support/ErrorOr.h"
     16  1.1  joerg #include "llvm/Support/Path.h"
     17  1.1  joerg #include "llvm/Support/VirtualFileSystem.h"
     18  1.1  joerg #include <utility>
     19  1.1  joerg 
     20  1.1  joerg using namespace clang;
     21  1.1  joerg 
     22  1.1  joerg void FileSystemStatCache::anchor() {}
     23  1.1  joerg 
     24  1.1  joerg /// FileSystemStatCache::get - Get the 'stat' information for the specified
     25  1.1  joerg /// path, using the cache to accelerate it if possible.  This returns true if
     26  1.1  joerg /// the path does not exist or false if it exists.
     27  1.1  joerg ///
     28  1.1  joerg /// If isFile is true, then this lookup should only return success for files
     29  1.1  joerg /// (not directories).  If it is false this lookup should only return
     30  1.1  joerg /// success for directories (not files).  On a successful file lookup, the
     31  1.1  joerg /// implementation can optionally fill in FileDescriptor with a valid
     32  1.1  joerg /// descriptor and the client guarantees that it will close it.
     33  1.1  joerg std::error_code
     34  1.1  joerg FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
     35  1.1  joerg                          bool isFile, std::unique_ptr<llvm::vfs::File> *F,
     36  1.1  joerg                          FileSystemStatCache *Cache,
     37  1.1  joerg                          llvm::vfs::FileSystem &FS) {
     38  1.1  joerg   bool isForDir = !isFile;
     39  1.1  joerg   std::error_code RetCode;
     40  1.1  joerg 
     41  1.1  joerg   // If we have a cache, use it to resolve the stat query.
     42  1.1  joerg   if (Cache)
     43  1.1  joerg     RetCode = Cache->getStat(Path, Status, isFile, F, FS);
     44  1.1  joerg   else if (isForDir || !F) {
     45  1.1  joerg     // If this is a directory or a file descriptor is not needed and we have
     46  1.1  joerg     // no cache, just go to the file system.
     47  1.1  joerg     llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);
     48  1.1  joerg     if (!StatusOrErr) {
     49  1.1  joerg       RetCode = StatusOrErr.getError();
     50  1.1  joerg     } else {
     51  1.1  joerg       Status = *StatusOrErr;
     52  1.1  joerg     }
     53  1.1  joerg   } else {
     54  1.1  joerg     // Otherwise, we have to go to the filesystem.  We can always just use
     55  1.1  joerg     // 'stat' here, but (for files) the client is asking whether the file exists
     56  1.1  joerg     // because it wants to turn around and *open* it.  It is more efficient to
     57  1.1  joerg     // do "open+fstat" on success than it is to do "stat+open".
     58  1.1  joerg     //
     59  1.1  joerg     // Because of this, check to see if the file exists with 'open'.  If the
     60  1.1  joerg     // open succeeds, use fstat to get the stat info.
     61  1.1  joerg     auto OwnedFile = FS.openFileForRead(Path);
     62  1.1  joerg 
     63  1.1  joerg     if (!OwnedFile) {
     64  1.1  joerg       // If the open fails, our "stat" fails.
     65  1.1  joerg       RetCode = OwnedFile.getError();
     66  1.1  joerg     } else {
     67  1.1  joerg       // Otherwise, the open succeeded.  Do an fstat to get the information
     68  1.1  joerg       // about the file.  We'll end up returning the open file descriptor to the
     69  1.1  joerg       // client to do what they please with it.
     70  1.1  joerg       llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
     71  1.1  joerg       if (StatusOrErr) {
     72  1.1  joerg         Status = *StatusOrErr;
     73  1.1  joerg         *F = std::move(*OwnedFile);
     74  1.1  joerg       } else {
     75  1.1  joerg         // fstat rarely fails.  If it does, claim the initial open didn't
     76  1.1  joerg         // succeed.
     77  1.1  joerg         *F = nullptr;
     78  1.1  joerg         RetCode = StatusOrErr.getError();
     79  1.1  joerg       }
     80  1.1  joerg     }
     81  1.1  joerg   }
     82  1.1  joerg 
     83  1.1  joerg   // If the path doesn't exist, return failure.
     84  1.1  joerg   if (RetCode)
     85  1.1  joerg     return RetCode;
     86  1.1  joerg 
     87  1.1  joerg   // If the path exists, make sure that its "directoryness" matches the clients
     88  1.1  joerg   // demands.
     89  1.1  joerg   if (Status.isDirectory() != isForDir) {
     90  1.1  joerg     // If not, close the file if opened.
     91  1.1  joerg     if (F)
     92  1.1  joerg       *F = nullptr;
     93  1.1  joerg     return std::make_error_code(
     94  1.1  joerg         Status.isDirectory() ?
     95  1.1  joerg             std::errc::is_a_directory : std::errc::not_a_directory);
     96  1.1  joerg   }
     97  1.1  joerg 
     98  1.1  joerg   return std::error_code();
     99  1.1  joerg }
    100  1.1  joerg 
    101  1.1  joerg std::error_code
    102  1.1  joerg MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
    103  1.1  joerg                            bool isFile,
    104  1.1  joerg                            std::unique_ptr<llvm::vfs::File> *F,
    105  1.1  joerg                            llvm::vfs::FileSystem &FS) {
    106  1.1  joerg   auto err = get(Path, Status, isFile, F, nullptr, FS);
    107  1.1  joerg   if (err) {
    108  1.1  joerg     // Do not cache failed stats, it is easy to construct common inconsistent
    109  1.1  joerg     // situations if we do, and they are not important for PCH performance
    110  1.1  joerg     // (which currently only needs the stats to construct the initial
    111  1.1  joerg     // FileManager entries).
    112  1.1  joerg     return err;
    113  1.1  joerg   }
    114  1.1  joerg 
    115  1.1  joerg   // Cache file 'stat' results and directories with absolutely paths.
    116  1.1  joerg   if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
    117  1.1  joerg     StatCalls[Path] = Status;
    118  1.1  joerg 
    119  1.1  joerg   return std::error_code();
    120  1.1  joerg }
    121