-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathportable-data-paths.js
More file actions
211 lines (185 loc) · 7.2 KB
/
Copy pathportable-data-paths.js
File metadata and controls
211 lines (185 loc) · 7.2 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
const fs = require('fs');
const path = require('path');
const PORTABLE_DATA_FOLDER_NAME = 'ElectronCapture-data';
const PORTABLE_PROFILE_MARKER_NAME = '.profile-initialized.json';
const PORTABLE_COPY_REQUEST_NAME = '.copy-profile-on-next-start.json';
const MIGRATION_EXCLUDED_NAMES = new Set([
'SingletonLock',
'SingletonSocket',
'SingletonCookie',
'DevTools Active Port',
'LOCK',
'Cache',
'Code Cache',
'GPUCache',
'DawnCache',
'ShaderCache',
'GrShaderCache',
'Crashpad',
]);
const PORTABLE_DATA_README = `Electron Capture portable data
================================
This folder contains the portable app's settings, browser sessions, cache, logs, and crash reports.
Keep this folder beside elecap.exe. You can replace the EXE during an update without losing these
files, or move the EXE and this folder together.
Important limitations:
- Saved sign-ins protected by Windows may require you to sign in again after moving to another computer or Windows account.
- Local media and custom file selections still point to their original locations. Move those files separately or relink them.
- Windows itself may keep system-level records such as Defender scans or Prefetch entries outside this folder.
`;
function normalizeEnvironmentPath(value) {
const normalized = String(value || '').trim();
return normalized ? path.resolve(normalized) : '';
}
function resolveEarlyDataPaths(environment = process.env, platform = process.platform) {
const explicitUserDataDir = normalizeEnvironmentPath(environment.ELECTRON_CAPTURE_USER_DATA_DIR);
if (explicitUserDataDir) {
return {
mode: 'explicit',
dataRoot: explicitUserDataDir,
userData: explicitUserDataDir,
sessionData: explicitUserDataDir,
logs: path.join(explicitUserDataDir, 'logs'),
crashes: path.join(explicitUserDataDir, 'crashes'),
};
}
const portableExecutableDir = normalizeEnvironmentPath(environment.PORTABLE_EXECUTABLE_DIR);
if (platform !== 'win32' || !portableExecutableDir) return null;
const dataRoot = path.join(portableExecutableDir, PORTABLE_DATA_FOLDER_NAME);
const profile = path.join(dataRoot, 'profile');
return {
mode: 'portable',
dataRoot,
userData: profile,
sessionData: profile,
logs: path.join(dataRoot, 'logs'),
crashes: path.join(dataRoot, 'crashes'),
};
}
function verifyDirectoryWritable(directory) {
const probePath = path.join(
directory,
`.electron-capture-write-test-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
);
let descriptor = null;
try {
descriptor = fs.openSync(probePath, 'wx');
} finally {
if (descriptor !== null) fs.closeSync(descriptor);
try {
fs.unlinkSync(probePath);
} catch (_) { }
}
}
function writePortableReadme(dataRoot) {
const readmePath = path.join(dataRoot, 'README.txt');
try {
fs.writeFileSync(readmePath, PORTABLE_DATA_README, { encoding: 'utf8', flag: 'wx' });
} catch (error) {
if (!error || error.code !== 'EEXIST') throw error;
}
}
function directoryHasData(directory) {
try {
return fs.readdirSync(directory).length > 0;
} catch (_) {
return false;
}
}
function assertPortableProfileTarget(paths) {
if (!paths || paths.mode !== 'portable') {
throw new Error('Portable profile operations require portable data paths.');
}
const expectedProfile = path.resolve(paths.dataRoot, 'profile');
const actualProfile = path.resolve(paths.userData);
if (actualProfile !== expectedProfile || actualProfile === path.parse(actualProfile).root) {
throw new Error(`Refusing to replace unexpected portable profile path: ${actualProfile}`);
}
}
function copyLegacyProfile(sourceDirectory, destinationDirectory) {
for (const entry of fs.readdirSync(sourceDirectory, { withFileTypes: true })) {
if (MIGRATION_EXCLUDED_NAMES.has(entry.name)) continue;
const sourcePath = path.join(sourceDirectory, entry.name);
const destinationPath = path.join(destinationDirectory, entry.name);
fs.cpSync(sourcePath, destinationPath, {
recursive: true,
force: true,
filter: (candidate) => !MIGRATION_EXCLUDED_NAMES.has(path.basename(candidate)),
});
}
}
function markPortableProfileInitialized(paths, action) {
const markerPath = path.join(paths.dataRoot, PORTABLE_PROFILE_MARKER_NAME);
fs.writeFileSync(markerPath, JSON.stringify({
action,
initializedAt: new Date().toISOString(),
}, null, 2));
}
function writePortableCopyRequest(paths) {
assertPortableProfileTarget(paths);
const requestPath = path.join(paths.dataRoot, PORTABLE_COPY_REQUEST_NAME);
fs.writeFileSync(requestPath, JSON.stringify({ requestedAt: new Date().toISOString() }, null, 2));
}
function consumePortableCopyRequest(paths, legacyUserData) {
if (!paths || paths.mode !== 'portable') return { action: 'not-portable' };
const requestPath = path.join(paths.dataRoot, PORTABLE_COPY_REQUEST_NAME);
if (!fs.existsSync(requestPath)) return { action: 'none' };
assertPortableProfileTarget(paths);
const sourceDirectory = normalizeEnvironmentPath(legacyUserData);
if (!sourceDirectory || path.resolve(sourceDirectory) === path.resolve(paths.userData) || !directoryHasData(sourceDirectory)) {
fs.unlinkSync(requestPath);
markPortableProfileInitialized(paths, 'copy-source-missing');
return { action: 'copy-source-missing' };
}
fs.rmSync(paths.userData, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
fs.mkdirSync(paths.userData, { recursive: true });
copyLegacyProfile(sourceDirectory, paths.userData);
markPortableProfileInitialized(paths, 'copy');
fs.unlinkSync(requestPath);
return { action: 'copy', legacyUserData: sourceDirectory };
}
function initializePortableProfile(paths, options = {}) {
if (!paths || paths.mode !== 'portable') return { action: 'not-portable' };
const markerPath = path.join(paths.dataRoot, PORTABLE_PROFILE_MARKER_NAME);
if (fs.existsSync(markerPath)) return { action: 'already-initialized' };
if (directoryHasData(paths.userData)) {
markPortableProfileInitialized(paths, 'existing');
return { action: 'existing' };
}
const legacyUserData = normalizeEnvironmentPath(options.legacyUserData);
if (!legacyUserData || path.resolve(legacyUserData) === path.resolve(paths.userData) || !directoryHasData(legacyUserData)) {
markPortableProfileInitialized(paths, 'new');
return { action: 'new' };
}
const requestedChoice = String(options.choice || '').trim().toLowerCase();
if (requestedChoice !== 'copy' && requestedChoice !== 'fresh') {
return { action: 'pending', legacyUserData };
}
if (requestedChoice === 'copy') {
copyLegacyProfile(legacyUserData, paths.userData);
}
markPortableProfileInitialized(paths, requestedChoice);
return { action: requestedChoice, legacyUserData };
}
function prepareEarlyDataPaths(paths) {
if (!paths) return;
const directories = [...new Set([paths.dataRoot, paths.userData, paths.sessionData, paths.logs, paths.crashes])];
for (const directory of directories) {
fs.mkdirSync(directory, { recursive: true });
verifyDirectoryWritable(directory);
}
if (paths.mode === 'portable') writePortableReadme(paths.dataRoot);
}
module.exports = {
PORTABLE_DATA_FOLDER_NAME,
PORTABLE_PROFILE_MARKER_NAME,
PORTABLE_COPY_REQUEST_NAME,
resolveEarlyDataPaths,
prepareEarlyDataPaths,
initializePortableProfile,
copyLegacyProfile,
markPortableProfileInitialized,
writePortableCopyRequest,
consumePortableCopyRequest,
};