All files / json-pack/src/nfs/v3/mount MountEncoder.ts

86.36% Statements 57/66
68% Branches 17/25
93.33% Functions 14/15
87.5% Lines 56/64

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 1371x 1x   1x         1x     21x 21x       29x 22x 29x                 7x       2x   1x   2x   1x   1x             22x       14x   3x           5x             4x 4x       14x       7x 7x 3x 3x   4x 4x 4x 4x       12x 12x 6x 6x   6x 6x 6x       11x 11x 5x 5x   6x 6x 6x 6x       2x       14x 14x 14x 4x 4x 4x 11x           3x       2x       5x      
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {XdrEncoder} from '../../../xdr/XdrEncoder';
import {MountProc} from './constants';
import {Nfsv3EncodingError} from '../errors';
import type * as msg from './messages';
import type * as structs from './structs';
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers';
 
export class MountEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> {
  protected readonly xdr: XdrEncoder;
 
  constructor(public readonly writer: W = new Writer() as any) {
    this.xdr = new XdrEncoder(writer);
  }
 
  public encodeMessage(message: msg.MountMessage, proc: MountProc, isRequest: boolean): Uint8Array {
    if (isRequest) this.writeRequest(message as msg.MountRequest, proc);
    else this.writeResponse(message as msg.MountResponse, proc);
    return this.writer.flush();
  }
 
  public writeMessage(message: msg.MountMessage, proc: MountProc, isRequest: boolean): void {
    if (isRequest) this.writeRequest(message as msg.MountRequest, proc);
    else this.writeResponse(message as msg.MountResponse, proc);
  }
 
  private writeRequest(request: msg.MountRequest, proc: MountProc): void {
    switch (proc) {
      case MountProc.NULL:
        return;
      case MountProc.MNT:
        return this.writeMntRequest(request as msg.MountMntRequest);
      case MountProc.DUMP:
        return;
      case MountProc.UMNT:
        return this.writeUmntRequest(request as msg.MountUmntRequest);
      case MountProc.UMNTALL:
        return;
      case MountProc.EXPORT:
        return;
      default:
        throw new Nfsv3EncodingError(`Unknown MOUNT procedure: ${proc}`);
    }
  }
 
  private writeResponse(response: msg.MountResponse, proc: MountProc): void {
    switch (proc) {
      case MountProc.NULL:
        return;
      case MountProc.MNT:
        return this.writeMntResponse(response as msg.MountMntResponse);
      case MountProc.DUMP:
        return this.writeDumpResponse(response as msg.MountDumpResponse);
      case MountProc.UMNT:
        return;
      case MountProc.UMNTALL:
        return;
      case MountProc.EXPORT:
        return this.writeExportResponse(response as msg.MountExportResponse);
      default:
        throw new Nfsv3EncodingError(`Unknown MOUNT procedure: ${proc}`);
    }
  }
 
  private writeFhandle3(fh: structs.MountFhandle3): void {
    const data = fh.data.uint8;
    this.xdr.writeVarlenOpaque(data);
  }
 
  private writeDirpath(path: string): void {
    this.xdr.writeStr(path);
  }
 
  private writeMountBody(body: structs.MountBody | undefined): void {
    const xdr = this.xdr;
    if (!body) {
      xdr.writeBoolean(false);
      return;
    }
    xdr.writeBoolean(true);
    xdr.writeStr(body.hostname);
    this.writeDirpath(body.directory);
    this.writeMountBody(body.next);
  }
 
  private writeGroupNode(group: structs.MountGroupNode | undefined): void {
    const xdr = this.xdr;
    if (!group) {
      xdr.writeBoolean(false);
      return;
    }
    xdr.writeBoolean(true);
    xdr.writeStr(group.name);
    this.writeGroupNode(group.next);
  }
 
  private writeExportNode(exportNode: structs.MountExportNode | undefined): void {
    const xdr = this.xdr;
    if (!exportNode) {
      xdr.writeBoolean(false);
      return;
    }
    xdr.writeBoolean(true);
    this.writeDirpath(exportNode.dir);
    this.writeGroupNode(exportNode.groups);
    this.writeExportNode(exportNode.next);
  }
 
  private writeMntRequest(req: msg.MountMntRequest): void {
    this.writeDirpath(req.dirpath);
  }
 
  private writeMntResponse(res: msg.MountMntResponse): void {
    const xdr = this.xdr;
    xdr.writeUnsignedInt(res.status);
    if (res.status === 0 && res.mountinfo) {
      this.writeFhandle3(res.mountinfo.fhandle);
      xdr.writeUnsignedInt(res.mountinfo.authFlavors.length);
      for (const flavor of res.mountinfo.authFlavors) {
        xdr.writeUnsignedInt(flavor);
      }
    }
  }
 
  private writeDumpResponse(res: msg.MountDumpResponse): void {
    this.writeMountBody(res.mountlist);
  }
 
  private writeUmntRequest(req: msg.MountUmntRequest): void {
    this.writeDirpath(req.dirpath);
  }
 
  private writeExportResponse(res: msg.MountExportResponse): void {
    this.writeExportNode(res.exports);
  }
}