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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | 2x 2x 2x 2x 44x 44x 44x 44x 44x 44x 44x 45x 44x 44x 44x 6x 5x 2x 2x 1x 1x 1x 1x 1x 10x 9x 9x 9x 9x 9x 9x 9x 9x 9x 94x 9x 2x 2x 4x 3x 1x 1x 9x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 24x 6x 6x 6x 3x 5x 5x 5x 5x 18x 18x 18x 18x 18x 18x 18x 18x 5x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 5x 6x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 5x 6x 6x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 2x 11x 2x 2x 2x 4x 4x | import {EventEmitter} from 'events';
import {Readable, Writable} from 'stream';
import type * as msg from '../messages';
import type * as structs from '../structs';
import {nfs} from '../builder';
import {Nfsv4Stat, Nfsv4StableHow} from '../constants';
import type * as misc from 'memfs/lib/node/types/misc';
import type * as opts from 'memfs/lib/node/types/options';
import type {Nfsv4FsClient} from './Nfsv4FsClient';
/**
* Implements Node.js-like FileHandle interface for NFS v4 file operations.
*/
export class NfsFsFileHandle extends EventEmitter implements misc.IFileHandle {
public readonly fd: number;
private closed: boolean = false;
constructor(
fd: number,
public readonly path: string,
private readonly client: Nfsv4FsClient,
private readonly stateid: structs.Nfsv4Stateid,
private readonly openOwner: structs.Nfsv4OpenOwner,
) {
super();
this.fd = fd;
}
getAsyncId(): number {
return this.fd;
}
async close(): Promise<void> {
if (this.closed) return;
this.closed = true;
await this.client.closeStateid(this.openOwner, this.stateid);
this.emit('close');
}
async stat(options?: opts.IStatOptions): Promise<misc.IStats> {
if (this.closed) throw new Error('File handle is closed');
return this.client.stat(this.path, options);
}
async appendFile(data: misc.TData, options?: opts.IAppendFileOptions | string): Promise<void> {
Iif (this.closed) throw new Error('File handle is closed');
return this.client.appendFile(this.path, data, options);
}
async chmod(mode: misc.TMode): Promise<void> {
Iif (this.closed) throw new Error('File handle is closed');
return this.client.chmod(this.path, mode);
}
async chown(uid: number, gid: number): Promise<void> {
Iif (this.closed) throw new Error('File handle is closed');
return this.client.chown(this.path, uid, gid);
}
async datasync(): Promise<void> {
Iif (this.closed) throw new Error('File handle is closed');
}
async read(
buffer: Buffer | Uint8Array,
offset: number,
length: number,
position?: number | null,
): Promise<misc.TFileHandleReadResult> {
if (this.closed) throw new Error('File handle is closed');
const readPos = position !== null && position !== undefined ? BigInt(position) : BigInt(0);
const readOps: msg.Nfsv4Request[] = [nfs.READ(readPos, length, this.stateid)];
const response = await this.client.fs.compound(readOps);
Iif (response.status !== Nfsv4Stat.NFS4_OK) {
throw new Error(`Failed to read file: ${response.status}`);
}
const readRes = response.resarray[0] as msg.Nfsv4ReadResponse;
Iif (readRes.status !== Nfsv4Stat.NFS4_OK || !readRes.resok) {
throw new Error(`Failed to read file: ${readRes.status}`);
}
const data = readRes.resok.data;
const bytesToCopy = Math.min(data.length, length);
for (let i = 0; i < bytesToCopy; i++) {
buffer[offset + i] = data[i];
}
return {bytesRead: bytesToCopy, buffer};
}
async readFile(options?: opts.IReadFileOptions | string): Promise<misc.TDataOut> {
Iif (this.closed) throw new Error('File handle is closed');
return this.client.readFile(this.path, options);
}
async truncate(len?: number): Promise<void> {
if (this.closed) throw new Error('File handle is closed');
return this.client.truncate(this.path, len);
}
async utimes(atime: misc.TTime, mtime: misc.TTime): Promise<void> {
Iif (this.closed) throw new Error('File handle is closed');
return this.client.utimes(this.path, atime, mtime);
}
async write(
buffer: Buffer | ArrayBufferView | DataView,
offset?: number,
length?: number,
position?: number | null,
): Promise<misc.TFileHandleWriteResult> {
if (this.closed) throw new Error('File handle is closed');
const actualOffset = offset ?? 0;
const actualLength = length ?? buffer.byteLength - actualOffset;
const writePos = position !== null && position !== undefined ? BigInt(position) : BigInt(0);
let data: Uint8Array;
if (buffer instanceof Uint8Array) {
data = Uint8Array.prototype.slice.call(buffer, actualOffset, actualOffset + actualLength);
} else Eif (Buffer.isBuffer(buffer)) {
data = new Uint8Array(buffer.buffer, buffer.byteOffset + actualOffset, actualLength);
} else if (buffer instanceof DataView) {
data = new Uint8Array(buffer.buffer, buffer.byteOffset + actualOffset, actualLength);
} else {
data = new Uint8Array(
(buffer as ArrayBufferView).buffer,
(buffer as ArrayBufferView).byteOffset + actualOffset,
actualLength,
);
}
const writeOps: msg.Nfsv4Request[] = [nfs.WRITE(this.stateid, writePos, Nfsv4StableHow.FILE_SYNC4, data)];
const response = await this.client.fs.compound(writeOps);
Iif (response.status !== Nfsv4Stat.NFS4_OK) {
throw new Error(`Failed to write file: ${response.status}`);
}
const writeRes = response.resarray[0] as msg.Nfsv4WriteResponse;
Iif (writeRes.status !== Nfsv4Stat.NFS4_OK || !writeRes.resok) {
throw new Error(`Failed to write file: ${writeRes.status}`);
}
const resultBuffer =
buffer instanceof Uint8Array || Buffer.isBuffer(buffer) ? buffer : new Uint8Array(buffer.buffer);
return {bytesWritten: writeRes.resok.count, buffer: resultBuffer};
}
async writeFile(data: misc.TData, options?: opts.IWriteFileOptions): Promise<void> {
Iif (this.closed) throw new Error('File handle is closed');
return this.client.writeFile(this.path, data, options);
}
async readv(buffers: ArrayBufferView[], position?: number | null): Promise<misc.TFileHandleReadvResult> {
Iif (this.closed) throw new Error('File handle is closed');
let currentPosition = position !== null && position !== undefined ? BigInt(position) : BigInt(0);
let totalBytesRead = 0;
for (const buffer of buffers) {
const readOps: msg.Nfsv4Request[] = [nfs.READ(currentPosition, buffer.byteLength, this.stateid)];
const response = await this.client.fs.compound(readOps);
Iif (response.status !== Nfsv4Stat.NFS4_OK) {
throw new Error(`Failed to read file: ${response.status}`);
}
const readRes = response.resarray[0] as msg.Nfsv4ReadResponse;
Iif (readRes.status !== Nfsv4Stat.NFS4_OK || !readRes.resok) {
throw new Error(`Failed to read file: ${readRes.status}`);
}
const data = readRes.resok.data;
const bytesToCopy = Math.min(data.length, buffer.byteLength);
const uint8View = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
for (let i = 0; i < bytesToCopy; i++) {
uint8View[i] = data[i];
}
totalBytesRead += bytesToCopy;
currentPosition += BigInt(bytesToCopy);
if (readRes.resok.eof || bytesToCopy < buffer.byteLength) break;
}
return {bytesRead: totalBytesRead, buffers};
}
async writev(buffers: ArrayBufferView[], position?: number | null): Promise<misc.TFileHandleWritevResult> {
Iif (this.closed) throw new Error('File handle is closed');
let currentPosition = position !== null && position !== undefined ? BigInt(position) : BigInt(0);
let totalBytesWritten = 0;
for (const buffer of buffers) {
const data = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
const writeOps: msg.Nfsv4Request[] = [nfs.WRITE(this.stateid, currentPosition, Nfsv4StableHow.FILE_SYNC4, data)];
const response = await this.client.fs.compound(writeOps);
Iif (response.status !== Nfsv4Stat.NFS4_OK) {
throw new Error(`Failed to write file: ${response.status}`);
}
const writeRes = response.resarray[0] as msg.Nfsv4WriteResponse;
Iif (writeRes.status !== Nfsv4Stat.NFS4_OK || !writeRes.resok) {
throw new Error(`Failed to write file: ${writeRes.status}`);
}
totalBytesWritten += writeRes.resok.count;
currentPosition += BigInt(writeRes.resok.count);
}
return {bytesWritten: totalBytesWritten, buffers};
}
readableWebStream(options?: opts.IReadableWebStreamOptions): ReadableStream {
Iif (this.closed) throw new Error('File handle is closed');
const stream = this.createReadStream(options as any);
return Readable.toWeb(stream as any) as ReadableStream;
}
createReadStream(options?: opts.IFileHandleReadStreamOptions): misc.IReadStream {
Iif (this.closed) throw new Error('File handle is closed');
const start = options?.start ?? 0;
const end = options?.end;
const highWaterMark = options?.highWaterMark ?? 64 * 1024;
let position = typeof start === 'number' ? start : 0;
const endPosition = typeof end === 'number' ? end : Infinity;
let reading = false;
const self = this;
const stream = new Readable({
highWaterMark,
async read(size) {
Iif (reading) return;
reading = true;
try {
while (true) {
if (position >= endPosition) {
this.push(null);
break;
}
const bytesToRead = Math.min(size, endPosition - position);
Iif (bytesToRead <= 0) {
this.push(null);
break;
}
const buffer = Buffer.alloc(bytesToRead);
const result = await self.read(buffer, 0, bytesToRead, position);
Iif (result.bytesRead === 0) {
this.push(null);
break;
}
position += result.bytesRead;
const chunk = buffer.slice(0, result.bytesRead);
Iif (!this.push(chunk)) break;
if (result.bytesRead < bytesToRead) {
this.push(null);
break;
}
}
} catch (err) {
this.destroy(err as Error);
} finally {
reading = false;
}
},
}) as misc.IReadStream;
stream.path = this.path;
return stream;
}
createWriteStream(options?: opts.IFileHandleWriteStreamOptions): misc.IWriteStream {
Iif (this.closed) throw new Error('File handle is closed');
const start = options?.start ?? 0;
const highWaterMark = options?.highWaterMark ?? 64 * 1024;
let position = typeof start === 'number' ? start : 0;
const self = this;
const stream = new Writable({
highWaterMark,
async write(chunk, encoding, callback) {
try {
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
const result = await self.write(buffer, 0, buffer.length, position);
position += result.bytesWritten;
callback();
} catch (err) {
callback(err as Error);
}
},
async writev(chunks, callback) {
try {
const buffers = chunks.map(({chunk}) => (Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
const result = await self.writev(buffers, position);
position += result.bytesWritten;
callback();
} catch (err) {
callback(err as Error);
}
},
}) as misc.IWriteStream;
stream.path = this.path;
return stream;
}
}
|