(stateFile, state)
| 126 | } |
| 127 | |
| 128 | function replaceStateFile(stateFile, state) { |
| 129 | const dir = path.dirname(stateFile); |
| 130 | ensurePrivateDir(dir); |
| 131 | const tmp = `${stateFile}.${process.pid}.tmp`; |
| 132 | writePrivateFile(tmp, JSON.stringify(state || {}, null, 2) + '\n'); |
| 133 | // Windows: renameSync throws EPERM when the destination file already |
| 134 | // exists, unlike POSIX where rename(2) atomically replaces the target. |
| 135 | if (process.platform === 'win32') { |
| 136 | try { fs.unlinkSync(stateFile); } catch (e) { |
| 137 | if (e.code !== 'ENOENT') throw e; |
| 138 | } |
| 139 | } |
| 140 | fs.renameSync(tmp, stateFile); |
| 141 | bestEffortChmod(stateFile, PRIVATE_FILE_MODE); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Merge a partial state write with the latest on-disk state. |
no test coverage detected