| 1 | //===- OnDiskCommon.h -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIB_CAS_ONDISKCOMMON_H |
| 10 | #define LLVM_LIB_CAS_ONDISKCOMMON_H |
| 11 | |
| 12 | #include "llvm/Support/Error.h" |
| 13 | #include "llvm/Support/FileSystem.h" |
| 14 | #include <chrono> |
| 15 | #include <optional> |
| 16 | |
| 17 | namespace llvm::cas::ondisk { |
| 18 | |
| 19 | /// The version for all the ondisk database files. It needs to be bumped when |
| 20 | /// compatibility breaking changes are introduced. |
| 21 | constexpr StringLiteral CASFormatVersion = "v1" ; |
| 22 | |
| 23 | /// Retrieves an overridden maximum mapping size for CAS files, if any, |
| 24 | /// speicified by LLVM_CAS_MAX_MAPPING_SIZE in the environment or set by |
| 25 | /// `setMaxMappingSize()`. If the value from environment is unreadable, returns |
| 26 | /// an error. |
| 27 | Expected<std::optional<uint64_t>> getOverriddenMaxMappingSize(); |
| 28 | |
| 29 | /// Set MaxMappingSize for ondisk CAS. This function is not thread-safe and |
| 30 | /// should be set before creaing any ondisk CAS and does not affect CAS already |
| 31 | /// created. Set value 0 to use default size. |
| 32 | LLVM_ABI_FOR_TEST void setMaxMappingSize(uint64_t Size); |
| 33 | |
| 34 | /// Whether to use a small file mapping for ondisk databases created in \p Path. |
| 35 | /// |
| 36 | /// For some file system that doesn't support sparse file, use a smaller file |
| 37 | /// mapping to avoid consuming too much disk space on creation. |
| 38 | bool useSmallMappingSize(const Twine &Path); |
| 39 | |
| 40 | /// Thread-safe alternative to \c sys::fs::lockFile. This does not support all |
| 41 | /// the platforms that \c sys::fs::lockFile does, so keep it in the CAS library |
| 42 | /// for now. |
| 43 | std::error_code lockFileThreadSafe(int FD, llvm::sys::fs::LockKind Kind); |
| 44 | |
| 45 | /// Thread-safe alternative to \c sys::fs::unlockFile. This does not support all |
| 46 | /// the platforms that \c sys::fs::lockFile does, so keep it in the CAS library |
| 47 | /// for now. |
| 48 | std::error_code unlockFileThreadSafe(int FD); |
| 49 | |
| 50 | /// Thread-safe alternative to \c sys::fs::tryLockFile. This does not support |
| 51 | /// all the platforms that \c sys::fs::lockFile does, so keep it in the CAS |
| 52 | /// library for now. |
| 53 | std::error_code tryLockFileThreadSafe( |
| 54 | int FD, std::chrono::milliseconds Timeout = std::chrono::milliseconds(0), |
| 55 | llvm::sys::fs::LockKind Kind = llvm::sys::fs::LockKind::Exclusive); |
| 56 | |
| 57 | /// Allocate space for the file \p FD on disk, if the filesystem supports it. |
| 58 | /// |
| 59 | /// On filesystems that support this operation, this ensures errors such as |
| 60 | /// \c std::errc::no_space_on_device are detected before we write data. |
| 61 | /// |
| 62 | /// \returns the new size of the file, or an \c Error. |
| 63 | Expected<size_t> preallocateFileTail(int FD, size_t CurrentSize, |
| 64 | size_t NewSize); |
| 65 | |
| 66 | /// Get boot time for the OS. This can be used to check if the CAS has been |
| 67 | /// validated since boot. |
| 68 | /// |
| 69 | /// \returns the boot time in seconds (0 if operation not supported), or an \c |
| 70 | /// Error. |
| 71 | Expected<uint64_t> getBootTime(); |
| 72 | |
| 73 | } // namespace llvm::cas::ondisk |
| 74 | |
| 75 | #endif // LLVM_LIB_CAS_ONDISKCOMMON_H |
| 76 | |