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

Method mustBeFile

src/core/file_system.ts:408–460  ·  view source on GitHub ↗
(e: ApiError, stats?: Stats)

Source from the content-addressed store, hash-verified

406 }
407 public open(p: string, flag: FileFlag, mode: number, cb: BFSCallback<File>): void {
408 const mustBeFile = (e: ApiError, stats?: Stats): void => {
409 if (e) {
410 // File does not exist.
411 switch (flag.pathNotExistsAction()) {
412 case ActionType.CREATE_FILE:
413 // Ensure parent exists.
414 return this.stat(path.dirname(p), false, (e: ApiError, parentStats?: Stats) => {
415 if (e) {
416 cb(e);
417 } else if (parentStats && !parentStats.isDirectory()) {
418 cb(ApiError.ENOTDIR(path.dirname(p)));
419 } else {
420 this.createFile(p, flag, mode, cb);
421 }
422 });
423 case ActionType.THROW_EXCEPTION:
424 return cb(ApiError.ENOENT(p));
425 default:
426 return cb(new ApiError(ErrorCode.EINVAL, 'Invalid FileFlag object.'));
427 }
428 } else {
429 // File exists.
430 if (stats && stats.isDirectory()) {
431 return cb(ApiError.EISDIR(p));
432 }
433 switch (flag.pathExistsAction()) {
434 case ActionType.THROW_EXCEPTION:
435 return cb(ApiError.EEXIST(p));
436 case ActionType.TRUNCATE_FILE:
437 // NOTE: In a previous implementation, we deleted the file and
438 // re-created it. However, this created a race condition if another
439 // asynchronous request was trying to read the file, as the file
440 // would not exist for a small period of time.
441 return this.openFile(p, flag, (e: ApiError, fd?: File): void => {
442 if (e) {
443 cb(e);
444 } else if (fd) {
445 fd.truncate(0, () => {
446 fd.sync(() => {
447 cb(null, fd);
448 });
449 });
450 } else {
451 fail();
452 }
453 });
454 case ActionType.NOP:
455 return this.openFile(p, flag, cb);
456 default:
457 return cb(new ApiError(ErrorCode.EINVAL, 'Invalid FileFlag object.'));
458 }
459 }
460 };
461 this.stat(p, false, mustBeFile);
462 }
463 public rename(oldPath: string, newPath: string, cb: BFSOneArgCallback): void {

Callers

nothing calls this directly

Calls 14

statMethod · 0.95
createFileMethod · 0.95
openFileMethod · 0.95
failFunction · 0.90
cbFunction · 0.85
pathNotExistsActionMethod · 0.80
ENOTDIRMethod · 0.80
ENOENTMethod · 0.80
EISDIRMethod · 0.80
pathExistsActionMethod · 0.80
EEXISTMethod · 0.80
truncateMethod · 0.65

Tested by

no test coverage detected