Skip to content
Bitcoin Roots

v29.4-roots.2 · Commit 2: [PATCH 02/21] runtime: preserve shared operator controls

src/util/fs.cpp

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

File metadata
index 348c1b338377fcef051c92141020fd24ee7e2365..0d0c03971a539b480d62a7e635fb0d72b31a246e 100644

Download original patch · Release notes

Diff rows 1–53 of 53. 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
@@ -117,9 +137,9 @@ bool FileLock::TryLock()
117
 
118
  std::string get_filesystem_error_message(const fs::filesystem_error& e)
119
  {
120
- #ifndef WIN32
 
121
  return e.what();
122
- #else
123
  // Convert from Multi Byte to utf-16
124
  std::string mb_string(e.what());
125
  int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
 
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
 
137
 
138
  std::string get_filesystem_error_message(const fs::filesystem_error& e)
139
  {
140
+ return e.code().message();
141
+ #if 0
142
  return e.what();
 
143
  // Convert from Multi Byte to utf-16
144
  std::string mb_string(e.what());
145
  int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);