v29.4-roots.3 · Commit 2: [PATCH 02/30] 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. The two line-number columns refer to before and after.
src/util/fs.cpp
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Distributed under the MIT software license, see the accompanying
|
|
3
3
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
4
4
|
|
|
5
|
+
#include <util/check.h>
|
|
5
6
|
#include <util/fs.h>
|
|
6
7
|
#include <util/syserror.h>
|
|
7
8
|
|
|
@@ -13,7 +14,13 @@
|
|
|
13
14
|
#include <unistd.h>
|
|
14
15
|
#else
|
|
15
16
|
#include <codecvt>
|
|
17
|
+
#include <cstring>
|
|
18
|
+
#include <fcntl.h>
|
|
19
|
+
#include <io.h>
|
|
16
20
|
#include <limits>
|
|
21
|
+
#include <locale>
|
|
22
|
+
#include <share.h>
|
|
23
|
+
#include <sys/stat.h>
|
|
17
24
|
#include <windows.h>
|
|
18
25
|
#endif
|
|
19
26
|
|
|
@@ -25,9 +32,22 @@ namespace fsbridge {
|
|
|
25
32
|
|
|
26
33
|
FILE *fopen(const fs::path& p, const char *mode)
|
|
27
34
|
{
|
|
35
|
+
const bool exclusive{strchr(mode, 'x') != nullptr};
|
|
28
36
|
#ifndef WIN32
|
|
37
|
+
Assume((!exclusive) || !strcmp(mode, "wbx"));
|
|
29
38
|
return ::fopen(p.c_str(), mode);
|
|
30
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
|
+
|
|
31
51
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> utf8_cvt;
|
|
32
52
|
return ::_wfopen(p.wstring().c_str(), utf8_cvt.from_bytes(mode).c_str());
|
|
33
53
|
#endif
|
|
@@ -117,9 +137,9 @@ bool FileLock::TryLock()
|
|
|
117
137
|
|
|
118
138
|
std::string get_filesystem_error_message(const fs::filesystem_error& e)
|
|
119
139
|
{
|
|
120
|
-
#ifndef WIN32
|
|
140
|
+
return e.code().message();
|
|
141
|
+
#if 0
|
|
121
142
|
return e.what();
|
|
122
|
-
#else
|
|
123
143
|
// Convert from Multi Byte to utf-16
|
|
124
144
|
std::string mb_string(e.what());
|
|
125
145
|
int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
|