(absolute: string, id: string)
| 252 | } |
| 253 | |
| 254 | function readTrailFile(absolute: string, id: string): StoredTrail | null { |
| 255 | let fd: number | null = null; |
| 256 | try { |
| 257 | // O_NOFOLLOW closes the race between path validation and the actual open: |
| 258 | // replacing a checked file with a symlink must fail rather than follow it. |
| 259 | // O_NONBLOCK keeps a FIFO with no writer from parking the whole server on |
| 260 | // this open; a regular file ignores it, and `isFile()` below rejects the rest. |
| 261 | fd = fs.openSync( |
| 262 | absolute, |
| 263 | fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0) | (fs.constants.O_NONBLOCK ?? 0) |
| 264 | ); |
| 265 | const stat = fs.fstatSync(fd); |
| 266 | // A file too big to be a trail is skipped rather than read: this directory |
| 267 | // is inside the project, and something else may one day put a log in it. |
| 268 | if (!stat.isFile() || stat.size > MAX_TRAIL_FILE_BYTES) return null; |
| 269 | return parseTrail(id, fs.readFileSync(fd, 'utf-8')); |
| 270 | } catch { |
| 271 | return null; |
| 272 | } finally { |
| 273 | if (fd !== null) fs.closeSync(fd); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** One trail by id, or `null` when there is no such file. */ |
| 278 | export function readStoredTrail(projectRoot: string, id: string): StoredTrail | null { |
no test coverage detected