From: Sergey Matveev Date: Mon, 9 Dec 2019 13:20:47 +0000 (+0300) Subject: Sync directories for rename assurance X-Git-Tag: v5.1.2^2~5 X-Git-Url: http://www.git.cypherpunks.ru/?p=nncp.git;a=commitdiff_plain;h=f70e9df35c2ff6324c07125916ae8e214ff7f420 Sync directories for rename assurance --- diff --git a/VERSION b/VERSION index ac14c3d..61fcc87 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -5.1.1 +5.1.2 diff --git a/doc/news.ru.texi b/doc/news.ru.texi index 6cf154b..d99a621 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -1,6 +1,16 @@ @node Новости @section Новости +@node Релиз 5.1.2 +@subsection Релиз 5.1.2 +@itemize + +@item +Явная синхронизация (fsync) директорий для гарантированного +переименования файлов. + +@end itemize + @node Релиз 5.1.1 @subsection Релиз 5.1.1 @itemize diff --git a/doc/news.texi b/doc/news.texi index 3e04abb..5b9d06b 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -3,6 +3,15 @@ See also this page @ref{Новости, on russian}. +@node Release 5.1.2 +@section Release 5.1.2 +@itemize + +@item +Explicit directories fsync-ing for guaranteed files renaming. + +@end itemize + @node Release 5.1.1 @section Release 5.1.1 @itemize diff --git a/src/cmd/nncp-bundle/main.go b/src/cmd/nncp-bundle/main.go index 380b391..60bb113 100644 --- a/src/cmd/nncp-bundle/main.go +++ b/src/cmd/nncp-bundle/main.go @@ -378,6 +378,9 @@ func main() { if err = os.Rename(tmp.Name(), dstPath); err != nil { log.Fatalln("Error during renaming:", err) } + if err = nncp.DirSync(selfPath); err != nil { + log.Fatalln("Error during syncing:", err) + } } } ctx.LogI("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{ diff --git a/src/cmd/nncp-reass/main.go b/src/cmd/nncp-reass/main.go index d7d79b0..161e86f 100644 --- a/src/cmd/nncp-reass/main.go +++ b/src/cmd/nncp-reass/main.go @@ -238,6 +238,9 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo if err = os.Rename(tmp.Name(), dstPath); err != nil { log.Fatalln(err) } + if err = nncp.DirSync(mainDir); err != nil { + log.Fatalln(err) + } ctx.LogI("nncp-reass", nncp.SDS{"path": path}, "done") return !hasErrors } diff --git a/src/cmd/nncp-xfer/main.go b/src/cmd/nncp-xfer/main.go index cfe16b7..c784b4e 100644 --- a/src/cmd/nncp-xfer/main.go +++ b/src/cmd/nncp-xfer/main.go @@ -330,6 +330,11 @@ Tx: isBad = true continue } + if err = nncp.DirSync(dstPath); err != nil { + ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "sync") + isBad = true + continue + } os.Remove(filepath.Join(dstPath, pktName+".part")) delete(sds, "tmp") ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{ diff --git a/src/sp.go b/src/sp.go index 540e0e5..b6e49b4 100644 --- a/src/sp.go +++ b/src/sp.go @@ -838,12 +838,12 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { sdsp["xx"] = string(TRx) sdsp["hash"] = ToBase32(file.Hash[:]) sdsp["size"] = strconv.Itoa(len(file.Payload)) - filePath := filepath.Join( + dirToSync := filepath.Join( state.Ctx.Spool, state.Node.Id.String(), string(TRx), - ToBase32(file.Hash[:]), ) + filePath := filepath.Join(dirToSync, ToBase32(file.Hash[:])) state.Ctx.LogD("sp-file", sdsp, "opening part") fd, err := os.OpenFile( filePath+PartSuffix, @@ -901,7 +901,14 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { return } state.Ctx.LogI("sp-done", SdsAdd(sdsp, SDS{"xx": string(TRx)}), "") - os.Rename(filePath+PartSuffix, filePath) + if err = os.Rename(filePath+PartSuffix, filePath); err != nil { + state.Ctx.LogE("sp-file", SdsAdd(sdsp, SDS{"err": err}), "rename") + return + } + if err = DirSync(dirToSync); err != nil { + state.Ctx.LogE("sp-file", SdsAdd(sdsp, SDS{"err": err}), "sync") + return + } state.Lock() delete(state.infosTheir, *file.Hash) state.Unlock() diff --git a/src/tmp.go b/src/tmp.go index 95e45ee..0784b99 100644 --- a/src/tmp.go +++ b/src/tmp.go @@ -79,6 +79,19 @@ func (tmp *TmpFileWHash) Cancel() { os.Remove(tmp.Fd.Name()) } +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 (tmp *TmpFileWHash) Commit(dir string) error { var err error if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil { @@ -95,5 +108,8 @@ func (tmp *TmpFileWHash) Commit(dir string) error { tmp.Fd.Close() checksum := ToBase32(tmp.Hsh.Sum(nil)) tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit") - return os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)) + if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil { + return err + } + return DirSync(dir) } diff --git a/src/toss.go b/src/toss.go index 586f378..5bc8b79 100644 --- a/src/toss.go +++ b/src/toss.go @@ -261,6 +261,10 @@ func (ctx *Ctx) Toss( ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename") isBad = true } + if err = DirSync(*incoming); err != nil { + ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sync") + isBad = true + } delete(sds, "tmp") } ctx.LogI("rx", sds, "")