Skip to content

Commit 6aa29f5

Browse files
committed
Added DeviceDriver.readD and DeviceDriver.writeD
Deprecated `DeviceDriver.read` and `.write` Added documentation to new `FileSystem` methods "Fixed" `FileSystem.read` and `.readSync` not having a buffer parameter (*breaking*) `Passthrough.sync` and `.syncSync` now sync some stats The `Inode` constructor now accepts a partial `InodeLike`
1 parent 53ed21e commit 6aa29f5

File tree

9 files changed

+220
-114
lines changed

9 files changed

+220
-114
lines changed

src/backends/overlay.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ export class UnmutexedOverlayFS extends FileSystem {
8585
this.writable.syncSync(path, data, stats);
8686
}
8787

88-
public async read(path: string, offset: number, length: number): Promise<Uint8Array> {
89-
return (await this.writable.exists(path)) ? await this.writable.read(path, offset, length) : await this.readable.read(path, offset, length);
88+
public async read(path: string, buffer: Uint8Array, offset: number, end: number): Promise<void> {
89+
return (await this.writable.exists(path)) ? await this.writable.read(path, buffer, offset, end) : await this.readable.read(path, buffer, offset, end);
9090
}
9191

92-
public readSync(path: string, offset: number, length: number): Uint8Array {
93-
return this.writable.existsSync(path) ? this.writable.readSync(path, offset, length) : this.readable.readSync(path, offset, length);
92+
public readSync(path: string, buffer: Uint8Array, offset: number, end: number): void {
93+
return this.writable.existsSync(path) ? this.writable.readSync(path, buffer, offset, end) : this.readable.readSync(path, buffer, offset, end);
9494
}
9595

9696
public async write(path: string, buffer: Uint8Array, offset: number): Promise<void> {

src/backends/passthrough.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type * as fs from 'node:fs';
2-
import type { Backend } from './backend.js';
3-
import { FileSystem } from '../filesystem.js';
42
import type { Errno } from '../error.js';
53
import { ErrnoError } from '../error.js';
6-
import { Stats } from '../stats.js';
74
import { File, type FileReadResult } from '../file.js';
5+
import { FileSystem } from '../filesystem.js';
6+
import { Stats } from '../stats.js';
87
import { join, resolve } from '../vfs/path.js';
8+
import type { Backend } from './backend.js';
9+
import type { InodeLike } from './store/inode.js';
910

1011
// Type for Node.js fs module
1112
export type NodeFS = typeof fs;
@@ -333,9 +334,13 @@ export class PassthroughFS extends FileSystem {
333334
/**
334335
* Synchronize data to the file system.
335336
*/
336-
public async sync(path: string, data: Uint8Array, stats: Stats): Promise<void> {
337+
public async sync(path: string, data: Uint8Array, stats: Readonly<InodeLike>): Promise<void> {
337338
try {
338-
await this.nodeFS.promises.writeFile(this.path(path), data);
339+
await using handle = await this.nodeFS.promises.open(this.path(path), 'w');
340+
await handle.writeFile(data);
341+
await handle.chmod(stats.mode);
342+
await handle.chown(stats.uid, stats.gid);
343+
await handle.utimes(stats.atimeMs, stats.mtimeMs);
339344
} catch (err) {
340345
this.error(err, path);
341346
}
@@ -344,9 +349,13 @@ export class PassthroughFS extends FileSystem {
344349
/**
345350
* Synchronize data to the file system synchronously.
346351
*/
347-
public syncSync(path: string, data: Uint8Array, stats: Stats): void {
352+
public syncSync(path: string, data: Uint8Array, stats: Readonly<InodeLike>): void {
348353
try {
349-
this.nodeFS.writeFileSync(this.path(path), data);
354+
const p = this.path(path);
355+
this.nodeFS.writeFileSync(p, data);
356+
this.nodeFS.chmodSync(p, stats.mode);
357+
this.nodeFS.chownSync(p, stats.uid, stats.gid);
358+
this.nodeFS.utimesSync(p, stats.atimeMs, stats.mtimeMs);
350359
} catch (err) {
351360
this.error(err, path);
352361
}
@@ -374,23 +383,20 @@ export class PassthroughFS extends FileSystem {
374383
}
375384
}
376385

377-
public async read(path: string, offset: number, length: number): Promise<Uint8Array> {
386+
public async read(path: string, buffer: Uint8Array, offset: number, end: number): Promise<void> {
378387
try {
379388
await using handle = await this.nodeFS.promises.open(this.path(path), 'r');
380-
const buffer = new Uint8Array(length);
381-
await handle.read({ buffer, offset, length });
382-
return buffer;
389+
await handle.read({ buffer, offset, length: end - offset });
383390
} catch (err) {
384391
this.error(err, path);
385392
}
386393
}
387-
public readSync(path: string, offset: number, length: number): Uint8Array {
394+
395+
public readSync(path: string, buffer: Uint8Array, offset: number, end: number): void {
388396
let fd;
389397
try {
390398
fd = this.nodeFS.openSync(this.path(path), 'r');
391-
const buffer = new Uint8Array(length);
392-
this.nodeFS.readSync(fd, buffer, { offset, length });
393-
return buffer;
399+
this.nodeFS.readSync(fd, buffer, { offset, length: end - offset });
394400
} catch (err) {
395401
this.error(err, path);
396402
} finally {
@@ -400,6 +406,7 @@ export class PassthroughFS extends FileSystem {
400406
// unreachable
401407
throw ErrnoError.With('EIO', path, 'read');
402408
}
409+
403410
public async write(path: string, buffer: Uint8Array, offset: number): Promise<void> {
404411
try {
405412
await using handle = await this.nodeFS.promises.open(this.path(path), 'w');
@@ -408,6 +415,7 @@ export class PassthroughFS extends FileSystem {
408415
this.error(err, path);
409416
}
410417
}
418+
411419
public writeSync(path: string, buffer: Uint8Array, offset: number): void {
412420
let fd;
413421
try {

src/backends/port/fs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ export class PortFS extends Async(FileSystem) {
107107
return this.rpc('link', srcpath, dstpath);
108108
}
109109

110-
public read(path: string, offset: number, length: number): Promise<Uint8Array> {
111-
return this.rpc('read', path, offset, length);
110+
public read(path: string, buffer: Uint8Array, offset: number, length: number): Promise<void> {
111+
return this.rpc('read', path, buffer, offset, length);
112112
}
113113

114114
public write(path: string, buffer: Uint8Array, offset: number): Promise<void> {

src/backends/store/fs.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -399,34 +399,22 @@ export class StoreFS<T extends Store = Store> extends FileSystem {
399399
tx.commitSync();
400400
}
401401

402-
/**
403-
* Used by lazy file
404-
* @internal
405-
*/
406-
public async read(path: string, offset: number, length: number): Promise<Uint8Array> {
402+
public async read(path: string, buffer: Uint8Array, offset: number, end: number): Promise<void> {
407403
await using tx = this.store.transaction();
408404
const inode = await this.findInode(tx, path, 'read');
409405

410-
const buffer = (await tx.get(inode.data)) ?? _throw(ErrnoError.With('ENODATA', path, 'read'));
411-
return buffer.slice(offset, offset + length);
406+
const data = (await tx.get(inode.data)) ?? _throw(ErrnoError.With('ENODATA', path, 'read'));
407+
buffer.set(data.subarray(offset, end));
412408
}
413409

414-
/**
415-
* Used by lazy file
416-
* @internal
417-
*/
418-
public readSync(path: string, offset: number, length: number): Uint8Array {
410+
public readSync(path: string, buffer: Uint8Array, offset: number, end: number): void {
419411
using tx = this.store.transaction();
420412
const inode = this.findInodeSync(tx, path, 'read');
421413

422-
const buffer = tx.getSync(inode.data) ?? _throw(ErrnoError.With('ENODATA', path, 'read'));
423-
return buffer.slice(offset, offset + length);
414+
const data = tx.getSync(inode.data) ?? _throw(ErrnoError.With('ENODATA', path, 'read'));
415+
buffer.set(data.subarray(offset, end));
424416
}
425417

426-
/**
427-
* Used by lazy file
428-
* @internal
429-
*/
430418
public async write(path: string, data: Uint8Array, offset: number): Promise<void> {
431419
await using tx = this.store.transaction();
432420

@@ -438,10 +426,6 @@ export class StoreFS<T extends Store = Store> extends FileSystem {
438426
await this.sync(path, buffer, inode);
439427
}
440428

441-
/**
442-
* Used by lazy file
443-
* @internal
444-
*/
445429
public writeSync(path: string, data: Uint8Array, offset: number): void {
446430
using tx = this.store.transaction();
447431

src/backends/store/inode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const _inode_fields = ['ino', 'data', 'size', 'mode', 'flags', 'nlink', '
2727
*/
2828
@struct()
2929
export class Inode implements InodeLike {
30-
public constructor(data?: ArrayBufferLike | ArrayBufferView | Readonly<InodeLike>) {
30+
public constructor(data?: ArrayBufferLike | ArrayBufferView | Readonly<Partial<InodeLike>>) {
3131
if (!data) return;
3232

3333
if (!('byteLength' in data)) {

0 commit comments

Comments
 (0)