blob: 9886e032b436c46c1486b40f4ff46fa1c040f748 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- FileManager.cpp - File System Probing and Caching ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the FileManager interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// TODO: This should index all interesting directories with dirent calls.
15// getdirentries ?
16// opendir/readdir_r/closedir ?
17//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Basic/FileManager.h"
21#include "llvm/ADT/SmallString.h"
22#include <iostream>
23using namespace clang;
24
25// FIXME: Enhance libsystem to support inode and other fields.
26#include <sys/stat.h>
27
28
29/// NON_EXISTANT_DIR - A special value distinct from null that is used to
30/// represent a dir name that doesn't exist on the disk.
31#define NON_EXISTANT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
32
33/// getDirectory - Lookup, cache, and verify the specified directory. This
34/// returns null if the directory doesn't exist.
35///
36const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
37 const char *NameEnd) {
38 ++NumDirLookups;
39 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
40 DirEntries.GetOrCreateValue(NameStart, NameEnd);
41
42 // See if there is already an entry in the map.
43 if (NamedDirEnt.getValue())
44 return NamedDirEnt.getValue() == NON_EXISTANT_DIR
45 ? 0 : NamedDirEnt.getValue();
46
47 ++NumDirCacheMisses;
48
49 // By default, initialize it to invalid.
50 NamedDirEnt.setValue(NON_EXISTANT_DIR);
51
52 // Get the null-terminated directory name as stored as the key of the
53 // DirEntries map.
54 const char *InterndDirName = NamedDirEnt.getKeyData();
55
56 // Check to see if the directory exists.
57 struct stat StatBuf;
58 if (stat(InterndDirName, &StatBuf) || // Error stat'ing.
59 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
60 return 0;
61
62 // It exists. See if we have already opened a directory with the same inode.
63 // This occurs when one dir is symlinked to another, for example.
64 DirectoryEntry &UDE =
65 UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
66
67 NamedDirEnt.setValue(&UDE);
68 if (UDE.getName()) // Already have an entry with this inode, return it.
69 return &UDE;
70
71 // Otherwise, we don't have this directory yet, add it. We use the string
72 // key from the DirEntries map as the string.
73 UDE.Name = InterndDirName;
74 return &UDE;
75}
76
77/// NON_EXISTANT_FILE - A special value distinct from null that is used to
78/// represent a filename that doesn't exist on the disk.
79#define NON_EXISTANT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
80
81/// getFile - Lookup, cache, and verify the specified file. This returns null
82/// if the file doesn't exist.
83///
84const FileEntry *FileManager::getFile(const char *NameStart,
85 const char *NameEnd) {
86 ++NumFileLookups;
87
88 // See if there is already an entry in the map.
89 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
90 FileEntries.GetOrCreateValue(NameStart, NameEnd);
91
92 // See if there is already an entry in the map.
93 if (NamedFileEnt.getValue())
94 return NamedFileEnt.getValue() == NON_EXISTANT_FILE
95 ? 0 : NamedFileEnt.getValue();
96
97 ++NumFileCacheMisses;
98
99 // By default, initialize it to invalid.
100 NamedFileEnt.setValue(NON_EXISTANT_FILE);
101
102 // Figure out what directory it is in. If the string contains a / in it,
103 // strip off everything after it.
104 // FIXME: this logic should be in sys::Path.
105 const char *SlashPos = NameEnd-1;
106 while (SlashPos >= NameStart && SlashPos[0] != '/')
107 --SlashPos;
108
109 const DirectoryEntry *DirInfo;
110 if (SlashPos < NameStart) {
111 // Use the current directory if file has no path component.
112 const char *Name = ".";
113 DirInfo = getDirectory(Name, Name+1);
114 } else if (SlashPos == NameEnd-1)
115 return 0; // If filename ends with a /, it's a directory.
116 else
117 DirInfo = getDirectory(NameStart, SlashPos);
118
119 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
120 return 0;
121
122 // Get the null-terminated file name as stored as the key of the
123 // FileEntries map.
124 const char *InterndFileName = NamedFileEnt.getKeyData();
125
126 // FIXME: Use the directory info to prune this, before doing the stat syscall.
127 // FIXME: This will reduce the # syscalls.
128
129 // Nope, there isn't. Check to see if the file exists.
130 struct stat StatBuf;
131 //std::cerr << "STATING: " << Filename;
132 if (stat(InterndFileName, &StatBuf) || // Error stat'ing.
133 S_ISDIR(StatBuf.st_mode)) { // A directory?
134 // If this file doesn't exist, we leave a null in FileEntries for this path.
135 //std::cerr << ": Not existing\n";
136 return 0;
137 }
138 //std::cerr << ": exists\n";
139
140 // It exists. See if we have already opened a directory with the same inode.
141 // This occurs when one dir is symlinked to another, for example.
142 FileEntry &UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
143
144 NamedFileEnt.setValue(&UFE);
145 if (UFE.getName()) // Already have an entry with this inode, return it.
146 return &UFE;
147
148 // Otherwise, we don't have this directory yet, add it.
149 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
150 // key.
151 UFE.Name = InterndFileName;
152 UFE.Size = StatBuf.st_size;
153 UFE.ModTime = StatBuf.st_mtime;
154 UFE.Dir = DirInfo;
155 UFE.UID = NextFileUID++;
156 return &UFE;
157}
158
159void FileManager::PrintStats() const {
160 std::cerr << "\n*** File Manager Stats:\n";
161 std::cerr << UniqueFiles.size() << " files found, "
162 << UniqueDirs.size() << " dirs found.\n";
163 std::cerr << NumDirLookups << " dir lookups, "
164 << NumDirCacheMisses << " dir cache misses.\n";
165 std::cerr << NumFileLookups << " file lookups, "
166 << NumFileCacheMisses << " file cache misses.\n";
167
168 //std::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
169}