mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
This makes it easier for a class or a struct to own a lock on a directory for the duration of its lifetime. It is used in the next commit.
100 lines
2.8 KiB
C++
100 lines
2.8 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2023 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_UTIL_FS_HELPERS_H
|
|
#define BITCOIN_UTIL_FS_HELPERS_H
|
|
|
|
#include <util/fs.h>
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <iosfwd>
|
|
#include <limits>
|
|
#include <optional>
|
|
|
|
/**
|
|
* Ensure file contents are fully committed to disk, using a platform-specific
|
|
* feature analogous to fsync().
|
|
*/
|
|
bool FileCommit(FILE* file);
|
|
|
|
/**
|
|
* Sync directory contents. This is required on some environments to ensure that
|
|
* newly created files are committed to disk.
|
|
*/
|
|
void DirectoryCommit(const fs::path& dirname);
|
|
|
|
bool TruncateFile(FILE* file, unsigned int length);
|
|
int RaiseFileDescriptorLimit(int nMinFD);
|
|
void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length);
|
|
|
|
/**
|
|
* Rename src to dest.
|
|
* @return true if the rename was successful.
|
|
*/
|
|
[[nodiscard]] bool RenameOver(fs::path src, fs::path dest);
|
|
|
|
namespace util {
|
|
enum class LockResult {
|
|
Success,
|
|
ErrorWrite,
|
|
ErrorLock,
|
|
};
|
|
[[nodiscard]] LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only = false);
|
|
} // namespace util
|
|
void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name);
|
|
|
|
class DirectoryLock
|
|
{
|
|
fs::path m_path;
|
|
std::string m_name;
|
|
|
|
public:
|
|
explicit DirectoryLock(fs::path dir_path, std::string name);
|
|
~DirectoryLock();
|
|
|
|
DirectoryLock(const DirectoryLock&) = delete;
|
|
DirectoryLock& operator=(const DirectoryLock&) = delete;
|
|
|
|
DirectoryLock(DirectoryLock&& other) noexcept;
|
|
DirectoryLock& operator=(DirectoryLock&& other) noexcept;
|
|
};
|
|
|
|
bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0);
|
|
|
|
/** Get the size of a file by scanning it.
|
|
*
|
|
* @param[in] path The file path
|
|
* @param[in] max Stop seeking beyond this limit
|
|
* @return The file size or max
|
|
*/
|
|
std::streampos GetFileSize(const char* path, std::streamsize max = std::numeric_limits<std::streamsize>::max());
|
|
|
|
/** Release all directory locks. This is used for unit testing only, at runtime
|
|
* the global destructor will take care of the locks.
|
|
*/
|
|
void ReleaseDirectoryLocks();
|
|
|
|
bool TryCreateDirectories(const fs::path& p);
|
|
fs::path GetDefaultDataDir();
|
|
|
|
/** Convert fs::perms to symbolic string of the form 'rwxrwxrwx'
|
|
*
|
|
* @param[in] p the perms to be converted
|
|
* @return Symbolic permissions string
|
|
*/
|
|
std::string PermsToSymbolicString(fs::perms p);
|
|
/** Interpret a custom permissions level string as fs::perms
|
|
*
|
|
* @param[in] s Permission level string
|
|
* @return Permissions as fs::perms
|
|
*/
|
|
std::optional<fs::perms> InterpretPermString(const std::string& s);
|
|
|
|
#ifdef WIN32
|
|
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
|
|
#endif
|
|
|
|
#endif // BITCOIN_UTIL_FS_HELPERS_H
|