From 399112fe3684dfba26b34a22f6cab5741281647b Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Thu, 9 Mar 2017 23:51:03 +0000 Subject: [PATCH 1/2] Actually report errors with moving of creating static index Turns out that cli only exits when the error is created with cli.NewExitError. Signed-off-by: Patrick Uiterwijk --- server/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index 6aedb036..548f651f 100644 --- a/server/server.go +++ b/server/server.go @@ -111,7 +111,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 From c082ae510461cfc97151a3c4aa3109003e7beff8 Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Fri, 10 Mar 2017 00:01:51 +0000 Subject: [PATCH 2/2] Implement moveFile to move the generated index os.Rename only works across the same mountpoint, since it creates a new hardlink and unlinks the old one. Since we generated the index in /tmp, which is most likely not the final endpoint, this often does not work. Fixes: #8 Signed-off-by: Patrick Uiterwijk --- server/server.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index 548f651f..f44b61f1 100644 --- a/server/server.go +++ b/server/server.go @@ -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" @@ -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