All files / urkel/lib/optimized file.js

2.97% Statements 3/101
0% Branches 0/44
0% Functions 0/15
2.97% Lines 3/101

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                1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                           1x  
/*!
 * file.js - tree file backend
 * Copyright (c) 2018, Christopher Jeffrey (MIT License).
 * https://github.com/handshake-org/urkel
 */
 
'use strict';
 
const assert = require('bsert');
const {IOError} = require('./errors');
 
/**
 * File
 */
 
class File {
  constructor(fs, index) {
    assert(fs);
    assert((index >>> 0) === index);
 
    this.fs = fs;
    this.index = index;
    this.fd = -1;
    this.size = 0;
    this.reads = 0;
  }
 
  async open(path, flags) {
    assert(typeof path === 'string');
    assert(typeof flags === 'string');
 
    if (this.fd !== -1)
      throw new Error('File already open.');
 
    this.fd = await this.fs.open(path, flags, 0o640);
 
    const stat = await this.fs.fstat(this.fd);
 
    this.size = stat.size;
  }
 
  openSync(path, flags) {
    assert(typeof path === 'string');
    assert(typeof flags === 'string');
 
    if (this.fd !== -1)
      throw new Error('File already open.');
 
    this.fd = this.fs.openSync(path, flags, 0o640);
 
    const stat = this.fs.fstatSync(this.fd);
 
    this.size = stat.size;
  }
 
  async close() {
    if (this.fd === -1)
      throw new Error('File already closed.');
 
    const fd = this.fd;
 
    this.fd = -1;
    this.size = 0;
    this.reads = 0;
 
    return this.fs.close(fd);
  }
 
  closeSync() {
    if (this.fd === -1)
      throw new Error('File already closed.');
 
    const fd = this.fd;
 
    this.fd = -1;
    this.size = 0;
    this.reads = 0;
 
    this.fs.closeSync(fd);
  }
 
  async sync() {
    if (this.fd === -1)
      throw new Error('File already closed.');
 
    return this.fs.fsync(this.fd);
  }
 
  syncSync() {
    if (this.fd === -1)
      throw new Error('File already closed.');
 
    this.fs.fsyncSync(this.fd);
  }
 
  async truncate(size) {
    if (this.fd === -1)
      throw new Error('File already closed.');
 
    if (size === this.size)
      return undefined;
 
    return this.fs.ftruncate(this.fd, size);
  }
 
  truncateSync(size) {
    if (this.fd === -1)
      throw new Error('File already closed.');
 
    if (size === this.size)
      return;
 
    this.fs.ftruncateSync(this.fd, size);
  }
 
  async read(pos, size) {
    if (this.fd === -1)
      throw new Error('File is closed.');
 
    const buf = Buffer.allocUnsafe(size);
 
    this.reads += 1;
 
    let r;
 
    try {
      r = await this.fs.read(this.fd, buf, 0, size, pos);
    } finally {
      this.reads -= 1;
    }
 
    if (r !== size)
      throw new IOError('read', this.index, pos, size);
 
    return buf;
  }
 
  readSync(pos, size) {
    if (this.fd === -1)
      throw new Error('File is closed.');
 
    const buf = Buffer.allocUnsafe(size);
    const r = this.fs.readSync(this.fd, buf, 0, size, pos);
 
    if (r !== size)
      throw new IOError('read', this.index, pos, size);
 
    return buf;
  }
 
  async write(data) {
    if (this.fd === -1)
      throw new Error('File is closed.');
 
    const pos = this.size;
 
    const w = await this.fs.write(this.fd, data, 0, data.length, null);
 
    if (w !== data.length)
      throw new IOError('write', this.index, pos, data.length);
 
    this.size += w;
 
    return pos;
  }
 
  writeSync(data) {
    if (this.fd === -1)
      throw new Error('File is closed.');
 
    const pos = this.size;
 
    const w = this.fs.writeSync(this.fd, data, 0, data.length, null);
 
    if (w !== data.length)
      throw new IOError('write', this.index, pos, data.length);
 
    this.size += w;
 
    return pos;
  }
 
  async rawRead(pos, size, out) {
    assert(size <= out.length);
 
    if (this.fd === -1)
      throw new Error('File is closed.');
 
    const r = await this.fs.read(this.fd, out, 0, size, pos);
 
    if (r !== size)
      throw new IOError('read', this.index, pos, size);
 
    return out;
  }
 
  rawReadSync(pos, size, out) {
    assert(size <= out.length);
 
    if (this.fd === -1)
      throw new Error('File is closed.');
 
    const r = this.fs.readSync(this.fd, out, 0, size, pos);
 
    if (r !== size)
      throw new IOError('read', this.index, pos, size);
 
    return out;
  }
}
 
/*
 * Expose
 */
 
module.exports = File;