@@ -13,6 +13,7 @@ import {
1313 createFileInDirectory ,
1414 isChildDirectory ,
1515 isSameDirectory ,
16+ moveFileAcrossDevices ,
1617 overwriteFileInDirectory ,
1718 renameIntoDirectory ,
1819} from "../pinned-directory.js" ;
@@ -309,6 +310,183 @@ describe("renameIntoDirectory", () => {
309310 ) ;
310311} ) ;
311312
313+ // Each test here runs on one device: the helper is what moveFile falls back
314+ // to on EXDEV, and it never calls rename, so one device exercises it fully.
315+ describe ( "moveFileAcrossDevices" , ( ) => {
316+ const fail = ( code : string ) =>
317+ ( ( ...args : unknown [ ] ) => {
318+ ( args [ args . length - 1 ] as ( error : Error ) => void ) ( errno ( code ) ) ;
319+ } ) as never ;
320+
321+ it ( "copies the content, mode and times, then removes the source" , async ( ) => {
322+ const source = path . join ( dir , "a.bin" ) ;
323+ const destination = path . join ( outside , "b.bin" ) ;
324+ // More than one 64 KiB chunk.
325+ const content = Buffer . alloc ( 200 * 1024 , 7 ) ;
326+ fs . writeFileSync ( source , content ) ;
327+ fs . chmodSync ( source , 0o640 ) ;
328+ fs . utimesSync ( source , new Date ( 1_000_000_000 ) , new Date ( 1_000_000_000 ) ) ;
329+ await moveFileAcrossDevices ( source , destination ) ;
330+ expect ( fs . readFileSync ( destination ) . equals ( content ) ) . toBe ( true ) ;
331+ expect ( fs . existsSync ( source ) ) . toBe ( false ) ;
332+ const moved = fs . statSync ( destination ) ;
333+ if ( process . platform !== "win32" ) {
334+ expect ( moved . mode & 0o777 ) . toBe ( 0o640 ) ;
335+ }
336+ expect ( moved . mtimeMs ) . toBe ( 1_000_000_000 ) ;
337+ expect ( process . cwd ( ) ) . toBe ( cwd ) ;
338+ } ) ;
339+
340+ it ( "moves an empty file" , async ( ) => {
341+ const source = path . join ( dir , "empty" ) ;
342+ fs . writeFileSync ( source , "" ) ;
343+ await moveFileAcrossDevices ( source , path . join ( outside , "empty" ) ) ;
344+ expect ( fs . readFileSync ( path . join ( outside , "empty" ) , "utf-8" ) ) . toBe ( "" ) ;
345+ expect ( fs . existsSync ( source ) ) . toBe ( false ) ;
346+ } ) ;
347+
348+ it ( "refuses a directory, and leaves it in place" , async ( ) => {
349+ const source = path . join ( dir , "d" ) ;
350+ fs . mkdirSync ( source ) ;
351+ await expect (
352+ moveFileAcrossDevices ( source , path . join ( outside , "d" ) ) ,
353+ ) . rejects . toThrow ( `Cannot move a directory across filesystems: ${ source } .` ) ;
354+ expect ( fs . statSync ( source ) . isDirectory ( ) ) . toBe ( true ) ;
355+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
356+ } ) ;
357+
358+ it . skipIf ( ! symlinks ) (
359+ "refuses a symlink rather than following or recreating it" ,
360+ async ( ) => {
361+ const target = path . join ( dir , "t.txt" ) ;
362+ fs . writeFileSync ( target , "t" ) ;
363+ const source = path . join ( dir , "l.txt" ) ;
364+ fs . symlinkSync ( target , source ) ;
365+ await expect (
366+ moveFileAcrossDevices ( source , path . join ( outside , "l.txt" ) ) ,
367+ ) . rejects . toThrow (
368+ `Cannot move across filesystems: not a regular file: ${ source } ` ,
369+ ) ;
370+ expect ( fs . lstatSync ( source ) . isSymbolicLink ( ) ) . toBe ( true ) ;
371+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
372+ } ,
373+ ) ;
374+
375+ it ( "refuses an existing destination with EEXIST, and keeps both" , async ( ) => {
376+ const source = path . join ( dir , "a.txt" ) ;
377+ const destination = path . join ( outside , "b.txt" ) ;
378+ fs . writeFileSync ( source , "a" ) ;
379+ fs . writeFileSync ( destination , "b" ) ;
380+ await expect ( moveFileAcrossDevices ( source , destination ) ) . rejects . toThrow (
381+ expect . objectContaining ( { code : "EEXIST" } ) ,
382+ ) ;
383+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
384+ expect ( fs . readFileSync ( destination , "utf-8" ) ) . toBe ( "b" ) ;
385+ } ) ;
386+
387+ it . skipIf ( ! symlinks ) (
388+ "refuses a destination directory that resolves somewhere else" ,
389+ async ( ) => {
390+ fs . symlinkSync ( outside , path . join ( dir , "link" ) ) ;
391+ const source = path . join ( dir , "a.txt" ) ;
392+ fs . writeFileSync ( source , "a" ) ;
393+ await expect (
394+ moveFileAcrossDevices ( source , path . join ( dir , "link" , "b.txt" ) ) ,
395+ ) . rejects . toThrow ( "Access denied" ) ;
396+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
397+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
398+ } ,
399+ ) ;
400+
401+ it . each ( [
402+ [ "another file" , { ino : - 1 } ] ,
403+ [ "not a regular file" , { isFile : ( ) => false } ] ,
404+ ] ) (
405+ "refuses, and closes the source, when it was replaced while being opened (%s)" ,
406+ async ( _ , replaced ) => {
407+ const source = path . join ( dir , "a.txt" ) ;
408+ fs . writeFileSync ( source , "a" ) ;
409+ const realFstat = fs . fstatSync ;
410+ vi . spyOn ( fs , "fstatSync" ) . mockImplementationOnce ( ( fd ) =>
411+ Object . assign ( realFstat ( fd ) , replaced ) ,
412+ ) ;
413+ const closeSync = vi . spyOn ( fs , "closeSync" ) ;
414+ await expect (
415+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
416+ ) . rejects . toThrow (
417+ `Refusing to move: ${ source } was replaced while it was being opened` ,
418+ ) ;
419+ expect ( closeSync ) . toHaveBeenCalled ( ) ;
420+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
421+ } ,
422+ ) ;
423+
424+ it . each ( [ "read" , "writeFile" , "fchmod" , "futimes" ] as const ) (
425+ "removes the partial copy, and keeps the source, when %s fails" ,
426+ async ( step ) => {
427+ const source = path . join ( dir , "a.txt" ) ;
428+ fs . writeFileSync ( source , "a" ) ;
429+ vi . spyOn ( fs , step ) . mockImplementationOnce ( fail ( "EIO" ) ) ;
430+ await expect (
431+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
432+ ) . rejects . toThrow ( "EIO" ) ;
433+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
434+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
435+ } ,
436+ ) ;
437+
438+ it ( "reports the copy's failure when removing the copy fails too" , async ( ) => {
439+ const source = path . join ( dir , "a.txt" ) ;
440+ fs . writeFileSync ( source , "a" ) ;
441+ vi . spyOn ( fs , "read" ) . mockImplementationOnce ( fail ( "EIO" ) ) ;
442+ vi . spyOn ( fs , "unlinkSync" ) . mockImplementationOnce ( ( ) => {
443+ throw errno ( "EBUSY" ) ;
444+ } ) ;
445+ await expect (
446+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
447+ ) . rejects . toThrow ( "EIO" ) ;
448+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
449+ } ) ;
450+
451+ it ( "removes the copy, and keeps the source, when the source cannot be removed" , async ( ) => {
452+ const source = path . join ( dir , "a.txt" ) ;
453+ fs . writeFileSync ( source , "a" ) ;
454+ const realUnlink = fs . unlinkSync ;
455+ vi . spyOn ( fs , "unlinkSync" )
456+ . mockImplementationOnce ( ( ) => {
457+ throw errno ( "EACCES" ) ;
458+ } )
459+ . mockImplementation ( realUnlink ) ;
460+ await expect (
461+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
462+ ) . rejects . toThrow ( "EACCES" ) ;
463+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
464+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
465+ } ) ;
466+
467+ it ( "removes the copy, and leaves the new file, when the source was replaced during the copy" , async ( ) => {
468+ const source = path . join ( dir , "a.txt" ) ;
469+ fs . writeFileSync ( source , "a" ) ;
470+ const realFchmod = fs . fchmod ;
471+ vi . spyOn ( fs , "fchmod" ) . mockImplementationOnce ( ( (
472+ fd : number ,
473+ mode : number ,
474+ callback : ( error : NodeJS . ErrnoException | null ) => void ,
475+ ) => {
476+ fs . unlinkSync ( source ) ;
477+ fs . writeFileSync ( source , "replacement" ) ;
478+ realFchmod ( fd , mode , callback ) ;
479+ } ) as typeof fs . fchmod ) ;
480+ await expect (
481+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
482+ ) . rejects . toThrow (
483+ `Refusing to remove: ${ source } was replaced during the move` ,
484+ ) ;
485+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
486+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "replacement" ) ;
487+ } ) ;
488+ } ) ;
489+
312490describe ( "createDirectoryInPlace" , ( ) => {
313491 it ( "creates missing parents, and restores the working directory" , ( ) => {
314492 createDirectoryInPlace ( path . join ( dir , "a" , "b" , "c" ) ) ;
0 commit comments