All files / json-pack/src/nfs/v3/locks NlmDecoder.ts

93.33% Statements 112/120
81.48% Branches 22/27
100% Functions 26/26
93.33% Lines 112/120

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 2311x 1x   1x 1x 1x   1x       30x       41x 41x 41x 41x 22x   19x                       22x       10x   4x   1x   1x   1x   2x   1x   1x   1x             19x       2x           15x     2x             40x 40x       43x 43x       1x 1x 1x 1x 1x 1x 1x       18x 18x 18x 18x 18x 18x 18x 18x       3x 3x 3x 3x 3x 3x 3x       11x 11x 11x 11x       5x 5x 5x 5x 5x 5x 5x 5x       1x 1x 1x 1x 1x 1x       1x 1x 1x       3x 3x 3x 3x       10x 10x       2x 2x 2x 2x 2x       4x 4x       15x 15x 15x       1x 1x       1x 1x       1x 1x       2x 2x       2x 2x 2x 2x 2x       1x 1x       1x 1x       1x 1x 1x 1x 1x      
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {XdrDecoder} from '../../../xdr/XdrDecoder';
import {NlmProc, Nlm4Stat} from './constants';
import {Nfsv3DecodingError} from '../errors';
import * as msg from './messages';
import * as structs from './structs';
 
export class NlmDecoder {
  protected readonly xdr: XdrDecoder;
 
  constructor(reader: Reader = new Reader()) {
    this.xdr = new XdrDecoder(reader);
  }
 
  public decodeMessage(reader: Reader, proc: NlmProc, isRequest: boolean): msg.NlmMessage | 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: NlmProc): msg.NlmRequest | undefined {
    switch (proc) {
      case NlmProc.NULL:
        return undefined;
      case NlmProc.TEST:
        return this.decodeTestRequest();
      case NlmProc.LOCK:
        return this.decodeLockRequest();
      case NlmProc.CANCEL:
        return this.decodeCancelRequest();
      case NlmProc.UNLOCK:
        return this.decodeUnlockRequest();
      case NlmProc.GRANTED:
        return this.decodeGrantedRequest();
      case NlmProc.SHARE:
        return this.decodeShareRequest();
      case NlmProc.UNSHARE:
        return this.decodeUnshareRequest();
      case NlmProc.NM_LOCK:
        return this.decodeNmLockRequest();
      case NlmProc.FREE_ALL:
        return this.decodeFreeAllRequest();
      default:
        throw new Nfsv3DecodingError(`Unknown NLM procedure: ${proc}`);
    }
  }
 
  private decodeResponse(proc: NlmProc): msg.NlmResponse | undefined {
    switch (proc) {
      case NlmProc.NULL:
        return undefined;
      case NlmProc.TEST:
        return this.decodeTestResponse();
      case NlmProc.LOCK:
      case NlmProc.CANCEL:
      case NlmProc.UNLOCK:
      case NlmProc.GRANTED:
      case NlmProc.NM_LOCK:
        return this.decodeResponse4();
      case NlmProc.SHARE:
      case NlmProc.UNSHARE:
        return this.decodeShareResponse();
      default:
        throw new Nfsv3DecodingError(`Unknown NLM procedure: ${proc}`);
    }
  }
 
  private readCookie(): Reader {
    const data = this.xdr.readVarlenOpaque();
    return new Reader(data);
  }
 
  private readNetobj(): Reader {
    const data = this.xdr.readVarlenOpaque();
    return new Reader(data);
  }
 
  private readNlm4Holder(): structs.Nlm4Holder {
    const xdr = this.xdr;
    const exclusive = xdr.readBoolean();
    const svid = xdr.readInt();
    const oh = this.readNetobj();
    const offset = xdr.readUnsignedHyper();
    const length = xdr.readUnsignedHyper();
    return new structs.Nlm4Holder(exclusive, svid, oh, offset, length);
  }
 
  private readNlm4Lock(): structs.Nlm4Lock {
    const xdr = this.xdr;
    const callerName = xdr.readString();
    const fh = this.readNetobj();
    const oh = this.readNetobj();
    const svid = xdr.readInt();
    const offset = xdr.readUnsignedHyper();
    const length = xdr.readUnsignedHyper();
    return new structs.Nlm4Lock(callerName, fh, oh, svid, offset, length);
  }
 
  private readNlm4Share(): structs.Nlm4Share {
    const xdr = this.xdr;
    const callerName = xdr.readString();
    const fh = this.readNetobj();
    const oh = this.readNetobj();
    const mode = xdr.readUnsignedInt();
    const access = xdr.readUnsignedInt();
    return new structs.Nlm4Share(callerName, fh, oh, mode, access);
  }
 
  private readTestArgs(): msg.Nlm4TestArgs {
    const cookie = this.readCookie();
    const exclusive = this.xdr.readBoolean();
    const lock = this.readNlm4Lock();
    return new msg.Nlm4TestArgs(cookie, exclusive, lock);
  }
 
  private readLockArgs(): msg.Nlm4LockArgs {
    const xdr = this.xdr;
    const cookie = this.readCookie();
    const block = xdr.readBoolean();
    const exclusive = xdr.readBoolean();
    const lock = this.readNlm4Lock();
    const reclaim = xdr.readBoolean();
    const state = xdr.readInt();
    return new msg.Nlm4LockArgs(cookie, block, exclusive, lock, reclaim, state);
  }
 
  private readCancelArgs(): msg.Nlm4CancelArgs {
    const xdr = this.xdr;
    const cookie = this.readCookie();
    const block = xdr.readBoolean();
    const exclusive = xdr.readBoolean();
    const lock = this.readNlm4Lock();
    return new msg.Nlm4CancelArgs(cookie, block, exclusive, lock);
  }
 
  private readUnlockArgs(): msg.Nlm4UnlockArgs {
    const cookie = this.readCookie();
    const lock = this.readNlm4Lock();
    return new msg.Nlm4UnlockArgs(cookie, lock);
  }
 
  private readShareArgs(): msg.Nlm4ShareArgs {
    const cookie = this.readCookie();
    const share = this.readNlm4Share();
    const reclaim = this.xdr.readBoolean();
    return new msg.Nlm4ShareArgs(cookie, share, reclaim);
  }
 
  private decodeTestRequest(): msg.Nlm4TestRequest {
    const args = this.readTestArgs();
    return new msg.Nlm4TestRequest(args);
  }
 
  private decodeTestResponse(): msg.Nlm4TestResponse {
    const xdr = this.xdr;
    const cookie = this.readCookie();
    const stat = xdr.readUnsignedInt() as Nlm4Stat;
    const holder = stat === Nlm4Stat.NLM4_DENIED ? this.readNlm4Holder() : undefined;
    return new msg.Nlm4TestResponse(cookie, stat, holder);
  }
 
  private decodeLockRequest(): msg.Nlm4LockRequest {
    const args = this.readLockArgs();
    return new msg.Nlm4LockRequest(args);
  }
 
  private decodeResponse4(): msg.Nlm4Response {
    const cookie = this.readCookie();
    const stat = this.xdr.readUnsignedInt() as Nlm4Stat;
    return new msg.Nlm4Response(cookie, stat);
  }
 
  private decodeCancelRequest(): msg.Nlm4CancelRequest {
    const args = this.readCancelArgs();
    return new msg.Nlm4CancelRequest(args);
  }
 
  private decodeUnlockRequest(): msg.Nlm4UnlockRequest {
    const args = this.readUnlockArgs();
    return new msg.Nlm4UnlockRequest(args);
  }
 
  private decodeGrantedRequest(): msg.Nlm4GrantedRequest {
    const args = this.readTestArgs();
    return new msg.Nlm4GrantedRequest(args);
  }
 
  private decodeShareRequest(): msg.Nlm4ShareRequest {
    const args = this.readShareArgs();
    return new msg.Nlm4ShareRequest(args);
  }
 
  private decodeShareResponse(): msg.Nlm4ShareResponse {
    const xdr = this.xdr;
    const cookie = this.readCookie();
    const stat = xdr.readUnsignedInt() as Nlm4Stat;
    const sequence = xdr.readInt();
    return new msg.Nlm4ShareResponse(cookie, stat, sequence);
  }
 
  private decodeUnshareRequest(): msg.Nlm4UnshareRequest {
    const args = this.readShareArgs();
    return new msg.Nlm4UnshareRequest(args);
  }
 
  private decodeNmLockRequest(): msg.Nlm4NmLockRequest {
    const args = this.readLockArgs();
    return new msg.Nlm4NmLockRequest(args);
  }
 
  private decodeFreeAllRequest(): msg.Nlm4FreeAllRequest {
    const xdr = this.xdr;
    const name = xdr.readString();
    const state = xdr.readInt();
    const notify = new structs.Nlm4Notify(name, state);
    return new msg.Nlm4FreeAllRequest(notify);
  }
}