1 //===--- FileManager.cpp - File System Probing and Caching ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the FileManager interface.
12 //===----------------------------------------------------------------------===//
14 // TODO: This should index all interesting directories with dirent calls.
16 // opendir/readdir_r/closedir ?
18 //===----------------------------------------------------------------------===//
20 #include "clang/Basic/FileManager.h"
21 #include "clang/Basic/FileSystemStatCache.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/system_error.h"
29 #include "llvm/Config/config.h"
34 // FIXME: This is terrible, we need this for ::close.
35 #if !defined(_MSC_VER) && !defined(__MINGW32__)
41 using namespace clang
;
43 // FIXME: Enhance libsystem to support inode and other fields.
46 /// NON_EXISTENT_DIR - A special value distinct from null that is used to
47 /// represent a dir name that doesn't exist on the disk.
48 #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
50 /// NON_EXISTENT_FILE - A special value distinct from null that is used to
51 /// represent a filename that doesn't exist on the disk.
52 #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
55 FileEntry::~FileEntry() {
56 // If this FileEntry owns an open file descriptor that never got used, close
58 if (FD
!= -1) ::close(FD
);
61 //===----------------------------------------------------------------------===//
63 //===----------------------------------------------------------------------===//
67 #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
70 static std::string
GetFullPath(const char *relPath
) {
71 char *absPathStrPtr
= _fullpath(NULL
, relPath
, 0);
72 assert(absPathStrPtr
&& "_fullpath() returned NULL!");
74 std::string
absPath(absPathStrPtr
);
81 class FileManager::UniqueDirContainer
{
82 /// UniqueDirs - Cache from full path to existing directories/files.
84 llvm::StringMap
<DirectoryEntry
> UniqueDirs
;
87 DirectoryEntry
&getDirectory(const char *Name
, struct stat
&StatBuf
) {
88 std::string
FullPath(GetFullPath(Name
));
89 return UniqueDirs
.GetOrCreateValue(FullPath
).getValue();
92 size_t size() const { return UniqueDirs
.size(); }
95 class FileManager::UniqueFileContainer
{
96 /// UniqueFiles - Cache from full path to existing directories/files.
98 llvm::StringMap
<FileEntry
, llvm::BumpPtrAllocator
> UniqueFiles
;
101 FileEntry
&getFile(const char *Name
, struct stat
&StatBuf
) {
102 std::string
FullPath(GetFullPath(Name
));
104 // LowercaseString because Windows filesystem is case insensitive.
105 FullPath
= llvm::LowercaseString(FullPath
);
106 return UniqueFiles
.GetOrCreateValue(FullPath
).getValue();
109 size_t size() const { return UniqueFiles
.size(); }
112 //===----------------------------------------------------------------------===//
113 // Unix-like Systems.
114 //===----------------------------------------------------------------------===//
118 #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
120 class FileManager::UniqueDirContainer
{
121 /// UniqueDirs - Cache from ID's to existing directories/files.
122 std::map
<std::pair
<dev_t
, ino_t
>, DirectoryEntry
> UniqueDirs
;
125 DirectoryEntry
&getDirectory(const char *Name
, struct stat
&StatBuf
) {
126 return UniqueDirs
[std::make_pair(StatBuf
.st_dev
, StatBuf
.st_ino
)];
129 size_t size() const { return UniqueDirs
.size(); }
132 class FileManager::UniqueFileContainer
{
133 /// UniqueFiles - Cache from ID's to existing directories/files.
134 std::set
<FileEntry
> UniqueFiles
;
137 FileEntry
&getFile(const char *Name
, struct stat
&StatBuf
) {
139 const_cast<FileEntry
&>(
140 *UniqueFiles
.insert(FileEntry(StatBuf
.st_dev
,
142 StatBuf
.st_mode
)).first
);
145 size_t size() const { return UniqueFiles
.size(); }
150 //===----------------------------------------------------------------------===//
152 //===----------------------------------------------------------------------===//
154 FileManager::FileManager(const FileSystemOptions
&FSO
)
155 : FileSystemOpts(FSO
),
156 UniqueDirs(*new UniqueDirContainer()),
157 UniqueFiles(*new UniqueFileContainer()),
158 DirEntries(64), FileEntries(64), NextFileUID(0) {
159 NumDirLookups
= NumFileLookups
= 0;
160 NumDirCacheMisses
= NumFileCacheMisses
= 0;
163 FileManager::~FileManager() {
166 for (unsigned i
= 0, e
= VirtualFileEntries
.size(); i
!= e
; ++i
)
167 delete VirtualFileEntries
[i
];
170 void FileManager::addStatCache(FileSystemStatCache
*statCache
,
172 assert(statCache
&& "No stat cache provided?");
173 if (AtBeginning
|| StatCache
.get() == 0) {
174 statCache
->setNextStatCache(StatCache
.take());
175 StatCache
.reset(statCache
);
179 FileSystemStatCache
*LastCache
= StatCache
.get();
180 while (LastCache
->getNextStatCache())
181 LastCache
= LastCache
->getNextStatCache();
183 LastCache
->setNextStatCache(statCache
);
186 void FileManager::removeStatCache(FileSystemStatCache
*statCache
) {
190 if (StatCache
.get() == statCache
) {
191 // This is the first stat cache.
192 StatCache
.reset(StatCache
->takeNextStatCache());
196 // Find the stat cache in the list.
197 FileSystemStatCache
*PrevCache
= StatCache
.get();
198 while (PrevCache
&& PrevCache
->getNextStatCache() != statCache
)
199 PrevCache
= PrevCache
->getNextStatCache();
201 assert(PrevCache
&& "Stat cache not found for removal");
202 PrevCache
->setNextStatCache(statCache
->getNextStatCache());
205 /// \brief Retrieve the directory that the given file name resides in.
206 static const DirectoryEntry
*getDirectoryFromFile(FileManager
&FileMgr
,
207 llvm::StringRef Filename
) {
208 // Figure out what directory it is in. If the string contains a / in it,
209 // strip off everything after it.
210 // FIXME: this logic should be in sys::Path.
211 size_t SlashPos
= Filename
.size();
212 while (SlashPos
!= 0 && !IS_DIR_SEPARATOR_CHAR(Filename
[SlashPos
-1]))
215 // Use the current directory if file has no path component.
217 return FileMgr
.getDirectory(".");
219 if (SlashPos
== Filename
.size()-1)
220 return 0; // If filename ends with a /, it's a directory.
222 // Ignore repeated //'s.
223 while (SlashPos
!= 0 && IS_DIR_SEPARATOR_CHAR(Filename
[SlashPos
-1]))
226 return FileMgr
.getDirectory(Filename
.substr(0, SlashPos
));
229 /// getDirectory - Lookup, cache, and verify the specified directory. This
230 /// returns null if the directory doesn't exist.
232 const DirectoryEntry
*FileManager::getDirectory(llvm::StringRef Filename
) {
233 // stat doesn't like trailing separators (at least on Windows).
234 if (Filename
.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename
.back()))
235 Filename
= Filename
.substr(0, Filename
.size()-1);
238 llvm::StringMapEntry
<DirectoryEntry
*> &NamedDirEnt
=
239 DirEntries
.GetOrCreateValue(Filename
);
241 // See if there is already an entry in the map.
242 if (NamedDirEnt
.getValue())
243 return NamedDirEnt
.getValue() == NON_EXISTENT_DIR
244 ? 0 : NamedDirEnt
.getValue();
248 // By default, initialize it to invalid.
249 NamedDirEnt
.setValue(NON_EXISTENT_DIR
);
251 // Get the null-terminated directory name as stored as the key of the
253 const char *InterndDirName
= NamedDirEnt
.getKeyData();
255 // Check to see if the directory exists.
257 if (getStatValue(InterndDirName
, StatBuf
, 0/*directory lookup*/))
260 // It exists. See if we have already opened a directory with the same inode.
261 // This occurs when one dir is symlinked to another, for example.
262 DirectoryEntry
&UDE
= UniqueDirs
.getDirectory(InterndDirName
, StatBuf
);
264 NamedDirEnt
.setValue(&UDE
);
265 if (UDE
.getName()) // Already have an entry with this inode, return it.
268 // Otherwise, we don't have this directory yet, add it. We use the string
269 // key from the DirEntries map as the string.
270 UDE
.Name
= InterndDirName
;
274 /// getFile - Lookup, cache, and verify the specified file. This returns null
275 /// if the file doesn't exist.
277 const FileEntry
*FileManager::getFile(llvm::StringRef Filename
) {
280 // See if there is already an entry in the map.
281 llvm::StringMapEntry
<FileEntry
*> &NamedFileEnt
=
282 FileEntries
.GetOrCreateValue(Filename
);
284 // See if there is already an entry in the map.
285 if (NamedFileEnt
.getValue())
286 return NamedFileEnt
.getValue() == NON_EXISTENT_FILE
287 ? 0 : NamedFileEnt
.getValue();
289 ++NumFileCacheMisses
;
291 // By default, initialize it to invalid.
292 NamedFileEnt
.setValue(NON_EXISTENT_FILE
);
295 // Get the null-terminated file name as stored as the key of the
297 const char *InterndFileName
= NamedFileEnt
.getKeyData();
300 // Look up the directory for the file. When looking up something like
301 // sys/foo.h we'll discover all of the search directories that have a 'sys'
302 // subdirectory. This will let us avoid having to waste time on known-to-fail
303 // searches when we go to find sys/bar.h, because all the search directories
304 // without a 'sys' subdir will get a cached failure result.
305 const DirectoryEntry
*DirInfo
= getDirectoryFromFile(*this, Filename
);
306 if (DirInfo
== 0) // Directory doesn't exist, file can't exist.
309 // FIXME: Use the directory info to prune this, before doing the stat syscall.
310 // FIXME: This will reduce the # syscalls.
312 // Nope, there isn't. Check to see if the file exists.
313 int FileDescriptor
= -1;
315 if (getStatValue(InterndFileName
, StatBuf
, &FileDescriptor
))
318 // It exists. See if we have already opened a file with the same inode.
319 // This occurs when one dir is symlinked to another, for example.
320 FileEntry
&UFE
= UniqueFiles
.getFile(InterndFileName
, StatBuf
);
322 NamedFileEnt
.setValue(&UFE
);
323 if (UFE
.getName()) { // Already have an entry with this inode, return it.
324 // If the stat process opened the file, close it to avoid a FD leak.
325 if (FileDescriptor
!= -1)
326 close(FileDescriptor
);
331 // Otherwise, we don't have this directory yet, add it.
332 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
334 UFE
.Name
= InterndFileName
;
335 UFE
.Size
= StatBuf
.st_size
;
336 UFE
.ModTime
= StatBuf
.st_mtime
;
338 UFE
.UID
= NextFileUID
++;
339 UFE
.FD
= FileDescriptor
;
344 FileManager::getVirtualFile(llvm::StringRef Filename
, off_t Size
,
345 time_t ModificationTime
) {
348 // See if there is already an entry in the map.
349 llvm::StringMapEntry
<FileEntry
*> &NamedFileEnt
=
350 FileEntries
.GetOrCreateValue(Filename
);
352 // See if there is already an entry in the map.
353 if (NamedFileEnt
.getValue())
354 return NamedFileEnt
.getValue() == NON_EXISTENT_FILE
355 ? 0 : NamedFileEnt
.getValue();
357 ++NumFileCacheMisses
;
359 // By default, initialize it to invalid.
360 NamedFileEnt
.setValue(NON_EXISTENT_FILE
);
362 const DirectoryEntry
*DirInfo
= getDirectoryFromFile(*this, Filename
);
363 if (DirInfo
== 0) // Directory doesn't exist, file can't exist.
366 FileEntry
*UFE
= new FileEntry();
367 VirtualFileEntries
.push_back(UFE
);
368 NamedFileEnt
.setValue(UFE
);
370 // Get the null-terminated file name as stored as the key of the
372 const char *InterndFileName
= NamedFileEnt
.getKeyData();
374 UFE
->Name
= InterndFileName
;
376 UFE
->ModTime
= ModificationTime
;
378 UFE
->UID
= NextFileUID
++;
380 // If this virtual file resolves to a file, also map that file to the
381 // newly-created file entry.
382 int FileDescriptor
= -1;
384 if (getStatValue(InterndFileName
, StatBuf
, &FileDescriptor
))
387 UFE
->FD
= FileDescriptor
;
388 llvm::SmallString
<128> FilePath(UFE
->Name
);
389 llvm::sys::fs::make_absolute(FilePath
);
390 FileEntries
[FilePath
] = UFE
;
394 void FileManager::FixupRelativePath(llvm::sys::Path
&path
,
395 const FileSystemOptions
&FSOpts
) {
396 if (FSOpts
.WorkingDir
.empty() || llvm::sys::path::is_absolute(path
.str()))
399 llvm::SmallString
<128> NewPath(FSOpts
.WorkingDir
);
400 llvm::sys::path::append(NewPath
, path
.str());
404 llvm::MemoryBuffer
*FileManager::
405 getBufferForFile(const FileEntry
*Entry
, std::string
*ErrorStr
) {
406 llvm::OwningPtr
<llvm::MemoryBuffer
> Result
;
408 if (FileSystemOpts
.WorkingDir
.empty()) {
409 const char *Filename
= Entry
->getName();
410 // If the file is already open, use the open file descriptor.
411 if (Entry
->FD
!= -1) {
412 ec
= llvm::MemoryBuffer::getOpenFile(Entry
->FD
, Filename
, Result
,
415 *ErrorStr
= ec
.message();
416 // getOpenFile will have closed the file descriptor, don't reuse or
419 return Result
.take();
422 // Otherwise, open the file.
423 ec
= llvm::MemoryBuffer::getFile(Filename
, Result
, Entry
->getSize());
425 *ErrorStr
= ec
.message();
426 return Result
.take();
429 llvm::sys::Path
FilePath(Entry
->getName());
430 FixupRelativePath(FilePath
, FileSystemOpts
);
431 ec
= llvm::MemoryBuffer::getFile(FilePath
.c_str(), Result
, Entry
->getSize());
433 *ErrorStr
= ec
.message();
434 return Result
.take();
437 llvm::MemoryBuffer
*FileManager::
438 getBufferForFile(llvm::StringRef Filename
, std::string
*ErrorStr
) {
439 llvm::OwningPtr
<llvm::MemoryBuffer
> Result
;
441 if (FileSystemOpts
.WorkingDir
.empty()) {
442 ec
= llvm::MemoryBuffer::getFile(Filename
, Result
);
444 *ErrorStr
= ec
.message();
445 return Result
.take();
448 llvm::sys::Path
FilePath(Filename
);
449 FixupRelativePath(FilePath
, FileSystemOpts
);
450 ec
= llvm::MemoryBuffer::getFile(FilePath
.c_str(), Result
);
452 *ErrorStr
= ec
.message();
453 return Result
.take();
456 /// getStatValue - Get the 'stat' information for the specified path, using the
457 /// cache to accelerate it if possible. This returns true if the path does not
458 /// exist or false if it exists.
460 /// The isForDir member indicates whether this is a directory lookup or not.
461 /// This will return failure if the lookup isn't the expected kind.
462 bool FileManager::getStatValue(const char *Path
, struct stat
&StatBuf
,
463 int *FileDescriptor
) {
464 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
466 if (FileSystemOpts
.WorkingDir
.empty())
467 return FileSystemStatCache::get(Path
, StatBuf
, FileDescriptor
,
470 llvm::sys::Path
FilePath(Path
);
471 FixupRelativePath(FilePath
, FileSystemOpts
);
473 return FileSystemStatCache::get(FilePath
.c_str(), StatBuf
, FileDescriptor
,
479 void FileManager::PrintStats() const {
480 llvm::errs() << "\n*** File Manager Stats:\n";
481 llvm::errs() << UniqueFiles
.size() << " files found, "
482 << UniqueDirs
.size() << " dirs found.\n";
483 llvm::errs() << NumDirLookups
<< " dir lookups, "
484 << NumDirCacheMisses
<< " dir cache misses.\n";
485 llvm::errs() << NumFileLookups
<< " file lookups, "
486 << NumFileCacheMisses
<< " file cache misses.\n";
488 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;