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 if (PruneInterval <= 0 || PruneAfter <= 0)
64 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
67 llvm::sys::path::append(TimestampFile,
"modules.timestamp");
70 llvm::sys::fs::file_status StatBuf;
71 if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) {
73 if (EC == std::errc::no_such_file_or_directory)
80 time_t TimestampModTime =
81 llvm::sys::toTimeT(StatBuf.getLastModificationTime());
82 time_t CurrentTime = time(
nullptr);
83 if (CurrentTime - TimestampModTime <= PruneInterval)
94 auto TryPruneFile = [&](StringRef FilePath) {
96 StringRef Filename = llvm::sys::path::filename(FilePath);
97 StringRef Extension = llvm::sys::path::extension(FilePath);
98 if (Extension !=
".pcm" && Extension !=
".timestamp" &&
99 Filename !=
"modules.idx")
103 if (Filename ==
"modules.timestamp")
108 if (llvm::sys::fs::status(FilePath, StatBuf))
112 time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime());
113 if (CurrentTime - FileAccessTime <= PruneAfter)
117 llvm::sys::fs::remove(FilePath);
120 std::string TimestampFilename = FilePath.str() +
".timestamp";
121 llvm::sys::fs::remove(TimestampFilename);
124 for (llvm::sys::fs::directory_iterator Dir(Path, EC), DirEnd;
125 Dir != DirEnd && !EC; Dir.increment(EC)) {
127 if (!llvm::sys::fs::is_directory(Dir->path())) {
129 TryPruneFile(Dir->path());
134 for (llvm::sys::fs::directory_iterator
File(Dir->path(), EC), FileEnd;
135 File != FileEnd && !EC;
File.increment(EC))
136 TryPruneFile(
File->path());
140 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
141 llvm::sys::fs::directory_iterator() &&
143 llvm::sys::fs::remove(Dir->path());
148 off_t &Size, time_t &ModTime) {
149 StringRef Extension = llvm::sys::path::extension(Path);
151 ModelPath +=
"-%%%%%%%%";
152 ModelPath += Extension;
158 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath))) {
159 if (EC != std::errc::no_such_file_or_directory)
162 StringRef Dir = llvm::sys::path::parent_path(Path);
163 if (std::error_code InnerEC = llvm::sys::fs::create_directories(Dir))
166 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath)))
170 llvm::sys::fs::file_status Status;
172 llvm::raw_fd_ostream OS(FD,
true);
173 OS << Buffer.getBuffer();
175 if ((EC = llvm::sys::fs::status(FD, Status)))
179 Size = Status.getSize();
180 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
183 if ((EC = llvm::sys::fs::rename(TmpPath, Path)))
192 llvm::sys::fs::openNativeFileForRead(
FileName);
194 return FD.takeError();
195 llvm::scope_exit CloseFD([&FD]() { llvm::sys::fs::closeFile(*FD); });
196 llvm::sys::fs::file_status Status;
197 if (std::error_code EC = llvm::sys::fs::status(*FD, Status))
198 return llvm::errorCodeToError(EC);
199 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
200 llvm::MemoryBuffer::getOpenFile(*FD,
FileName, Status.getSize(),
203 return llvm::errorCodeToError(Buf.getError());
204 Size = Status.getSize();
205 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
206 return std::move(*Buf);
210class CrossProcessModuleCache :
public ModuleCache {
214 std::unique_ptr<llvm::AdvisoryLock>
215 getLock(StringRef ModuleFilename)
override {
216 return std::make_unique<llvm::LockFileManager>(ModuleFilename);
219 std::time_t getModuleTimestamp(StringRef ModuleFilename)
override {
221 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
223 std::string TimestampFilename =
225 llvm::sys::fs::file_status Status;
226 if (llvm::sys::fs::status(TimestampFilename, Status) != std::error_code{})
228 return llvm::sys::toTimeT(Status.getLastModificationTime());
231 void updateModuleTimestamp(StringRef ModuleFilename)
override {
233 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
237 llvm::raw_fd_ostream
OS(
239 llvm::sys::fs::OF_TextWithCRLF);
242 OS <<
"Timestamp file\n";
247 void maybePrune(StringRef Path, time_t PruneInterval,
248 time_t PruneAfter)
override {
250 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
255 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
256 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
260 std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
261 off_t &Size, time_t &ModTime)
override {
263 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
265 return writeImpl(Path, Buffer, Size, ModTime);
268 Expected<std::unique_ptr<llvm::MemoryBuffer>>
269 read(StringRef
FileName, off_t &Size, time_t &ModTime)
override {
271 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
279 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().
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter, bool PruneTopLevel=false)
Shared implementation of ModuleCache::maybePrune().
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().