* 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)
| 2770 | * string on any error so callers fall through to unconditional behavior. |
| 2771 | */ |
| 2772 | function readFileTailSync(fullPath: string): string { |
| 2773 | let fd: number | undefined |
| 2774 | try { |
| 2775 | fd = openSync(fullPath, 'r') |
| 2776 | const st = fstatSync(fd) |
| 2777 | const tailOffset = Math.max(0, st.size - LITE_READ_BUF_SIZE) |
| 2778 | const buf = Buffer.allocUnsafe( |
| 2779 | Math.min(LITE_READ_BUF_SIZE, st.size - tailOffset), |
| 2780 | ) |
| 2781 | const bytesRead = readSync(fd, buf, 0, buf.length, tailOffset) |
| 2782 | return buf.toString('utf8', 0, bytesRead) |
| 2783 | } catch { |
| 2784 | return '' |
| 2785 | } finally { |
| 2786 | if (fd !== undefined) { |
| 2787 | try { |
| 2788 | closeSync(fd) |
| 2789 | } catch { |
| 2790 | // closeSync can throw; swallow to preserve return '' contract |
| 2791 | } |
| 2792 | } |
| 2793 | } |
| 2794 | } |
| 2795 | /* eslint-enable custom-rules/no-sync-fs */ |
| 2796 | |
| 2797 | export async function saveCustomTitle( |
no test coverage detected