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
17namespace 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.
21constexpr 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.
27Expected<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.
32LLVM_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.
38bool 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.
43LLVM_ABI_FOR_TEST std::error_code
44lockFileThreadSafe(int FD, llvm::sys::fs::LockKind Kind);
45
46/// Thread-safe alternative to \c sys::fs::unlockFile. This does not support all
47/// the platforms that \c sys::fs::lockFile does, so keep it in the CAS library
48/// for now.
49LLVM_ABI_FOR_TEST std::error_code unlockFileThreadSafe(int FD);
50
51/// Thread-safe alternative to \c sys::fs::tryLockFile. This does not support
52/// all the platforms that \c sys::fs::lockFile does, so keep it in the CAS
53/// library for now.
54std::error_code tryLockFileThreadSafe(
55 int FD, std::chrono::milliseconds Timeout = std::chrono::milliseconds(0),
56 llvm::sys::fs::LockKind Kind = llvm::sys::fs::LockKind::Exclusive);
57
58/// Allocate space for the file \p FD on disk, if the filesystem supports it.
59///
60/// On filesystems that support this operation, this ensures errors such as
61/// \c std::errc::no_space_on_device are detected before we write data.
62///
63/// \returns the new size of the file, or an \c Error.
64Expected<size_t> preallocateFileTail(int FD, size_t CurrentSize,
65 size_t NewSize);
66
67/// Get boot time for the OS. This can be used to check if the CAS has been
68/// validated since boot.
69///
70/// \returns the boot time in platform-specific units (0 if operation not
71/// supported), or an \c Error.
72LLVM_ABI_FOR_TEST Expected<uint64_t> getBootTime();
73
74/// Helper RAII class for copying a file to a unique file path. At destruction
75/// time it will delete any new temporary files created.
76class UniqueTempFile {
77public:
78 UniqueTempFile() = default;
79 ~UniqueTempFile();
80
81 /// Create a new unique file path under \p ParentPath and copy the contents
82 /// of \p CopyFromPath into it. It will use file cloning when applicable.
83 ///
84 /// \returns the new unique file path.
85 Expected<StringRef> createAndCopyFrom(StringRef ParentPath,
86 StringRef CopyFromPath);
87
88 /// Rename the new unique file to \p RenameToPath. This is useful to indicate
89 /// that the unique file doesn't need to be cleared at destruction time.
90 Error renameTo(StringRef RenameToPath);
91
92private:
93 SmallString<256> TmpPath;
94 SmallString<256> UniqueTmpPath;
95};
96
97} // namespace llvm::cas::ondisk
98
99#endif // LLVM_LIB_CAS_ONDISKCOMMON_H
100