From 9aceb99952fca0d32ce42d97eabb2105d56feaeb Mon Sep 17 00:00:00 2001 From: Fabien Meurillon Date: Mon, 9 Jul 2018 17:19:11 +0200 Subject: [PATCH] Perform a hard copy if the temporary file cannot be moved --- CHANGELOG.md | 9 ++++++++- updater.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2febcbe..756b401 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/updater.go b/updater.go index 2d724cc..cd7a191 100644 --- a/updater.go +++ b/updater.go @@ -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) } @@ -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() +}