1218887Sdim//===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
2218887Sdim//
3218887Sdim//                     The LLVM Compiler Infrastructure
4218887Sdim//
5218887Sdim// This file is distributed under the University of Illinois Open Source
6218887Sdim// License. See LICENSE.TXT for details.
7218887Sdim//
8218887Sdim//===----------------------------------------------------------------------===//
9218887Sdim//
10218887Sdim//  This file defines the FileSystemStatCache interface.
11218887Sdim//
12218887Sdim//===----------------------------------------------------------------------===//
13218887Sdim
14218887Sdim#include "clang/Basic/FileSystemStatCache.h"
15218887Sdim#include "llvm/Support/Path.h"
16218887Sdim#include <fcntl.h>
17218887Sdim
18218887Sdim// FIXME: This is terrible, we need this for ::close.
19218887Sdim#if !defined(_MSC_VER) && !defined(__MINGW32__)
20218887Sdim#include <unistd.h>
21218887Sdim#include <sys/uio.h>
22218887Sdim#else
23218887Sdim#include <io.h>
24218887Sdim#endif
25218887Sdimusing namespace clang;
26218887Sdim
27218887Sdim#if defined(_MSC_VER)
28218887Sdim#define S_ISDIR(s) ((_S_IFDIR & s) !=0)
29218887Sdim#endif
30218887Sdim
31234353Sdimvoid FileSystemStatCache::anchor() { }
32234353Sdim
33218887Sdim/// FileSystemStatCache::get - Get the 'stat' information for the specified
34218887Sdim/// path, using the cache to accelerate it if possible.  This returns true if
35218887Sdim/// the path does not exist or false if it exists.
36218887Sdim///
37249423Sdim/// If isFile is true, then this lookup should only return success for files
38249423Sdim/// (not directories).  If it is false this lookup should only return
39218887Sdim/// success for directories (not files).  On a successful file lookup, the
40218887Sdim/// implementation can optionally fill in FileDescriptor with a valid
41218887Sdim/// descriptor and the client guarantees that it will close it.
42218887Sdimbool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
43249423Sdim                              bool isFile, int *FileDescriptor,
44249423Sdim                              FileSystemStatCache *Cache) {
45218887Sdim  LookupResult R;
46249423Sdim  bool isForDir = !isFile;
47218887Sdim
48218887Sdim  // If we have a cache, use it to resolve the stat query.
49218887Sdim  if (Cache)
50249423Sdim    R = Cache->getStat(Path, StatBuf, isFile, FileDescriptor);
51249423Sdim  else if (isForDir || !FileDescriptor) {
52249423Sdim    // If this is a directory or a file descriptor is not needed and we have
53249423Sdim    // no cache, just go to the file system.
54218887Sdim    R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
55218887Sdim  } else {
56218887Sdim    // Otherwise, we have to go to the filesystem.  We can always just use
57218887Sdim    // 'stat' here, but (for files) the client is asking whether the file exists
58218887Sdim    // because it wants to turn around and *open* it.  It is more efficient to
59218887Sdim    // do "open+fstat" on success than it is to do "stat+open".
60218887Sdim    //
61218887Sdim    // Because of this, check to see if the file exists with 'open'.  If the
62218887Sdim    // open succeeds, use fstat to get the stat info.
63218887Sdim    int OpenFlags = O_RDONLY;
64218887Sdim#ifdef O_BINARY
65218887Sdim    OpenFlags |= O_BINARY;  // Open input file in binary mode on win32.
66218887Sdim#endif
67218887Sdim    *FileDescriptor = ::open(Path, OpenFlags);
68218887Sdim
69218887Sdim    if (*FileDescriptor == -1) {
70218887Sdim      // If the open fails, our "stat" fails.
71218887Sdim      R = CacheMissing;
72218887Sdim    } else {
73218887Sdim      // Otherwise, the open succeeded.  Do an fstat to get the information
74218887Sdim      // about the file.  We'll end up returning the open file descriptor to the
75218887Sdim      // client to do what they please with it.
76218887Sdim      if (::fstat(*FileDescriptor, &StatBuf) == 0)
77218887Sdim        R = CacheExists;
78218887Sdim      else {
79218887Sdim        // fstat rarely fails.  If it does, claim the initial open didn't
80218887Sdim        // succeed.
81218887Sdim        R = CacheMissing;
82218887Sdim        ::close(*FileDescriptor);
83218887Sdim        *FileDescriptor = -1;
84218887Sdim      }
85218887Sdim    }
86218887Sdim  }
87218887Sdim
88218887Sdim  // If the path doesn't exist, return failure.
89218887Sdim  if (R == CacheMissing) return true;
90218887Sdim
91218887Sdim  // If the path exists, make sure that its "directoryness" matches the clients
92218887Sdim  // demands.
93218887Sdim  if (S_ISDIR(StatBuf.st_mode) != isForDir) {
94218887Sdim    // If not, close the file if opened.
95218887Sdim    if (FileDescriptor && *FileDescriptor != -1) {
96218887Sdim      ::close(*FileDescriptor);
97218887Sdim      *FileDescriptor = -1;
98218887Sdim    }
99218887Sdim
100218887Sdim    return true;
101218887Sdim  }
102218887Sdim
103218887Sdim  return false;
104218887Sdim}
105218887Sdim
106218887Sdim
107218887SdimMemorizeStatCalls::LookupResult
108218887SdimMemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
109249423Sdim                           bool isFile, int *FileDescriptor) {
110249423Sdim  LookupResult Result = statChained(Path, StatBuf, isFile, FileDescriptor);
111218887Sdim
112218887Sdim  // Do not cache failed stats, it is easy to construct common inconsistent
113218887Sdim  // situations if we do, and they are not important for PCH performance (which
114218887Sdim  // currently only needs the stats to construct the initial FileManager
115218887Sdim  // entries).
116218887Sdim  if (Result == CacheMissing)
117218887Sdim    return Result;
118218887Sdim
119218887Sdim  // Cache file 'stat' results and directories with absolutely paths.
120218887Sdim  if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::path::is_absolute(Path))
121218887Sdim    StatCalls[Path] = StatBuf;
122218887Sdim
123218887Sdim  return Result;
124218887Sdim}
125