14#include "llvm/ADT/ScopeExit.h"
15#include "llvm/Support/Error.h"
16#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/IOSandbox.h"
18#include "llvm/Support/LockFileManager.h"
19#include "llvm/Support/Path.h"
24 auto [ByNameIt, ByNameInserted] = ByPath.insert({Path,
nullptr});
25 if (!ByNameIt->second) {
27 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
31 llvm::sys::fs::file_status Status;
32 if (std::error_code EC = llvm::sys::fs::status(Path, Status)) {
34 if (EC != std::errc::no_such_file_or_directory)
37 if (llvm::sys::fs::create_directories(Path))
40 if (llvm::sys::fs::status(Path, Status))
44 llvm::sys::fs::UniqueID UID = Status.getUniqueID();
45 auto [ByUIDIt, ByUIDInserted] = ByUID.insert({UID,
nullptr});
47 ByUIDIt->second = std::make_unique<ModuleCacheDirectory>();
48 ByNameIt->second = ByUIDIt->second.get();
50 return ByNameIt->second;
56 llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::OF_None);
60 time_t PruneAfter,
bool PruneTopLevel,
61 llvm::function_ref<
void(StringRef)> OnPrune) {
62 if (PruneInterval <= 0 || PruneAfter <= 0)
66 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
69 (void)llvm::sys::fs::make_absolute(RootPath);
72 llvm::sys::path::append(TimestampFile,
"modules.timestamp");
75 llvm::sys::fs::file_status StatBuf;
76 if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) {
78 if (EC == std::errc::no_such_file_or_directory)
85 time_t TimestampModTime =
86 llvm::sys::toTimeT(StatBuf.getLastModificationTime());
87 time_t CurrentTime = time(
nullptr);
88 if (CurrentTime - TimestampModTime <= PruneInterval)
96 auto NotifyPruned = [&](StringRef RemovedPath) {
104 auto TryPruneFile = [&](StringRef FilePath) {
106 StringRef Filename = llvm::sys::path::filename(FilePath);
107 StringRef Extension = llvm::sys::path::extension(FilePath);
108 if (Extension !=
".pcm" && Extension !=
".timestamp" &&
109 Filename !=
"modules.idx")
113 if (Filename ==
"modules.timestamp")
118 if (llvm::sys::fs::status(FilePath, StatBuf))
122 time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime());
123 if (CurrentTime - FileAccessTime <= PruneAfter)
127 if (!llvm::sys::fs::remove(FilePath))
128 NotifyPruned(FilePath);
131 std::string TimestampFilename = FilePath.str() +
".timestamp";
132 if (!llvm::sys::fs::remove(TimestampFilename))
133 NotifyPruned(TimestampFilename);
136 for (llvm::sys::fs::directory_iterator Dir(RootPath, EC), DirEnd;
137 Dir != DirEnd && !EC; Dir.increment(EC)) {
139 if (!llvm::sys::fs::is_directory(Dir->path())) {
141 TryPruneFile(Dir->path());
146 for (llvm::sys::fs::directory_iterator
File(Dir->path(), EC), FileEnd;
147 File != FileEnd && !EC;
File.increment(EC))
148 TryPruneFile(
File->path());
152 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
153 llvm::sys::fs::directory_iterator() &&
155 if (!llvm::sys::fs::remove(Dir->path()))
156 NotifyPruned(Dir->path());
162 off_t &Size, time_t &ModTime) {
163 StringRef Extension = llvm::sys::path::extension(Path);
165 ModelPath +=
"-%%%%%%%%";
166 ModelPath += Extension;
172 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath))) {
173 if (EC != std::errc::no_such_file_or_directory)
176 StringRef Dir = llvm::sys::path::parent_path(Path);
177 if (std::error_code InnerEC = llvm::sys::fs::create_directories(Dir))
180 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath)))
184 llvm::sys::fs::file_status Status;
186 llvm::raw_fd_ostream OS(FD,
true);
187 OS << Buffer.getBuffer();
189 if ((EC = llvm::sys::fs::status(FD, Status)))
193 Size = Status.getSize();
194 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
197 if ((EC = llvm::sys::fs::rename(TmpPath, Path)))
206 llvm::sys::fs::openNativeFileForRead(
FileName);
208 return FD.takeError();
209 llvm::scope_exit CloseFD([&FD]() { llvm::sys::fs::closeFile(*FD); });
210 llvm::sys::fs::file_status Status;
211 if (std::error_code EC = llvm::sys::fs::status(*FD, Status))
212 return llvm::errorCodeToError(EC);
213 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
214 llvm::MemoryBuffer::getOpenFile(*FD,
FileName, Status.getSize(),
217 return llvm::errorCodeToError(Buf.getError());
218 Size = Status.getSize();
219 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
220 return std::move(*Buf);
226class CrossProcessModuleCache :
public ModuleCache {
230 explicit CrossProcessModuleCache()
233 std::unique_ptr<llvm::AdvisoryLock>
234 getLock(StringRef ModuleFilename)
override {
235 return std::make_unique<llvm::LockFileManager>(ModuleFilename);
238 std::time_t getModuleTimestamp(StringRef ModuleFilename)
override {
240 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
242 std::string TimestampFilename =
244 llvm::sys::fs::file_status Status;
245 if (llvm::sys::fs::status(TimestampFilename, Status) != std::error_code{})
247 return llvm::sys::toTimeT(Status.getLastModificationTime());
250 void updateModuleTimestamp(StringRef ModuleFilename)
override {
252 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
256 llvm::raw_fd_ostream
OS(
258 llvm::sys::fs::OF_TextWithCRLF);
261 OS <<
"Timestamp file\n";
266 void maybePrune(StringRef Path, time_t PruneInterval,
267 time_t PruneAfter)
override {
269 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
274 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
275 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
279 std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
280 off_t &Size, time_t &ModTime)
override {
282 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
284 return writeImpl(Path, Buffer, Size, ModTime);
287 Expected<std::unique_ptr<llvm::MemoryBuffer>>
288 read(StringRef
FileName, off_t &Size, time_t &ModTime)
override {
290 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
298 return std::make_shared<CrossProcessModuleCache>();
Defines a logger where each line is written atomically to the file.
static void writeTimestampFile(StringRef TimestampFile)
Write a new timestamp file with the given path.
In-memory cache for modules.
The address of an instance of this class represents the identity of a module cache directory.
The module cache used for compiling modules implicitly.
virtual const ModuleCacheDirectory * getDirectoryPtr(StringRef Path)
Returns an opaque pointer representing the module cache directory.
static std::string getTimestampFilename(StringRef FileName)
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
The JSON file list parser is used to communicate input to InstallAPI.
Expected< std::unique_ptr< llvm::MemoryBuffer > > readImpl(StringRef FileName, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::read().
std::shared_ptr< ModuleCache > createCrossProcessModuleCache()
Creates new ModuleCache backed by a file system directory that may be operated on by multiple process...
std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::write().
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter, bool PruneTopLevel=false, llvm::function_ref< void(StringRef)> OnPrune={})
Shared implementation of ModuleCache::maybePrune().