Merge pull request #10873 from siddhantk232/rm-createdirs

use `std::filesystem::create_directories` for createDirs
This commit is contained in:
Eelco Dolstra 2024-06-24 14:54:37 +02:00 committed by GitHub
commit 903acc7c0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 27 deletions

View file

@ -19,6 +19,7 @@
#include "signals.hh" #include "signals.hh"
#include "users.hh" #include "users.hh"
#include <filesystem>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using json = nlohmann::json;
@ -1303,7 +1304,7 @@ ref<Store> openStore(StoreReference && storeURI)
if (!pathExists(chrootStore)) { if (!pathExists(chrootStore)) {
try { try {
createDirs(chrootStore); createDirs(chrootStore);
} catch (Error & e) { } catch (SystemError & e) {
return std::make_shared<LocalStore>(params); return std::make_shared<LocalStore>(params);
} }
warn("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore); warn("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore);

View file

@ -413,30 +413,13 @@ void deletePath(const fs::path & path)
} }
Paths createDirs(const Path & path) void createDirs(const Path & path)
{ {
Paths created; try {
if (path == "/") return created; fs::create_directories(path);
} catch (fs::filesystem_error & e) {
struct stat st; throw SysError("creating directory '%1%'", path);
if (STAT(path.c_str(), &st) == -1) {
created = createDirs(dirOf(path));
if (mkdir(path.c_str()
#ifndef _WIN32 // TODO abstract mkdir perms for Windows
, 0777
#endif
) == -1 && errno != EEXIST)
throw SysError("creating directory '%1%'", path);
st = STAT(path);
created.push_back(path);
} }
if (S_ISLNK(st.st_mode) && stat(path.c_str(), &st) == -1)
throw SysError("statting symlink '%1%'", path);
if (!S_ISDIR(st.st_mode)) throw Error("'%1%' is not a directory", path);
return created;
} }

View file

@ -148,11 +148,10 @@ void deletePath(const std::filesystem::path & path);
void deletePath(const std::filesystem::path & path, uint64_t & bytesFreed); void deletePath(const std::filesystem::path & path, uint64_t & bytesFreed);
/** /**
* Create a directory and all its parents, if necessary. Returns the * Create a directory and all its parents, if necessary.
* list of created directories, in order of creation.
*/ */
Paths createDirs(const Path & path); void createDirs(const Path & path);
inline Paths createDirs(PathView path) inline void createDirs(PathView path)
{ {
return createDirs(Path(path)); return createDirs(Path(path));
} }