13#include "llvm/ADT/ScopeExit.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/IOSandbox.h"
17#include "llvm/Support/LockFileManager.h"
18#include "llvm/Support/Path.h"
23 auto [ByNameIt, ByNameInserted] = ByPath.insert({Path,
nullptr});
24 if (!ByNameIt->second) {
26 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
30 llvm::sys::fs::file_status Status;
31 if (std::error_code EC = llvm::sys::fs::status(Path, Status)) {
33 if (EC != std::errc::no_such_file_or_directory)
36 if (llvm::sys::fs::create_directories(Path))
39 if (llvm::sys::fs::status(Path, Status))
43 llvm::sys::fs::UniqueID UID = Status.getUniqueID();
44 auto [ByUIDIt, ByUIDInserted] = ByUID.insert({UID,
nullptr});
46 ByUIDIt->second = std::make_unique<ModuleCacheDirectory>();
47 ByNameIt->second = ByUIDIt->second.get();
49 return ByNameIt->second;
55 llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::OF_None);
59 time_t PruneAfter,
bool PruneTopLevel,
60 llvm::function_ref<
void(StringRef)> OnPrune) {
61 if (PruneInterval <= 0 || PruneAfter <= 0)
65 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
68 (void)llvm::sys::fs::make_absolute(RootPath);
71 llvm::sys::path::append(TimestampFile,
"modules.timestamp");
74 llvm::sys::fs::file_status StatBuf;
75 if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) {
77 if (EC == std::errc::no_such_file_or_directory)
84 time_t TimestampModTime =
85 llvm::sys::toTimeT(StatBuf.getLastModificationTime());
86 time_t CurrentTime = time(
nullptr);
87 if (CurrentTime - TimestampModTime <= PruneInterval)
95 auto NotifyPruned = [&](StringRef RemovedPath) {
103 auto TryPruneFile = [&](StringRef FilePath) {
105 StringRef Filename = llvm::sys::path::filename(FilePath);
106 StringRef Extension = llvm::sys::path::extension(FilePath);
107 if (Extension !=
".pcm" && Extension !=
".timestamp" &&
108 Filename !=
"modules.idx")
112 if (Filename ==
"modules.timestamp")
117 if (llvm::sys::fs::status(FilePath, StatBuf))
121 time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime());
122 if (CurrentTime - FileAccessTime <= PruneAfter)
126 if (!llvm::sys::fs::remove(FilePath))
127 NotifyPruned(FilePath);
130 std::string TimestampFilename = FilePath.str() +
".timestamp";
131 if (!llvm::sys::fs::remove(TimestampFilename))
132 NotifyPruned(TimestampFilename);
135 for (llvm::sys::fs::directory_iterator Dir(RootPath, EC), DirEnd;
136 Dir != DirEnd && !EC; Dir.increment(EC)) {
138 if (!llvm::sys::fs::is_directory(Dir->path())) {
140 TryPruneFile(Dir->path());
145 for (llvm::sys::fs::directory_iterator
File(Dir->path(), EC), FileEnd;
146 File != FileEnd && !EC;
File.increment(EC))
147 TryPruneFile(
File->path());
151 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
152 llvm::sys::fs::directory_iterator() &&
154 if (!llvm::sys::fs::remove(Dir->path()))
155 NotifyPruned(Dir->path());
161 off_t &Size, time_t &ModTime) {
162 StringRef Extension = llvm::sys::path::extension(Path);
164 ModelPath +=
"-%%%%%%%%";
165 ModelPath += Extension;
171 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath))) {
172 if (EC != std::errc::no_such_file_or_directory)
175 StringRef Dir = llvm::sys::path::parent_path(Path);
176 if (std::error_code InnerEC = llvm::sys::fs::create_directories(Dir))
179 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath)))
183 llvm::sys::fs::file_status Status;
185 llvm::raw_fd_ostream OS(FD,
true);
186 OS << Buffer.getBuffer();
188 if ((EC = llvm::sys::fs::status(FD, Status)))
192 Size = Status.getSize();
193 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
196 if ((EC = llvm::sys::fs::rename(TmpPath, Path)))
205 llvm::sys::fs::openNativeFileForRead(
FileName);
207 return FD.takeError();
208 llvm::scope_exit CloseFD([&FD]() { llvm::sys::fs::closeFile(*FD); });
209 llvm::sys::fs::file_status Status;
210 if (std::error_code EC = llvm::sys::fs::status(*FD, Status))
211 return llvm::errorCodeToError(EC);
212 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
213 llvm::MemoryBuffer::getOpenFile(*FD,
FileName, Status.getSize(),
216 return llvm::errorCodeToError(Buf.getError());
217 Size = Status.getSize();
218 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
219 return std::move(*Buf);
223class CrossProcessModuleCache :
public ModuleCache {
227 std::unique_ptr<llvm::AdvisoryLock>
228 getLock(StringRef ModuleFilename)
override {
229 return std::make_unique<llvm::LockFileManager>(ModuleFilename);
232 std::time_t getModuleTimestamp(StringRef ModuleFilename)
override {
234 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
236 std::string TimestampFilename =
238 llvm::sys::fs::file_status Status;
239 if (llvm::sys::fs::status(TimestampFilename, Status) != std::error_code{})
241 return llvm::sys::toTimeT(Status.getLastModificationTime());
244 void updateModuleTimestamp(StringRef ModuleFilename)
override {
246 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
250 llvm::raw_fd_ostream
OS(
252 llvm::sys::fs::OF_TextWithCRLF);
255 OS <<
"Timestamp file\n";
260 void maybePrune(StringRef Path, time_t PruneInterval,
261 time_t PruneAfter)
override {
263 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
268 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
269 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
273 std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
274 off_t &Size, time_t &ModTime)
override {
276 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
278 return writeImpl(Path, Buffer, Size, ModTime);
281 Expected<std::unique_ptr<llvm::MemoryBuffer>>
282 read(StringRef
FileName, off_t &Size, time_t &ModTime)
override {
284 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
292 return std::make_shared<CrossProcessModuleCache>();
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().