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