From 7463a4bbcd0ace5790125f2d3f01eb48abaca985 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 6 Dec 2019 16:30:35 +0300 Subject: [PATCH] Sync directories renaming --- gocheese.go | 8 ++++++++ tmp.go | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/gocheese.go b/gocheese.go index 8a4a6e5..75a7c1c 100644 --- a/gocheese.go +++ b/gocheese.go @@ -202,6 +202,10 @@ func refreshDir( http.Error(w, err.Error(), http.StatusInternalServerError) return false } + if err = DirSync(dirPath); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return false + } } if filename == filenameGet || gpgUpdate { if _, err = os.Stat(path); err == nil { @@ -426,6 +430,10 @@ func serveUpload(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + if err = DirSync(dirPath); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } if err = WriteFileSync(dirPath, path+SHA256Ext, digest); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/tmp.go b/tmp.go index 9438646..70d3f11 100644 --- a/tmp.go +++ b/tmp.go @@ -31,6 +31,19 @@ func TempFile(dir string) (*os.File, error) { return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666)) } +func DirSync(dirPath string) error { + fd, err := os.Open(dirPath) + if err != nil { + return err + } + err = fd.Sync() + if err != nil { + fd.Close() + return err + } + return fd.Close() +} + func WriteFileSync(dirPath, filePath string, data []byte) error { dst, err := TempFile(dirPath) if err != nil { @@ -47,5 +60,8 @@ func WriteFileSync(dirPath, filePath string, data []byte) error { return err } dst.Close() - return os.Rename(dst.Name(), filePath) + if err = os.Rename(dst.Name(), filePath); err != nil { + return err + } + return DirSync(dirPath) } -- 2.44.0