( db: Database, inode: number, size: number, mode: number, mtime: number, isTouched: (idx: number, start: number, end: number) => boolean, buildChunkBytes: (idx: number, start: number, end: number, existing: Uint8Array) => Uint8Array, )
| 423 | // rowids and the surrounding rows do not churn. The manifest is |
| 424 | // invalidated rather than recomputed; sync rebuilds it lazily. |
| 425 | function applyChunkedInodeUpdate( |
| 426 | db: Database, |
| 427 | inode: number, |
| 428 | size: number, |
| 429 | mode: number, |
| 430 | mtime: number, |
| 431 | isTouched: (idx: number, start: number, end: number) => boolean, |
| 432 | buildChunkBytes: (idx: number, start: number, end: number, existing: Uint8Array) => Uint8Array, |
| 433 | ): void { |
| 434 | const oldChunks = existingChunkRefs(db, inode); |
| 435 | const chunkCount = Math.ceil(size / CHUNK_SIZE); |
| 436 | const oldChunkCount = oldChunks.length; |
| 437 | |
| 438 | for (let idx = 0; idx < chunkCount; idx++) { |
| 439 | const start = idx * CHUNK_SIZE; |
| 440 | const end = Math.min(start + CHUNK_SIZE, size); |
| 441 | const intendedSize = end - start; |
| 442 | const old = oldChunks[idx]; |
| 443 | const touched = isTouched(idx, start, end); |
| 444 | // Stable chunk: existed before with the same logical size and the |
| 445 | // caller did not flag it as touched. Skip without issuing SQL so |
| 446 | // its rowid stays put. |
| 447 | if (old !== undefined && old.size === intendedSize && !touched) continue; |
| 448 | |
| 449 | const existingBytes = old !== undefined ? readChunkBytes(db, inode, idx) : new Uint8Array(); |
| 450 | const chunkBytes = buildChunkBytes(idx, start, end, existingBytes); |
| 451 | if (chunkBytes.byteLength !== intendedSize) { |
| 452 | throw createWorkspaceError("EIO", "chunk builder returned wrong size"); |
| 453 | } |
| 454 | const chunk = { hash: sha256(chunkBytes), bytes: chunkBytes, size: chunkBytes.byteLength }; |
| 455 | upsertChunkBlob(db, chunk, mtime); |
| 456 | db.run( |
| 457 | "INSERT OR REPLACE INTO vfs_chunks (inode, idx, hash, size) VALUES (?, ?, ?, ?)", |
| 458 | inode, |
| 459 | idx, |
| 460 | chunk.hash, |
| 461 | chunk.size, |
| 462 | ); |
| 463 | } |
| 464 | |
| 465 | // Drop any old chunks past the new end of file (shrink case). |
| 466 | if (oldChunkCount > chunkCount) { |
| 467 | db.run("DELETE FROM vfs_chunks WHERE inode = ? AND idx >= ?", inode, chunkCount); |
| 468 | } |
| 469 | |
| 470 | const rev = incrementRev(db); |
| 471 | db.run( |
| 472 | "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = ?, manifest_hash = NULL WHERE inode = ?", |
| 473 | mode, |
| 474 | mtime, |
| 475 | rev, |
| 476 | size, |
| 477 | inode, |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | export function createFileSync( |
| 482 | db: Database, |
no test coverage detected