X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Ftmp.go;h=62ec5bbc9cbdd4d253858666c850168595ab3ba9;hb=4e08a1c97600e0372680e86a651f916c70e89342;hp=95e45ee4a6162a3eb01ea084ecce9c6606f804e6;hpb=dd887c15fa21071a2f4931f7248e10c4ab1029d2;p=nncp.git diff --git a/src/tmp.go b/src/tmp.go index 95e45ee..62ec5bb 100644 --- a/src/tmp.go +++ b/src/tmp.go @@ -1,6 +1,6 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2019 Sergey Matveev +Copyright (C) 2016-2020 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -74,9 +74,22 @@ func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) { } func (tmp *TmpFileWHash) Cancel() { - tmp.Fd.Truncate(0) - tmp.Fd.Close() - os.Remove(tmp.Fd.Name()) + tmp.Fd.Truncate(0) // #nosec G104 + tmp.Fd.Close() // #nosec G104 + os.Remove(tmp.Fd.Name()) // #nosec G104 +} + +func DirSync(dirPath string) error { + fd, err := os.Open(dirPath) + if err != nil { + return err + } + err = fd.Sync() + if err != nil { + fd.Close() // #nosec G104 + return err + } + return fd.Close() } func (tmp *TmpFileWHash) Commit(dir string) error { @@ -85,15 +98,20 @@ func (tmp *TmpFileWHash) Commit(dir string) error { return err } if err = tmp.W.Flush(); err != nil { - tmp.Fd.Close() + tmp.Fd.Close() // #nosec G104 return err } if err = tmp.Fd.Sync(); err != nil { - tmp.Fd.Close() + tmp.Fd.Close() // #nosec G104 + return err + } + if err = tmp.Fd.Close(); err != nil { return err } - tmp.Fd.Close() - checksum := ToBase32(tmp.Hsh.Sum(nil)) + checksum := Base32Codec.EncodeToString(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) }