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

92.7% Statements 89/96
79.31% Branches 23/29
96.29% Functions 26/27
93.61% Lines 88/94

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 2061x 1x   1x         1x     30x 30x       41x 19x 41x                 22x       10x   4x   1x   1x   1x   2x   1x   1x   1x             19x       2x           15x     2x             40x 40x       43x 43x       1x 1x 1x 1x 1x 1x       18x 18x 18x 18x 18x 18x 18x       3x 3x 3x 3x 3x 3x       11x 11x 11x       5x 5x 5x 5x 5x 5x 5x       1x 1x 1x 1x 1x       1x 1x       3x 3x 3x       10x       2x 2x 2x 2x 1x         4x       15x 15x       1x       1x       1x       2x       2x 2x 2x 2x       1x       1x       1x 1x 1x      
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {XdrEncoder} from '../../../xdr/XdrEncoder';
import {NlmProc} 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 NlmEncoder<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.NlmMessage, proc: NlmProc, isRequest: boolean): Uint8Array {
    if (isRequest) this.writeRequest(message as msg.NlmRequest, proc);
    else this.writeResponse(message as msg.NlmResponse, proc);
    return this.writer.flush();
  }
 
  public writeMessage(message: msg.NlmMessage, proc: NlmProc, isRequest: boolean): void {
    if (isRequest) this.writeRequest(message as msg.NlmRequest, proc);
    else this.writeResponse(message as msg.NlmResponse, proc);
  }
 
  private writeRequest(request: msg.NlmRequest, proc: NlmProc): void {
    switch (proc) {
      case NlmProc.NULL:
        return;
      case NlmProc.TEST:
        return this.writeTestRequest(request as msg.Nlm4TestRequest);
      case NlmProc.LOCK:
        return this.writeLockRequest(request as msg.Nlm4LockRequest);
      case NlmProc.CANCEL:
        return this.writeCancelRequest(request as msg.Nlm4CancelRequest);
      case NlmProc.UNLOCK:
        return this.writeUnlockRequest(request as msg.Nlm4UnlockRequest);
      case NlmProc.GRANTED:
        return this.writeGrantedRequest(request as msg.Nlm4GrantedRequest);
      case NlmProc.SHARE:
        return this.writeShareRequest(request as msg.Nlm4ShareRequest);
      case NlmProc.UNSHARE:
        return this.writeUnshareRequest(request as msg.Nlm4UnshareRequest);
      case NlmProc.NM_LOCK:
        return this.writeNmLockRequest(request as msg.Nlm4NmLockRequest);
      case NlmProc.FREE_ALL:
        return this.writeFreeAllRequest(request as msg.Nlm4FreeAllRequest);
      default:
        throw new Nfsv3EncodingError(`Unknown NLM procedure: ${proc}`);
    }
  }
 
  private writeResponse(response: msg.NlmResponse, proc: NlmProc): void {
    switch (proc) {
      case NlmProc.NULL:
        return;
      case NlmProc.TEST:
        return this.writeTestResponse(response as msg.Nlm4TestResponse);
      case NlmProc.LOCK:
      case NlmProc.CANCEL:
      case NlmProc.UNLOCK:
      case NlmProc.GRANTED:
      case NlmProc.NM_LOCK:
        return this.writeResponse4(response as msg.Nlm4Response);
      case NlmProc.SHARE:
      case NlmProc.UNSHARE:
        return this.writeShareResponse(response as msg.Nlm4ShareResponse);
      default:
        throw new Nfsv3EncodingError(`Unknown NLM procedure: ${proc}`);
    }
  }
 
  private writeCookie(cookie: any): void {
    const data = cookie.uint8;
    this.xdr.writeVarlenOpaque(data);
  }
 
  private writeNetobj(obj: any): void {
    const data = obj.uint8;
    this.xdr.writeVarlenOpaque(data);
  }
 
  private writeNlm4Holder(holder: structs.Nlm4Holder): void {
    const xdr = this.xdr;
    xdr.writeBoolean(holder.exclusive);
    xdr.writeInt(holder.svid);
    this.writeNetobj(holder.oh);
    xdr.writeUnsignedHyper(holder.offset);
    xdr.writeUnsignedHyper(holder.length);
  }
 
  private writeNlm4Lock(lock: structs.Nlm4Lock): void {
    const xdr = this.xdr;
    xdr.writeStr(lock.callerName);
    this.writeNetobj(lock.fh);
    this.writeNetobj(lock.oh);
    xdr.writeInt(lock.svid);
    xdr.writeUnsignedHyper(lock.offset);
    xdr.writeUnsignedHyper(lock.length);
  }
 
  private writeNlm4Share(share: structs.Nlm4Share): void {
    const xdr = this.xdr;
    xdr.writeStr(share.callerName);
    this.writeNetobj(share.fh);
    this.writeNetobj(share.oh);
    xdr.writeUnsignedInt(share.mode);
    xdr.writeUnsignedInt(share.access);
  }
 
  private writeTestArgs(args: msg.Nlm4TestArgs): void {
    this.writeCookie(args.cookie);
    this.xdr.writeBoolean(args.exclusive);
    this.writeNlm4Lock(args.lock);
  }
 
  private writeLockArgs(args: msg.Nlm4LockArgs): void {
    const xdr = this.xdr;
    this.writeCookie(args.cookie);
    xdr.writeBoolean(args.block);
    xdr.writeBoolean(args.exclusive);
    this.writeNlm4Lock(args.lock);
    xdr.writeBoolean(args.reclaim);
    xdr.writeInt(args.state);
  }
 
  private writeCancelArgs(args: msg.Nlm4CancelArgs): void {
    const xdr = this.xdr;
    this.writeCookie(args.cookie);
    xdr.writeBoolean(args.block);
    xdr.writeBoolean(args.exclusive);
    this.writeNlm4Lock(args.lock);
  }
 
  private writeUnlockArgs(args: msg.Nlm4UnlockArgs): void {
    this.writeCookie(args.cookie);
    this.writeNlm4Lock(args.lock);
  }
 
  private writeShareArgs(args: msg.Nlm4ShareArgs): void {
    this.writeCookie(args.cookie);
    this.writeNlm4Share(args.share);
    this.xdr.writeBoolean(args.reclaim);
  }
 
  private writeTestRequest(req: msg.Nlm4TestRequest): void {
    this.writeTestArgs(req.args);
  }
 
  private writeTestResponse(res: msg.Nlm4TestResponse): void {
    const xdr = this.xdr;
    this.writeCookie(res.cookie);
    xdr.writeUnsignedInt(res.stat);
    if (res.stat === 1 && res.holder) {
      this.writeNlm4Holder(res.holder);
    }
  }
 
  private writeLockRequest(req: msg.Nlm4LockRequest): void {
    this.writeLockArgs(req.args);
  }
 
  private writeResponse4(res: msg.Nlm4Response): void {
    this.writeCookie(res.cookie);
    this.xdr.writeUnsignedInt(res.stat);
  }
 
  private writeCancelRequest(req: msg.Nlm4CancelRequest): void {
    this.writeCancelArgs(req.args);
  }
 
  private writeUnlockRequest(req: msg.Nlm4UnlockRequest): void {
    this.writeUnlockArgs(req.args);
  }
 
  private writeGrantedRequest(req: msg.Nlm4GrantedRequest): void {
    this.writeTestArgs(req.args);
  }
 
  private writeShareRequest(req: msg.Nlm4ShareRequest): void {
    this.writeShareArgs(req.args);
  }
 
  private writeShareResponse(res: msg.Nlm4ShareResponse): void {
    const xdr = this.xdr;
    this.writeCookie(res.cookie);
    xdr.writeUnsignedInt(res.stat);
    xdr.writeInt(res.sequence);
  }
 
  private writeUnshareRequest(req: msg.Nlm4UnshareRequest): void {
    this.writeShareArgs(req.args);
  }
 
  private writeNmLockRequest(req: msg.Nlm4NmLockRequest): void {
    this.writeLockArgs(req.args);
  }
 
  private writeFreeAllRequest(req: msg.Nlm4FreeAllRequest): void {
    const xdr = this.xdr;
    xdr.writeStr(req.notify.name);
    xdr.writeInt(req.notify.state);
  }
}