LCOV - code coverage report
Current view: top level - src/util - fs_helpers.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 15 95 15.8 %
Date: 2023-09-26 12:08:55 Functions: 5 15 33.3 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2023 The Bitcoin Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #include <util/fs_helpers.h>
       7             : 
       8             : #if defined(HAVE_CONFIG_H)
       9             : #include <config/bitcoin-config.h>
      10             : #endif
      11             : 
      12             : #include <logging.h>
      13             : #include <sync.h>
      14             : #include <tinyformat.h>
      15             : #include <util/fs.h>
      16             : #include <util/getuniquepath.h>
      17             : #include <util/syserror.h>
      18             : 
      19             : #include <cerrno>
      20             : #include <filesystem>
      21             : #include <fstream>
      22             : #include <map>
      23             : #include <memory>
      24             : #include <string>
      25             : #include <system_error>
      26             : #include <utility>
      27             : 
      28             : #ifndef WIN32
      29             : // for posix_fallocate, in configure.ac we check if it is present after this
      30             : #ifdef __linux__
      31             : 
      32             : #ifdef _POSIX_C_SOURCE
      33             : #undef _POSIX_C_SOURCE
      34             : #endif
      35             : 
      36             : #define _POSIX_C_SOURCE 200112L
      37             : 
      38             : #endif // __linux__
      39             : 
      40             : #include <fcntl.h>
      41             : #include <sys/resource.h>
      42             : #include <unistd.h>
      43             : #else
      44             : #include <io.h> /* For _get_osfhandle, _chsize */
      45             : #include <shlobj.h> /* For SHGetSpecialFolderPathW */
      46             : #endif // WIN32
      47             : 
      48             : /** Mutex to protect dir_locks. */
      49             : static GlobalMutex cs_dir_locks;
      50             : /** A map that contains all the currently held directory locks. After
      51             :  * successful locking, these will be held here until the global destructor
      52             :  * cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks
      53             :  * is called.
      54             :  */
      55           2 : static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks GUARDED_BY(cs_dir_locks);
      56             : 
      57           0 : bool LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only)
      58             : {
      59           0 :     LOCK(cs_dir_locks);
      60           0 :     fs::path pathLockFile = directory / lockfile_name;
      61             : 
      62             :     // If a lock for this directory already exists in the map, don't try to re-lock it
      63           0 :     if (dir_locks.count(fs::PathToString(pathLockFile))) {
      64           0 :         return true;
      65             :     }
      66             : 
      67             :     // Create empty lock file if it doesn't exist.
      68           0 :     FILE* file = fsbridge::fopen(pathLockFile, "a");
      69           0 :     if (file) fclose(file);
      70           0 :     auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
      71           0 :     if (!lock->TryLock()) {
      72           0 :         return error("Error while attempting to lock directory %s: %s", fs::PathToString(directory), lock->GetReason());
      73             :     }
      74           2 :     if (!probe_only) {
      75             :         // Lock successful and we're not just probing, put it into the map
      76           0 :         dir_locks.emplace(fs::PathToString(pathLockFile), std::move(lock));
      77           0 :     }
      78           0 :     return true;
      79           0 : }
      80             : 
      81           0 : void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name)
      82             : {
      83           0 :     LOCK(cs_dir_locks);
      84           0 :     dir_locks.erase(fs::PathToString(directory / lockfile_name));
      85           0 : }
      86             : 
      87           0 : void ReleaseDirectoryLocks()
      88             : {
      89           0 :     LOCK(cs_dir_locks);
      90           0 :     dir_locks.clear();
      91           0 : }
      92             : 
      93           0 : bool DirIsWritable(const fs::path& directory)
      94             : {
      95           0 :     fs::path tmpFile = GetUniquePath(directory);
      96             : 
      97           0 :     FILE* file = fsbridge::fopen(tmpFile, "a");
      98           0 :     if (!file) return false;
      99             : 
     100           0 :     fclose(file);
     101           0 :     remove(tmpFile);
     102             : 
     103           0 :     return true;
     104           0 : }
     105             : 
     106           2 : bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes)
     107             : {
     108           2 :     constexpr uint64_t min_disk_space = 52428800; // 50 MiB
     109             : 
     110           2 :     uint64_t free_bytes_available = fs::space(dir).available;
     111           2 :     return free_bytes_available >= min_disk_space + additional_bytes;
     112             : }
     113             : 
     114           0 : std::streampos GetFileSize(const char* path, std::streamsize max)
     115             : {
     116           0 :     std::ifstream file{path, std::ios::binary};
     117           0 :     file.ignore(max);
     118           0 :     return file.gcount();
     119           0 : }
     120             : 
     121           0 : bool FileCommit(FILE* file)
     122             : {
     123           0 :     if (fflush(file) != 0) { // harmless if redundantly called
     124           0 :         LogPrintf("fflush failed: %s\n", SysErrorString(errno));
     125           0 :         return false;
     126             :     }
     127             : #ifdef WIN32
     128             :     HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
     129             :     if (FlushFileBuffers(hFile) == 0) {
     130             :         LogPrintf("FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
     131             :         return false;
     132             :     }
     133             : #elif defined(MAC_OSX) && defined(F_FULLFSYNC)
     134             :     if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) { // Manpage says "value other than -1" is returned on success
     135             :         LogPrintf("fcntl F_FULLFSYNC failed: %s\n", SysErrorString(errno));
     136             :         return false;
     137             :     }
     138             : #elif HAVE_FDATASYNC
     139           0 :     if (fdatasync(fileno(file)) != 0 && errno != EINVAL) { // Ignore EINVAL for filesystems that don't support sync
     140           0 :         LogPrintf("fdatasync failed: %s\n", SysErrorString(errno));
     141           0 :         return false;
     142             :     }
     143             : #else
     144             :     if (fsync(fileno(file)) != 0 && errno != EINVAL) {
     145             :         LogPrintf("fsync failed: %s\n", SysErrorString(errno));
     146             :         return false;
     147             :     }
     148             : #endif
     149           0 :     return true;
     150           0 : }
     151             : 
     152           0 : void DirectoryCommit(const fs::path& dirname)
     153             : {
     154             : #ifndef WIN32
     155           0 :     FILE* file = fsbridge::fopen(dirname, "r");
     156           0 :     if (file) {
     157           0 :         fsync(fileno(file));
     158           0 :         fclose(file);
     159           0 :     }
     160             : #endif
     161           0 : }
     162             : 
     163           0 : bool TruncateFile(FILE* file, unsigned int length)
     164             : {
     165             : #if defined(WIN32)
     166             :     return _chsize(_fileno(file), length) == 0;
     167             : #else
     168           0 :     return ftruncate(fileno(file), length) == 0;
     169             : #endif
     170             : }
     171             : 
     172             : /**
     173             :  * this function tries to raise the file descriptor limit to the requested number.
     174             :  * It returns the actual file descriptor limit (which may be more or less than nMinFD)
     175             :  */
     176           1 : int RaiseFileDescriptorLimit(int nMinFD)
     177             : {
     178             : #if defined(WIN32)
     179             :     return 2048;
     180             : #else
     181             :     struct rlimit limitFD;
     182           1 :     if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
     183           1 :         if (limitFD.rlim_cur < (rlim_t)nMinFD) {
     184           0 :             limitFD.rlim_cur = nMinFD;
     185           0 :             if (limitFD.rlim_cur > limitFD.rlim_max)
     186           0 :                 limitFD.rlim_cur = limitFD.rlim_max;
     187           0 :             setrlimit(RLIMIT_NOFILE, &limitFD);
     188           0 :             getrlimit(RLIMIT_NOFILE, &limitFD);
     189           0 :         }
     190           1 :         return limitFD.rlim_cur;
     191             :     }
     192           0 :     return nMinFD; // getrlimit failed, assume it's fine
     193             : #endif
     194           1 : }
     195             : 
     196             : /**
     197             :  * this function tries to make a particular range of a file allocated (corresponding to disk space)
     198             :  * it is advisory, and the range specified in the arguments will never contain live data
     199             :  */
     200           2 : void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
     201             : {
     202             : #if defined(WIN32)
     203             :     // Windows-specific version
     204             :     HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
     205             :     LARGE_INTEGER nFileSize;
     206             :     int64_t nEndPos = (int64_t)offset + length;
     207             :     nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
     208             :     nFileSize.u.HighPart = nEndPos >> 32;
     209             :     SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
     210             :     SetEndOfFile(hFile);
     211             : #elif defined(MAC_OSX)
     212             :     // OSX specific version
     213             :     // NOTE: Contrary to other OS versions, the OSX version assumes that
     214             :     // NOTE: offset is the size of the file.
     215             :     fstore_t fst;
     216             :     fst.fst_flags = F_ALLOCATECONTIG;
     217             :     fst.fst_posmode = F_PEOFPOSMODE;
     218             :     fst.fst_offset = 0;
     219             :     fst.fst_length = length; // mac os fst_length takes the # of free bytes to allocate, not desired file size
     220             :     fst.fst_bytesalloc = 0;
     221             :     if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
     222             :         fst.fst_flags = F_ALLOCATEALL;
     223             :         fcntl(fileno(file), F_PREALLOCATE, &fst);
     224             :     }
     225             :     ftruncate(fileno(file), static_cast<off_t>(offset) + length);
     226             : #else
     227             : #if defined(HAVE_POSIX_FALLOCATE)
     228             :     // Version using posix_fallocate
     229           2 :     off_t nEndPos = (off_t)offset + length;
     230           2 :     if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;
     231             : #endif
     232             :     // Fallback version
     233             :     // TODO: just write one byte per block
     234             :     static const char buf[65536] = {};
     235           0 :     if (fseek(file, offset, SEEK_SET)) {
     236           0 :         return;
     237             :     }
     238           0 :     while (length > 0) {
     239           0 :         unsigned int now = 65536;
     240           0 :         if (length < now)
     241           0 :             now = length;
     242           0 :         fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
     243           0 :         length -= now;
     244             :     }
     245             : #endif
     246           2 : }
     247             : 
     248             : #ifdef WIN32
     249             : fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
     250             : {
     251             :     WCHAR pszPath[MAX_PATH] = L"";
     252             : 
     253             :     if (SHGetSpecialFolderPathW(nullptr, pszPath, nFolder, fCreate)) {
     254             :         return fs::path(pszPath);
     255             :     }
     256             : 
     257             :     LogPrintf("SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
     258             :     return fs::path("");
     259             : }
     260             : #endif
     261             : 
     262           0 : bool RenameOver(fs::path src, fs::path dest)
     263             : {
     264             : #ifdef __MINGW64__
     265             :     // This is a workaround for a bug in libstdc++ which
     266             :     // implements std::filesystem::rename with _wrename function.
     267             :     // This bug has been fixed in upstream:
     268             :     //  - GCC 10.3: 8dd1c1085587c9f8a21bb5e588dfe1e8cdbba79e
     269             :     //  - GCC 11.1: 1dfd95f0a0ca1d9e6cbc00e6cbfd1fa20a98f312
     270             :     // For more details see the commits mentioned above.
     271             :     return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
     272             :                        MOVEFILE_REPLACE_EXISTING) != 0;
     273             : #else
     274           0 :     std::error_code error;
     275           0 :     fs::rename(src, dest, error);
     276           0 :     return !error;
     277             : #endif
     278             : }
     279             : 
     280             : /**
     281             :  * Ignores exceptions thrown by create_directories if the requested directory exists.
     282             :  * Specifically handles case where path p exists, but it wasn't possible for the user to
     283             :  * write to the parent directory.
     284             :  */
     285           0 : bool TryCreateDirectories(const fs::path& p)
     286             : {
     287             :     try {
     288           0 :         return fs::create_directories(p);
     289           0 :     } catch (const fs::filesystem_error&) {
     290           0 :         if (!fs::exists(p) || !fs::is_directory(p))
     291           0 :             throw;
     292           0 :     }
     293             : 
     294             :     // create_directories didn't create the directory, it had to have existed already
     295           0 :     return false;
     296           0 : }

Generated by: LCOV version 1.14