Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion llvm/include/llvm/CAS/ActionCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ using AsyncCASIDValue = AsyncValue<CASID>;
struct AsyncErrorValue {
Error take() { return std::move(Value); }

AsyncErrorValue() : Value(Error::success()) {}
AsyncErrorValue(Error &&E) : Value(std::move(E)) {}

private:
Expand Down
1 change: 0 additions & 1 deletion llvm/include/llvm/CAS/CASID.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class CASID {
template <typename T> struct AsyncValue {
Expected<std::optional<T>> take() { return std::move(Value); }

AsyncValue() : Value(std::nullopt) {}
AsyncValue(Error &&E) : Value(std::move(E)) {}
AsyncValue(T &&V) : Value(std::move(V)) {}
AsyncValue(std::nullopt_t) : Value(std::nullopt) {}
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/Support/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ LLVM_ABI std::error_code copy_file(const Twine &From, int ToFD);
/// platform-specific error_code.
LLVM_ABI std::error_code resize_file(int FD, uint64_t Size);

/// Resize path to size with sparse files explicitly enabled. It uses
/// FSCTL_SET_SPARSE On Windows. This is the same as resize_file on
/// non-Windows
LLVM_ABI std::error_code resize_file_sparse(int FD, uint64_t Size);

/// Resize \p FD to \p Size before mapping \a mapped_file_region::readwrite. On
/// non-Windows, this calls \a resize_file(). On Windows, this is a no-op,
/// since the subsequent mapping (via \c CreateFileMapping) automatically
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/CAS/HierarchicalTreeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ static StringRef canonicalize(SmallVectorImpl<char> &Path,
TreeEntry::EntryKind Kind,
sys::path::Style PathStyle) {
const char PathSeparatorChar = get_separator(PathStyle)[0];
// Make absolute.
if (Path.empty() || !is_absolute(Path, PathStyle))
// Make absolute. Use has_root_directory instead of is_absolute
// because we may not have the drive like C: for abstract tree
// structures on Windows.
if (Path.empty() || !has_root_directory(Path, PathStyle))
Path.insert(Path.begin(), PathSeparatorChar);

// FIXME: consider rejecting ".." instead of removing them.
Expand Down Expand Up @@ -153,7 +155,7 @@ Expected<ObjectProxy> HierarchicalTreeBuilder::create(ObjectStore &CAS) {
Tree *Current = &Root;
StringRef Path = Entry.getPath();
{
assert(is_absolute(Path, PathStyle) && "Expected absolute paths");
assert(has_root_directory(Path, PathStyle) && "Expected absolute paths");
StringRef root_path = sys::path::root_path(Path, PathStyle);
assert(!root_path.empty() && "Expected canonical POSIX absolute paths");
Path.consume_front(root_path);
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/CAS/MappedFileRegionBumpPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ Expected<MappedFileRegionBumpPtr> MappedFileRegionBumpPtr::create(
// We are initializing the file; it may be empty, or may have been shrunk
// during a previous close.
// FIXME: Detect a case where someone opened it with a smaller capacity.
// FIXME: On Windows we should use FSCTL_SET_SPARSE and FSCTL_SET_ZERO_DATA
// to make this a sparse region, if supported.
assert(InitLock.Locked == FileLockRAII::Exclusive);
if (std::error_code EC = sys::fs::resize_file(FD, Capacity))
if (std::error_code EC = sys::fs::resize_file_sparse(FD, Capacity))
return createFileError(Result.Path, EC);

if (Result.Logger)
Expand Down Expand Up @@ -230,7 +228,8 @@ void MappedFileRegionBumpPtr::destroyImpl() {
assert(Size < Capacity);
// sync to file system to make sure all contents are up-to-date.
(void)Region.sync();
(void)sys::fs::resize_file(*FD, size());
Region.unmap();
(void)sys::fs::resize_file(*FD, Size);
(void)unlockFileThreadSafe(*SharedLockFD);

if (Logger)
Expand Down Expand Up @@ -338,4 +337,4 @@ ErrorOr<FileSizeInfo> FileSizeInfo::get(sys::fs::file_t File) {
return EC;
return FileSizeInfo{Status.getSize(), Status.getSize()};
#endif
}
}
4 changes: 4 additions & 0 deletions llvm/lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ std::error_code resize_file(int FD, uint64_t Size) {
return std::error_code();
}

std::error_code resize_file_sparse(int FD, uint64_t Size) {
return resize_file(FD, Size);
}

static int convertAccessMode(AccessMode Mode) {
switch (Mode) {
case AccessMode::Exist:
Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/Support/Windows/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "llvm/Support/Windows/WindowsSupport.h"
#include <shellapi.h>
#include <shlobj.h>
#include <winioctl.h>

#undef max

Expand Down Expand Up @@ -623,6 +624,22 @@ std::error_code resize_file(int FD, uint64_t Size) {
return std::error_code(error, std::generic_category());
}

std::error_code resize_file_sparse(int FD, uint64_t Size) {
HANDLE hFile = reinterpret_cast<HANDLE>(::_get_osfhandle(FD));
DWORD temp;
if (!DeviceIoControl(hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &temp,
NULL)) {
return mapWindowsError(GetLastError());
}
LARGE_INTEGER liSize;
liSize.QuadPart = Size;
if (!SetFilePointerEx(hFile, liSize, NULL, FILE_BEGIN) ||
!SetEndOfFile(hFile)) {
return mapWindowsError(GetLastError());
}
return std::error_code();
}

std::error_code access(const Twine &Path, AccessMode Mode) {
SmallVector<wchar_t, 128> PathUtf16;

Expand Down
41 changes: 33 additions & 8 deletions llvm/unittests/CAS/CASConfigurationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,46 @@ TEST(CASConfigurationTest, roundTrips) {
}

TEST(CASConfigurationTest, configFileSearch) {
// /a/b/c
SmallString<261> PathToC = sys::path::get_separator();
sys::path::append(PathToC, "a", "b", "c");
// /a/b/c/d
SmallString<261> PathToD = PathToC;
sys::path::append(PathToD, "d");
// /a/b/c/d/e
SmallString<261> PathToE = PathToD;
sys::path::append(PathToE, "e");
// /a/b/c/.cas-config
SmallString<261> PathToCConfig = PathToC;
sys::path::append(PathToCConfig, ".cas-config");
// /a/b/c/d/.cas-config
SmallString<261> PathToDConfig = PathToD;
sys::path::append(PathToDConfig, ".cas-config");

auto VFS = makeIntrusiveRefCnt<vfs::InMemoryFileSystem>();
ASSERT_FALSE(CASConfiguration::createFromSearchConfigFile("/a/b/c/d/e", VFS));
ASSERT_FALSE(
CASConfiguration::createFromSearchConfigFile(PathToE.str(), VFS));

// Add an empty file.
VFS->addFile("/a/b/c/.cas-config", 0,
VFS->addFile(PathToCConfig.str(), 0,
llvm::MemoryBuffer::getMemBufferCopy(""));
ASSERT_FALSE(CASConfiguration::createFromSearchConfigFile("/a/b/c/d/e", VFS));
ASSERT_FALSE(
CASConfiguration::createFromSearchConfigFile(PathToE.str(), VFS));

VFS->addFile(PathToDConfig.str(), 0,
#ifndef _WIN32
llvm::MemoryBuffer::getMemBufferCopy("{\"CASPath\": \"/tmp\"}"));
#else
llvm::MemoryBuffer::getMemBufferCopy("{\"CASPath\": \"\\\\tmp\"}"));
#endif

VFS->addFile("/a/b/c/d/.cas-config", 0,
llvm::MemoryBuffer::getMemBufferCopy("{\"CASPath\": \"/tmp\"}"));
CASConfiguration Config;
Config.CASPath = "/tmp";
SmallString<261> CASPath = sys::path::get_separator();
sys::path::append(CASPath, "tmp");
Config.CASPath = CASPath.str();
auto NewConfig =
CASConfiguration::createFromSearchConfigFile("/a/b/c/d/e", VFS);
CASConfiguration::createFromSearchConfigFile(PathToE.str(), VFS);
ASSERT_TRUE(NewConfig);
ASSERT_TRUE(NewConfig->first == "/a/b/c/d/.cas-config");
ASSERT_TRUE(NewConfig->first == PathToDConfig.str());
ASSERT_TRUE(Config == NewConfig->second);
}
4 changes: 2 additions & 2 deletions llvm/unittests/CAS/ObjectStoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ TEST_P(CASTest, BlobsBigParallel) {
}

#if LLVM_ENABLE_ONDISK_CAS
#ifndef _WIN32 // create_link won't work for directories on Windows
TEST(OnDiskCASTest, BlobsParallelMultiCAS) {
// This test intentionally uses symlinked paths to the same CAS to subvert the
// shared memory mappings that would normally be created within a single
Expand Down Expand Up @@ -402,8 +403,8 @@ TEST(OnDiskCASTest, BlobsBigParallelMultiCAS) {
uint64_t Size = 100ULL * 1024;
ASSERT_NO_FATAL_FAILURE(testBlobsParallel(*CAS1, *CAS2, *CAS3, *CAS4, Size));
}
#endif // _WIN32

#ifndef _WIN32 // FIXME: resize support on Windows.
TEST(OnDiskCASTest, DiskSize) {
setMaxOnDiskCASMappingSize();
unittest::TempDir Temp("on-disk-cas", /*Unique=*/true);
Expand Down Expand Up @@ -451,7 +452,6 @@ TEST(OnDiskCASTest, DiskSize) {
CAS.reset();
CheckFileSizes(/*Mapped=*/false);
}
#endif // _WIN32
#endif // LLVM_ENABLE_ONDISK_CAS
#endif // LLVM_ENABLE_THREADS

5 changes: 5 additions & 0 deletions llvm/unittests/CAS/OnDiskCASLoggerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ TEST(OnDiskCASLoggerTest, MultiProcess) {
SmallVector<ProcessInfo> PIs;
for (int I = 0; I < 5; ++I) {
bool ExecutionFailed;
#ifndef _WIN32
auto PI = ExecuteNoWait(Executable, Argv, ArrayRef<StringRef>{}, {}, 0, &Error,
#else
// CreateProcessW will fail with zero-length env on Windows
auto PI = ExecuteNoWait(Executable, Argv, std::nullopt, {}, 0, &Error,
#endif
&ExecutionFailed);
ASSERT_FALSE(ExecutionFailed) << Error;
PIs.push_back(std::move(PI));
Expand Down
5 changes: 5 additions & 0 deletions llvm/unittests/CAS/PluginCASTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ static std::string getCASPluginPath() {
sys::fs::getMainExecutable(TestMainArgv0, &TestStringArg1);
llvm::SmallString<256> PathBuf(sys::path::parent_path(
sys::path::parent_path(sys::path::parent_path(Executable))));
#ifndef _WIN32
std::string LibName = "libCASPluginTest";
sys::path::append(PathBuf, "lib", LibName + LLVM_PLUGIN_EXT);
#else
std::string LibName = "CASPluginTest";
sys::path::append(PathBuf, "bin", LibName + LLVM_PLUGIN_EXT);
#endif
return std::string(PathBuf);
}

Expand Down