LLVM 24.0.0git
OnDiskCommon.h
Go to the documentation of this file.
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"
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.
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.
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.
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
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),
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.
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.
77public:
78 UniqueTempFile() = default;
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.
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
uint64_t uint64_t
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:216
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:888
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Error renameTo(StringRef RenameToPath)
Rename the new unique file to RenameToPath.
Expected< StringRef > createAndCopyFrom(StringRef ParentPath, StringRef CopyFromPath)
Create a new unique file path under ParentPath and copy the contents of CopyFromPath into it.
constexpr StringLiteral CASFormatVersion
The version for all the ondisk database files.
LLVM_ABI_FOR_TEST void setMaxMappingSize(uint64_t Size)
Set MaxMappingSize for ondisk CAS.
Expected< std::optional< uint64_t > > getOverriddenMaxMappingSize()
Retrieves an overridden maximum mapping size for CAS files, if any, speicified by LLVM_CAS_MAX_MAPPIN...
LLVM_ABI_FOR_TEST std::error_code lockFileThreadSafe(int FD, llvm::sys::fs::LockKind Kind)
Thread-safe alternative to sys::fs::lockFile.
LLVM_ABI_FOR_TEST std::error_code unlockFileThreadSafe(int FD)
Thread-safe alternative to sys::fs::unlockFile.
std::error_code tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout=std::chrono::milliseconds(0), llvm::sys::fs::LockKind Kind=llvm::sys::fs::LockKind::Exclusive)
Thread-safe alternative to sys::fs::tryLockFile.
LLVM_ABI_FOR_TEST Expected< uint64_t > getBootTime()
Get boot time for the OS.
Expected< size_t > preallocateFileTail(int FD, size_t CurrentSize, size_t NewSize)
Allocate space for the file FD on disk, if the filesystem supports it.
bool useSmallMappingSize(const Twine &Path)
Whether to use a small file mapping for ondisk databases created in Path.
LockKind
An enumeration for the lock kind.
@ Timeout
Reached timeout while waiting for the owner to release the lock.