diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index a47df1692313..5ce8e98cd110 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -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); } diff --git a/src/libutil/nar-accessor.cc b/src/libutil/nar-accessor.cc index 7387007e63b1..4cc956bbb9b5 100644 --- a/src/libutil/nar-accessor.cc +++ b/src/libutil/nar-accessor.cc @@ -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(std::move(fd))]( uint64_t offset, uint64_t length) { return inner(offset, length); }; diff --git a/src/nix/cat.cc b/src/nix/cat.cc index dcf47f1fa2bf..5b2a309a6ef7 100644 --- a/src/nix/cat.cc +++ b/src/nix/cat.cc @@ -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) 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 diff --git a/src/nix/ls.cc b/src/nix/ls.cc index 66f52a18afc0..9918132a6fff 100644 --- a/src/nix/ls.cc +++ b/src/nix/ls.cc @@ -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}); }