]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/tmp.go
Forbid any later GNU GPL versions autousage
[nncp.git] / src / cypherpunks.ru / nncp / tmp.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package nncp
19
20 import (
21         "bufio"
22         "hash"
23         "io"
24         "io/ioutil"
25         "os"
26         "path/filepath"
27
28         "golang.org/x/crypto/blake2b"
29 )
30
31 func (ctx *Ctx) NewTmpFile() (*os.File, error) {
32         jobsPath := filepath.Join(ctx.Spool, "tmp")
33         var err error
34         if err = os.MkdirAll(jobsPath, os.FileMode(0700)); err != nil {
35                 return nil, err
36         }
37         fd, err := ioutil.TempFile(jobsPath, "")
38         if err == nil {
39                 ctx.LogD("tmp", SDS{"src": fd.Name()}, "created")
40         }
41         return fd, err
42 }
43
44 type TmpFileWHash struct {
45         W   *bufio.Writer
46         Fd  *os.File
47         Hsh hash.Hash
48         ctx *Ctx
49 }
50
51 func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
52         tmp, err := ctx.NewTmpFile()
53         if err != nil {
54                 return nil, err
55         }
56         hsh, err := blake2b.New256(nil)
57         if err != nil {
58                 return nil, err
59         }
60         return &TmpFileWHash{
61                 W:   bufio.NewWriter(io.MultiWriter(hsh, tmp)),
62                 Fd:  tmp,
63                 Hsh: hsh,
64                 ctx: ctx,
65         }, nil
66 }
67
68 func (tmp *TmpFileWHash) Cancel() {
69         tmp.Fd.Truncate(0)
70         tmp.Fd.Close()
71         os.Remove(tmp.Fd.Name())
72 }
73
74 func (tmp *TmpFileWHash) Commit(dir string) error {
75         var err error
76         if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil {
77                 return err
78         }
79         if err = tmp.W.Flush(); err != nil {
80                 tmp.Fd.Close()
81                 return err
82         }
83         if err = tmp.Fd.Sync(); err != nil {
84                 tmp.Fd.Close()
85                 return err
86         }
87         tmp.Fd.Close()
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))
91 }