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