blob: 5ee032ef0332df661832c2258fe3e9571beeee57 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +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 <iostream>
22using namespace llvm;
23using namespace clang;
24
25// FIXME: Enhance libsystem to support inode and other fields.
26#include <sys/stat.h>
27
Chris Lattneraf653752006-10-30 03:06:54 +000028
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
Chris Lattner22eb9722006-06-18 05:43:12 +000033/// getDirectory - Lookup, cache, and verify the specified directory. This
34/// returns null if the directory doesn't exist.
35///
36const DirectoryEntry *FileManager::getDirectory(const std::string &Filename) {
37 ++NumDirLookups;
Chris Lattneraf653752006-10-30 03:06:54 +000038
39 DirectoryEntry *&NamedDirEnt =
40 DirEntries.GetOrCreateValue(&Filename[0], &Filename[0] + Filename.size());
41
Chris Lattner22eb9722006-06-18 05:43:12 +000042 // See if there is already an entry in the map.
Chris Lattneraf653752006-10-30 03:06:54 +000043 if (NamedDirEnt)
44 return NamedDirEnt == NON_EXISTANT_DIR ? 0 : NamedDirEnt;
Chris Lattner22eb9722006-06-18 05:43:12 +000045
46 ++NumDirCacheMisses;
47
Chris Lattneraf653752006-10-30 03:06:54 +000048 // By default, initialize it to invalid.
49 NamedDirEnt = NON_EXISTANT_DIR;
Chris Lattner22eb9722006-06-18 05:43:12 +000050
Chris Lattneraf653752006-10-30 03:06:54 +000051 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +000052 struct stat StatBuf;
53 if (stat(Filename.c_str(), &StatBuf) || // Error stat'ing.
54 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
55 return 0;
56
57 // It exists. See if we have already opened a directory with the same inode.
58 // This occurs when one dir is symlinked to another, for example.
Chris Lattner8b1e8482006-10-30 02:45:16 +000059 DirectoryEntry &UDE =
Chris Lattner22eb9722006-06-18 05:43:12 +000060 UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
61
Chris Lattnera85cbe22006-10-30 03:11:40 +000062 if (UDE.getName()) // Already have an entry with this inode, return it.
Chris Lattneraf653752006-10-30 03:06:54 +000063 return NamedDirEnt = &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +000064
Chris Lattnera85cbe22006-10-30 03:11:40 +000065 // Otherwise, we don't have this directory yet, add it. We use the string
66 // key from the DirEntries map as the string.
67 UDE.Name = DirEntries.GetKeyForValueInMap(NamedDirEnt);
Chris Lattneraf653752006-10-30 03:06:54 +000068 return NamedDirEnt = &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +000069}
70
71/// getFile - Lookup, cache, and verify the specified file. This returns null
72/// if the file doesn't exist.
73///
74const FileEntry *FileManager::getFile(const std::string &Filename) {
75 ++NumFileLookups;
76
77 // See if there is already an entry in the map.
78 std::map<std::string, FileEntry*>::iterator I =
79 FileEntries.lower_bound(Filename);
80 if (I != FileEntries.end() && I->first == Filename)
81 return I->second;
82
83 ++NumFileCacheMisses;
84
85 // By default, zero initialize it.
86 FileEntry *&Ent =
87 FileEntries.insert(I, std::make_pair(Filename, (FileEntry*)0))->second;
88
89 // Figure out what directory it is in.
90 std::string DirName;
91
92 // If the string contains a / in it, strip off everything after it.
93 // FIXME: this logic should be in sys::Path.
94 std::string::size_type SlashPos = Filename.find_last_of('/');
95 if (SlashPos == std::string::npos)
96 DirName = "."; // Use the current directory if file has no path component.
97 else if (SlashPos == Filename.size()-1)
98 return 0; // If filename ends with a /, it's a directory.
99 else
100 DirName = std::string(Filename.begin(), Filename.begin()+SlashPos);
101
102 const DirectoryEntry *DirInfo = getDirectory(DirName);
103 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
104 return 0;
105
106 // FIXME: Use the directory info to prune this, before doing the stat syscall.
107 // FIXME: This will reduce the # syscalls.
108
109 // Nope, there isn't. Check to see if the file exists.
110 struct stat StatBuf;
Chris Lattner81500bc2006-07-19 03:40:07 +0000111 //std::cerr << "STATING: " << Filename;
Chris Lattner22eb9722006-06-18 05:43:12 +0000112 if (stat(Filename.c_str(), &StatBuf) || // Error stat'ing.
Chris Lattner81500bc2006-07-19 03:40:07 +0000113 S_ISDIR(StatBuf.st_mode)) { // A directory?
114 // If this file doesn't exist, we leave a null in FileEntries for this path.
115 //std::cerr << ": Not existing\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000116 return 0;
Chris Lattner81500bc2006-07-19 03:40:07 +0000117 }
118 //std::cerr << ": exists\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000119
120 // It exists. See if we have already opened a directory with the same inode.
121 // This occurs when one dir is symlinked to another, for example.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000122 FileEntry &UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
Chris Lattner22eb9722006-06-18 05:43:12 +0000123
Chris Lattner8b1e8482006-10-30 02:45:16 +0000124 if (UFE.getUID() != ~0U) // Already have an entry with this inode, return it.
125 return Ent = &UFE;
Chris Lattner269c2322006-06-25 06:23:00 +0000126
Chris Lattner22eb9722006-06-18 05:43:12 +0000127 // Otherwise, we don't have this directory yet, add it.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000128 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
129 // key.
130 UFE.Name = Filename;
131 UFE.Size = StatBuf.st_size;
132 UFE.ModTime = StatBuf.st_mtime;
133 UFE.Dir = DirInfo;
134 UFE.UID = NextFileUID++;
135 return Ent = &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000136}
137
138void FileManager::PrintStats() const {
139 std::cerr << "\n*** File Manager Stats:\n";
140 std::cerr << UniqueFiles.size() << " files found, "
141 << UniqueDirs.size() << " dirs found.\n";
142 std::cerr << NumDirLookups << " dir lookups, "
143 << NumDirCacheMisses << " dir cache misses.\n";
144 std::cerr << NumFileLookups << " file lookups, "
145 << NumFileCacheMisses << " file cache misses.\n";
146
147 //std::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
148}