| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 |
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const fp = require('./flatpak');
const ar = require('./archive');
const { cleanEntries } = require('./applist');
function sourceDistro() {
try {
const text = fs.readFileSync('/etc/os-release', 'utf8');
const match = text.match(/^PRETTY_NAME="?(.*?)"?$/m);
if (match) return match[1];
} catch { }
return `${os.type()} ${os.release()}`;
}
function packName(hostname) {
const date = new Date().toISOString().slice(0, 10);
const safeHost = (hostname || 'machine').replace(/[^a-zA-Z0-9._-]/g, '-');
return `flatpak-backup-${safeHost}-${date}.fmpack`;
}
async function runBackup({ apps, list = [], outFile }, onProgress) {
const emit = (payload) => { if (onProgress) onProgress(payload); };
const total = apps.length;
const compressor = await ar.detectCompressor();
const stage = ar.makeStage(outFile, 'backup');
const log = [];
const record = (level, message) => { log.push({ level, message }); };
try {
fs.mkdirSync(path.join(stage, 'apps'), { recursive: true });
fs.mkdirSync(path.join(stage, 'overrides', 'app'), { recursive: true });
const manifestApps = [];
for (let index = 0; index < apps.length; index += 1) {
const app = apps[index];
const blobName = `apps/${app.id}.${compressor.ext}`;
const blobFile = path.join(stage, blobName);
emit({ index, total, appId: app.id, step: 'packing', message: `Packing ${app.name}` });
const full = Boolean(app.full);
const leaveOut = full ? [] : fp.throwawayPaths(fp.dataDir(app.id));
const packed = await ar.packAppData({
id: app.id,
parentDir: fp.VAR_APP,
outFile: blobFile,
full,
leaveOut,
});
if (!packed.ok) {
record('error', `${app.name}: ${packed.error}`);
emit({ index, total, appId: app.id, step: 'failed', message: packed.error });
continue;
}
if (packed.warning) {
record('warning', `${app.name}: ${packed.warning}. Close it and back up again if the restored copy misbehaves.`);
}
emit({ index, total, appId: app.id, step: 'hashing', message: `Checksumming ${app.name}` });
const sha256 = await ar.sha256File(blobFile);
const blobBytes = fs.statSync(blobFile).size;
const override = fp.readOverrideFile(app.id);
if (override) {
fs.writeFileSync(path.join(stage, 'overrides', 'app', app.id), override.text, 'utf8');
}
const permissionsRaw = await fp.showPermissions(app.id);
manifestApps.push({
id: app.id,
name: app.name,
branch: app.branch,
arch: app.arch,
origin: app.origin,
scope: app.scope,
data_bytes: app.dataBytes || 0,
blob: blobName,
blob_bytes: blobBytes,
sha256,
full,
cache_included: full,
left_out: leaveOut.length,
has_overrides: Boolean(override),
overrides_scope: override ? override.scope : null,
permissions: fp.permissionsToArgs(permissionsRaw),
permissions_raw: permissionsRaw,
});
emit({ index, total, appId: app.id, step: 'done', message: `${app.name} packed` });
}
const globalOverride = fp.readGlobalOverride();
if (globalOverride) {
fs.writeFileSync(path.join(stage, 'overrides', 'global'), globalOverride, 'utf8');
}
const packed = new Set(manifestApps.map((a) => a.id));
const carried = cleanEntries([
...list,
...apps.map((a) => ({ id: a.id, name: a.name, remote: a.origin })),
]).map((e) => ({ ...e, keep: packed.has(e.id) }));
const flatpakInfo = await fp.probe();
const manifest = {
format_version: ar.FORMAT_VERSION,
created: new Date().toISOString(),
source_host: os.hostname(),
source_distro: sourceDistro(),
flatpak_version: flatpakInfo.version,
compression: compressor.name,
has_global_override: Boolean(globalOverride),
remotes: await fp.listRemotes(),
list: carried,
apps: manifestApps,
};
fs.writeFileSync(path.join(stage, 'manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`, 'utf8');
emit({ index: total, total, appId: null, step: 'sealing', message: 'Writing the backup file' });
const sealed = await ar.sealPack({ stageDir: stage, outFile });
if (!sealed.ok) {
return { ok: false, error: sealed.error, log };
}
const bytes = fs.statSync(outFile).size;
return { ok: true, file: outFile, bytes, apps: manifestApps.length, log };
} catch (error) {
return { ok: false, error: error.message, log };
} finally {
ar.removeStage(stage);
}
}
module.exports = { runBackup, packName }; |