@@ -579,38 +579,6 @@ func (update *Updater) preOrderDepthFirstWalk(targetFilePath string) (*metadata.
579
579
return nil , fmt .Errorf ("target %s not found" , targetFilePath )
580
580
}
581
581
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
-
614
582
// persistMetadata writes metadata to disk atomically to avoid data loss
615
583
func (update * Updater ) persistMetadata (roleName string , data []byte ) error {
616
584
log := metadata .GetLogger ()
@@ -620,12 +588,8 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error {
620
588
}
621
589
// caching enabled, proceed with persisting the metadata locally
622
590
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
- }
627
591
// create a temporary file
628
- file , err := os .CreateTemp (cwd , "tuf_tmp" )
592
+ file , err := os .CreateTemp (update . cfg . LocalMetadataDir , "tuf_tmp" )
629
593
if err != nil {
630
594
return err
631
595
}
@@ -642,9 +606,12 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error {
642
606
}
643
607
644
608
// 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
+ }
646
613
// if all okay, rename the temporary file to the desired one
647
- err = moveFile (file .Name (), fileName )
614
+ err = os . Rename (file .Name (), fileName )
648
615
if err != nil {
649
616
return err
650
617
}
0 commit comments