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

Function releaseWriteBufferSync

packages/dofs/src/fs/writeFile.ts:590–639  ·  view source on GitHub ↗
(db: Database, path: string, now: () => number)

Source from the content-addressed store, hash-verified

588// time so an intermediate chmod survives. Pending-create entries
589// emit their INSERT + dirent + chunks in the same transaction.
590export function releaseWriteBufferSync(db: Database, path: string, now: () => number): void {
591 const { path: canonical } = canonicalizePath(path);
592 const pending = getPendingWriteBufferByPath(db, canonical);
593 if (pending !== undefined) {
594 releasePendingBuffer(db, pending, now);
595 return;
596 }
597 const node = resolveFileInode(db, path);
598 const entry = getWriteBuffer(db, node.inode);
599 if (entry === undefined) return;
600 entry.openCount -= 1;
601 if (entry.openCount > 0) return;
602
603 if (!entry.dirty) {
604 deleteWriteBuffer(db, node.inode);
605 return;
606 }
607
608 const mtime = now();
609 const mode = entry.mode & 0o7777;
610 const buffered = entry.buf.subarray(0, entry.size);
611
612 db.transactionSync(() => {
613 if (entry.size === 0) {
614 // An empty file owns no chunk rows; clear any old ones the
615 // buffer would otherwise have replaced and bump metadata.
616 db.run("DELETE FROM vfs_chunks WHERE inode = ?", node.inode);
617 const rev = incrementRev(db);
618 db.run(
619 "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = 0, manifest_hash = NULL WHERE inode = ?",
620 mode,
621 mtime,
622 rev,
623 node.inode,
624 );
625 return;
626 }
627 applyChunkedInodeUpdate(
628 db,
629 node.inode,
630 entry.size,
631 mode,
632 mtime,
633 (_idx, start, end) => start < entry.size && end > 0,
634 (_idx, start, end) => buffered.subarray(start, Math.min(end, entry.size)),
635 );
636 });
637
638 deleteWriteBuffer(db, node.inode);
639}
640
641// Commit a pending-create buffer to SQLite. Returns the real inode
642// allocated by the INSERT, or throws. Promotes the cache entry's key

Callers 1

Calls 10

canonicalizePathFunction · 0.85
releasePendingBufferFunction · 0.85
resolveFileInodeFunction · 0.85
getWriteBufferFunction · 0.85
deleteWriteBufferFunction · 0.85
incrementRevFunction · 0.85
applyChunkedInodeUpdateFunction · 0.85
transactionSyncMethod · 0.65
runMethod · 0.45

Tested by

no test coverage detected