2007-12-05 06:42:20 +08:00
|
|
|
///===--- FileManager.cpp - File System Probing and Caching ----------------===//
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the FileManager interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// TODO: This should index all interesting directories with dirent calls.
|
|
|
|
// getdirentries ?
|
|
|
|
// opendir/readdir_r/closedir ?
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2006-10-30 11:55:17 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2007-12-05 02:21:35 +08:00
|
|
|
#include "llvm/Bitcode/Serialize.h"
|
|
|
|
#include "llvm/Bitcode/Deserialize.h"
|
2008-01-12 04:42:05 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-02-24 11:15:25 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
// FIXME: Enhance libsystem to support inode and other fields.
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2007-09-04 02:37:14 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#define S_ISDIR(s) (_S_IFDIR & s)
|
|
|
|
#endif
|
2006-10-30 11:06:54 +08:00
|
|
|
|
2008-01-12 04:42:05 +08:00
|
|
|
/// NON_EXISTENT_DIR - A special value distinct from null that is used to
|
2006-10-30 11:06:54 +08:00
|
|
|
/// represent a dir name that doesn't exist on the disk.
|
2008-01-12 04:42:05 +08:00
|
|
|
#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
|
2006-10-30 11:06:54 +08:00
|
|
|
|
2009-01-28 08:27:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Windows.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-24 11:15:25 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
|
|
|
|
#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static std::string GetFullPath(const char *relPath)
|
|
|
|
{
|
|
|
|
char *absPathStrPtr = _fullpath(NULL, relPath, 0);
|
|
|
|
assert(absPathStrPtr && "_fullpath() returned NULL!");
|
|
|
|
|
|
|
|
std::string absPath(absPathStrPtr);
|
|
|
|
|
|
|
|
free(absPathStrPtr);
|
|
|
|
return absPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileManager::UniqueDirContainer {
|
|
|
|
/// UniqueDirs - Cache from full path to existing directories/files.
|
|
|
|
///
|
|
|
|
llvm::StringMap<DirectoryEntry> UniqueDirs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
|
|
|
|
std::string FullPath(GetFullPath(Name));
|
|
|
|
return UniqueDirs.GetOrCreateValue(
|
|
|
|
FullPath.c_str(),
|
|
|
|
FullPath.c_str() + FullPath.size()
|
|
|
|
).getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() { return UniqueDirs.size(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileManager::UniqueFileContainer {
|
|
|
|
/// UniqueFiles - Cache from full path to existing directories/files.
|
|
|
|
///
|
|
|
|
llvm::StringMap<FileEntry> UniqueFiles;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileEntry &getFile(const char *Name, struct stat &StatBuf) {
|
|
|
|
std::string FullPath(GetFullPath(Name));
|
|
|
|
return UniqueFiles.GetOrCreateValue(
|
|
|
|
FullPath.c_str(),
|
|
|
|
FullPath.c_str() + FullPath.size()
|
|
|
|
).getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() { return UniqueFiles.size(); }
|
|
|
|
};
|
|
|
|
|
2009-01-28 08:27:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Unix-like Systems.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-24 11:15:25 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
|
|
|
|
|
|
|
|
class FileManager::UniqueDirContainer {
|
|
|
|
/// UniqueDirs - Cache from ID's to existing directories/files.
|
|
|
|
///
|
|
|
|
std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
|
|
|
|
return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() { return UniqueDirs.size(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileManager::UniqueFileContainer {
|
|
|
|
/// UniqueFiles - Cache from ID's to existing directories/files.
|
|
|
|
///
|
|
|
|
std::set<FileEntry> UniqueFiles;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileEntry &getFile(const char *Name, struct stat &StatBuf) {
|
|
|
|
return
|
|
|
|
const_cast<FileEntry&>(
|
|
|
|
*UniqueFiles.insert(FileEntry(StatBuf.st_dev,
|
|
|
|
StatBuf.st_ino)).first);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() { return UniqueFiles.size(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 08:27:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Common logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-24 11:15:25 +08:00
|
|
|
|
|
|
|
FileManager::FileManager() : UniqueDirs(*new UniqueDirContainer),
|
|
|
|
UniqueFiles(*new UniqueFileContainer),
|
|
|
|
DirEntries(64), FileEntries(64), NextFileUID(0)
|
|
|
|
{
|
|
|
|
NumDirLookups = NumFileLookups = 0;
|
|
|
|
NumDirCacheMisses = NumFileCacheMisses = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileManager::~FileManager() {
|
|
|
|
delete &UniqueDirs;
|
|
|
|
delete &UniqueFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getDirectory - Lookup, cache, and verify the specified directory. This
|
|
|
|
/// returns null if the directory doesn't exist.
|
|
|
|
///
|
2006-10-30 11:55:17 +08:00
|
|
|
const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
|
|
|
|
const char *NameEnd) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumDirLookups;
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
|
2007-02-09 03:08:49 +08:00
|
|
|
DirEntries.GetOrCreateValue(NameStart, NameEnd);
|
2006-10-30 11:06:54 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// See if there is already an entry in the map.
|
2007-02-09 03:08:49 +08:00
|
|
|
if (NamedDirEnt.getValue())
|
2008-01-12 04:42:05 +08:00
|
|
|
return NamedDirEnt.getValue() == NON_EXISTENT_DIR
|
2007-02-09 03:08:49 +08:00
|
|
|
? 0 : NamedDirEnt.getValue();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
++NumDirCacheMisses;
|
|
|
|
|
2006-10-30 11:06:54 +08:00
|
|
|
// By default, initialize it to invalid.
|
2008-01-12 04:42:05 +08:00
|
|
|
NamedDirEnt.setValue(NON_EXISTENT_DIR);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-30 11:40:58 +08:00
|
|
|
// Get the null-terminated directory name as stored as the key of the
|
|
|
|
// DirEntries map.
|
2007-02-09 03:08:49 +08:00
|
|
|
const char *InterndDirName = NamedDirEnt.getKeyData();
|
2006-10-30 11:40:58 +08:00
|
|
|
|
2006-10-30 11:06:54 +08:00
|
|
|
// Check to see if the directory exists.
|
2006-06-18 13:43:12 +08:00
|
|
|
struct stat StatBuf;
|
2006-10-30 11:40:58 +08:00
|
|
|
if (stat(InterndDirName, &StatBuf) || // Error stat'ing.
|
|
|
|
!S_ISDIR(StatBuf.st_mode)) // Not a directory?
|
2006-06-18 13:43:12 +08:00
|
|
|
return 0;
|
2008-02-24 11:15:25 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// It exists. See if we have already opened a directory with the same inode.
|
2007-12-19 04:45:25 +08:00
|
|
|
// This occurs when one dir is symlinked to another, for example.
|
2008-02-24 11:15:25 +08:00
|
|
|
DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-02-09 03:08:49 +08:00
|
|
|
NamedDirEnt.setValue(&UDE);
|
|
|
|
if (UDE.getName()) // Already have an entry with this inode, return it.
|
|
|
|
return &UDE;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-30 11:11:40 +08:00
|
|
|
// Otherwise, we don't have this directory yet, add it. We use the string
|
|
|
|
// key from the DirEntries map as the string.
|
2006-10-30 11:40:58 +08:00
|
|
|
UDE.Name = InterndDirName;
|
2007-02-09 03:08:49 +08:00
|
|
|
return &UDE;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2008-01-12 04:42:05 +08:00
|
|
|
/// NON_EXISTENT_FILE - A special value distinct from null that is used to
|
2006-10-30 11:55:17 +08:00
|
|
|
/// represent a filename that doesn't exist on the disk.
|
2008-01-12 04:42:05 +08:00
|
|
|
#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
|
2006-10-30 11:55:17 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getFile - Lookup, cache, and verify the specified file. This returns null
|
|
|
|
/// if the file doesn't exist.
|
|
|
|
///
|
2006-10-30 11:55:17 +08:00
|
|
|
const FileEntry *FileManager::getFile(const char *NameStart,
|
|
|
|
const char *NameEnd) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumFileLookups;
|
|
|
|
|
|
|
|
// See if there is already an entry in the map.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
|
2007-02-09 03:08:49 +08:00
|
|
|
FileEntries.GetOrCreateValue(NameStart, NameEnd);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-30 11:55:17 +08:00
|
|
|
// See if there is already an entry in the map.
|
2007-02-09 03:08:49 +08:00
|
|
|
if (NamedFileEnt.getValue())
|
2008-01-12 04:42:05 +08:00
|
|
|
return NamedFileEnt.getValue() == NON_EXISTENT_FILE
|
2007-02-09 03:08:49 +08:00
|
|
|
? 0 : NamedFileEnt.getValue();
|
2006-10-30 11:55:17 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumFileCacheMisses;
|
|
|
|
|
2006-10-30 11:55:17 +08:00
|
|
|
// By default, initialize it to invalid.
|
2008-01-12 04:42:05 +08:00
|
|
|
NamedFileEnt.setValue(NON_EXISTENT_FILE);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-30 12:34:28 +08:00
|
|
|
// Figure out what directory it is in. If the string contains a / in it,
|
|
|
|
// strip off everything after it.
|
2006-06-18 13:43:12 +08:00
|
|
|
// FIXME: this logic should be in sys::Path.
|
2006-10-30 11:55:17 +08:00
|
|
|
const char *SlashPos = NameEnd-1;
|
2008-02-24 11:15:25 +08:00
|
|
|
while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
|
2006-10-30 11:55:17 +08:00
|
|
|
--SlashPos;
|
|
|
|
|
2006-10-30 12:34:28 +08:00
|
|
|
const DirectoryEntry *DirInfo;
|
2006-10-30 11:55:17 +08:00
|
|
|
if (SlashPos < NameStart) {
|
|
|
|
// Use the current directory if file has no path component.
|
2006-10-30 12:34:28 +08:00
|
|
|
const char *Name = ".";
|
|
|
|
DirInfo = getDirectory(Name, Name+1);
|
2006-10-30 11:55:17 +08:00
|
|
|
} else if (SlashPos == NameEnd-1)
|
2006-06-18 13:43:12 +08:00
|
|
|
return 0; // If filename ends with a /, it's a directory.
|
|
|
|
else
|
2006-10-30 12:34:28 +08:00
|
|
|
DirInfo = getDirectory(NameStart, SlashPos);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (DirInfo == 0) // Directory doesn't exist, file can't exist.
|
|
|
|
return 0;
|
|
|
|
|
2006-10-30 11:55:17 +08:00
|
|
|
// Get the null-terminated file name as stored as the key of the
|
|
|
|
// FileEntries map.
|
2007-02-09 03:08:49 +08:00
|
|
|
const char *InterndFileName = NamedFileEnt.getKeyData();
|
2006-10-30 11:55:17 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// FIXME: Use the directory info to prune this, before doing the stat syscall.
|
|
|
|
// FIXME: This will reduce the # syscalls.
|
|
|
|
|
|
|
|
// Nope, there isn't. Check to see if the file exists.
|
|
|
|
struct stat StatBuf;
|
2008-01-12 04:42:05 +08:00
|
|
|
//llvm::cerr << "STATING: " << Filename;
|
2006-10-30 11:55:17 +08:00
|
|
|
if (stat(InterndFileName, &StatBuf) || // Error stat'ing.
|
2006-07-19 11:40:07 +08:00
|
|
|
S_ISDIR(StatBuf.st_mode)) { // A directory?
|
|
|
|
// If this file doesn't exist, we leave a null in FileEntries for this path.
|
2008-01-12 04:42:05 +08:00
|
|
|
//llvm::cerr << ": Not existing\n";
|
2006-06-18 13:43:12 +08:00
|
|
|
return 0;
|
2006-07-19 11:40:07 +08:00
|
|
|
}
|
2008-01-12 04:42:05 +08:00
|
|
|
//llvm::cerr << ": exists\n";
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-12-19 06:29:39 +08:00
|
|
|
// It exists. See if we have already opened a file with the same inode.
|
2006-06-18 13:43:12 +08:00
|
|
|
// This occurs when one dir is symlinked to another, for example.
|
2008-02-24 11:15:25 +08:00
|
|
|
FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-02-09 03:08:49 +08:00
|
|
|
NamedFileEnt.setValue(&UFE);
|
2006-10-30 11:55:17 +08:00
|
|
|
if (UFE.getName()) // Already have an entry with this inode, return it.
|
2007-02-09 03:08:49 +08:00
|
|
|
return &UFE;
|
2006-06-25 14:23:00 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Otherwise, we don't have this directory yet, add it.
|
2006-10-30 10:45:16 +08:00
|
|
|
// FIXME: Change the name to be a char* that points back to the 'FileEntries'
|
|
|
|
// key.
|
2006-10-30 11:55:17 +08:00
|
|
|
UFE.Name = InterndFileName;
|
|
|
|
UFE.Size = StatBuf.st_size;
|
|
|
|
UFE.ModTime = StatBuf.st_mtime;
|
|
|
|
UFE.Dir = DirInfo;
|
|
|
|
UFE.UID = NextFileUID++;
|
2007-02-09 03:08:49 +08:00
|
|
|
return &UFE;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileManager::PrintStats() const {
|
2008-01-12 04:42:05 +08:00
|
|
|
llvm::cerr << "\n*** File Manager Stats:\n";
|
|
|
|
llvm::cerr << UniqueFiles.size() << " files found, "
|
|
|
|
<< UniqueDirs.size() << " dirs found.\n";
|
|
|
|
llvm::cerr << NumDirLookups << " dir lookups, "
|
|
|
|
<< NumDirCacheMisses << " dir cache misses.\n";
|
|
|
|
llvm::cerr << NumFileLookups << " file lookups, "
|
|
|
|
<< NumFileCacheMisses << " file cache misses.\n";
|
|
|
|
|
|
|
|
//llvm::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|