]> Cypherpunks.ru repositories - nncp.git/blob - src/tmp.go
More errors checking
[nncp.git] / src / tmp.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 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         "os"
25         "path/filepath"
26         "strconv"
27         "time"
28
29         "golang.org/x/crypto/blake2b"
30 )
31
32 func TempFile(dir, prefix string) (*os.File, error) {
33         // Assume that probability of suffix collision is negligible
34         suffix := strconv.FormatInt(time.Now().UnixNano()+int64(os.Getpid()), 16)
35         name := filepath.Join(dir, "nncp"+prefix+suffix)
36         return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
37 }
38
39 func (ctx *Ctx) NewTmpFile() (*os.File, error) {
40         jobsPath := filepath.Join(ctx.Spool, "tmp")
41         var err error
42         if err = os.MkdirAll(jobsPath, os.FileMode(0777)); err != nil {
43                 return nil, err
44         }
45         fd, err := TempFile(jobsPath, "")
46         if err == nil {
47                 ctx.LogD("tmp", SDS{"src": fd.Name()}, "created")
48         }
49         return fd, err
50 }
51
52 type TmpFileWHash struct {
53         W   *bufio.Writer
54         Fd  *os.File
55         Hsh hash.Hash
56         ctx *Ctx
57 }
58
59 func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
60         tmp, err := ctx.NewTmpFile()
61         if err != nil {
62                 return nil, err
63         }
64         hsh, err := blake2b.New256(nil)
65         if err != nil {
66                 return nil, err
67         }
68         return &TmpFileWHash{
69                 W:   bufio.NewWriter(io.MultiWriter(hsh, tmp)),
70                 Fd:  tmp,
71                 Hsh: hsh,
72                 ctx: ctx,
73         }, nil
74 }
75
76 func (tmp *TmpFileWHash) Cancel() {
77         tmp.Fd.Truncate(0) // #nosec G104
78         tmp.Fd.Close() // #nosec G104
79         os.Remove(tmp.Fd.Name()) // #nosec G104
80 }
81
82 func DirSync(dirPath string) error {
83         fd, err := os.Open(dirPath)
84         if err != nil {
85                 return err
86         }
87         err = fd.Sync()
88         if err != nil {
89                 fd.Close() // #nosec G104
90                 return err
91         }
92         return fd.Close()
93 }
94
95 func (tmp *TmpFileWHash) Commit(dir string) error {
96         var err error
97         if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
98                 return err
99         }
100         if err = tmp.W.Flush(); err != nil {
101                 tmp.Fd.Close() // #nosec G104
102                 return err
103         }
104         if err = tmp.Fd.Sync(); err != nil {
105                 tmp.Fd.Close() // #nosec G104
106                 return err
107         }
108         if err = tmp.Fd.Close(); err != nil {
109                 return err
110         }
111         checksum := Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
112         tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit")
113         if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil {
114                 return err
115         }
116         return DirSync(dir)
117 }