LCOV - code coverage report
Current view: top level - src/util - fs.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 5 35 14.3 %
Date: 2023-11-10 23:46:46 Functions: 2 7 28.6 %
Branches: 4 32 12.5 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2017-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <util/fs.h>
       6                 :            : #include <util/syserror.h>
       7                 :            : 
       8                 :            : #ifndef WIN32
       9                 :            : #include <cstring>
      10                 :            : #include <fcntl.h>
      11                 :            : #include <sys/file.h>
      12                 :            : #include <sys/utsname.h>
      13                 :            : #include <unistd.h>
      14                 :            : #else
      15                 :            : #include <codecvt>
      16                 :            : #include <limits>
      17                 :            : #include <windows.h>
      18                 :            : #endif
      19                 :            : 
      20                 :            : #include <cassert>
      21                 :            : #include <string>
      22                 :            : 
      23                 :            : namespace fsbridge {
      24                 :            : 
      25                 :       1481 : FILE *fopen(const fs::path& p, const char *mode)
      26                 :            : {
      27                 :            : #ifndef WIN32
      28                 :       1481 :     return ::fopen(p.c_str(), mode);
      29                 :            : #else
      30                 :            :     std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> utf8_cvt;
      31                 :            :     return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
      32                 :            : #endif
      33                 :            : }
      34                 :            : 
      35                 :          1 : fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
      36                 :            : {
      37         [ +  - ]:          1 :     assert(base.is_absolute());
      38 [ +  - ][ #  # ]:          1 :     return path.empty() ? base : fs::path(base / path);
         [ #  # ][ +  - ]
         [ +  - ][ #  # ]
                 [ #  # ]
      39                 :          0 : }
      40                 :            : 
      41                 :            : #ifndef WIN32
      42                 :            : 
      43                 :          0 : static std::string GetErrorReason()
      44                 :            : {
      45                 :          0 :     return SysErrorString(errno);
      46                 :            : }
      47                 :            : 
      48                 :          0 : FileLock::FileLock(const fs::path& file)
      49                 :            : {
      50         [ #  # ]:          0 :     fd = open(file.c_str(), O_RDWR);
      51         [ #  # ]:          0 :     if (fd == -1) {
      52         [ #  # ]:          0 :         reason = GetErrorReason();
      53                 :          0 :     }
      54                 :          0 : }
      55                 :            : 
      56                 :          0 : FileLock::~FileLock()
      57                 :            : {
      58         [ #  # ]:          0 :     if (fd != -1) {
      59         [ #  # ]:          0 :         close(fd);
      60                 :          0 :     }
      61                 :          0 : }
      62                 :            : 
      63                 :          0 : bool FileLock::TryLock()
      64                 :            : {
      65         [ #  # ]:          0 :     if (fd == -1) {
      66                 :          0 :         return false;
      67                 :            :     }
      68                 :            : 
      69                 :            :     struct flock lock;
      70                 :          0 :     lock.l_type = F_WRLCK;
      71                 :          0 :     lock.l_whence = SEEK_SET;
      72                 :          0 :     lock.l_start = 0;
      73                 :          0 :     lock.l_len = 0;
      74         [ #  # ]:          0 :     if (fcntl(fd, F_SETLK, &lock) == -1) {
      75                 :          0 :         reason = GetErrorReason();
      76                 :          0 :         return false;
      77                 :            :     }
      78                 :            : 
      79                 :          0 :     return true;
      80                 :          0 : }
      81                 :            : #else
      82                 :            : 
      83                 :            : static std::string GetErrorReason() {
      84                 :            :     return Win32ErrorString(GetLastError());
      85                 :            : }
      86                 :            : 
      87                 :            : FileLock::FileLock(const fs::path& file)
      88                 :            : {
      89                 :            :     hFile = CreateFileW(file.wstring().c_str(),  GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
      90                 :            :         nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
      91                 :            :     if (hFile == INVALID_HANDLE_VALUE) {
      92                 :            :         reason = GetErrorReason();
      93                 :            :     }
      94                 :            : }
      95                 :            : 
      96                 :            : FileLock::~FileLock()
      97                 :            : {
      98                 :            :     if (hFile != INVALID_HANDLE_VALUE) {
      99                 :            :         CloseHandle(hFile);
     100                 :            :     }
     101                 :            : }
     102                 :            : 
     103                 :            : bool FileLock::TryLock()
     104                 :            : {
     105                 :            :     if (hFile == INVALID_HANDLE_VALUE) {
     106                 :            :         return false;
     107                 :            :     }
     108                 :            :     _OVERLAPPED overlapped = {};
     109                 :            :     if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, std::numeric_limits<DWORD>::max(), std::numeric_limits<DWORD>::max(), &overlapped)) {
     110                 :            :         reason = GetErrorReason();
     111                 :            :         return false;
     112                 :            :     }
     113                 :            :     return true;
     114                 :            : }
     115                 :            : #endif
     116                 :            : 
     117                 :          0 : std::string get_filesystem_error_message(const fs::filesystem_error& e)
     118                 :            : {
     119                 :            : #ifndef WIN32
     120         [ #  # ]:          0 :     return e.what();
     121                 :            : #else
     122                 :            :     // Convert from Multi Byte to utf-16
     123                 :            :     std::string mb_string(e.what());
     124                 :            :     int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
     125                 :            : 
     126                 :            :     std::wstring utf16_string(size, L'\0');
     127                 :            :     MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
     128                 :            :     // Convert from utf-16 to utf-8
     129                 :            :     return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
     130                 :            : #endif
     131                 :          0 : }
     132                 :            : 
     133                 :            : } // fsbridge

Generated by: LCOV version 1.14