MCPcopy
hub / github.com/jvilk/BrowserFS / commitNewFile

Method commitNewFile

src/generic/key_value_filesystem.ts:545–580  ·  view source on GitHub ↗

* Commits a new file (well, a FILE or a DIRECTORY) to the file system with * the given mode. * Note: This will commit the transaction. * @param p The path to the new file. * @param type The type of the new file. * @param mode The mode to create the new file with. * @param data The

(tx: SyncKeyValueRWTransaction, p: string, type: FileType, mode: number, data: Buffer)

Source from the content-addressed store, hash-verified

543 * @return The Inode for the new file.
544 */
545 private commitNewFile(tx: SyncKeyValueRWTransaction, p: string, type: FileType, mode: number, data: Buffer): Inode {
546 const parentDir = path.dirname(p),
547 fname = path.basename(p),
548 parentNode = this.findINode(tx, parentDir),
549 dirListing = this.getDirListing(tx, parentDir, parentNode),
550 currTime = (new Date()).getTime();
551
552 // Invariant: The root always exists.
553 // If we don't check this prior to taking steps below, we will create a
554 // file with name '' in root should p == '/'.
555 if (p === '/') {
556 throw ApiError.EEXIST(p);
557 }
558
559 // Check if file already exists.
560 if (dirListing[fname]) {
561 throw ApiError.EEXIST(p);
562 }
563
564 let fileNode: Inode;
565 try {
566 // Commit data.
567 const dataId = this.addNewNode(tx, data);
568 fileNode = new Inode(dataId, data.length, mode | type, currTime, currTime, currTime);
569 // Commit file node.
570 const fileNodeId = this.addNewNode(tx, fileNode.toBuffer());
571 // Update and commit parent directory listing.
572 dirListing[fname] = fileNodeId;
573 tx.put(parentNode.id, Buffer.from(JSON.stringify(dirListing)), true);
574 } catch (e) {
575 tx.abort();
576 throw e;
577 }
578 tx.commit();
579 return fileNode;
580 }
581
582 /**
583 * Remove all traces of the given path from the file system.

Callers 2

createFileSyncMethod · 0.95
mkdirSyncMethod · 0.95

Calls 8

findINodeMethod · 0.95
getDirListingMethod · 0.95
addNewNodeMethod · 0.95
EEXISTMethod · 0.80
putMethod · 0.65
abortMethod · 0.65
commitMethod · 0.65
toBufferMethod · 0.45

Tested by

no test coverage detected