@floydous/pi-agent-kernel 0.4.0 → 0.4.2

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.
@@ -1,26 +1,65 @@
1
1
  import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { randomBytes } from "node:crypto";
2
4
  import { kernelDebug } from "./kernel_debug";
3
5
 
6
+ function syncDirectory(directory: string): void {
7
+ if (process.platform === "win32") return;
8
+ try {
9
+ const fd = fs.openSync(directory, fs.constants.O_RDONLY);
10
+ try {
11
+ fs.fsyncSync(fd);
12
+ } finally {
13
+ fs.closeSync(fd);
14
+ }
15
+ } catch (error) {
16
+ // Directory fsync is not available on every filesystem; rename is still
17
+ // atomic, and the failure is intentionally non-fatal after the rename.
18
+ kernelDebug(error);
19
+ }
20
+ }
21
+
4
22
  /**
5
- * Write a file atomically: write to a sibling temp file, then rename over the
6
- * destination. A plain writeFileSync can leave a truncated/corrupt JSON store
7
- * behind if the process dies mid-write; with tmp+rename the destination either
8
- * holds the old complete file or the new complete file, never a partial one.
23
+ * Write a file through an exclusive sibling temp file, preserving metadata
24
+ * where the platform exposes it. A plain writeFileSync can leave a truncated
25
+ * destination after a crash; fsync + rename prevents that partial state.
9
26
  */
10
- export function writeFileSyncAtomic(
11
- filePath: string,
12
- data: string | Buffer,
13
- ): void {
14
- const tmp = `${filePath}.${process.pid}.${Date.now()}.tmp`;
15
- try {
16
- fs.writeFileSync(tmp, data);
17
- fs.renameSync(tmp, filePath);
18
- } catch (err) {
19
- try {
20
- fs.unlinkSync(tmp);
21
- } catch (cleanupErr) {
22
- kernelDebug(cleanupErr);
23
- }
24
- throw err;
25
- }
27
+ export function writeFileSyncAtomic(filePath: string, data: string | Buffer): void {
28
+ const target = path.resolve(filePath);
29
+ let targetStat: fs.Stats | undefined;
30
+ try {
31
+ targetStat = fs.lstatSync(target);
32
+ if (targetStat.isSymbolicLink()) {
33
+ throw new Error("Refusing to atomically replace a symbolic-link target");
34
+ }
35
+ } catch (error: any) {
36
+ if (error?.code !== "ENOENT") throw error;
37
+ }
38
+
39
+ const mode = targetStat ? (targetStat.mode & 0o7777) : 0o600;
40
+ const temp = `${target}.${process.pid}.${Date.now()}.${randomBytes(6).toString("hex")}.tmp`;
41
+ let fd: number | undefined;
42
+ try {
43
+ fd = fs.openSync(temp, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL, mode);
44
+ fs.writeFileSync(fd, data);
45
+ fs.fsyncSync(fd);
46
+ fs.closeSync(fd);
47
+ fd = undefined;
48
+
49
+ if (targetStat && process.platform !== "win32") {
50
+ fs.chmodSync(temp, mode);
51
+ if (targetStat.uid !== process.getuid?.() || targetStat.gid !== process.getgid?.()) {
52
+ fs.chownSync(temp, targetStat.uid, targetStat.gid);
53
+ }
54
+ }
55
+
56
+ fs.renameSync(temp, target);
57
+ syncDirectory(path.dirname(target));
58
+ } catch (error) {
59
+ if (fd !== undefined) {
60
+ try { fs.closeSync(fd); } catch (closeError) { kernelDebug(closeError); }
61
+ }
62
+ try { fs.unlinkSync(temp); } catch (cleanupError) { kernelDebug(cleanupError); }
63
+ throw error;
64
+ }
26
65
  }