Skip to content

Commit 6b18723

Browse files
nix store gc: account for auto-optimised store
Before the change on a system with `auto-optimise-store = true`: $ nix store gc --verbose --max 1 deleted all the paths instead of one path (we requested 1 byte limit). It happens because every file in `auto-optimise-store = true` has at least 2 links: file itself and a link in /nix/store/.links/ directory. The change conservatively assumes that any file that has one (as before) or two links (assume auto-potimise mode) will free space. Co-authored-by: Sandro <sandro.jaeckel@gmail.com>
1 parent d532269 commit 6b18723

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/libstore/gc.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
841841
if (unlink(path.c_str()) == -1)
842842
throw SysError("deleting '%1%'", path);
843843

844-
results.bytesFreed += st.st_size;
844+
/* Do not accound for deleted file here. Rely on deletePath()
845+
accounting. */
845846
}
846847

847848
struct stat st;

src/libutil/util.cc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,29 @@ static void _deletePath(int parentfd, const Path & path, uint64_t & bytesFreed)
406406
throw SysError("getting status of '%1%'", path);
407407
}
408408

409-
if (!S_ISDIR(st.st_mode) && st.st_nlink == 1)
410-
bytesFreed += st.st_size;
409+
if (!S_ISDIR(st.st_mode)) {
410+
/* We are about to delete a file. Will it likely free space? */
411+
412+
switch (st.st_nlink) {
413+
/* Yes: last link. */
414+
case 1:
415+
bytesFreed += st.st_size;
416+
break;
417+
/* Maybe: yes, if 'auto-optimise-store' or manual optimisation
418+
was performed. Instead of checking for real let's assume
419+
it's an optimised file and space will be freed.
420+
421+
In worst case we will double count on freed space for files
422+
with exactly two hardlinks for unoptimised packages.
423+
*/
424+
case 2:
425+
bytesFreed += st.st_size;
426+
break;
427+
/* No: 3+ links. */
428+
default:
429+
break;
430+
}
431+
}
411432

412433
if (S_ISDIR(st.st_mode)) {
413434
/* Make the directory accessible. */

0 commit comments

Comments
 (0)