mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-23 08:15:29 +00:00
Merge reviewed PR #3952: preserve recordings when fallback writes fail
This commit is contained in:
3 files changed
+177
-4
No files matched your search
@@ -51,6 +51,15 @@ function renameAtomic(tempPath, resolvedPath, options) {
|
||||
}
|
||||
}
|
||||
|
||||
function readExistingFileMode(filePath) {
|
||||
try {
|
||||
return fs.statSync(filePath).mode & 0o777;
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') return undefined;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function writeFileAtomic(filePath, content, options = {}) {
|
||||
const resolvedPath = path.resolve(filePath);
|
||||
const parentDir = path.dirname(resolvedPath);
|
||||
@@ -66,9 +75,12 @@ function writeFileAtomic(filePath, content, options = {}) {
|
||||
let descriptor;
|
||||
try {
|
||||
if (options.validateParent) options.validateParent();
|
||||
descriptor = fs.openSync(tempPath, 'wx', mode);
|
||||
const existingMode = options.preserveExistingMode ? readExistingFileMode(resolvedPath) : undefined;
|
||||
descriptor = fs.openSync(tempPath, 'wx', existingMode ?? mode);
|
||||
if (options.validateParent) options.validateParent();
|
||||
fs.writeFileSync(descriptor, content, { encoding: options.encoding || 'utf8' });
|
||||
// Restore existing permissions after writing, without applying the process umask a second time.
|
||||
if (existingMode !== undefined) fs.fchmodSync(descriptor, existingMode);
|
||||
fs.fsyncSync(descriptor);
|
||||
fs.closeSync(descriptor);
|
||||
descriptor = undefined;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const { readFileWithSharingRetry, writeFileAtomic } = require('../atomic-write');
|
||||
|
||||
const SESSION_SCHEMA_VERSION = 'ecc.session.v1';
|
||||
const SESSION_RECORDING_SCHEMA_VERSION = 'ecc.session.recording.v1';
|
||||
@@ -290,7 +291,7 @@ function readExistingRecording(filePath) {
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
return JSON.parse(readFileWithSharingRetry(filePath, 'utf8'));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
@@ -319,8 +320,10 @@ function writeFallbackSessionRecording(snapshot, options = {}) {
|
||||
: [{ recordedAt, snapshot }]
|
||||
};
|
||||
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(filePath, JSON.stringify(payload, null, 2) + '\n', 'utf8');
|
||||
writeFileAtomic(filePath, JSON.stringify(payload, null, 2) + '\n', {
|
||||
mode: 0o666,
|
||||
preserveExistingMode: true
|
||||
});
|
||||
|
||||
return {
|
||||
backend: 'json-file',
|
||||
|
||||
Reference in new issue
Block a user