Skip to content

Commit

Permalink
fix, write objects atomically (#7160)
Browse files Browse the repository at this point in the history
This will help with the error users get about corrupted objects in case
the process interrupted during the import.
With this PR the objects are written to a tmp file first, and once done
successfully, they’re renamed.
  • Loading branch information
davidfirst authored Mar 16, 2023
1 parent 2c015aa commit 47c9351
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
"vinyl": "2.2.1",
"vinyl-file": "3.0.0",
"winston": "3.3.3",
"write-file-atomic": "5.0.0",
"yargs": "17.0.1",
"yn": "2.0.0"
},
Expand Down
8 changes: 6 additions & 2 deletions src/utils/fs-write-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'fs-extra';
import pathLib from 'path';
import writeFileAtomic from 'write-file-atomic';
import { userInfo } from 'os';

export type ChownOptions = {
Expand All @@ -11,9 +13,11 @@ export default async function writeFile(
contents: string | Buffer,
options: ChownOptions = {}
): Promise<void> {
await fs.outputFile(path, contents);
let chown;
if (options.gid || options.uid) {
const user = userInfo();
await fs.chown(path, options.uid || user.uid, options.gid || user.gid);
chown = { uid: options.uid || user.uid, gid: options.gid || user.gid };
}
await fs.ensureDir(pathLib.dirname(path));
await writeFileAtomic(path, contents, { chown });
}

0 comments on commit 47c9351

Please sign in to comment.