| 633 | } |
| 634 | } |
| 635 | public readFile(fname: string, encoding: string | null, flag: FileFlag, cb: BFSCallback<string | Buffer>): void { |
| 636 | // Wrap cb in file closing code. |
| 637 | const oldCb = cb; |
| 638 | // Get file. |
| 639 | this.open(fname, flag, 0x1a4, (err, fd) => { |
| 640 | if (err) { |
| 641 | return cb(err); |
| 642 | } |
| 643 | cb = function(err?: ApiError | null, arg?: string | Buffer) { |
| 644 | fd!.close(function(err2: any) { |
| 645 | if (!err) { |
| 646 | err = err2; |
| 647 | } |
| 648 | return oldCb(err, arg); |
| 649 | }); |
| 650 | }; |
| 651 | fd!.stat((err, stat?) => { |
| 652 | if (err) { |
| 653 | return cb(err); |
| 654 | } |
| 655 | // Allocate buffer. |
| 656 | const buf = Buffer.alloc(stat!.size); |
| 657 | fd!.read(buf, 0, stat!.size, 0, (err?: ApiError | null) => { |
| 658 | if (err) { |
| 659 | return cb(err); |
| 660 | } else if (encoding === null) { |
| 661 | return cb(err, buf); |
| 662 | } |
| 663 | try { |
| 664 | cb(null, buf.toString(encoding)); |
| 665 | } catch (e) { |
| 666 | cb(e); |
| 667 | } |
| 668 | }); |
| 669 | }); |
| 670 | }); |
| 671 | } |
| 672 | public readFileSync(fname: string, encoding: string | null, flag: FileFlag): any { |
| 673 | // Get file. |
| 674 | const fd = this.openSync(fname, flag, 0x1a4); |