2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 "golang.org/x/crypto/blake2b"
31 func (ctx *Ctx) NewTmpFile() (*os.File, error) {
32 jobsPath := filepath.Join(ctx.Spool, "tmp")
34 if err = os.MkdirAll(jobsPath, os.FileMode(0700)); err != nil {
37 fd, err := ioutil.TempFile(jobsPath, "")
39 ctx.LogD("tmp", SDS{"src": fd.Name()}, "created")
44 type TmpFileWHash struct {
51 func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
52 tmp, err := ctx.NewTmpFile()
56 hsh, err := blake2b.New256(nil)
61 W: bufio.NewWriter(io.MultiWriter(hsh, tmp)),
68 func (tmp *TmpFileWHash) Cancel() {
71 os.Remove(tmp.Fd.Name())
74 func (tmp *TmpFileWHash) Commit(dir string) error {
76 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
79 if err = tmp.W.Flush(); err != nil {
83 if err = tmp.Fd.Sync(); err != nil {
88 checksum := ToBase32(tmp.Hsh.Sum(nil))
89 tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit")
90 return os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum))