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

Method openSync

src/core/file_system.ts:491–533  ·  view source on GitHub ↗
(p: string, flag: FileFlag, mode: number)

Source from the content-addressed store, hash-verified

489 throw new ApiError(ErrorCode.ENOTSUP);
490 }
491 public openSync(p: string, flag: FileFlag, mode: number): File {
492 // Check if the path exists, and is a file.
493 let stats: Stats;
494 try {
495 stats = this.statSync(p, false);
496 } catch (e) {
497 // File does not exist.
498 switch (flag.pathNotExistsAction()) {
499 case ActionType.CREATE_FILE:
500 // Ensure parent exists.
501 const parentStats = this.statSync(path.dirname(p), false);
502 if (!parentStats.isDirectory()) {
503 throw ApiError.ENOTDIR(path.dirname(p));
504 }
505 return this.createFileSync(p, flag, mode);
506 case ActionType.THROW_EXCEPTION:
507 throw ApiError.ENOENT(p);
508 default:
509 throw new ApiError(ErrorCode.EINVAL, 'Invalid FileFlag object.');
510 }
511 }
512
513 // File exists.
514 if (stats.isDirectory()) {
515 throw ApiError.EISDIR(p);
516 }
517 switch (flag.pathExistsAction()) {
518 case ActionType.THROW_EXCEPTION:
519 throw ApiError.EEXIST(p);
520 case ActionType.TRUNCATE_FILE:
521 // Delete file.
522 this.unlinkSync(p);
523 // Create file. Use the same mode as the old file.
524 // Node itself modifies the ctime when this occurs, so this action
525 // will preserve that behavior if the underlying file system
526 // supports those properties.
527 return this.createFileSync(p, flag, stats.mode);
528 case ActionType.NOP:
529 return this.openFileSync(p, flag, mode);
530 default:
531 throw new ApiError(ErrorCode.EINVAL, 'Invalid FileFlag object.');
532 }
533 }
534 public unlink(p: string, cb: BFSOneArgCallback): void {
535 cb(new ApiError(ErrorCode.ENOTSUP));
536 }

Callers 4

truncateSyncMethod · 0.95
readFileSyncMethod · 0.95
writeFileSyncMethod · 0.95
appendFileSyncMethod · 0.95

Calls 11

statSyncMethod · 0.95
createFileSyncMethod · 0.95
unlinkSyncMethod · 0.95
openFileSyncMethod · 0.95
pathNotExistsActionMethod · 0.80
ENOTDIRMethod · 0.80
ENOENTMethod · 0.80
EISDIRMethod · 0.80
pathExistsActionMethod · 0.80
EEXISTMethod · 0.80
isDirectoryMethod · 0.45

Tested by

no test coverage detected