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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 "golang.org/x/crypto/blake2b"
32 func (ctx *Ctx) NewTmpFile() (*os.File, error) {
33 jobsPath := filepath.Join(ctx.Spool, "tmp")
35 if err = os.MkdirAll(jobsPath, os.FileMode(0700)); err != nil {
38 fd, err := ioutil.TempFile(jobsPath, "")
40 ctx.LogD("tmp", SDS{"src": fd.Name()}, "created")
45 type TmpFileWHash struct {
52 func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
53 tmp, err := ctx.NewTmpFile()
57 hsh, err := blake2b.New256(nil)
62 W: bufio.NewWriter(io.MultiWriter(hsh, tmp)),
69 func (tmp *TmpFileWHash) Cancel() {
72 os.Remove(tmp.Fd.Name())
75 func (tmp *TmpFileWHash) Commit(dir string) error {
77 if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
80 if err = tmp.W.Flush(); err != nil {
84 if err = tmp.Fd.Sync(); err != nil {
89 checksum := ToBase32(tmp.Hsh.Sum(nil))
90 tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit")
91 return os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum))