Skip to content

Commit

Permalink
Merge pull request NixOS#11379 from nix-windows/nix-collect-garbage-s…
Browse files Browse the repository at this point in the history
…td-filesystem-path

More `std::filesystem` for `nix-collect-garbage`
  • Loading branch information
Ericson2314 authored Aug 26, 2024
2 parents 2a1a26f + 8bce63f commit 8af73f0
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/nix-collect-garbage/nix-collect-garbage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <iostream>
#include <cerrno>

namespace nix::fs { using namespace std::filesystem; }

using namespace nix;

std::string deleteOlderThan;
Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 8af73f0

Please sign in to comment.