Skip to content
Bitcoin Roots

v30.3-roots.1 · Commit 2: [PATCH 02/68] feat(runtime): preserve Roots operator controls on Core 30.3

src/util/fs.cpp

Before: src/util/fs.cpp · After: src/util/fs.cpp

File metadata
index ec4d551e7881f36a5fe341798496d6cfc05c3551..a90b4f2cb920f4f2cfa74966f8d44570af831c62 100644

Download original patch · Release notes

Diff rows 1–51 of 51. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/util/fs.cpp CHANGED
@@ -2,6 +2,7 @@
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
 
@@ -13,7 +14,13 @@
13
  #include <unistd.h>
14
  #else
15
  #include <codecvt>
 
 
 
16
  #include <limits>
 
 
 
17
  #include <windows.h>
18
  #endif
19
 
@@ -25,9 +32,22 @@ namespace fsbridge {
25
 
26
  FILE *fopen(const fs::path& p, const char *mode)
27
  {
 
28
  #ifndef WIN32
 
29
  return ::fopen(p.c_str(), mode);
30
  #else
 
 
 
 
 
 
 
 
 
 
 
31
  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> utf8_cvt;
32
  return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
33
  #endif
@@ -115,4 +135,9 @@ bool FileLock::TryLock()
115
  }
116
  #endif
117
 
 
 
 
 
 
118
  } // namespace fsbridge
 
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/check.h>
6
  #include <util/fs.h>
7
  #include <util/syserror.h>
8
 
 
14
  #include <unistd.h>
15
  #else
16
  #include <codecvt>
17
+ #include <cstring>
18
+ #include <fcntl.h>
19
+ #include <io.h>
20
  #include <limits>
21
+ #include <locale>
22
+ #include <share.h>
23
+ #include <sys/stat.h>
24
  #include <windows.h>
25
  #endif
26
 
 
32
 
33
  FILE *fopen(const fs::path& p, const char *mode)
34
  {
35
+ const bool exclusive{strchr(mode, 'x') != nullptr};
36
  #ifndef WIN32
37
+ Assume((!exclusive) || !strcmp(mode, "wbx"));
38
  return ::fopen(p.c_str(), mode);
39
  #else
40
+ if (exclusive) {
41
+ Assert(!strcmp(mode, "wbx"));
42
+ int fd;
43
+ if (::_wsopen_s(&fd, p.wstring().c_str(), _O_WRONLY | _O_CREAT | _O_EXCL | _O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE)) {
44
+ return nullptr;
45
+ }
46
+ FILE* fp = ::_fdopen(fd, "wb");
47
+ if (!fp) ::_close(fd);
48
+ return fp;
49
+ }
50
+
51
  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> utf8_cvt;
52
  return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
53
  #endif
 
135
  }
136
  #endif
137
 
138
+ std::string get_filesystem_error_message(const fs::filesystem_error& e)
139
+ {
140
+ return e.code().message();
141
+ }
142
+
143
  } // namespace fsbridge