Merge pull request #14962 from NixOS/fix-mingw

Fix mingw build (once again), add openFileReadonly and clean up error.hh to include WinError
This commit is contained in:
Sergei Zimmerman
2026-01-10 00:39:46 +00:00
committed by GitHub
21 changed files with 105 additions and 100 deletions

View File

@@ -21,7 +21,9 @@ RemoteFSAccessor::RemoteFSAccessor(
std::filesystem::path RemoteFSAccessor::makeCacheFile(std::string_view hashPart, const std::string & ext)
{
assert(cacheDir);
return (*cacheDir / hashPart) + "." + ext;
auto res = (*cacheDir / hashPart);
res.concat(concatStrings(".", ext));
return res;
}
ref<SourceAccessor> RemoteFSAccessor::addToCache(std::string_view hashPart, std::string && nar)

View File

@@ -7,7 +7,6 @@
# include <errhandlingapi.h>
# include <fileapi.h>
# include <windows.h>
# include "nix/util/windows-error.hh"
namespace nix {

View File

@@ -6,7 +6,6 @@
#ifdef _WIN32
# include <winnt.h>
# include <fileapi.h>
# include "nix/util/windows-error.hh"
#endif
namespace nix {

View File

@@ -7,7 +7,6 @@
#ifdef _WIN32
# include <fileapi.h>
# include "nix/util/file-path.hh"
# include "nix/util/windows-error.hh"
#endif
#include "util-config-private.hh"

View File

@@ -27,6 +27,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _WIN32
# include <errhandlingapi.h>
#endif
namespace nix {
@@ -288,25 +291,6 @@ public:
}
};
#ifdef _WIN32
namespace windows {
class WinError;
}
#endif
/**
* Convenience alias for when we use a `errno`-based error handling
* function on Unix, and `GetLastError()`-based error handling on on
* Windows.
*/
using NativeSysError =
#ifdef _WIN32
windows::WinError
#else
SysError
#endif
;
/**
* Throw an exception for the purpose of checking that exception
* handling works; see 'initLibUtil()'.
@@ -326,4 +310,67 @@ void panic(std::string_view msg);
*/
[[gnu::noinline, gnu::cold, noreturn]] void unreachable(std::source_location loc = std::source_location::current());
#ifdef _WIN32
namespace windows {
/**
* Windows Error type.
*
* Unless you need to catch a specific error number, don't catch this in
* portable code. Catch `SystemError` instead.
*/
class WinError : public SystemError
{
public:
DWORD lastError;
/**
* Construct using the explicitly-provided error number.
* `FormatMessageA` will be used to try to add additional
* information to the message.
*/
template<typename... Args>
WinError(DWORD lastError, const Args &... args)
: SystemError("")
, lastError(lastError)
{
auto hf = HintFmt(args...);
err.msg = HintFmt("%1%: %2%", Uncolored(hf.str()), renderError(lastError));
}
/**
* Construct using `GetLastError()` and the ambient "last error".
*
* Be sure to not perform another last-error-modifying operation
* before calling this constructor!
*/
template<typename... Args>
WinError(const Args &... args)
: WinError(GetLastError(), args...)
{
}
private:
std::string renderError(DWORD lastError);
};
} // namespace windows
#endif
/**
* Convenience alias for when we use a `errno`-based error handling
* function on Unix, and `GetLastError()`-based error handling on on
* Windows.
*/
using NativeSysError =
#ifdef _WIN32
windows::WinError
#else
SysError
#endif
;
} // namespace nix

View File

@@ -164,6 +164,13 @@ std::filesystem::path readLink(const std::filesystem::path & path);
*/
Descriptor openDirectory(const std::filesystem::path & path);
/**
* Open a `Descriptor` with read-only access to the given file.
*
* @note For directories use @ref openDirectory.
*/
Descriptor openFileReadonly(const std::filesystem::path & path);
/**
* Read the contents of a file into a string.
*/

View File

@@ -10,7 +10,6 @@
# include <poll.h>
#else
# include <ioapiset.h>
# include "nix/util/windows-error.hh"
#endif
namespace nix {

View File

@@ -4,6 +4,7 @@
#include "nix/util/memory-source-accessor.hh"
#include <functional>
#include <filesystem>
#include <nlohmann/json_fwd.hpp>
@@ -30,7 +31,7 @@ using GetNarBytes = std::function<std::string(uint64_t, uint64_t)>;
/**
* The canonical GetNarBytes function for a seekable Source.
*/
GetNarBytes seekableGetNarBytes(const Path & path);
GetNarBytes seekableGetNarBytes(const std::filesystem::path & path);
GetNarBytes seekableGetNarBytes(Descriptor fd);

View File

@@ -1,6 +1,7 @@
#include "nix/util/nar-accessor.hh"
#include "nix/util/file-descriptor.hh"
#include "nix/util/archive.hh"
#include "nix/util/error.hh"
#include <map>
#include <stack>
@@ -263,17 +264,11 @@ ref<SourceAccessor> makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes
return make_ref<NarAccessor>(source, getNarBytes);
}
GetNarBytes seekableGetNarBytes(const Path & path)
GetNarBytes seekableGetNarBytes(const std::filesystem::path & path)
{
AutoCloseFD fd = toDescriptor(open(
path.c_str(),
O_RDONLY
#ifdef O_CLOEXEC
| O_CLOEXEC
#endif
));
AutoCloseFD fd = openFileReadonly(path);
if (!fd)
throw SysError("opening NAR cache file '%s'", path);
throw NativeSysError("opening NAR cache file '%s'", path);
return [inner = seekableGetNarBytes(fd.get()), fd = make_ref<AutoCloseFD>(std::move(fd))](
uint64_t offset, uint64_t length) { return inner(offset, length); };

View File

@@ -13,7 +13,6 @@
#ifdef _WIN32
# include <fileapi.h>
# include "nix/util/windows-error.hh"
#else
# include <poll.h>
#endif

View File

@@ -27,6 +27,11 @@ Descriptor openDirectory(const std::filesystem::path & path)
return open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
}
Descriptor openFileReadonly(const std::filesystem::path & path)
{
return open(path.c_str(), O_RDONLY | O_CLOEXEC);
}
std::filesystem::path defaultTempDir()
{
return getEnvNonEmpty("TMPDIR").value_or("/tmp");

View File

@@ -1,5 +1,5 @@
#include "nix/util/current-process.hh"
#include "nix/util/windows-error.hh"
#include "nix/util/error.hh"
#include <cmath>
#ifdef _WIN32

View File

@@ -2,7 +2,6 @@
#include "nix/util/signals.hh"
#include "nix/util/finally.hh"
#include "nix/util/serialise.hh"
#include "nix/util/windows-error.hh"
#include "nix/util/file-path.hh"
#include <fileapi.h>

View File

@@ -1,5 +1,4 @@
#include "nix/util/file-system.hh"
#include "nix/util/windows-error.hh"
#include "nix/util/logging.hh"
namespace nix {
@@ -24,10 +23,22 @@ Descriptor openDirectory(const std::filesystem::path & path)
path.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
/*lpSecurityAttributes=*/nullptr,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
/*hTemplateFile=*/nullptr);
}
Descriptor openFileReadonly(const std::filesystem::path & path)
{
return CreateFileW(
path.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_DELETE,
/*lpSecurityAttributes=*/nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
/*hTemplateFile=*/nullptr);
}
std::filesystem::path defaultTempDir()

View File

@@ -5,5 +5,4 @@ include_dirs += include_directories('../..')
headers += files(
'signals-impl.hh',
'windows-async-pipe.hh',
'windows-error.hh',
)

View File

@@ -1,54 +0,0 @@
#pragma once
///@file
#ifdef _WIN32
# include <errhandlingapi.h>
# include "nix/util/error.hh"
namespace nix::windows {
/**
* Windows Error type.
*
* Unless you need to catch a specific error number, don't catch this in
* portable code. Catch `SystemError` instead.
*/
class WinError : public SystemError
{
public:
DWORD lastError;
/**
* Construct using the explicitly-provided error number.
* `FormatMessageA` will be used to try to add additional
* information to the message.
*/
template<typename... Args>
WinError(DWORD lastError, const Args &... args)
: SystemError("")
, lastError(lastError)
{
auto hf = HintFmt(args...);
err.msg = HintFmt("%1%: %2%", Uncolored(hf.str()), renderError(lastError));
}
/**
* Construct using `GetLastError()` and the ambient "last error".
*
* Be sure to not perform another last-error-modifying operation
* before calling this constructor!
*/
template<typename... Args>
WinError(const Args &... args)
: WinError(GetLastError(), args...)
{
}
private:
std::string renderError(DWORD lastError);
};
} // namespace nix::windows
#endif

View File

@@ -1,6 +1,5 @@
#ifdef _WIN32
# include <ioapiset.h>
# include "nix/util/windows-error.hh"
# include "nix/util/logging.hh"
# include "nix/util/util.hh"

View File

@@ -10,7 +10,6 @@
#include "nix/util/serialise.hh"
#include "nix/util/file-system.hh"
#include "nix/util/util.hh"
#include "nix/util/windows-error.hh"
#include <cerrno>
#include <cstdlib>

View File

@@ -2,7 +2,6 @@
#include "nix/util/users.hh"
#include "nix/util/environment-variables.hh"
#include "nix/util/file-system.hh"
#include "nix/util/windows-error.hh"
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN

View File

@@ -2,7 +2,6 @@
#ifdef _WIN32
# include "nix/util/windows-async-pipe.hh"
# include "nix/util/windows-error.hh"
namespace nix::windows {

View File

@@ -1,5 +1,7 @@
#ifdef _WIN32
# include "nix/util/windows-error.hh"
# include "nix/util/error.hh"
# include <error.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>