disk-space-saviour 0.0.0-stage → 0.11.30

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/src/audit.js ADDED
@@ -0,0 +1,84 @@
1
+ /**
2
+ * JSON audit log written on every run (scan, clean, emergency), including
3
+ * dry runs, so every decision and every deleted byte can be traced later.
4
+ *
5
+ * Location: `auditDir` option, `$DSS_AUDIT_DIR`, `$XDG_STATE_HOME/
6
+ * disk-space-saviour/audit`, or `~/.local/state/disk-space-saviour/audit`.
7
+ * Container log backups default to a `backups` directory next to it.
8
+ */
9
+
10
+ import { promises as fsp } from 'node:fs';
11
+ import os from 'node:os';
12
+ import path from 'node:path';
13
+
14
+ export const AUDIT_SCHEMA = 1;
15
+
16
+ function stateDir(vars) {
17
+ const base =
18
+ vars.XDG_STATE_HOME || path.join(os.homedir(), '.local', 'state');
19
+ return path.join(base, 'disk-space-saviour');
20
+ }
21
+
22
+ /**
23
+ * @param {{auditDir?: string|null}} [options]
24
+ * @param {Record<string, string|undefined>} [vars] environment variables
25
+ */
26
+ export function auditDirectory(options = {}, vars = process.env) {
27
+ return (
28
+ options.auditDir || vars.DSS_AUDIT_DIR || path.join(stateDir(vars), 'audit')
29
+ );
30
+ }
31
+
32
+ /**
33
+ * Where `docker logs` and `docker inspect` backups of removed containers go.
34
+ */
35
+ export function backupDirectory(options = {}, vars = process.env) {
36
+ return (
37
+ options.backupDir ||
38
+ vars.DSS_BACKUP_DIR ||
39
+ path.join(stateDir(vars), 'backups')
40
+ );
41
+ }
42
+
43
+ /**
44
+ * Starts an audit record.
45
+ * @param {string} command `scan`, `clean`, `emergency`
46
+ * @param {object} [fields]
47
+ */
48
+ export function startAudit(command, fields = {}) {
49
+ return {
50
+ schema: AUDIT_SCHEMA,
51
+ tool: 'disk-space-saviour',
52
+ command,
53
+ pid: process.pid,
54
+ user: os.userInfo().username,
55
+ hostname: os.hostname(),
56
+ startedAt: new Date().toISOString(),
57
+ finishedAt: null,
58
+ dryRun: false,
59
+ entries: [],
60
+ ...fields,
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Writes the audit record, returning the file path.
66
+ * @param {object} audit
67
+ * @param {{auditDir?: string|null}} [options]
68
+ * @returns {Promise<string>}
69
+ */
70
+ export async function writeAudit(audit, options = {}) {
71
+ const dir = auditDirectory(options);
72
+ await fsp.mkdir(dir, { recursive: true });
73
+ if (!options.inProgress) {
74
+ audit.finishedAt ??= new Date().toISOString();
75
+ }
76
+ const stamp = audit.startedAt.replace(/[:.]/g, '-');
77
+ const file = path.join(
78
+ dir,
79
+ `dss-${audit.command}-${stamp}-${audit.pid}.json`
80
+ );
81
+ await fsp.writeFile(file, `${JSON.stringify(audit, null, 2)}\n`);
82
+ audit.file = file;
83
+ return file;
84
+ }