Merge pull request #9 from puiterwijk/issue-8

Fix Issue 8 by reporting the error correctly and using a moveFile instead of os.Rename across mount points
This commit is contained in:
Jess Frazelle 2017-03-15 09:22:21 -07:00 committed by GitHub
commit 598f9e670f

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
@ -42,6 +43,30 @@ func preload(c *cli.Context) (err error) {
return nil
}
func moveFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
tmp, err := ioutil.TempFile(filepath.Dir(dst), "")
if err != nil {
return err
}
_, err = io.Copy(tmp, in)
if err != nil {
tmp.Close()
os.Remove(tmp.Name())
return err
}
if err = tmp.Close(); err != nil {
os.Remove(tmp.Name())
return err
}
defer os.Remove(src)
return os.Rename(tmp.Name(), dst)
}
func main() {
app := cli.NewApp()
app.Name = "reg-server"
@ -111,7 +136,7 @@ func main() {
// create the initial index
if err := createStaticIndex(r, staticDir, c.GlobalString("clair")); err != nil {
return err
return cli.NewExitError(fmt.Sprintf("Error creating index: %s", err.Error()), 1)
}
// parse the duration
@ -278,7 +303,7 @@ func createStaticIndex(r *registry.Registry, staticDir, clairURI string) error {
index := filepath.Join(staticDir, "index.html")
logrus.Infof("renaming the temporary file %s to %s", f.Name(), index)
if err := os.Rename(f.Name(), index); err != nil {
if err := moveFile(f.Name(), index); err != nil {
return fmt.Errorf("renaming result from %s to %s failed: %v", f.Name(), index, err)
}
updating = false