1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
//===--- FileManager.cpp - File System Probing and Caching ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// 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"
#include <iostream>
using namespace llvm;
using namespace clang;
// FIXME: Enhance libsystem to support inode and other fields.
#include <sys/stat.h>
/// getDirectory - Lookup, cache, and verify the specified directory. This
/// returns null if the directory doesn't exist.
///
const DirectoryEntry *FileManager::getDirectory(const std::string &Filename) {
++NumDirLookups;
// See if there is already an entry in the map.
std::map<std::string, DirectoryEntry*>::iterator I =
DirEntries.lower_bound(Filename);
if (I != DirEntries.end() && I->first == Filename)
return I->second;
++NumDirCacheMisses;
// By default, zero initialize it.
DirectoryEntry *&Ent =
DirEntries.insert(I, std::make_pair(Filename, (DirectoryEntry*)0))->second;
// Nope, there isn't. Check to see if the directory exists.
struct stat StatBuf;
if (stat(Filename.c_str(), &StatBuf) || // Error stat'ing.
!S_ISDIR(StatBuf.st_mode)) // Not a directory?
return 0;
// It exists. See if we have already opened a directory with the same inode.
// This occurs when one dir is symlinked to another, for example.
DirectoryEntry *&UDE =
UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
if (UDE) // Already have an entry with this inode, return it.
return Ent = UDE;
// Otherwise, we don't have this directory yet, add it.
DirectoryEntry *DE = new DirectoryEntry();
DE->Name = Filename;
return Ent = UDE = DE;
}
/// getFile - Lookup, cache, and verify the specified file. This returns null
/// if the file doesn't exist.
///
const FileEntry *FileManager::getFile(const std::string &Filename) {
++NumFileLookups;
// See if there is already an entry in the map.
std::map<std::string, FileEntry*>::iterator I =
FileEntries.lower_bound(Filename);
if (I != FileEntries.end() && I->first == Filename)
return I->second;
++NumFileCacheMisses;
// By default, zero initialize it.
FileEntry *&Ent =
FileEntries.insert(I, std::make_pair(Filename, (FileEntry*)0))->second;
// Figure out what directory it is in.
std::string DirName;
// If the string contains a / in it, strip off everything after it.
// FIXME: this logic should be in sys::Path.
std::string::size_type SlashPos = Filename.find_last_of('/');
if (SlashPos == std::string::npos)
DirName = "."; // Use the current directory if file has no path component.
else if (SlashPos == Filename.size()-1)
return 0; // If filename ends with a /, it's a directory.
else
DirName = std::string(Filename.begin(), Filename.begin()+SlashPos);
const DirectoryEntry *DirInfo = getDirectory(DirName);
if (DirInfo == 0) // Directory doesn't exist, file can't exist.
return 0;
// 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;
//std::cerr << "STATING: " << Filename;
if (stat(Filename.c_str(), &StatBuf) || // Error stat'ing.
S_ISDIR(StatBuf.st_mode)) { // A directory?
// If this file doesn't exist, we leave a null in FileEntries for this path.
//std::cerr << ": Not existing\n";
return 0;
}
//std::cerr << ": exists\n";
// It exists. See if we have already opened a directory with the same inode.
// This occurs when one dir is symlinked to another, for example.
FileEntry *&UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
if (UFE) // Already have an entry with this inode, return it.
return Ent = UFE;
// Otherwise, we don't have this directory yet, add it.
FileEntry *FE = new FileEntry();
FE->Name = Filename;
FE->Size = StatBuf.st_size;
FE->ModTime = StatBuf.st_mtime;
FE->Dir = DirInfo;
FE->UID = NextFileUID++;
return Ent = UFE = FE;
}
void FileManager::PrintStats() const {
std::cerr << "\n*** File Manager Stats:\n";
std::cerr << UniqueFiles.size() << " files found, "
<< UniqueDirs.size() << " dirs found.\n";
std::cerr << NumDirLookups << " dir lookups, "
<< NumDirCacheMisses << " dir cache misses.\n";
std::cerr << NumFileLookups << " file lookups, "
<< NumFileCacheMisses << " file cache misses.\n";
//std::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
}
|