* 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)
| 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. |
no test coverage detected