blob: c6b11e9024ccd3e7a07022e50284900a0514108b [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"
15#include "llvm/System/Path.h"
Chris Lattnerb2b93c12010-11-23 21:53:56 +000016
17// FIXME: This is terrible, we need this for ::close.
18#if !defined(_MSC_VER) && !defined(__MINGW32__)
19#include <unistd.h>
20#include <sys/uio.h>
21#else
22#include <io.h>
23#endif
Chris Lattner10e286a2010-11-23 19:19:34 +000024using namespace clang;
25
Chris Lattner72f61302010-11-23 20:07:39 +000026#if defined(_MSC_VER)
27#define S_ISDIR(s) (_S_IFDIR & s)
28#endif
29
Chris Lattner898a0612010-11-23 21:17:56 +000030/// FileSystemStatCache::get - Get the 'stat' information for the specified
31/// path, using the cache to accellerate it if possible. This returns true if
32/// the path does not exist or false if it exists.
33///
34/// If FileDescriptor is non-null, then this lookup should only return success
35/// for files (not directories). If it is null this lookup should only return
36/// success for directories (not files). On a successful file lookup, the
37/// implementation can optionally fill in FileDescriptor with a valid
38/// descriptor and the client guarantees that it will close it.
39bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
40 int *FileDescriptor, FileSystemStatCache *Cache) {
41 LookupResult R;
42
43 if (Cache)
44 R = Cache->getStat(Path, StatBuf, FileDescriptor);
45 else
46 R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
47
Chris Lattnerb2b93c12010-11-23 21:53:56 +000048 // If the path doesn't exist, return failure.
Chris Lattner898a0612010-11-23 21:17:56 +000049 if (R == CacheMissing) return true;
50
Chris Lattnerb2b93c12010-11-23 21:53:56 +000051 // If the path exists, make sure that its "directoryness" matches the clients
52 // demands.
Chris Lattner898a0612010-11-23 21:17:56 +000053 bool isForDir = FileDescriptor == 0;
Chris Lattnerb2b93c12010-11-23 21:53:56 +000054 if (S_ISDIR(StatBuf.st_mode) != isForDir) {
55 // If not, close the file if opened.
56 if (FileDescriptor && *FileDescriptor != -1) {
57 ::close(*FileDescriptor);
58 *FileDescriptor = -1;
59 }
60
61 return true;
62 }
63
64 return false;
Chris Lattner898a0612010-11-23 21:17:56 +000065}
66
67
Chris Lattner10e286a2010-11-23 19:19:34 +000068MemorizeStatCalls::LookupResult
Chris Lattner898a0612010-11-23 21:17:56 +000069MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
70 int *FileDescriptor) {
71 LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
Chris Lattner10e286a2010-11-23 19:19:34 +000072
Chris Lattner10e286a2010-11-23 19:19:34 +000073 // Do not cache failed stats, it is easy to construct common inconsistent
74 // situations if we do, and they are not important for PCH performance (which
75 // currently only needs the stats to construct the initial FileManager
76 // entries).
Chris Lattnerd6f61112010-11-23 20:05:15 +000077 if (Result == CacheMissing)
Chris Lattner10e286a2010-11-23 19:19:34 +000078 return Result;
79
80 // Cache file 'stat' results and directories with absolutely paths.
81 if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
Chris Lattner74e976b2010-11-23 19:28:12 +000082 StatCalls[Path] = StatBuf;
Chris Lattner10e286a2010-11-23 19:19:34 +000083
84 return Result;
85}