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 <puiterwijk@redhat.com>
This commit is contained in:
Patrick Uiterwijk 2017-03-10 00:01:51 +00:00
parent 399112fe36
commit c082ae5104
No known key found for this signature in database
GPG key ID: 8657980D9AB51E50

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -42,6 +43,30 @@ func preload(c *cli.Context) (err error) {
return nil 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() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "reg-server" app.Name = "reg-server"
@ -278,7 +303,7 @@ func createStaticIndex(r *registry.Registry, staticDir, clairURI string) error {
index := filepath.Join(staticDir, "index.html") index := filepath.Join(staticDir, "index.html")
logrus.Infof("renaming the temporary file %s to %s", f.Name(), index) 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) return fmt.Errorf("renaming result from %s to %s failed: %v", f.Name(), index, err)
} }
updating = false updating = false