@@ -13,6 +13,7 @@ import {
1313 createFileInDirectory ,
1414 isChildDirectory ,
1515 isSameDirectory ,
16+ moveFileAcrossDevices ,
1617 overwriteFileInDirectory ,
1718 renameIntoDirectory ,
1819} from "../pinned-directory.js" ;
@@ -429,6 +430,183 @@ describe("renameIntoDirectory", () => {
429430 ) ;
430431} ) ;
431432
433+ // Each test here runs on one device: the helper is what moveFile falls back
434+ // to on EXDEV, and it never calls rename, so one device exercises it fully.
435+ describe ( "moveFileAcrossDevices" , ( ) => {
436+ const fail = ( code : string ) =>
437+ ( ( ...args : unknown [ ] ) => {
438+ ( args [ args . length - 1 ] as ( error : Error ) => void ) ( errno ( code ) ) ;
439+ } ) as never ;
440+
441+ it ( "copies the content, mode and times, then removes the source" , async ( ) => {
442+ const source = path . join ( dir , "a.bin" ) ;
443+ const destination = path . join ( outside , "b.bin" ) ;
444+ // More than one 64 KiB chunk.
445+ const content = Buffer . alloc ( 200 * 1024 , 7 ) ;
446+ fs . writeFileSync ( source , content ) ;
447+ fs . chmodSync ( source , 0o640 ) ;
448+ fs . utimesSync ( source , new Date ( 1_000_000_000 ) , new Date ( 1_000_000_000 ) ) ;
449+ await moveFileAcrossDevices ( source , destination ) ;
450+ expect ( fs . readFileSync ( destination ) . equals ( content ) ) . toBe ( true ) ;
451+ expect ( fs . existsSync ( source ) ) . toBe ( false ) ;
452+ const moved = fs . statSync ( destination ) ;
453+ if ( process . platform !== "win32" ) {
454+ expect ( moved . mode & 0o777 ) . toBe ( 0o640 ) ;
455+ }
456+ expect ( moved . mtimeMs ) . toBe ( 1_000_000_000 ) ;
457+ expect ( process . cwd ( ) ) . toBe ( cwd ) ;
458+ } ) ;
459+
460+ it ( "moves an empty file" , async ( ) => {
461+ const source = path . join ( dir , "empty" ) ;
462+ fs . writeFileSync ( source , "" ) ;
463+ await moveFileAcrossDevices ( source , path . join ( outside , "empty" ) ) ;
464+ expect ( fs . readFileSync ( path . join ( outside , "empty" ) , "utf-8" ) ) . toBe ( "" ) ;
465+ expect ( fs . existsSync ( source ) ) . toBe ( false ) ;
466+ } ) ;
467+
468+ it ( "refuses a directory, and leaves it in place" , async ( ) => {
469+ const source = path . join ( dir , "d" ) ;
470+ fs . mkdirSync ( source ) ;
471+ await expect (
472+ moveFileAcrossDevices ( source , path . join ( outside , "d" ) ) ,
473+ ) . rejects . toThrow ( `Cannot move a directory across filesystems: ${ source } .` ) ;
474+ expect ( fs . statSync ( source ) . isDirectory ( ) ) . toBe ( true ) ;
475+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
476+ } ) ;
477+
478+ it . skipIf ( ! symlinks ) (
479+ "refuses a symlink rather than following or recreating it" ,
480+ async ( ) => {
481+ const target = path . join ( dir , "t.txt" ) ;
482+ fs . writeFileSync ( target , "t" ) ;
483+ const source = path . join ( dir , "l.txt" ) ;
484+ fs . symlinkSync ( target , source ) ;
485+ await expect (
486+ moveFileAcrossDevices ( source , path . join ( outside , "l.txt" ) ) ,
487+ ) . rejects . toThrow (
488+ `Cannot move across filesystems: not a regular file: ${ source } ` ,
489+ ) ;
490+ expect ( fs . lstatSync ( source ) . isSymbolicLink ( ) ) . toBe ( true ) ;
491+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
492+ } ,
493+ ) ;
494+
495+ it ( "refuses an existing destination with EEXIST, and keeps both" , async ( ) => {
496+ const source = path . join ( dir , "a.txt" ) ;
497+ const destination = path . join ( outside , "b.txt" ) ;
498+ fs . writeFileSync ( source , "a" ) ;
499+ fs . writeFileSync ( destination , "b" ) ;
500+ await expect ( moveFileAcrossDevices ( source , destination ) ) . rejects . toThrow (
501+ expect . objectContaining ( { code : "EEXIST" } ) ,
502+ ) ;
503+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
504+ expect ( fs . readFileSync ( destination , "utf-8" ) ) . toBe ( "b" ) ;
505+ } ) ;
506+
507+ it . skipIf ( ! symlinks ) (
508+ "refuses a destination directory that resolves somewhere else" ,
509+ async ( ) => {
510+ fs . symlinkSync ( outside , path . join ( dir , "link" ) ) ;
511+ const source = path . join ( dir , "a.txt" ) ;
512+ fs . writeFileSync ( source , "a" ) ;
513+ await expect (
514+ moveFileAcrossDevices ( source , path . join ( dir , "link" , "b.txt" ) ) ,
515+ ) . rejects . toThrow ( "Access denied" ) ;
516+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
517+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
518+ } ,
519+ ) ;
520+
521+ it . each ( [
522+ [ "another file" , { ino : - 1 } ] ,
523+ [ "not a regular file" , { isFile : ( ) => false } ] ,
524+ ] ) (
525+ "refuses, and closes the source, when it was replaced while being opened (%s)" ,
526+ async ( _ , replaced ) => {
527+ const source = path . join ( dir , "a.txt" ) ;
528+ fs . writeFileSync ( source , "a" ) ;
529+ const realFstat = fs . fstatSync ;
530+ vi . spyOn ( fs , "fstatSync" ) . mockImplementationOnce ( ( fd ) =>
531+ Object . assign ( realFstat ( fd ) , replaced ) ,
532+ ) ;
533+ const closeSync = vi . spyOn ( fs , "closeSync" ) ;
534+ await expect (
535+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
536+ ) . rejects . toThrow (
537+ `Refusing to move: ${ source } was replaced while it was being opened` ,
538+ ) ;
539+ expect ( closeSync ) . toHaveBeenCalled ( ) ;
540+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
541+ } ,
542+ ) ;
543+
544+ it . each ( [ "read" , "writeFile" , "fchmod" , "futimes" ] as const ) (
545+ "removes the partial copy, and keeps the source, when %s fails" ,
546+ async ( step ) => {
547+ const source = path . join ( dir , "a.txt" ) ;
548+ fs . writeFileSync ( source , "a" ) ;
549+ vi . spyOn ( fs , step ) . mockImplementationOnce ( fail ( "EIO" ) ) ;
550+ await expect (
551+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
552+ ) . rejects . toThrow ( "EIO" ) ;
553+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
554+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
555+ } ,
556+ ) ;
557+
558+ it ( "reports the copy's failure when removing the copy fails too" , async ( ) => {
559+ const source = path . join ( dir , "a.txt" ) ;
560+ fs . writeFileSync ( source , "a" ) ;
561+ vi . spyOn ( fs , "read" ) . mockImplementationOnce ( fail ( "EIO" ) ) ;
562+ vi . spyOn ( fs , "unlinkSync" ) . mockImplementationOnce ( ( ) => {
563+ throw errno ( "EBUSY" ) ;
564+ } ) ;
565+ await expect (
566+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
567+ ) . rejects . toThrow ( "EIO" ) ;
568+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
569+ } ) ;
570+
571+ it ( "removes the copy, and keeps the source, when the source cannot be removed" , async ( ) => {
572+ const source = path . join ( dir , "a.txt" ) ;
573+ fs . writeFileSync ( source , "a" ) ;
574+ const realUnlink = fs . unlinkSync ;
575+ vi . spyOn ( fs , "unlinkSync" )
576+ . mockImplementationOnce ( ( ) => {
577+ throw errno ( "EACCES" ) ;
578+ } )
579+ . mockImplementation ( realUnlink ) ;
580+ await expect (
581+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
582+ ) . rejects . toThrow ( "EACCES" ) ;
583+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
584+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "a" ) ;
585+ } ) ;
586+
587+ it ( "removes the copy, and leaves the new file, when the source was replaced during the copy" , async ( ) => {
588+ const source = path . join ( dir , "a.txt" ) ;
589+ fs . writeFileSync ( source , "a" ) ;
590+ const realFchmod = fs . fchmod ;
591+ vi . spyOn ( fs , "fchmod" ) . mockImplementationOnce ( ( (
592+ fd : number ,
593+ mode : number ,
594+ callback : ( error : NodeJS . ErrnoException | null ) => void ,
595+ ) => {
596+ fs . unlinkSync ( source ) ;
597+ fs . writeFileSync ( source , "replacement" ) ;
598+ realFchmod ( fd , mode , callback ) ;
599+ } ) as typeof fs . fchmod ) ;
600+ await expect (
601+ moveFileAcrossDevices ( source , path . join ( outside , "b.txt" ) ) ,
602+ ) . rejects . toThrow (
603+ `Refusing to remove: ${ source } was replaced during the move` ,
604+ ) ;
605+ expect ( fs . readdirSync ( outside ) ) . toEqual ( [ ] ) ;
606+ expect ( fs . readFileSync ( source , "utf-8" ) ) . toBe ( "replacement" ) ;
607+ } ) ;
608+ } ) ;
609+
432610describe ( "createDirectoryInPlace" , ( ) => {
433611 it ( "creates missing parents, and restores the working directory" , ( ) => {
434612 createDirectoryInPlace ( path . join ( dir , "a" , "b" , "c" ) ) ;
0 commit comments