Merge pull request #14970 from NixOS/more-openfile-readonly

Use openFileReadonly in more places
This commit is contained in:
John Ericson
2026-01-11 00:07:15 +00:00
committed by GitHub
4 changed files with 14 additions and 26 deletions

View File

@@ -252,21 +252,15 @@ Path readLink(const Path & path)
std::string readFile(const Path & path)
{
AutoCloseFD fd = toDescriptor(open(
path.c_str(),
O_RDONLY
#ifdef O_CLOEXEC
| O_CLOEXEC
#endif
));
if (!fd)
throw SysError("opening file '%1%'", path);
return readFile(fd.get());
return readFile(std::filesystem::path(path));
}
std::string readFile(const std::filesystem::path & path)
{
return readFile(os_string_to_string(PathViewNG{path}));
AutoCloseFD fd = openFileReadonly(path);
if (!fd)
throw NativeSysError("opening file %1%", path);
return readFile(fd.get());
}
void readFile(const Path & path, Sink & sink, bool memory_map)
@@ -285,15 +279,9 @@ void readFile(const Path & path, Sink & sink, bool memory_map)
}
// Stream the file instead if memory-mapping fails or is disabled.
AutoCloseFD fd = toDescriptor(open(
path.c_str(),
O_RDONLY
#ifdef O_CLOEXEC
| O_CLOEXEC
#endif
));
AutoCloseFD fd = openFileReadonly(std::filesystem::path(path));
if (!fd)
throw SysError("opening file '%s'", path);
throw NativeSysError("opening file %s", path);
drainFD(fd.get(), sink);
}

View File

@@ -268,7 +268,7 @@ GetNarBytes seekableGetNarBytes(const std::filesystem::path & path)
{
AutoCloseFD fd = openFileReadonly(path);
if (!fd)
throw NativeSysError("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

@@ -52,7 +52,7 @@ struct CmdCatStore : StoreCommand, MixCat
struct CmdCatNar : StoreCommand, MixCat
{
Path narPath;
std::filesystem::path narPath;
std::string path;
@@ -76,9 +76,9 @@ struct CmdCatNar : StoreCommand, MixCat
void run(ref<Store> store) override
{
AutoCloseFD fd = toDescriptor(open(narPath.c_str(), O_RDONLY));
AutoCloseFD fd = openFileReadonly(narPath);
if (!fd)
throw SysError("opening NAR file '%s'", narPath);
throw NativeSysError("opening NAR file %s", narPath);
auto source = FdSource{fd.get()};
struct CatRegularFileSink : NullFileSystemObjectSink

View File

@@ -126,7 +126,7 @@ struct CmdLsStore : StoreCommand, MixLs
struct CmdLsNar : Command, MixLs
{
Path narPath;
std::filesystem::path narPath;
std::string path;
@@ -150,9 +150,9 @@ struct CmdLsNar : Command, MixLs
void run() override
{
AutoCloseFD fd = toDescriptor(open(narPath.c_str(), O_RDONLY));
AutoCloseFD fd = openFileReadonly(narPath);
if (!fd)
throw SysError("opening NAR file '%s'", narPath);
throw NativeSysError("opening NAR file %s", narPath);
auto source = FdSource{fd.get()};
list(makeLazyNarAccessor(source, seekableGetNarBytes(fd.get())), CanonPath{path});
}