* Sync tail read for reAppendSessionMetadata's external-writer check. * fstat on the already-open fd (no extra path lookup); reads the same * LITE_READ_BUF_SIZE window that readLiteMetadata scans. Returns empty * string on any error so callers fall through to unconditional behavior.
(fullPath: string)
| 2629 | * string on any error so callers fall through to unconditional behavior. |
| 2630 | */ |
| 2631 | function readFileTailSync(fullPath: string): string { |
| 2632 | let fd: number | undefined |
| 2633 | try { |
| 2634 | fd = openSync(fullPath, 'r') |
| 2635 | const st = fstatSync(fd) |
| 2636 | const tailOffset = Math.max(0, st.size - LITE_READ_BUF_SIZE) |
| 2637 | const buf = Buffer.allocUnsafe( |
| 2638 | Math.min(LITE_READ_BUF_SIZE, st.size - tailOffset), |
| 2639 | ) |
| 2640 | const bytesRead = readSync(fd, buf, 0, buf.length, tailOffset) |
| 2641 | return buf.toString('utf8', 0, bytesRead) |
| 2642 | } catch { |
| 2643 | return '' |
| 2644 | } finally { |
| 2645 | if (fd !== undefined) { |
| 2646 | try { |
| 2647 | closeSync(fd) |
| 2648 | } catch { |
| 2649 | // closeSync can throw; swallow to preserve return '' contract |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | /* eslint-enable custom-rules/no-sync-fs */ |
| 2655 | |
| 2656 | export async function saveCustomTitle( |
no test coverage detected