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

86.84% Statements 66/76
68.18% Branches 15/22
100% Functions 14/14
86.11% Lines 62/72

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 1421x 1x   1x 1x 1x   1x       21x       29x 29x 29x 29x 7x   22x                       7x       2x   1x   2x   1x   1x             22x       14x   3x           5x             4x 4x       14x       7x 7x 4x 4x 4x 4x       12x 12x 6x 6x 6x       11x 11x 6x 6x 6x 6x       2x 2x       14x 14x 14x 10x   4x 4x 4x 4x 11x   4x 4x       3x 3x       2x 2x       5x 5x      
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {XdrDecoder} from '../../../xdr/XdrDecoder';
import {MountProc} from './constants';
import {Nfsv3DecodingError} from '../errors';
import * as msg from './messages';
import * as structs from './structs';
 
export class MountDecoder {
  protected readonly xdr: XdrDecoder;
 
  constructor(reader: Reader = new Reader()) {
    this.xdr = new XdrDecoder(reader);
  }
 
  public decodeMessage(reader: Reader, proc: MountProc, isRequest: boolean): msg.MountMessage | undefined {
    this.xdr.reader = reader;
    const startPos = reader.x;
    try {
      if (isRequest) {
        return this.decodeRequest(proc);
      } else {
        return this.decodeResponse(proc);
      }
    } catch (err) {
      Iif (err instanceof RangeError) {
        reader.x = startPos;
        return undefined;
      }
      throw err;
    }
  }
 
  private decodeRequest(proc: MountProc): msg.MountRequest | undefined {
    switch (proc) {
      case MountProc.NULL:
        return undefined;
      case MountProc.MNT:
        return this.decodeMntRequest();
      case MountProc.DUMP:
        return new msg.MountDumpRequest();
      case MountProc.UMNT:
        return this.decodeUmntRequest();
      case MountProc.UMNTALL:
        return new msg.MountUmntallRequest();
      case MountProc.EXPORT:
        return new msg.MountExportRequest();
      default:
        throw new Nfsv3DecodingError(`Unknown MOUNT procedure: ${proc}`);
    }
  }
 
  private decodeResponse(proc: MountProc): msg.MountResponse | undefined {
    switch (proc) {
      case MountProc.NULL:
        return undefined;
      case MountProc.MNT:
        return this.decodeMntResponse();
      case MountProc.DUMP:
        return this.decodeDumpResponse();
      case MountProc.UMNT:
        return undefined;
      case MountProc.UMNTALL:
        return undefined;
      case MountProc.EXPORT:
        return this.decodeExportResponse();
      default:
        throw new Nfsv3DecodingError(`Unknown MOUNT procedure: ${proc}`);
    }
  }
 
  private readFhandle3(): structs.MountFhandle3 {
    const data = this.xdr.readVarlenOpaque();
    return new structs.MountFhandle3(new Reader(data));
  }
 
  private readDirpath(): string {
    return this.xdr.readString();
  }
 
  private readMountBody(): structs.MountBody | undefined {
    const valueFollows = this.xdr.readBoolean();
    if (!valueFollows) return undefined;
    const hostname = this.xdr.readString();
    const directory = this.readDirpath();
    const next = this.readMountBody();
    return new structs.MountBody(hostname, directory, next);
  }
 
  private readGroupNode(): structs.MountGroupNode | undefined {
    const valueFollows = this.xdr.readBoolean();
    if (!valueFollows) return undefined;
    const name = this.xdr.readString();
    const next = this.readGroupNode();
    return new structs.MountGroupNode(name, next);
  }
 
  private readExportNode(): structs.MountExportNode | undefined {
    const valueFollows = this.xdr.readBoolean();
    if (!valueFollows) return undefined;
    const dir = this.readDirpath();
    const groups = this.readGroupNode();
    const next = this.readExportNode();
    return new structs.MountExportNode(dir, groups, next);
  }
 
  private decodeMntRequest(): msg.MountMntRequest {
    const dirpath = this.readDirpath();
    return new msg.MountMntRequest(dirpath);
  }
 
  private decodeMntResponse(): msg.MountMntResponse {
    const xdr = this.xdr;
    const status = xdr.readUnsignedInt();
    if (status !== 0) {
      return new msg.MountMntResponse(status);
    }
    const fhandle = this.readFhandle3();
    const authFlavorsCount = xdr.readUnsignedInt();
    const authFlavors: number[] = [];
    for (let i = 0; i < authFlavorsCount; i++) {
      authFlavors.push(xdr.readUnsignedInt());
    }
    const mountinfo = new msg.MountMntResOk(fhandle, authFlavors);
    return new msg.MountMntResponse(status, mountinfo);
  }
 
  private decodeDumpResponse(): msg.MountDumpResponse {
    const mountlist = this.readMountBody();
    return new msg.MountDumpResponse(mountlist);
  }
 
  private decodeUmntRequest(): msg.MountUmntRequest {
    const dirpath = this.readDirpath();
    return new msg.MountUmntRequest(dirpath);
  }
 
  private decodeExportResponse(): msg.MountExportResponse {
    const exports = this.readExportNode();
    return new msg.MountExportResponse(exports);
  }
}