All files / json-pack/src/nfs/v4/client Nfsv4FsClient.ts

85.78% Statements 537/626
54.66% Branches 123/225
90.9% Functions 40/44
86.76% Lines 518/597

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921      2x   2x                   2x 2x 2x 2x 2x 2x 2x 2x   2x 125x   125x 125x     294x       196x 196x 196x 196x 196x       98x       50x 50x 339x 339x 339x 80x   339x   50x       224x 224x 260x       132x 132x 21x   132x       81x 81x 93x   81x       41x 35x 35x           13x 9x     125x       98x 98x 98x 98x 98x                   125x       13x 13x 13x 13x 13x 13x 13x 13x 13x                   13x 13x     13x 13x     13x 13x 13x 13x 13x 13x 13x 13x     13x 13x     13x 13x 13x   13x     13x   13x 13x 13x 13x 13x 13x   13x     125x         36x 36x 36x 36x 36x 36x 36x 36x                   36x 36x 36x 36x 36x 36x 36x 36x 36x     36x 36x     36x 36x 36x 36x 36x 36x 27x 27x     27x     27x 27x     27x     36x       125x 37x 37x 37x 37x                     37x 37x 37x 37x     37x 37x     37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 74x 74x 74x 2368x 333x 333x   37x 37x   37x 37x   37x 37x   37x 37x   37x 37x   37x 37x   37x 37x 37x 37x     37x 37x 37x 37x     37x 37x 37x 37x         37x 37x                                             125x 6x     125x       14x 14x 14x     14x 14x 14x 14x 14x 14x 14x     14x 14x     14x     125x       8x 8x 8x 8x 8x 8x 8x 8x 8x 7x     7x 7x     7x 7x 7x 7x 10x 10x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 64x 2x 2x 2x       2x   8x     7x 1x   6x     6x     125x         5x 5x 5x 5x 5x 5x 5x 5x                   5x 5x 5x 5x 5x     5x 5x     5x 5x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x     5x 5x     5x     5x       125x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x     6x 6x         125x 3x 3x 3x     3x 3x 3x 3x 3x 1x   2x 2x         125x 3x 3x 3x     3x 3x 3x 3x 3x 1x   2x 2x         125x 12x 12x 12x     12x 12x 12x 7x 7x 7x 4x 4x 5x 5x               12x 12x 12x 12x 12x 12x 3x 1x   9x 9x       2x       125x 3x 3x 3x 3x 3x 3x           3x 3x 3x 1x   2x 2x         125x 3x 3x 3x 3x 3x     3x 3x 3x     3x 3x 3x 1x   3x 3x 3x 3x 3x     3x 3x         125x         2x 2x     125x       3x 3x 3x 3x 2x   1x     125x 2x 2x 2x 2x 2x     2x 2x 2x 2x 1x   2x 2x 2x 2x     2x 2x         125x         2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x     2x 2x         125x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x 5x         125x 2x 2x 2x 2x 2x 2x 2x     2x 2x     2x 2x         125x 6x 6x 6x 6x     125x 5x 5x 5x 5x 5x 1x     125x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x     6x 6x         125x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x     6x 6x         125x 2x     125x 2x     125x 2x     125x         44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 4x 40x 12x 12x                                           44x     44x 44x     44x 44x     44x 44x 44x     125x       125x                   125x        
import type {NfsFsClient, Nfsv4Client} from './types';
import type * as misc from 'memfs/lib/node/types/misc';
import type * as opts from 'memfs/lib/node/types/options';
import {nfs} from '../builder';
import type * as msg from '../messages';
import * as structs from '../structs';
import {
  Nfsv4Stat,
  Nfsv4OpenAccess,
  Nfsv4OpenDeny,
  Nfsv4StableHow,
  Nfsv4Attr,
  Nfsv4FType,
  Nfsv4Access,
} from '../constants';
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {XdrEncoder} from '../../../xdr/XdrEncoder';
import {XdrDecoder} from '../../../xdr/XdrDecoder';
import {NfsFsStats} from './NfsFsStats';
import {NfsFsDir} from './NfsFsDir';
import {NfsFsDirent} from './NfsFsDirent';
import {NfsFsFileHandle} from './NfsFsFileHandle';
 
export class Nfsv4FsClient implements NfsFsClient {
  constructor(public readonly fs: Nfsv4Client) {}
 
  private readonly openOwnerSeqids: Map<string, number> = new Map();
  private readonly defaultOpenOwnerId = new Uint8Array([1, 2, 3, 4]);
 
  private makeOpenOwnerKey(owner: structs.Nfsv4OpenOwner): string {
    return `${owner.clientid}:${Buffer.from(owner.owner).toString('hex')}`;
  }
 
  private nextOpenOwnerSeqid(owner: structs.Nfsv4OpenOwner): number {
    const key = this.makeOpenOwnerKey(owner);
    const last = this.openOwnerSeqids.get(key);
    const next = last === undefined ? 0 : last === 0xffffffff ? 1 : (last + 1) >>> 0;
    this.openOwnerSeqids.set(key, next);
    return next;
  }
 
  private createDefaultOpenOwner(): structs.Nfsv4OpenOwner {
    return nfs.OpenOwner(BigInt(1), new Uint8Array(this.defaultOpenOwnerId));
  }
 
  private attrNumsToBitmap(attrNums: number[]): number[] {
    const bitmap: number[] = [];
    for (const attrNum of attrNums) {
      const wordIndex = Math.floor(attrNum / 32);
      const bitIndex = attrNum % 32;
      while (bitmap.length <= wordIndex) {
        bitmap.push(0);
      }
      bitmap[wordIndex] |= 1 << bitIndex;
    }
    return bitmap;
  }
 
  private parsePath(path: string): string[] {
    const normalized = path.replace(/^\/+/, '').replace(/\/+$/, '');
    if (!normalized) return [];
    return normalized.split('/').filter((part) => part.length > 0);
  }
 
  private navigateToParent(parts: string[]): msg.Nfsv4Request[] {
    const operations: msg.Nfsv4Request[] = [nfs.PUTROOTFH()];
    for (const part of parts.slice(0, -1)) {
      operations.push(nfs.LOOKUP(part));
    }
    return operations;
  }
 
  private navigateToPath(parts: string[]): msg.Nfsv4Request[] {
    const operations: msg.Nfsv4Request[] = [nfs.PUTROOTFH()];
    for (const part of parts) {
      operations.push(nfs.LOOKUP(part));
    }
    return operations;
  }
 
  private encodeData(data: misc.TPromisesData): Uint8Array {
    if (data instanceof Uint8Array) return data;
    Iif (data instanceof ArrayBuffer) return new Uint8Array(data);
    if (typeof data === 'string') return new TextEncoder().encode(data);
    Iif (Buffer.isBuffer(data)) return new Uint8Array(data);
    throw new Error('Unsupported data type');
  }
 
  private decodeData(data: Uint8Array, encoding?: string): misc.TDataOut {
    if (!encoding || encoding === 'buffer') return Buffer.from(data);
    return new TextDecoder(encoding).decode(data);
  }
 
  public readonly closeStateid = async (
    openOwner: structs.Nfsv4OpenOwner,
    stateid: structs.Nfsv4Stateid,
  ): Promise<void> => {
    const key = this.makeOpenOwnerKey(openOwner);
    const previousSeqid = this.openOwnerSeqids.get(key);
    const seqid = this.nextOpenOwnerSeqid(openOwner);
    const response = await this.fs.compound([nfs.CLOSE(seqid, stateid)]);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      if (previousSeqid !== undefined) {
        this.openOwnerSeqids.set(key, previousSeqid);
      } else {
        this.openOwnerSeqids.delete(key);
      }
      throw new Error(`Failed to close file: ${response.status}`);
    }
  };
 
  public readonly readFile = async (
    id: misc.TFileHandle,
    options?: opts.IReadFileOptions | string,
  ): Promise<misc.TDataOut> => {
    const encoding = typeof options === 'string' ? options : options?.encoding;
    const path = typeof id === 'string' ? id : id.toString();
    const parts = this.parsePath(path);
    const operations = this.navigateToParent(parts);
    const filename = parts[parts.length - 1];
    const openOwner = this.createDefaultOpenOwner();
    const claim = nfs.OpenClaimNull(filename);
    const openSeqid = this.nextOpenOwnerSeqid(openOwner);
    operations.push(
      nfs.OPEN(
        openSeqid,
        Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_READ,
        Nfsv4OpenDeny.OPEN4_SHARE_DENY_NONE,
        openOwner,
        nfs.OpenHowNoCreate(),
        claim,
      ),
    );
    const openResponse = await this.fs.compound(operations);
    Iif (openResponse.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to open file: ${openResponse.status}`);
    }
    const openRes = openResponse.resarray[openResponse.resarray.length - 1] as msg.Nfsv4OpenResponse;
    Iif (openRes.status !== Nfsv4Stat.NFS4_OK || !openRes.resok) {
      throw new Error(`Failed to open file: ${openRes.status}`);
    }
    const stateid = openRes.resok.stateid;
    const chunks: Uint8Array[] = [];
    let offset = BigInt(0);
    const chunkSize = 65536;
    try {
      while (true) {
        const readResponse = await this.fs.compound([nfs.READ(offset, chunkSize, stateid)]);
        Iif (readResponse.status !== Nfsv4Stat.NFS4_OK) {
          throw new Error(`Failed to read file: ${readResponse.status}`);
        }
        const readRes = readResponse.resarray[0] as msg.Nfsv4ReadResponse;
        Iif (readRes.status !== Nfsv4Stat.NFS4_OK || !readRes.resok) {
          throw new Error(`Failed to read file: ${readRes.status}`);
        }
        if (readRes.resok.data.length > 0) {
          chunks.push(readRes.resok.data);
          offset += BigInt(readRes.resok.data.length);
        }
        if (readRes.resok.eof) break;
      }
    } finally {
      await this.closeStateid(openOwner, stateid);
    }
    const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
    const result = new Uint8Array(totalLength);
    let position = 0;
    for (const chunk of chunks) {
      result.set(chunk, position);
      position += chunk.length;
    }
    return this.decodeData(result, encoding);
  };
 
  public readonly writeFile = async (
    id: misc.TFileHandle,
    data: misc.TPromisesData,
    options?: opts.IWriteFileOptions,
  ): Promise<void> => {
    const path = typeof id === 'string' ? id : id.toString();
    const parts = this.parsePath(path);
    const operations = this.navigateToParent(parts);
    const filename = parts[parts.length - 1];
    const openOwner = this.createDefaultOpenOwner();
    const claim = nfs.OpenClaimNull(filename);
    const openSeqid = this.nextOpenOwnerSeqid(openOwner);
    operations.push(
      nfs.OPEN(
        openSeqid,
        Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE,
        Nfsv4OpenDeny.OPEN4_SHARE_DENY_NONE,
        openOwner,
        nfs.OpenHowCreateUnchecked(),
        claim,
      ),
    );
    const writer = new Writer(16);
    const xdr = new XdrEncoder(writer);
    xdr.writeUnsignedHyper(BigInt(0));
    const attrVals = writer.flush();
    const truncateAttrs = nfs.Fattr([Nfsv4Attr.FATTR4_SIZE], attrVals);
    const stateid = nfs.Stateid(0, new Uint8Array(12));
    operations.push(nfs.SETATTR(stateid, truncateAttrs));
    const openResponse = await this.fs.compound(operations);
    Iif (openResponse.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to open file: ${openResponse.status}`);
    }
    const openRes = openResponse.resarray[openResponse.resarray.length - 2] as msg.Nfsv4OpenResponse;
    Iif (openRes.status !== Nfsv4Stat.NFS4_OK || !openRes.resok) {
      throw new Error(`Failed to open file: ${openRes.status}`);
    }
    const openStateid = openRes.resok.stateid;
    const buffer = this.encodeData(data);
    const chunkSize = 65536;
    try {
      let offset = BigInt(0);
      for (let i = 0; i < buffer.length; i += chunkSize) {
        const chunk = buffer.slice(i, Math.min(i + chunkSize, buffer.length));
        const writeResponse = await this.fs.compound([
          nfs.WRITE(openStateid, offset, Nfsv4StableHow.FILE_SYNC4, chunk),
        ]);
        Iif (writeResponse.status !== Nfsv4Stat.NFS4_OK) {
          throw new Error(`Failed to write file: ${writeResponse.status}`);
        }
        const writeRes = writeResponse.resarray[0] as msg.Nfsv4WriteResponse;
        Iif (writeRes.status !== Nfsv4Stat.NFS4_OK || !writeRes.resok) {
          throw new Error(`Failed to write file: ${writeRes.status}`);
        }
        offset += BigInt(writeRes.resok.count);
      }
    } finally {
      await this.closeStateid(openOwner, openStateid);
    }
  };
 
  public readonly stat = async (path: misc.PathLike, options?: opts.IStatOptions): Promise<misc.IStats> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    const attrNums = [
      Nfsv4Attr.FATTR4_TYPE,
      Nfsv4Attr.FATTR4_SIZE,
      Nfsv4Attr.FATTR4_FILEID,
      Nfsv4Attr.FATTR4_MODE,
      Nfsv4Attr.FATTR4_NUMLINKS,
      Nfsv4Attr.FATTR4_SPACE_USED,
      Nfsv4Attr.FATTR4_TIME_ACCESS,
      Nfsv4Attr.FATTR4_TIME_MODIFY,
      Nfsv4Attr.FATTR4_TIME_METADATA,
    ];
    const attrMask = this.attrNumsToBitmap(attrNums);
    operations.push(nfs.GETATTR(attrMask));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to stat file: ${response.status}`);
    }
    const getattrRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4GetattrResponse;
    Iif (getattrRes.status !== Nfsv4Stat.NFS4_OK || !getattrRes.resok) {
      throw new Error(`Failed to get attributes: ${getattrRes.status}`);
    }
    const fattr = getattrRes.resok.objAttributes;
    const reader = new Reader();
    reader.reset(fattr.attrVals);
    const xdr = new XdrDecoder(reader);
    let fileType = Nfsv4FType.NF4REG;
    let size = 0;
    let fileid = 0;
    let mode = 0;
    let nlink = 1;
    let spaceUsed = 0;
    let atime = new Date(0);
    let mtime = new Date(0);
    let ctime = new Date(0);
    const returnedMask = fattr.attrmask.mask;
    for (let i = 0; i < returnedMask.length; i++) {
      const word = returnedMask[i];
      Iif (!word) continue;
      for (let bit = 0; bit < 32; bit++) {
        if (!(word & (1 << bit))) continue;
        const attrNum = i * 32 + bit;
        switch (attrNum) {
          case Nfsv4Attr.FATTR4_TYPE:
            fileType = xdr.readUnsignedInt();
            break;
          case Nfsv4Attr.FATTR4_SIZE:
            size = Number(xdr.readUnsignedHyper());
            break;
          case Nfsv4Attr.FATTR4_FILEID:
            fileid = Number(xdr.readUnsignedHyper());
            break;
          case Nfsv4Attr.FATTR4_MODE:
            mode = xdr.readUnsignedInt();
            break;
          case Nfsv4Attr.FATTR4_NUMLINKS:
            nlink = xdr.readUnsignedInt();
            break;
          case Nfsv4Attr.FATTR4_SPACE_USED:
            spaceUsed = Number(xdr.readUnsignedHyper());
            break;
          case Nfsv4Attr.FATTR4_TIME_ACCESS: {
            const seconds = Number(xdr.readHyper());
            const nseconds = xdr.readUnsignedInt();
            atime = new Date(seconds * 1000 + nseconds / 1000000);
            break;
          }
          case Nfsv4Attr.FATTR4_TIME_MODIFY: {
            const seconds = Number(xdr.readHyper());
            const nseconds = xdr.readUnsignedInt();
            mtime = new Date(seconds * 1000 + nseconds / 1000000);
            break;
          }
          case Nfsv4Attr.FATTR4_TIME_METADATA: {
            const seconds = Number(xdr.readHyper());
            const nseconds = xdr.readUnsignedInt();
            ctime = new Date(seconds * 1000 + nseconds / 1000000);
            break;
          }
        }
      }
    }
    const blocks = Math.ceil(spaceUsed / 512);
    return new NfsFsStats(
      0,
      0,
      0,
      4096,
      fileid,
      size,
      blocks,
      atime,
      mtime,
      ctime,
      mtime,
      atime.getTime(),
      mtime.getTime(),
      ctime.getTime(),
      mtime.getTime(),
      0,
      mode,
      nlink,
      fileType,
    );
  };
 
  public readonly lstat = async (path: misc.PathLike, options?: opts.IStatOptions): Promise<misc.IStats> => {
    return this.stat(path, options);
  };
 
  public readonly mkdir = async (
    path: misc.PathLike,
    options?: misc.TMode | opts.IMkdirOptions,
  ): Promise<string | undefined> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    Iif (parts.length === 0) {
      throw new Error('Cannot create root directory');
    }
    const operations = this.navigateToParent(parts);
    const dirname = parts[parts.length - 1];
    const createType = nfs.CreateTypeDir();
    const emptyAttrs = nfs.Fattr([], new Uint8Array(0));
    operations.push(nfs.CREATE(createType, dirname, emptyAttrs));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to create directory: ${response.status}`);
    }
    const createRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4CreateResponse;
    Iif (createRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to create directory: ${createRes.status}`);
    }
    return undefined;
  };
 
  public readonly readdir = async (
    path: misc.PathLike,
    options?: opts.IReaddirOptions | string,
  ): Promise<misc.TDataOut[] | misc.IDirent[]> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const withFileTypes = typeof options === 'object' && options?.withFileTypes;
    const encoding = typeof options === 'string' ? options : options?.encoding;
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    const attrNums = withFileTypes ? [Nfsv4Attr.FATTR4_TYPE] : [];
    const attrMask = this.attrNumsToBitmap(attrNums);
    operations.push(nfs.READDIR(attrMask));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to read directory: ${response.status}`);
    }
    const readdirRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4ReaddirResponse;
    Iif (readdirRes.status !== Nfsv4Stat.NFS4_OK || !readdirRes.resok) {
      throw new Error(`Failed to read directory: ${readdirRes.status}`);
    }
    const entries: string[] = [];
    const dirents: misc.IDirent[] = [];
    const entryList = readdirRes.resok.entries;
    for (let i = 0; i < entryList.length; i++) {
      const entry = entryList[i];
      const name = entry.name;
      if (withFileTypes) {
        const fattr = entry.attrs;
        const reader = new Reader();
        reader.reset(fattr.attrVals);
        const xdr = new XdrDecoder(reader);
        let fileType = Nfsv4FType.NF4REG;
        const returnedMask = fattr.attrmask.mask;
        for (let i = 0; i < returnedMask.length; i++) {
          const word = returnedMask[i];
          Iif (!word) continue;
          for (let bit = 0; bit < 32; bit++) {
            if (!(word & (1 << bit))) continue;
            const attrNum = i * 32 + bit;
            if (attrNum === Nfsv4Attr.FATTR4_TYPE) {
              fileType = xdr.readUnsignedInt();
            }
          }
        }
        dirents.push(new NfsFsDirent(name, fileType));
      } else {
        entries.push(name);
      }
    }
    if (withFileTypes) {
      return dirents;
    }
    Iif (encoding && encoding !== 'utf8') {
      return entries.map((name) => Buffer.from(name, 'utf8'));
    }
    return entries;
  };
 
  public readonly appendFile = async (
    path: misc.TFileHandle,
    data: misc.TData,
    options?: opts.IAppendFileOptions | string,
  ): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToParent(parts);
    const filename = parts[parts.length - 1];
    const openOwner = this.createDefaultOpenOwner();
    const claim = nfs.OpenClaimNull(filename);
    const openSeqid = this.nextOpenOwnerSeqid(openOwner);
    operations.push(
      nfs.OPEN(
        openSeqid,
        Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE,
        Nfsv4OpenDeny.OPEN4_SHARE_DENY_NONE,
        openOwner,
        nfs.OpenHowNoCreate(),
        claim,
      ),
    );
    const attrNums = [Nfsv4Attr.FATTR4_SIZE];
    const attrMask = this.attrNumsToBitmap(attrNums);
    operations.push(nfs.GETATTR(attrMask));
    const openResponse = await this.fs.compound(operations);
    Iif (openResponse.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to open file: ${openResponse.status}`);
    }
    const openRes = openResponse.resarray[openResponse.resarray.length - 2] as msg.Nfsv4OpenResponse;
    Iif (openRes.status !== Nfsv4Stat.NFS4_OK || !openRes.resok) {
      throw new Error(`Failed to open file: ${openRes.status}`);
    }
    const getattrRes = openResponse.resarray[openResponse.resarray.length - 1] as msg.Nfsv4GetattrResponse;
    Iif (getattrRes.status !== Nfsv4Stat.NFS4_OK || !getattrRes.resok) {
      throw new Error(`Failed to get attributes: ${getattrRes.status}`);
    }
    const fattr = getattrRes.resok.objAttributes;
    const reader = new Reader();
    reader.reset(fattr.attrVals);
    const xdr = new XdrDecoder(reader);
    const currentSize = Number(xdr.readUnsignedHyper());
    const openStateid = openRes.resok.stateid;
    const buffer = this.encodeData(data);
    const chunkSize = 65536;
    try {
      let offset = BigInt(currentSize);
      for (let i = 0; i < buffer.length; i += chunkSize) {
        const chunk = buffer.slice(i, Math.min(i + chunkSize, buffer.length));
        const writeResponse = await this.fs.compound([
          nfs.WRITE(openStateid, offset, Nfsv4StableHow.FILE_SYNC4, chunk),
        ]);
        Iif (writeResponse.status !== Nfsv4Stat.NFS4_OK) {
          throw new Error(`Failed to write file: ${writeResponse.status}`);
        }
        const writeRes = writeResponse.resarray[0] as msg.Nfsv4WriteResponse;
        Iif (writeRes.status !== Nfsv4Stat.NFS4_OK || !writeRes.resok) {
          throw new Error(`Failed to write file: ${writeRes.status}`);
        }
        offset += BigInt(writeRes.resok.count);
      }
    } finally {
      await this.closeStateid(openOwner, openStateid);
    }
  };
 
  public readonly truncate = async (path: misc.PathLike, len: number = 0): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    const writer = new Writer(16);
    const xdr = new XdrEncoder(writer);
    xdr.writeUnsignedHyper(BigInt(len));
    const attrVals = writer.flush();
    const sizeAttrs = nfs.Fattr([Nfsv4Attr.FATTR4_SIZE], attrVals);
    const stateid = nfs.Stateid(0, new Uint8Array(12));
    operations.push(nfs.SETATTR(stateid, sizeAttrs));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to truncate file: ${response.status}`);
    }
    const setattrRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4SetattrResponse;
    Iif (setattrRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to truncate file: ${setattrRes.status}`);
    }
  };
 
  public readonly unlink = async (path: misc.PathLike): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    Iif (parts.length === 0) {
      throw new Error('Cannot unlink root directory');
    }
    const operations = this.navigateToParent(parts);
    const filename = parts[parts.length - 1];
    operations.push(nfs.REMOVE(filename));
    const response = await this.fs.compound(operations);
    if (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to unlink file: ${response.status}`);
    }
    const removeRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4RemoveResponse;
    Iif (removeRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to unlink file: ${removeRes.status}`);
    }
  };
 
  public readonly rmdir = async (path: misc.PathLike, options?: opts.IRmdirOptions): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    Iif (parts.length === 0) {
      throw new Error('Cannot remove root directory');
    }
    const operations = this.navigateToParent(parts);
    const dirname = parts[parts.length - 1];
    operations.push(nfs.REMOVE(dirname));
    const response = await this.fs.compound(operations);
    if (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to remove directory: ${response.status}`);
    }
    const removeRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4RemoveResponse;
    Iif (removeRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to remove directory: ${removeRes.status}`);
    }
  };
 
  public readonly rm = async (path: misc.PathLike, options?: opts.IRmOptions): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    Iif (parts.length === 0) {
      throw new Error('Cannot remove root directory');
    }
    const force = options?.force ?? false;
    const recursive = options?.recursive ?? false;
    if (recursive) {
      try {
        const stats = await this.stat(path);
        if (stats.isDirectory()) {
          const entries = await this.readdir(path);
          for (const entry of entries) {
            const entryPath = pathStr + '/' + entry;
            await this.rm(entryPath, options);
          }
        }
      } catch (err) {
        Iif (!force) throw err;
        return;
      }
    }
    try {
      const operations = this.navigateToParent(parts);
      const name = parts[parts.length - 1];
      operations.push(nfs.REMOVE(name));
      const response = await this.fs.compound(operations);
      if (response.status !== Nfsv4Stat.NFS4_OK) {
        if (!force) throw new Error(`Failed to remove: ${response.status}`);
        return;
      }
      const removeRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4RemoveResponse;
      Iif (removeRes.status !== Nfsv4Stat.NFS4_OK) {
        Iif (!force) throw new Error(`Failed to remove: ${removeRes.status}`);
      }
    } catch (err) {
      if (!force) throw err;
    }
  };
 
  public readonly access = async (path: misc.PathLike, mode: number = 0): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    let accessMask = 0;
    if (mode === 0) {
      accessMask = Nfsv4Access.ACCESS4_READ;
    } else E{
      Iif (mode & 4) accessMask |= Nfsv4Access.ACCESS4_READ;
      Iif (mode & 2) accessMask |= Nfsv4Access.ACCESS4_MODIFY;
      Iif (mode & 1) accessMask |= Nfsv4Access.ACCESS4_EXECUTE;
    }
    operations.push(nfs.ACCESS(accessMask));
    const response = await this.fs.compound(operations);
    if (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Access denied: ${response.status}`);
    }
    const accessRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4AccessResponse;
    Iif (accessRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Access denied: ${accessRes.status}`);
    }
  };
 
  public readonly rename = async (oldPath: misc.PathLike, newPath: misc.PathLike): Promise<void> => {
    const oldPathStr = typeof oldPath === 'string' ? oldPath : oldPath.toString();
    const newPathStr = typeof newPath === 'string' ? newPath : newPath.toString();
    const oldParts = this.parsePath(oldPathStr);
    const newParts = this.parsePath(newPathStr);
    Iif (oldParts.length === 0 || newParts.length === 0) {
      throw new Error('Cannot rename root directory');
    }
    const operations: msg.Nfsv4Request[] = [];
    operations.push(nfs.PUTROOTFH());
    for (const part of oldParts.slice(0, -1)) {
      operations.push(nfs.LOOKUP(part));
    }
    operations.push(nfs.SAVEFH());
    operations.push(nfs.PUTROOTFH());
    for (const part of newParts.slice(0, -1)) {
      operations.push(nfs.LOOKUP(part));
    }
    const oldname = oldParts[oldParts.length - 1];
    const newname = newParts[newParts.length - 1];
    operations.push(nfs.RENAME(oldname, newname));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to rename: ${response.status}`);
    }
    const renameRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4RenameResponse;
    Iif (renameRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to rename: ${renameRes.status}`);
    }
  };
 
  public readonly copyFile = async (
    src: misc.PathLike,
    dest: misc.PathLike,
    flags?: misc.TFlagsCopy,
  ): Promise<void> => {
    const data = await this.readFile(src);
    await this.writeFile(dest, data);
  };
 
  public readonly realpath = async (
    path: misc.PathLike,
    options?: opts.IRealpathOptions | string,
  ): Promise<misc.TDataOut> => {
    const encoding = typeof options === 'string' ? options : options?.encoding;
    const pathStr = typeof path === 'string' ? path : path.toString();
    const normalized = '/' + this.parsePath(pathStr).join('/');
    if (!encoding || encoding === 'utf8') {
      return normalized;
    }
    return Buffer.from(normalized, 'utf8');
  };
 
  public readonly link = async (existingPath: misc.PathLike, newPath: misc.PathLike): Promise<void> => {
    const existingPathStr = typeof existingPath === 'string' ? existingPath : existingPath.toString();
    const newPathStr = typeof newPath === 'string' ? newPath : newPath.toString();
    const existingParts = this.parsePath(existingPathStr);
    const newParts = this.parsePath(newPathStr);
    Iif (newParts.length === 0) {
      throw new Error('Cannot create link at root');
    }
    const operations = this.navigateToPath(existingParts);
    operations.push(nfs.SAVEFH());
    operations.push(nfs.PUTROOTFH());
    for (const part of newParts.slice(0, -1)) {
      operations.push(nfs.LOOKUP(part));
    }
    const newname = newParts[newParts.length - 1];
    operations.push(nfs.LINK(newname));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to create link: ${response.status}`);
    }
    const linkRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4LinkResponse;
    Iif (linkRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to create link: ${linkRes.status}`);
    }
  };
 
  public readonly symlink = async (
    target: misc.PathLike,
    path: misc.PathLike,
    type?: misc.symlink.Type,
  ): Promise<void> => {
    const targetStr = typeof target === 'string' ? target : target.toString();
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    Iif (parts.length === 0) {
      throw new Error('Cannot create symlink at root');
    }
    const operations = this.navigateToParent(parts);
    const linkname = parts[parts.length - 1];
    const createType = new structs.Nfsv4CreateType(Nfsv4FType.NF4LNK, new structs.Nfsv4CreateTypeLink(targetStr));
    const emptyAttrs = nfs.Fattr([], new Uint8Array(0));
    operations.push(nfs.CREATE(createType, linkname, emptyAttrs));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to create symlink: ${response.status}`);
    }
    const createRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4CreateResponse;
    Iif (createRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to create symlink: ${createRes.status}`);
    }
  };
 
  public readonly utimes = async (path: misc.PathLike, atime: misc.TTime, mtime: misc.TTime): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    const atimeMs = typeof atime === 'number' ? atime : atime instanceof Date ? atime.getTime() : Date.now();
    const mtimeMs = typeof mtime === 'number' ? mtime : mtime instanceof Date ? mtime.getTime() : Date.now();
    const writer = new Writer(64);
    const xdr = new XdrEncoder(writer);
    xdr.writeUnsignedInt(1);
    xdr.writeHyper(BigInt(Math.floor(atimeMs / 1000)));
    xdr.writeUnsignedInt((atimeMs % 1000) * 1000000);
    xdr.writeUnsignedInt(1);
    xdr.writeHyper(BigInt(Math.floor(mtimeMs / 1000)));
    xdr.writeUnsignedInt((mtimeMs % 1000) * 1000000);
    const attrVals = writer.flush();
    const timeAttrs = nfs.Fattr([Nfsv4Attr.FATTR4_TIME_ACCESS_SET, Nfsv4Attr.FATTR4_TIME_MODIFY_SET], attrVals);
    const stateid = nfs.Stateid(0, new Uint8Array(12));
    operations.push(nfs.SETATTR(stateid, timeAttrs));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to set times: ${response.status}`);
    }
    const setattrRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4SetattrResponse;
    Iif (setattrRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to set times: ${setattrRes.status}`);
    }
  };
 
  public readonly readlink = async (path: misc.PathLike, options?: opts.IOptions): Promise<misc.TDataOut> => {
    const encoding = typeof options === 'string' ? options : options?.encoding;
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    operations.push(nfs.READLINK());
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to read link: ${response.status}`);
    }
    const readlinkRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4ReadlinkResponse;
    Iif (readlinkRes.status !== Nfsv4Stat.NFS4_OK || !readlinkRes.resok) {
      throw new Error(`Failed to read link: ${readlinkRes.status}`);
    }
    if (!encoding || encoding === 'utf8') {
      return readlinkRes.resok.link;
    }
    return Buffer.from(readlinkRes.resok.link, 'utf8');
  };
 
  public readonly opendir = async (path: misc.PathLike, options?: opts.IOpendirOptions): Promise<misc.IDir> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    return new NfsFsDir(pathStr, this.fs, operations);
  };
 
  public readonly mkdtemp = async (prefix: string, options?: opts.IOptions): Promise<misc.TDataOut> => {
    const encoding = typeof options === 'string' ? options : options?.encoding;
    const randomSuffix = Math.random().toString(36).substring(2, 8);
    const dirName = prefix + randomSuffix;
    await this.mkdir(dirName);
    if (!encoding || encoding === 'utf8') return dirName;
    return Buffer.from(dirName, 'utf8');
  };
 
  public readonly chmod = async (path: misc.PathLike, mode: misc.TMode): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    const modeValue = typeof mode === 'number' ? mode : parseInt(mode.toString(), 8);
    const writer = new Writer(8);
    const xdr = new XdrEncoder(writer);
    xdr.writeUnsignedInt(modeValue);
    const attrVals = writer.flush();
    const attrs = nfs.Fattr([Nfsv4Attr.FATTR4_MODE], attrVals);
    const stateid = nfs.Stateid(0, new Uint8Array(12));
    operations.push(nfs.SETATTR(stateid, attrs));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to chmod: ${response.status}`);
    }
    const setattrRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4SetattrResponse;
    Iif (setattrRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to chmod: ${setattrRes.status}`);
    }
  };
 
  public readonly chown = async (path: misc.PathLike, uid: number, gid: number): Promise<void> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToPath(parts);
    const writer = new Writer(64);
    const xdr = new XdrEncoder(writer);
    xdr.writeStr(uid.toString());
    xdr.writeStr(gid.toString());
    const attrVals = writer.flush();
    const attrs = nfs.Fattr([Nfsv4Attr.FATTR4_OWNER, Nfsv4Attr.FATTR4_OWNER_GROUP], attrVals);
    const stateid = nfs.Stateid(0, new Uint8Array(12));
    operations.push(nfs.SETATTR(stateid, attrs));
    const response = await this.fs.compound(operations);
    Iif (response.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to chown: ${response.status}`);
    }
    const setattrRes = response.resarray[response.resarray.length - 1] as msg.Nfsv4SetattrResponse;
    Iif (setattrRes.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to chown: ${setattrRes.status}`);
    }
  };
 
  public readonly lchmod = async (path: misc.PathLike, mode: misc.TMode): Promise<void> => {
    return this.chmod(path, mode);
  };
 
  public readonly lchown = async (path: misc.PathLike, uid: number, gid: number): Promise<void> => {
    return this.chown(path, uid, gid);
  };
 
  public readonly lutimes = async (path: misc.PathLike, atime: misc.TTime, mtime: misc.TTime): Promise<void> => {
    return this.utimes(path, atime, mtime);
  };
 
  public readonly open = async (
    path: misc.PathLike,
    flags?: misc.TFlags,
    mode?: misc.TMode,
  ): Promise<misc.IFileHandle> => {
    const pathStr = typeof path === 'string' ? path : path.toString();
    const parts = this.parsePath(pathStr);
    const operations = this.navigateToParent(parts);
    const filename = parts[parts.length - 1];
    const openOwner = this.createDefaultOpenOwner();
    const claim = nfs.OpenClaimNull(filename);
    let access = Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_READ;
    const openSeqid = this.nextOpenOwnerSeqid(openOwner);
    if (typeof flags === 'string') {
      if (flags.includes('r') && flags.includes('+')) {
        access = Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_BOTH;
      } else if (flags.includes('w') || flags.includes('a')) {
        access = Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE;
        Iif (flags.includes('+')) {
          access = Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_BOTH;
        }
      }
    } else IEif (typeof flags === 'number') {
      const O_RDONLY = 0;
      const O_WRONLY = 1;
      const O_RDWR = 2;
      const O_ACCMODE = 3;
      const accessMode = flags & O_ACCMODE;
      switch (accessMode) {
        case O_RDONLY:
          access = Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_READ;
          break;
        case O_WRONLY:
          access = Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE;
          break;
        case O_RDWR:
          access = Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_BOTH;
          break;
      }
    }
    operations.push(
      nfs.OPEN(openSeqid, access, Nfsv4OpenDeny.OPEN4_SHARE_DENY_NONE, openOwner, nfs.OpenHowNoCreate(), claim),
    );
    const openResponse = await this.fs.compound(operations);
    Iif (openResponse.status !== Nfsv4Stat.NFS4_OK) {
      throw new Error(`Failed to open file: ${openResponse.status}`);
    }
    const openRes = openResponse.resarray[openResponse.resarray.length - 1] as msg.Nfsv4OpenResponse;
    Iif (openRes.status !== Nfsv4Stat.NFS4_OK || !openRes.resok) {
      throw new Error(`Failed to open file: ${openRes.status}`);
    }
    const stateid = openRes.resok.stateid;
    const fd = Math.floor(Math.random() * 1000000);
    return new NfsFsFileHandle(fd, pathStr, this, stateid, openOwner);
  };
 
  public readonly statfs = (path: misc.PathLike, options?: opts.IStatOptions): Promise<misc.IStatFs> => {
    throw new Error('Not implemented.');
  };
 
  public readonly watch = (
    filename: misc.PathLike,
    options?: opts.IWatchOptions,
  ): AsyncIterableIterator<{
    eventType: string;
    filename: string | Buffer;
  }> => {
    throw new Error('Not implemented.');
  };
 
  public readonly glob = (pattern: string, options?: opts.IGlobOptions): Promise<string[]> => {
    throw new Error('Not implemented.');
  };
}