Skip to content

Commit

Permalink
Perform a hard copy if the temporary file cannot be moved
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienM committed Jul 9, 2018
1 parent 165c897 commit 9aceb99
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.0.1]

### Fixed

- Perform a hard copy if the temporary file cannot be moved

## [1.0.0]

### Added

- Initial import of the lib, handling simples HTML "file-index" pages

[Unreleased]: https://github.com/FabienM/updater/compare/1.0.0...master
[Unreleased]: https://github.com/FabienM/updater/compare/1.0.1...master
[1.0.1]: https://github.com/FabienM/updater/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/FabienM/updater/tag/1.0.0
31 changes: 29 additions & 2 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ func (u Updater) UpdateTo(build *BuildInfo) error {
return err
}

err = os.Rename(tmpPath, path)
if err != nil {
if err = updateFile(tmpPath, path); err != nil {
return fmt.Errorf("cannot move temporary file %s to %s: %s", tmpPath, path, err)
}

Expand Down Expand Up @@ -255,3 +254,31 @@ func (s *buildInfoSorter) Len() int {
func (s *buildInfoSorter) Swap(i, j int) {
s.builds[i], s.builds[j] = s.builds[j], s.builds[i]
}

func updateFile(src, dst string) error {
if err := os.Rename(src, dst); err == nil {
return nil
}

in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()

if _, err = io.Copy(out, in); err != nil {
return err
}

if err := out.Chmod(0755); err != nil {
return err
}

return out.Close()
}

0 comments on commit 9aceb99

Please sign in to comment.