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