@@ -579,38 +579,6 @@ func (update *Updater) preOrderDepthFirstWalk(targetFilePath string) (*metadata.
579579 return nil , fmt .Errorf ("target %s not found" , targetFilePath )
580580}
581581
582- func moveFile (source , destination string ) (err error ) {
583- // can only safely rename on any OS if source and destination are in the same directory
584- if filepath .Dir (source ) == filepath .Dir (destination ) {
585- return os .Rename (source , destination )
586- }
587-
588- inputFile , err := os .Open (source )
589- if err != nil {
590- return fmt .Errorf ("couldn't open source file: %s" , err )
591- }
592- defer inputFile .Close ()
593- outputFile , err := os .Create (destination )
594- if err != nil {
595- return fmt .Errorf ("couldn't open dest file: %s" , err )
596- }
597- defer outputFile .Close ()
598- c , err := io .Copy (outputFile , inputFile )
599- if err != nil {
600- return fmt .Errorf ("writing to output file failed: %s" , err )
601- }
602- if c <= 0 {
603- return fmt .Errorf ("nothing copied to output file" )
604- }
605- inputFile .Close ()
606- // The copy was successful, so now delete the original file
607- err = os .Remove (source )
608- if err != nil {
609- return fmt .Errorf ("failed removing original file: %s" , err )
610- }
611- return nil
612- }
613-
614582// persistMetadata writes metadata to disk atomically to avoid data loss
615583func (update * Updater ) persistMetadata (roleName string , data []byte ) error {
616584 log := metadata .GetLogger ()
@@ -620,12 +588,8 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error {
620588 }
621589 // caching enabled, proceed with persisting the metadata locally
622590 fileName := filepath .Join (update .cfg .LocalMetadataDir , fmt .Sprintf ("%s.json" , url .QueryEscape (roleName )))
623- cwd , err := os .Getwd ()
624- if err != nil {
625- return err
626- }
627591 // create a temporary file
628- file , err := os .CreateTemp (cwd , "tuf_tmp" )
592+ file , err := os .CreateTemp (update . cfg . LocalMetadataDir , "tuf_tmp" )
629593 if err != nil {
630594 return err
631595 }
@@ -642,9 +606,12 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error {
642606 }
643607
644608 // can't move/rename an open file on windows, so close it first
645- file .Close ()
609+ err = file .Close ()
610+ if err != nil {
611+ return err
612+ }
646613 // if all okay, rename the temporary file to the desired one
647- err = moveFile (file .Name (), fileName )
614+ err = os . Rename (file .Name (), fileName )
648615 if err != nil {
649616 return err
650617 }
0 commit comments