13#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/IOSandbox.h"
15#include "llvm/Support/LockFileManager.h"
16#include "llvm/Support/Path.h"
23 llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::OF_None);
28 if (PruneInterval <= 0 || PruneAfter <= 0)
32 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
35 llvm::sys::path::append(TimestampFile,
"modules.timestamp");
38 llvm::sys::fs::file_status StatBuf;
39 if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) {
41 if (EC == std::errc::no_such_file_or_directory)
48 time_t TimestampModTime =
49 llvm::sys::toTimeT(StatBuf.getLastModificationTime());
50 time_t CurrentTime = time(
nullptr);
51 if (CurrentTime - TimestampModTime <= PruneInterval)
62 for (llvm::sys::fs::directory_iterator Dir(Path, EC), DirEnd;
63 Dir != DirEnd && !EC; Dir.increment(EC)) {
65 if (!llvm::sys::fs::is_directory(Dir->path()))
69 for (llvm::sys::fs::directory_iterator
File(Dir->path(), EC), FileEnd;
70 File != FileEnd && !EC;
File.increment(EC)) {
72 StringRef Extension = llvm::sys::path::extension(
File->path());
73 if (Extension !=
".pcm" && Extension !=
".timestamp" &&
74 llvm::sys::path::filename(
File->path()) !=
"modules.idx")
79 if (llvm::sys::fs::status(
File->path(), StatBuf))
83 time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime());
84 if (CurrentTime - FileAccessTime <= PruneAfter)
88 llvm::sys::fs::remove(
File->path());
91 std::string TimpestampFilename =
File->path() +
".timestamp";
92 llvm::sys::fs::remove(TimpestampFilename);
97 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
98 llvm::sys::fs::directory_iterator() &&
100 llvm::sys::fs::remove(Dir->path());
105class CrossProcessModuleCache :
public ModuleCache {
109 void prepareForGetLock(StringRef ModuleFilename)
override {
111 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
115 StringRef Dir = llvm::sys::path::parent_path(ModuleFilename);
116 llvm::sys::fs::create_directories(Dir);
119 std::unique_ptr<llvm::AdvisoryLock>
120 getLock(StringRef ModuleFilename)
override {
121 return std::make_unique<llvm::LockFileManager>(ModuleFilename);
124 std::time_t getModuleTimestamp(StringRef ModuleFilename)
override {
126 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
128 std::string TimestampFilename =
130 llvm::sys::fs::file_status Status;
131 if (llvm::sys::fs::status(TimestampFilename, Status) != std::error_code{})
133 return llvm::sys::toTimeT(Status.getLastModificationTime());
136 void updateModuleTimestamp(StringRef ModuleFilename)
override {
138 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
142 llvm::raw_fd_ostream
OS(
144 llvm::sys::fs::OF_TextWithCRLF);
147 OS <<
"Timestamp file\n";
152 void maybePrune(StringRef Path, time_t PruneInterval,
153 time_t PruneAfter)
override {
155 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
160 InMemoryModuleCache &getInMemoryModuleCache()
override {
return InMemory; }
161 const InMemoryModuleCache &getInMemoryModuleCache()
const override {
168 return llvm::makeIntrusiveRefCnt<CrossProcessModuleCache>();
static void writeTimestampFile(StringRef TimestampFile)
Write a new timestamp file with the given path.
In-memory cache for modules.
The module cache used for compiling modules implicitly.
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.
IntrusiveRefCntPtr< ModuleCache > createCrossProcessModuleCache()
Creates new ModuleCache backed by a file system directory that may be operated on by multiple process...
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter)
Shared implementation of ModuleCache::maybePrune().