blob: 359e1924d00952dcc86b0055a7cb35535e6d7b36 [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 Lattner5cc1c732010-11-23 22:32:37 +000016#include <fcntl.h>
Chris Lattnerb2b93c12010-11-23 21:53:56 +000017
18// FIXME: This is terrible, we need this for ::close.
19#if !defined(_MSC_VER) && !defined(__MINGW32__)
20#include <unistd.h>
21#include <sys/uio.h>
22#else
23#include <io.h>
24#endif
Chris Lattner10e286a2010-11-23 19:19:34 +000025using namespace clang;
26
Chris Lattner72f61302010-11-23 20:07:39 +000027#if defined(_MSC_VER)
Francois Pichet0d4739ab2010-11-24 03:07:43 +000028#define S_ISDIR(s) ((_S_IFDIR & s) !=0)
Chris Lattner72f61302010-11-23 20:07:39 +000029#endif
30
Chris Lattner898a0612010-11-23 21:17:56 +000031/// FileSystemStatCache::get - Get the 'stat' information for the specified
32/// path, using the cache to accellerate it if possible. This returns true if
33/// the path does not exist or false if it exists.
34///
35/// If FileDescriptor is non-null, then this lookup should only return success
36/// for files (not directories). If it is null this lookup should only return
37/// success for directories (not files). On a successful file lookup, the
38/// implementation can optionally fill in FileDescriptor with a valid
39/// descriptor and the client guarantees that it will close it.
40bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
41 int *FileDescriptor, FileSystemStatCache *Cache) {
42 LookupResult R;
Chris Lattner5cc1c732010-11-23 22:32:37 +000043 bool isForDir = FileDescriptor == 0;
44
45 // If we have a cache, use it to resolve the stat query.
Chris Lattner898a0612010-11-23 21:17:56 +000046 if (Cache)
47 R = Cache->getStat(Path, StatBuf, FileDescriptor);
Chris Lattner5cc1c732010-11-23 22:32:37 +000048 else if (isForDir) {
49 // If this is a directory and we have no cache, just go to the file system.
Chris Lattner898a0612010-11-23 21:17:56 +000050 R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
Chris Lattner5cc1c732010-11-23 22:32:37 +000051 } else {
52 // Otherwise, we have to go to the filesystem. We can always just use
53 // 'stat' here, but (for files) the client is asking whether the file exists
54 // because it wants to turn around and *open* it. It is more efficient to
55 // do "open+fstat" on success than it is to do "stat+open".
56 //
57 // Because of this, check to see if the file exists with 'open'. If the
58 // open succeeds, use fstat to get the stat info.
59 int OpenFlags = O_RDONLY;
60#ifdef O_BINARY
61 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
62#endif
63 *FileDescriptor = ::open(Path, OpenFlags);
64
65 if (*FileDescriptor == -1) {
66 // If the open fails, our "stat" fails.
67 R = CacheMissing;
68 } else {
69 // Otherwise, the open succeeded. Do an fstat to get the information
70 // about the file. We'll end up returning the open file descriptor to the
71 // client to do what they please with it.
72 if (::fstat(*FileDescriptor, &StatBuf) == 0)
73 R = CacheExists;
74 else {
75 // fstat rarely fails. If it does, claim the initial open didn't
76 // succeed.
77 R = CacheMissing;
78 ::close(*FileDescriptor);
79 *FileDescriptor = -1;
80 }
81 }
82 }
Chris Lattner898a0612010-11-23 21:17:56 +000083
Chris Lattnerb2b93c12010-11-23 21:53:56 +000084 // If the path doesn't exist, return failure.
Chris Lattner898a0612010-11-23 21:17:56 +000085 if (R == CacheMissing) return true;
86
Chris Lattnerb2b93c12010-11-23 21:53:56 +000087 // If the path exists, make sure that its "directoryness" matches the clients
88 // demands.
Chris Lattnerb2b93c12010-11-23 21:53:56 +000089 if (S_ISDIR(StatBuf.st_mode) != isForDir) {
90 // If not, close the file if opened.
91 if (FileDescriptor && *FileDescriptor != -1) {
92 ::close(*FileDescriptor);
93 *FileDescriptor = -1;
94 }
95
96 return true;
97 }
98
99 return false;
Chris Lattner898a0612010-11-23 21:17:56 +0000100}
101
102
Chris Lattner10e286a2010-11-23 19:19:34 +0000103MemorizeStatCalls::LookupResult
Chris Lattner898a0612010-11-23 21:17:56 +0000104MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
105 int *FileDescriptor) {
106 LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
Chris Lattner10e286a2010-11-23 19:19:34 +0000107
Chris Lattner10e286a2010-11-23 19:19:34 +0000108 // Do not cache failed stats, it is easy to construct common inconsistent
109 // situations if we do, and they are not important for PCH performance (which
110 // currently only needs the stats to construct the initial FileManager
111 // entries).
Chris Lattnerd6f61112010-11-23 20:05:15 +0000112 if (Result == CacheMissing)
Chris Lattner10e286a2010-11-23 19:19:34 +0000113 return Result;
114
115 // Cache file 'stat' results and directories with absolutely paths.
116 if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
Chris Lattner74e976b2010-11-23 19:28:12 +0000117 StatCalls[Path] = StatBuf;
Chris Lattner10e286a2010-11-23 19:19:34 +0000118
119 return Result;
120}