real-size 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -71,7 +71,7 @@ prints the version and the binary's platform, for example `0.1.0
71
71
  ## Command line
72
72
 
73
73
  ```text
74
- real-size [--json] <path>...
74
+ real-size [--json] [--no-progress] <path>...
75
75
  ```
76
76
 
77
77
  Each path is measured on its own. Plain output prints one line per path, and a
@@ -97,23 +97,59 @@ that is not exact.
97
97
  "allocated": 4767227904,
98
98
  "private": 90292224,
99
99
  "files": 168433,
100
- "exact": false
100
+ "exact": false,
101
+ "skipped": 0
101
102
  }
102
103
  ],
103
104
  "total": {
104
105
  "allocated": 4767227904,
105
106
  "private": 90292224,
106
107
  "files": 168433,
107
- "exact": false
108
+ "exact": false,
109
+ "skipped": 0
108
110
  }
109
111
  }
110
112
  ```
111
113
 
112
- | Exit status | Meaning |
113
- | ----------- | ------------------------------------------------------------------------------------- |
114
- | 0 | Every path was measured. |
115
- | 1 | A usage error, or a path could not be read. The reason is on stderr, stdout is empty. |
116
- | 2 | The filesystem cannot report what deleting a path would free. Stdout is empty. |
114
+ ### Errors and exit status
115
+
116
+ An entry below a path that cannot be read, such as a directory without read
117
+ permission, is skipped and the walk goes on. Each one is named on stderr with
118
+ its own path, relative to the path as given, and the reason, up to ten lines;
119
+ the rest are counted in a last line:
120
+
121
+ ```console
122
+ $ real-size ~/Library
123
+ real-size: /Users/me/Library/Mail: Operation not permitted
124
+ real-size: and 3 more entries could not be read
125
+ /Users/me/Library: at least 2.1 GB private, 9.8 GB allocated, 210377 files (4 entries could not be read)
126
+ ```
127
+
128
+ A volume mounted inside the tree whose filesystem cannot report what deleting
129
+ it would free, for example a FAT or HFS+ disk image or an FSKit volume on
130
+ macOS, is skipped the same way, as one entry, with the reason "another
131
+ filesystem that cannot report private size". Nothing in it is counted.
132
+
133
+ The sizes are then lower bounds: `skipped` counts the entries left out and
134
+ `exact` is `false`. Like `du`, the command prints the result and exits with
135
+ status 1. An entry deleted while the walk runs is not an error: it frees
136
+ nothing, so it is left out without a message and does not count in `skipped`.
137
+
138
+ | Exit status | Meaning |
139
+ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
140
+ | 0 | Every path was measured in full. |
141
+ | 1 | Some entries could not be read: the result is on stdout with `skipped` above 0. Or a usage error, or a path itself could not be read: the reason is on stderr, stdout is empty. |
142
+ | 2 | The filesystem a path itself is on cannot report what deleting it would free. Stdout is empty. |
143
+
144
+ With exit status 1, a result on stdout tells the two cases apart.
145
+
146
+ ### Progress
147
+
148
+ When stderr is a terminal and a walk has run for more than two seconds,
149
+ `real-size` shows one status line on stderr with the files counted so far and
150
+ the directory it is in, and clears it before printing the result. It shows
151
+ nothing when stderr is not a terminal, so pipes, scripts and the library never
152
+ see it. `--no-progress` turns it off.
117
153
 
118
154
  `--version` prints the version and `--help` prints usage.
119
155
 
@@ -136,8 +172,12 @@ the promise rejects at once. It rejects with a `RealSizeError` whose `code` is:
136
172
 
137
173
  - `unsupported`: no prebuilt binary for this platform, or exit status 2.
138
174
  - `timeout`: `timeoutMs` ran out.
139
- - `failed`: exit status 1 or the binary could not start. `message` holds the
140
- binary's error.
175
+ - `failed`: exit status 1 without a result (a path that cannot be read) or the
176
+ binary could not start. `message` holds the binary's error.
177
+
178
+ When some entries below a path could not be read, the promise resolves with
179
+ the result and `total.skipped` above 0, rather than rejecting. Version 0.1.0
180
+ rejected the whole measurement instead.
141
181
 
142
182
  An aborted `signal` rejects with the signal's reason.
143
183
 
@@ -155,6 +195,9 @@ TypeScript types are included. Node `^20.19.4 || >=22.12.0`.
155
195
  - `files`: the regular files counted, each hard-linked file once.
156
196
  - `exact`: `true` when `private` is the exact answer, `false` when it is a
157
197
  lower bound.
198
+ - `skipped`: entries that could not be read and were left out, including
199
+ volumes mounted inside the tree that cannot report private sizes. Above 0,
200
+ every size is a lower bound and `exact` is `false`. Added in 0.1.1.
158
201
 
159
202
  Directories, symbolic links and metadata are not counted. Symbolic links are
160
203
  never followed, and neither are Windows junctions.
@@ -189,8 +232,17 @@ distributions alike.
189
232
 
190
233
  The walk uses `getattrlistbulk` and reads each file's
191
234
  `ATTR_CMNEXT_PRIVATESIZE`, which APFS reports as the bytes the file does not
192
- share with a clone. On a volume that does not report it (HFS+, FAT, SMB) the
193
- exit status is 2. The walk descends into volumes mounted inside the tree.
235
+ share with a clone. Only APFS reports it: on another volume format (HFS+, FAT,
236
+ SMB, FSKit volumes) the exit status is 2 before anything is walked. The walk
237
+ descends into volumes mounted inside the tree. Another APFS volume is measured
238
+ like the rest; any other volume is skipped as one entry and nothing in it is
239
+ counted.
240
+
241
+ Directories are listed on several threads, one per CPU and at most 4; past 4
242
+ the walk got slower on the machines measured. Each directory is opened by its
243
+ path when a thread takes it, so a parent directory replaced by a symbolic link
244
+ during the walk is followed, and the directories queued below a parent renamed
245
+ during the walk are left out. Linux and Windows walk on one thread.
194
246
 
195
247
  APFS also reports a private size below the allocation for transparently
196
248
  compressed files (decmpfs, for example files installed by some system tools),
@@ -212,8 +264,10 @@ the tree:
212
264
  mapping fails, counts as entirely shared: it adds to `allocated`, nothing to
213
265
  `private`, and sets `exact: false`.
214
266
  - Any other filesystem (overlayfs, NFS, ZFS, bcachefs, FUSE mounts, sysfs
215
- and so on) exits with status 2, because it may share blocks in ways the
216
- walk cannot see. Container users meet this first: a Docker container's root
267
+ and so on) cannot tell, because it may share blocks in ways the walk cannot
268
+ see. When the measured path is on one, the exit status is 2. When one is
269
+ mounted inside the tree, it is skipped as one entry and nothing in it is
270
+ counted. Container users meet this first: a Docker container's root
217
271
  is overlayfs, while a bind-mounted host folder on ext4 works.
218
272
 
219
273
  ### Windows
@@ -261,9 +315,9 @@ runner are in [docs/benchmarks.md](docs/benchmarks.md).
261
315
 
262
316
  | Machine | Filesystem | Tree | real-size | Baseline | Ratio |
263
317
  | ------------------------------ | ---------- | ------------------------------------- | --------- | -------- | ----- |
264
- | Apple M4 Pro, 14 cores | APFS | pnpm `node_modules`, 168,433 files | 1.01 s | 876.4 ms | 1.15x |
265
- | Apple M4 Pro | APFS | 30,000 files hard-linked from a store | 184.4 ms | 123.8 ms | 1.49x |
266
- | Apple M4 Pro | APFS | 117,186 small files, 7 levels | 754.7 ms | 551.9 ms | 1.37x |
318
+ | Apple M4 Pro, 14 cores | APFS | pnpm `node_modules`, 147,077 files | 482.6 ms | 864.7 ms | 0.56x |
319
+ | Apple M4 Pro | APFS | 30,000 files hard-linked from a store | 148.1 ms | 138.0 ms | 1.07x |
320
+ | Apple M4 Pro | APFS | 117,186 small files, 7 levels | 579.5 ms | 618.7 ms | 0.94x |
267
321
  | GitHub ubuntu-latest, 4 cores | ext4 | 30,000 hard-linked files | 91.4 ms | 123.8 ms | 0.74x |
268
322
  | GitHub ubuntu-latest | ext4 | 117,186 small files | 352.0 ms | 594.5 ms | 0.59x |
269
323
  | GitHub ubuntu-latest | btrfs | 117,186 small files | 1.47 s | 619.8 ms | 2.37x |
@@ -279,11 +333,10 @@ on APFS, the extent map on btrfs and XFS, and the link count on Windows.
279
333
  - Each path is measured on its own and the total is their sum. Two paths that
280
334
  hard-link or clone each other's files each report those bytes as shared, so
281
335
  the total can be lower than what deleting both would free.
282
- - The macOS and Linux walks stop at 256 nested directories and the Windows
283
- walk at 256 as well; deeper trees exit with status 1.
284
- - A path that cannot be read, such as a directory without read permission,
285
- exits with status 1 rather than reporting a partial size.
286
- - Files changed during the walk may be counted in either state.
336
+ - The walks stop at 256 nested directories; a deeper directory is skipped and
337
+ counted in `skipped`.
338
+ - Files changed during the walk may be counted in either state. On macOS a
339
+ parent directory replaced by a symbolic link during the walk is followed.
287
340
  - The plain output's sizes are rounded; use `--json` for bytes.
288
341
  - On Linux, filesystems other than ext2/3/4, tmpfs, btrfs and XFS exit with
289
342
  status 2 even where an answer could be given, for example ZFS without
package/index.d.mts CHANGED
@@ -5,8 +5,17 @@ export interface TreeSize {
5
5
  private: number;
6
6
  /** Regular files, each counted once however many links it has in the tree. */
7
7
  files: number;
8
- /** False when `private` is a lower bound because some counted file shares blocks with another file. */
8
+ /**
9
+ * False when `private` is a lower bound: some counted file shares blocks with another file, or some entries could
10
+ * not be read (`skipped` above 0).
11
+ */
9
12
  exact: boolean;
13
+ /**
14
+ * Entries the walk could not read, for example a directory without read permission, and left out of every other
15
+ * field. When above 0 every size is a lower bound; the binary names the entries on stderr and exits with status 1.
16
+ * Entries deleted during the walk are not counted here.
17
+ */
18
+ skipped: number;
10
19
  }
11
20
 
12
21
  export interface PathSize extends TreeSize {
@@ -38,15 +47,19 @@ export declare class RealSizeError extends Error {
38
47
  readonly name: "RealSizeError";
39
48
  /**
40
49
  * `unsupported`: no prebuilt binary for this platform, or the filesystem cannot report the bytes deleting the path
41
- * would free (exit status 2). `timeout`: `timeoutMs` ran out. `failed`: a usage or I/O error (exit status 1), or the
42
- * binary could not run.
50
+ * would free (exit status 2). `timeout`: `timeoutMs` ran out. `failed`: a usage error or a path that cannot be read (exit
51
+ * status 1 without a result), or the binary could not run.
43
52
  */
44
53
  readonly code: RealSizeErrorCode;
45
54
  /** The binary's exit status, null when it did not exit on its own. */
46
55
  readonly exitCode: number | null;
47
56
  }
48
57
 
49
- /** Measures each directory in its own walk, run in a child process. */
58
+ /**
59
+ * Measures each directory in its own walk, run in a child process. Resolves when every path was measured, including
60
+ * when some entries below a path could not be read (`total.skipped` above 0). Rejects when a path itself cannot be
61
+ * read.
62
+ */
50
63
  export declare function realSize(
51
64
  paths: string | readonly string[],
52
65
  options?: RealSizeOptions,
package/index.mjs CHANGED
@@ -91,8 +91,13 @@ export function realSize(paths, options = {}) {
91
91
  );
92
92
  child.on("close", (exitCode, exitSignal) =>
93
93
  finish(() => {
94
- const message = stderr.trim().replace(/^real-size: /, "");
95
- if (exitCode === 0) {
94
+ const message = (stderr.trim().split(/\r?\n/).at(-1) ?? "").replace(
95
+ /^real-size: /,
96
+ "",
97
+ );
98
+ const partial = exitCode === 1 ? parsePartial(stdout) : null;
99
+ if (partial) resolve(partial);
100
+ else if (exitCode === 0) {
96
101
  try {
97
102
  resolve(JSON.parse(stdout));
98
103
  } catch (error) {
@@ -117,3 +122,17 @@ export function realSize(paths, options = {}) {
117
122
  );
118
123
  });
119
124
  }
125
+
126
+ /**
127
+ * Exit status 1 with a document on stdout means some entries could not be read and were skipped.
128
+ * @param {string} stdout
129
+ * @returns {import("./index.d.mts").RealSizeResult | null}
130
+ */
131
+ function parsePartial(stdout) {
132
+ try {
133
+ const result = JSON.parse(stdout);
134
+ return result?.total?.skipped > 0 ? result : null;
135
+ } catch {
136
+ return null;
137
+ }
138
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "real-size",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "The bytes deleting a folder would actually free, on APFS clones, hard links and reflinks",
5
5
  "type": "module",
6
6
  "engines": {
@@ -48,7 +48,7 @@
48
48
  "license": "MIT",
49
49
  "scripts": {
50
50
  "build": "node scripts/build.mjs",
51
- "test": "node --test --test-concurrency=1 test/cli.test.mjs test/api.test.mjs test/launcher.test.mjs test/darwin.test.mjs test/linux.test.mjs test/windows.test.mjs test/package.test.mjs",
51
+ "test": "node --test --test-concurrency=1 test/cli.test.mjs test/errors.test.mjs test/api.test.mjs test/launcher.test.mjs test/darwin.test.mjs test/linux.test.mjs test/windows.test.mjs test/package.test.mjs",
52
52
  "test:package": "node scripts/package-smoke.mjs",
53
53
  "pack:release": "node scripts/pack-release.mjs",
54
54
  "typecheck": "tsc",
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/skill/SKILL.md CHANGED
@@ -19,9 +19,14 @@ Fields:
19
19
  - `allocated`: bytes the tree's files occupy on disk, each file counted once.
20
20
  - `private`: bytes deleting the tree would free. Report this one as reclaimable.
21
21
  - `files`: regular files counted.
22
- - `exact`: `false` means `private` is a lower bound: some file shares blocks with another file, possibly one inside the same tree, so deleting the whole tree may free more. Say "at least".
22
+ - `exact`: `false` means `private` is a lower bound: some file shares blocks with another file, possibly one inside the same tree, or some entries could not be read. Deleting the whole tree may free more. Say "at least".
23
+ - `skipped`: entries that could not be read (no permission, an I/O error, nesting deeper than 256 directories) and were left out of every number.
23
24
 
24
- Exit status 2 means the filesystem cannot report this (for example NFS, overlayfs, a network share, or a non-APFS volume on macOS). Then say real-size cannot tell on this filesystem, fall back to `du -sh`, and present that number as an upper bound. Exit status 1 is a usage or I/O error, with the reason on stderr.
25
+ Exit status 1 with a result on stdout means some entries were skipped: stderr names each one with its reason (the first ten, then a count). Still use the result: report `private` as "at least", give the number of entries that could not be read and the paths stderr names, and suggest rerunning with the needed permissions if the user wants the full figure. Exit status 1 with nothing on stdout is a usage error or a path that cannot be read at all, with the reason on stderr.
26
+
27
+ Exit status 2 means the filesystem the path is on cannot report this (for example NFS, overlayfs, a network share, or a non-APFS volume on macOS). Then say real-size cannot tell on this filesystem, fall back to `du -sh`, and present that number as an upper bound. A volume like that mounted inside the folder does not cause exit 2: it is one of the skipped entries of exit status 1, named on stderr as "another filesystem that cannot report private size", and nothing in it is counted.
28
+
29
+ Entries deleted while the walk runs are left out without a message; they free nothing.
25
30
 
26
31
  Example, a pnpm worktree's `node_modules` on macOS:
27
32