1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-16 09:50:21 -04:00

More std::filesystem for nix-collect-garbage

Co-Authored-By: siddhantCodes <siddhantk232@gmail.com>
This commit is contained in:
John Ericson 2024-08-26 16:44:19 -04:00
parent a97a08411c
commit 8bce63f30a

View file

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