(path: string, bytes: Buffer, expectedSha256: string)
| 76 | } |
| 77 | |
| 78 | function ensureFile(path: string, bytes: Buffer, expectedSha256: string): void { |
| 79 | if (readFileSha256(path) === expectedSha256) return; |
| 80 | |
| 81 | mkdirSync(dirname(path), { recursive: true }); |
| 82 | const tempPath = `${path}.${process.pid}.${Date.now()}.tmp`; |
| 83 | writeFileSync(tempPath, bytes, { mode: 0o644 }); |
| 84 | |
| 85 | try { |
| 86 | renameSync(tempPath, path); |
| 87 | return; |
| 88 | } catch { |
| 89 | if (readFileSha256(path) === expectedSha256) { |
| 90 | rmSync(tempPath, { force: true }); |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | rmSync(path, { force: true }); |
| 97 | renameSync(tempPath, path); |
| 98 | } catch (error) { |
| 99 | rmSync(tempPath, { force: true }); |
| 100 | if (readFileSha256(path) === expectedSha256) return; |
| 101 | throw error; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function assertSafeRelativePath(relativePath: string): void { |
| 106 | if ( |
no test coverage detected