Merge pull request #11379 from nix-windows/nix-collect-garbage-std-filesystem-path

More `std::filesystem` for `nix-collect-garbage`
This commit is contained in:
John Ericson 2024-08-26 18:27:55 -04:00 committed by GitHub
commit 8af73f0a74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,6 +11,8 @@
#include <iostream> #include <iostream>
#include <cerrno> #include <cerrno>
namespace nix::fs { using namespace std::filesystem; }
using namespace nix; using namespace nix;
std::string deleteOlderThan; std::string deleteOlderThan;
@ -21,23 +23,23 @@ bool dryRun = false;
* Of course, this makes rollbacks to before this point in time * Of course, this makes rollbacks to before this point in time
* impossible. */ * impossible. */
void removeOldGenerations(std::filesystem::path dir) void removeOldGenerations(fs::path dir)
{ {
if (access(dir.string().c_str(), R_OK) != 0) return; if (access(dir.string().c_str(), R_OK) != 0) return;
bool canWrite = access(dir.string().c_str(), W_OK) == 0; bool canWrite = access(dir.string().c_str(), W_OK) == 0;
for (auto & i : std::filesystem::directory_iterator{dir}) { for (auto & i : fs::directory_iterator{dir}) {
checkInterrupt(); checkInterrupt();
auto path = i.path().string(); auto path = i.path().string();
auto type = i.symlink_status().type(); auto type = i.symlink_status().type();
if (type == std::filesystem::file_type::symlink && canWrite) { if (type == fs::file_type::symlink && canWrite) {
std::string link; std::string link;
try { try {
link = readLink(path); link = readLink(path);
} catch (std::filesystem::filesystem_error & e) { } catch (fs::filesystem_error & e) {
if (e.code() == std::errc::no_such_file_or_directory) continue; if (e.code() == std::errc::no_such_file_or_directory) continue;
throw; throw;
} }
@ -49,7 +51,7 @@ void removeOldGenerations(std::filesystem::path dir)
} else } else
deleteOldGenerations(path, dryRun); deleteOldGenerations(path, dryRun);
} }
} else if (type == std::filesystem::file_type::directory) { } else if (type == fs::file_type::directory) {
removeOldGenerations(path); removeOldGenerations(path);
} }
} }
@ -81,8 +83,11 @@ static int main_nix_collect_garbage(int argc, char * * argv)
}); });
if (removeOld) { if (removeOld) {
std::set<std::filesystem::path> dirsToClean = { std::set<fs::path> dirsToClean = {
profilesDir(), settings.nixStateDir + "/profiles", dirOf(getDefaultProfile())}; profilesDir(),
fs::path{settings.nixStateDir} / "profiles",
fs::path{getDefaultProfile()}.parent_path(),
};
for (auto & dir : dirsToClean) for (auto & dir : dirsToClean)
removeOldGenerations(dir); removeOldGenerations(dir);
} }