Skip to content

Commit a8087eb

Browse files
authored
Merge pull request #14984 from NixOS/more-windows-fixes
More windows fixes
2 parents 252aff5 + 66fefcd commit a8087eb

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

src/libstore/local-binary-cache-store.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ struct LocalBinaryCacheStore : virtual BinaryCacheStore
7171
{
7272
try {
7373
readFile(config->binaryCacheDir + "/" + path, sink);
74-
} catch (SysError & e) {
75-
if (e.errNo == ENOENT)
74+
} catch (SystemError & e) {
75+
if (e.is(std::errc::no_such_file_or_directory))
7676
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path);
7777
throw;
7878
}

src/libutil/file-system.cc

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,21 @@ std::filesystem::path createTempDir(const std::filesystem::path & tmpRoot, const
540540
AutoCloseFD createAnonymousTempFile()
541541
{
542542
AutoCloseFD fd;
543-
#ifdef O_TMPFILE
543+
544+
#ifdef _WIN32
545+
auto path = makeTempPath(defaultTempDir(), "nix-anonymous");
546+
fd = CreateFileW(
547+
path.c_str(),
548+
GENERIC_READ | GENERIC_WRITE,
549+
/*dwShareMode=*/0,
550+
/*lpSecurityAttributes=*/nullptr,
551+
CREATE_NEW,
552+
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
553+
/*hTemplateFile=*/nullptr);
554+
if (!fd)
555+
throw windows::WinError("creating temporary file %1%", path);
556+
#else
557+
# ifdef O_TMPFILE
544558
static std::atomic_flag tmpfileUnsupported{};
545559
if (!tmpfileUnsupported.test()) /* Try with O_TMPFILE first. */ {
546560
/* Use O_EXCL, because the file is never supposed to be linked into filesystem. */
@@ -555,14 +569,14 @@ AutoCloseFD createAnonymousTempFile()
555569
return fd; /* Successfully created. */
556570
}
557571
}
558-
#endif
572+
# endif
559573
auto [fd2, path] = createTempFile("nix-anonymous");
560574
if (!fd2)
561575
throw SysError("creating temporary file '%s'", path);
562576
fd = std::move(fd2);
563-
#ifndef _WIN32
564577
unlink(requireCString(path)); /* We only care about the file descriptor. */
565578
#endif
579+
566580
return fd;
567581
}
568582

@@ -571,7 +585,6 @@ std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
571585
Path tmpl(defaultTempDir().string() + "/" + prefix + ".XXXXXX");
572586
// Strictly speaking, this is UB, but who cares...
573587
// FIXME: use O_TMPFILE.
574-
// FIXME: Windows should use FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE
575588
AutoCloseFD fd = toDescriptor(mkstemp((char *) tmpl.c_str()));
576589
if (!fd)
577590
throw SysError("creating temporary file '%s'", tmpl);

src/libutil/include/nix/util/error.hh

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,39 @@ MakeError(UsageError, Error);
240240
MakeError(UnimplementedError, Error);
241241

242242
/**
243-
* To use in catch-blocks.
243+
* To use in catch-blocks. Provides a convenience method to get the portable
244+
* std::error_code. Use when you want to catch and check an error condition like
245+
* no_such_file_or_directory (ENOENT) without ifdefs.
244246
*/
245-
MakeError(SystemError, Error);
247+
class SystemError : public Error
248+
{
249+
std::error_code errorCode;
250+
251+
public:
252+
template<typename... Args>
253+
SystemError(std::errc posixErrNo, Args &&... args)
254+
: Error(std::forward<Args>(args)...)
255+
, errorCode(std::make_error_code(posixErrNo))
256+
{
257+
}
258+
259+
template<typename... Args>
260+
SystemError(std::error_code errorCode, Args &&... args)
261+
: Error(std::forward<Args>(args)...)
262+
, errorCode(errorCode)
263+
{
264+
}
265+
266+
const std::error_code ec() const &
267+
{
268+
return errorCode;
269+
}
270+
271+
bool is(std::errc e) const
272+
{
273+
return errorCode == e;
274+
}
275+
};
246276

247277
/**
248278
* POSIX system error, created using `errno`, `strerror` friends.
@@ -271,7 +301,7 @@ public:
271301
*/
272302
template<typename... Args>
273303
SysError(int errNo, const Args &... args)
274-
: SystemError("")
304+
: SystemError(static_cast<std::errc>(errNo), "")
275305
, errNo(errNo)
276306
{
277307
auto hf = HintFmt(args...);
@@ -332,7 +362,7 @@ public:
332362
*/
333363
template<typename... Args>
334364
WinError(DWORD lastError, const Args &... args)
335-
: SystemError("")
365+
: SystemError(std::error_code(lastError, std::system_category()), "")
336366
, lastError(lastError)
337367
{
338368
auto hf = HintFmt(args...);

0 commit comments

Comments
 (0)