MCPcopy Create free account
hub / github.com/cloudflare/computer / applyChunkedInodeUpdate

Function applyChunkedInodeUpdate

packages/dofs/src/fs/writeFile.ts:425–479  ·  view source on GitHub ↗
(
  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,
)

Source from the content-addressed store, hash-verified

423// rowids and the surrounding rows do not churn. The manifest is
424// invalidated rather than recomputed; sync rebuilds it lazily.
425function 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
481export function createFileSync(
482 db: Database,

Callers 3

releaseWriteBufferSyncFunction · 0.85
writeRangeSyncFunction · 0.85
truncateFileSyncFunction · 0.85

Calls 7

existingChunkRefsFunction · 0.85
readChunkBytesFunction · 0.85
upsertChunkBlobFunction · 0.85
incrementRevFunction · 0.85
sha256Function · 0.70
createWorkspaceErrorFunction · 0.50
runMethod · 0.45

Tested by

no test coverage detected