Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libstore/remote-fs-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not two += instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty equivalent. More of a matter of taste

return res;
}

ref<SourceAccessor> RemoteFSAccessor::addToCache(std::string_view hashPart, std::string && nar)
Expand Down
1 change: 0 additions & 1 deletion src/libstore/windows/pathlocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# include <errhandlingapi.h>
# include <fileapi.h>
# include <windows.h>
# include "nix/util/windows-error.hh"

namespace nix {

Expand Down
1 change: 0 additions & 1 deletion src/libutil/file-descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#ifdef _WIN32
# include <winnt.h>
# include <fileapi.h>
# include "nix/util/windows-error.hh"
#endif

namespace nix {
Expand Down
1 change: 0 additions & 1 deletion src/libutil/fs-sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
85 changes: 66 additions & 19 deletions src/libutil/include/nix/util/error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _WIN32
# include <errhandlingapi.h>
#endif

namespace nix {

Expand Down Expand Up @@ -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()'.
Expand All @@ -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
7 changes: 7 additions & 0 deletions src/libutil/include/nix/util/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git grep O_RDONLY makes me think this could perhaps be used in more places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, yeah.


/**
* Read the contents of a file into a string.
*/
Expand Down
1 change: 0 additions & 1 deletion src/libutil/include/nix/util/muxable-pipe.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# include <poll.h>
#else
# include <ioapiset.h>
# include "nix/util/windows-error.hh"
#endif

namespace nix {
Expand Down
3 changes: 2 additions & 1 deletion src/libutil/include/nix/util/nar-accessor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "nix/util/memory-source-accessor.hh"

#include <functional>
#include <filesystem>

#include <nlohmann/json_fwd.hpp>

Expand All @@ -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);

Expand Down
13 changes: 4 additions & 9 deletions src/libutil/nar-accessor.cc
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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); };
Expand Down
1 change: 0 additions & 1 deletion src/libutil/serialise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#ifdef _WIN32
# include <fileapi.h>
# include "nix/util/windows-error.hh"
#else
# include <poll.h>
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/libutil/unix/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/windows/current-process.cc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion src/libutil/windows/file-descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
17 changes: 14 additions & 3 deletions src/libutil/windows/file-system.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "nix/util/file-system.hh"
#include "nix/util/windows-error.hh"
#include "nix/util/logging.hh"

namespace nix {
Expand All @@ -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()
Expand Down
1 change: 0 additions & 1 deletion src/libutil/windows/include/nix/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ include_dirs += include_directories('../..')
headers += files(
'signals-impl.hh',
'windows-async-pipe.hh',
'windows-error.hh',
)
54 changes: 0 additions & 54 deletions src/libutil/windows/include/nix/util/windows-error.hh

This file was deleted.

1 change: 0 additions & 1 deletion src/libutil/windows/muxable-pipe.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/libutil/windows/processes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 0 additions & 1 deletion src/libutil/windows/users.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/libutil/windows/windows-async-pipe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#ifdef _WIN32
# include "nix/util/windows-async-pipe.hh"
# include "nix/util/windows-error.hh"

namespace nix::windows {

Expand Down
4 changes: 3 additions & 1 deletion src/libutil/windows/windows-error.cc
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
Loading