@@ -26,23 +26,25 @@ export function renameSync(this: V_Context, oldPath: fs.PathLike, newPath: fs.Pa
26
26
newPath = normalizePath ( newPath ) ;
27
27
const oldMount = resolveMount ( oldPath , this ) ;
28
28
const newMount = resolveMount ( newPath , this ) ;
29
- if ( checkAccess && ! statSync . call < V_Context , Parameters < fs . StatSyncFn > , Stats > ( this , dirname ( oldPath ) ) . hasAccess ( constants . W_OK , this ) ) {
29
+
30
+ const oldStats = statSync . call < V_Context , Parameters < fs . StatSyncFn > , Stats > ( this , dirname ( oldPath ) ) ;
31
+
32
+ if ( checkAccess && ! oldStats . hasAccess ( constants . W_OK , this ) ) {
30
33
throw ErrnoError . With ( 'EACCES' , oldPath , 'rename' ) ;
31
34
}
32
- try {
33
- if ( oldMount === newMount ) {
34
- oldMount . fs . renameSync ( oldMount . path , newMount . path ) ;
35
- emitChange ( this , 'rename' , oldPath . toString ( ) ) ;
36
- emitChange ( this , 'change' , newPath . toString ( ) ) ;
37
- return ;
38
- }
39
35
40
- writeFileSync . call ( this , newPath , readFileSync ( oldPath ) ) ;
41
- unlinkSync . call ( this , oldPath ) ;
42
- emitChange ( this , 'rename' , oldPath . toString ( ) ) ;
36
+ if ( oldMount . fs !== newMount . fs ) {
37
+ throw new ErrnoError ( Errno . EXDEV , `cross-device link not permitted, rename '${ oldPath } ' -> '${ newPath } '` ) ;
38
+ }
39
+
40
+ try {
41
+ oldMount . fs . renameSync ( oldMount . path , newMount . path ) ;
43
42
} catch ( e ) {
44
43
throw fixError ( e as ErrnoError , { [ oldMount . path ] : oldPath , [ newMount . path ] : newPath } ) ;
45
44
}
45
+
46
+ emitChange ( this , 'rename' , oldPath . toString ( ) ) ;
47
+ emitChange ( this , 'change' , newPath . toString ( ) ) ;
46
48
}
47
49
renameSync satisfies typeof fs . renameSync ;
48
50
@@ -205,14 +207,6 @@ export function lopenSync(this: V_Context, path: fs.PathLike, flag: string, mode
205
207
return toFD ( _openSync . call ( this , path , { flag, mode, preserveSymlinks : true } ) ) ;
206
208
}
207
209
208
- function _readFileSync ( this : V_Context , path : fs . PathOrFileDescriptor , flag : string , preserveSymlinks : boolean ) : Uint8Array {
209
- using file = typeof path == 'number' ? fromFD ( this , path ) : _openSync . call ( this , path . toString ( ) , { flag, mode : 0o644 , preserveSymlinks } ) ;
210
- const { size } = file . stat ( ) ;
211
- const data = new Uint8Array ( size ) ;
212
- file . read ( data , 0 , size , 0 ) ;
213
- return data ;
214
- }
215
-
216
210
/**
217
211
* Synchronously reads the entire contents of a file.
218
212
* @option encoding The string encoding for the file contents. Defaults to `null`.
@@ -231,7 +225,15 @@ export function readFileSync(this: V_Context, path: fs.PathOrFileDescriptor, _op
231
225
if ( flag & constants . O_WRONLY ) {
232
226
throw new ErrnoError ( Errno . EINVAL , 'Flag passed to readFile must allow for reading' ) ;
233
227
}
234
- const data : Buffer = Buffer . from ( _readFileSync . call ( this , path , options . flag , false ) ) ;
228
+
229
+ using file =
230
+ typeof path == 'number'
231
+ ? fromFD ( this , path )
232
+ : _openSync . call ( this , path . toString ( ) , { flag : options . flag , mode : 0o644 , preserveSymlinks : false } ) ;
233
+ const { size } = file . stat ( ) ;
234
+ const data = Buffer . alloc ( size ) ;
235
+ file . read ( data , 0 , size , 0 ) ;
236
+
235
237
return options . encoding ? data . toString ( options . encoding ) : data ;
236
238
}
237
239
readFileSync satisfies typeof fs . readFileSync ;
0 commit comments