Skip to content
Merged
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
20 changes: 15 additions & 5 deletions llvm/lib/CAS/OnDiskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "llvm/Config/config.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Process.h"
#include <limits>
#include <mutex>
#include <optional>
#include <thread>
Expand Down Expand Up @@ -115,16 +116,25 @@ cas::ondisk::tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout,
}

Expected<size_t> cas::ondisk::preallocateFileTail(int FD, size_t CurrentSize, size_t NewSize) {
auto CreateErrorFromErrno = [&]() -> Expected<size_t> {
std::error_code EC = errnoAsErrorCode();
auto CreateError = [&](std::error_code EC) -> Expected<size_t> {
if (EC == std::errc::not_supported)
// Ignore ENOTSUP in case the filesystem cannot preallocate.
return NewSize;
#if defined(HAVE_POSIX_FALLOCATE)
if (EC == std::errc::invalid_argument &&
CurrentSize < NewSize && // len > 0
NewSize < std::numeric_limits<off_t>::max()) // 0 <= offset, len < max
// Prior to 2024, POSIX required EINVAL for cases that should be ENOTSUP,
// so handle it the same as above if it is not one of the other ways to
// get EINVAL.
return NewSize;
#endif
return createStringError(EC, "failed to allocate to CAS file: " + EC.message());
};
#if defined(HAVE_POSIX_FALLOCATE)
if (posix_fallocate(FD, CurrentSize, NewSize - CurrentSize))
return CreateErrorFromErrno();
// Note: posix_fallocate returns its error directly, not via errno.
if (int Err = posix_fallocate(FD, CurrentSize, NewSize - CurrentSize))
return CreateError(std::error_code(Err, std::generic_category()));
return NewSize;
#elif defined(__APPLE__)
fstore_t FAlloc;
Expand All @@ -134,7 +144,7 @@ Expected<size_t> cas::ondisk::preallocateFileTail(int FD, size_t CurrentSize, si
FAlloc.fst_length = NewSize - CurrentSize;
FAlloc.fst_bytesalloc = 0;
if (fcntl(FD, F_PREALLOCATE, &FAlloc))
return CreateErrorFromErrno();
return CreateError(errnoAsErrorCode());
assert(CurrentSize + FAlloc.fst_bytesalloc >= NewSize);
return CurrentSize + FAlloc.fst_bytesalloc;
#else
Expand Down