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
5 changes: 2 additions & 3 deletions src/libstore/gc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "nix/util/signals.hh"
#include "nix/util/serialise.hh"
#include "nix/util/util.hh"
#include "nix/util/file-system.hh"
#include "nix/store/posix-fs-canonicalise.hh"

#include "store-config-private.hh"
Expand Down Expand Up @@ -68,9 +69,7 @@ void LocalStore::createTempRootsFile()

/* Check whether the garbage collector didn't get in our
way. */
PosixStat st;
if (fstat(fromDescriptorReadOnly(fdTempRoots->get()), &st) == -1)
throw SysError("statting '%1%'", fnTempRoots);
auto st = nix::fstat(fromDescriptorReadOnly(fdTempRoots->get()));
if (st.st_size == 0)
break;

Expand Down
4 changes: 1 addition & 3 deletions src/libstore/unix/pathlocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ bool PathLocks::lockPaths(const std::set<std::filesystem::path> & paths, const s

/* Check that the lock file hasn't become stale (i.e.,
hasn't been unlinked). */
PosixStat st;
if (fstat(fd.get(), &st) == -1)
throw SysError("statting lock file %1%", PathFmt(lockPath));
auto st = nix::fstat(fd.get());
if (st.st_size != 0)
/* This lock file has been unlinked, so we're holding
a lock on a deleted file. This means that other
Expand Down
14 changes: 14 additions & 0 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ PosixStat lstat(const std::filesystem::path & path)
return st;
}

PosixStat fstat(int fd)
{
PosixStat st;
if (
#ifdef _WIN32
_fstat64
#else
::fstat
#endif
(fd, &st))
throw SysError("getting status of fd %d", fd);
return st;
}

std::optional<PosixStat> maybeStat(const std::filesystem::path & path)
{
std::optional<PosixStat> st{std::in_place};
Expand Down
4 changes: 1 addition & 3 deletions src/libutil/fs-sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ void RestoreRegularFile::isExecutable()
// Windows doesn't have a notion of executable file permissions we
// care about here, right?
#ifndef _WIN32
PosixStat st;
if (fstat(fd.get(), &st) == -1)
throw SysError("fstat");
auto st = nix::fstat(fd.get());
if (fchmod(fd.get(), st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
throw SysError("fchmod");
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/libutil/include/nix/util/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ PosixStat lstat(const std::filesystem::path & path);
* Get status of `path` following symlinks.
*/
PosixStat stat(const std::filesystem::path & path);
/**
* Get status of an open file descriptor.
*/
PosixStat fstat(int fd);
/**
* `lstat` the given path if it exists.
* @return std::nullopt if the path doesn't exist, or an optional containing the result of `lstat` otherwise
Expand Down
4 changes: 1 addition & 3 deletions src/libutil/posix-source-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ void PosixSourceAccessor::readFile(const CanonPath & path, Sink & sink, std::fun
if (!fd)
throw SysError("opening file '%1%'", ap.string());

PosixStat st;
if (fstat(fromDescriptorReadOnly(fd.get()), &st) == -1)
throw SysError("statting file");
auto st = nix::fstat(fromDescriptorReadOnly(fd.get()));
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, fromDescriptorReadOnly performs a destructive move out of HANDLE on windows and that HANDLE must not be used anymore or closed with CloseHandle. Instead it must use a the ::close from the C runtime, so this is kind of borked. A pre-existing issue, but still something to keep in mind :(

Copy link
Member

@Ericson2314 Ericson2314 Jan 28, 2026

Choose a reason for hiding this comment

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


sizeCallback(st.st_size);

Expand Down
5 changes: 1 addition & 4 deletions src/libutil/unix/file-descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ void pollFD(int fd, int events)

std::string readFile(int fd)
{
PosixStat st;
if (fstat(fd, &st) == -1)
throw SysError("statting file");

auto st = nix::fstat(fd);
return drainFD(fd, true, st.st_size);
}

Expand Down
Loading