1
1
import type * as fs from 'node:fs' ;
2
- import type { Backend } from './backend.js' ;
3
- import { FileSystem } from '../filesystem.js' ;
4
2
import type { Errno } from '../error.js' ;
5
3
import { ErrnoError } from '../error.js' ;
6
- import { Stats } from '../stats.js' ;
7
4
import { File , type FileReadResult } from '../file.js' ;
5
+ import { FileSystem } from '../filesystem.js' ;
6
+ import { Stats } from '../stats.js' ;
8
7
import { join , resolve } from '../vfs/path.js' ;
8
+ import type { Backend } from './backend.js' ;
9
+ import type { InodeLike } from './store/inode.js' ;
9
10
10
11
// Type for Node.js fs module
11
12
export type NodeFS = typeof fs ;
@@ -333,9 +334,13 @@ export class PassthroughFS extends FileSystem {
333
334
/**
334
335
* Synchronize data to the file system.
335
336
*/
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 > {
337
338
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 ) ;
339
344
} catch ( err ) {
340
345
this . error ( err , path ) ;
341
346
}
@@ -344,9 +349,13 @@ export class PassthroughFS extends FileSystem {
344
349
/**
345
350
* Synchronize data to the file system synchronously.
346
351
*/
347
- public syncSync ( path : string , data : Uint8Array , stats : Stats ) : void {
352
+ public syncSync ( path : string , data : Uint8Array , stats : Readonly < InodeLike > ) : void {
348
353
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 ) ;
350
359
} catch ( err ) {
351
360
this . error ( err , path ) ;
352
361
}
@@ -374,23 +383,20 @@ export class PassthroughFS extends FileSystem {
374
383
}
375
384
}
376
385
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 > {
378
387
try {
379
388
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 } ) ;
383
390
} catch ( err ) {
384
391
this . error ( err , path ) ;
385
392
}
386
393
}
387
- public readSync ( path : string , offset : number , length : number ) : Uint8Array {
394
+
395
+ public readSync ( path : string , buffer : Uint8Array , offset : number , end : number ) : void {
388
396
let fd ;
389
397
try {
390
398
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 } ) ;
394
400
} catch ( err ) {
395
401
this . error ( err , path ) ;
396
402
} finally {
@@ -400,6 +406,7 @@ export class PassthroughFS extends FileSystem {
400
406
// unreachable
401
407
throw ErrnoError . With ( 'EIO' , path , 'read' ) ;
402
408
}
409
+
403
410
public async write ( path : string , buffer : Uint8Array , offset : number ) : Promise < void > {
404
411
try {
405
412
await using handle = await this . nodeFS . promises . open ( this . path ( path ) , 'w' ) ;
@@ -408,6 +415,7 @@ export class PassthroughFS extends FileSystem {
408
415
this . error ( err , path ) ;
409
416
}
410
417
}
418
+
411
419
public writeSync ( path : string , buffer : Uint8Array , offset : number ) : void {
412
420
let fd ;
413
421
try {
0 commit comments