(e: ApiError, stats?: Stats)
| 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 { |
nothing calls this directly
no test coverage detected