]> Cypherpunks.ru repositories - nncp.git/blob - src/tmp.go
Note about buildability with 1.22
[nncp.git] / src / tmp.go
1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package nncp
17
18 import (
19         "bufio"
20         "fmt"
21         "hash"
22         "io"
23         "os"
24         "path/filepath"
25         "strconv"
26         "time"
27 )
28
29 var NoSync bool
30
31 func init() {
32         NoSync = os.Getenv(CfgNoSync) != ""
33 }
34
35 func TempFile(dir, prefix string) (*os.File, error) {
36         // Assume that probability of suffix collision is negligible
37         suffix := strconv.FormatInt(time.Now().UnixNano()+int64(os.Getpid()), 16)
38         name := filepath.Join(dir, "nncp"+prefix+suffix)
39         return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
40 }
41
42 func (ctx *Ctx) NewTmpFile() (*os.File, error) {
43         jobsPath := filepath.Join(ctx.Spool, "tmp")
44         if err := ensureDir(jobsPath); err != nil {
45                 return nil, err
46         }
47         fd, err := TempFile(jobsPath, "")
48         if err == nil {
49                 ctx.LogD("tmp", LEs{{"Src", fd.Name()}}, func(les LEs) string {
50                         return "Temporary file created: " + fd.Name()
51                 })
52         }
53         return fd, err
54 }
55
56 type TmpFileWHash struct {
57         W   *bufio.Writer
58         Fd  *os.File
59         Hsh hash.Hash
60         ctx *Ctx
61 }
62
63 func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
64         tmp, err := ctx.NewTmpFile()
65         if err != nil {
66                 return nil, err
67         }
68         hsh := MTHNew(0, 0)
69         return &TmpFileWHash{
70                 W:   bufio.NewWriter(io.MultiWriter(hsh, tmp)),
71                 Fd:  tmp,
72                 Hsh: hsh,
73                 ctx: ctx,
74         }, nil
75 }
76
77 func (tmp *TmpFileWHash) Cancel() {
78         tmp.Fd.Truncate(0)
79         tmp.Fd.Close()
80         os.Remove(tmp.Fd.Name())
81 }
82
83 func DirSync(dirPath string) error {
84         if NoSync {
85                 return nil
86         }
87         fd, err := os.Open(dirPath)
88         if err != nil {
89                 return err
90         }
91         err = fd.Sync()
92         if err != nil {
93                 fd.Close()
94                 return err
95         }
96         return fd.Close()
97 }
98
99 func (tmp *TmpFileWHash) Checksum() string {
100         return Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
101 }
102
103 func (tmp *TmpFileWHash) Commit(dir string) error {
104         var err error
105         if err = ensureDir(dir); err != nil {
106                 return err
107         }
108         if err = tmp.W.Flush(); err != nil {
109                 tmp.Fd.Close()
110                 return err
111         }
112         if !NoSync {
113                 if err = tmp.Fd.Sync(); err != nil {
114                         tmp.Fd.Close()
115                         return err
116                 }
117         }
118         if err = tmp.Fd.Close(); err != nil {
119                 return err
120         }
121         checksum := tmp.Checksum()
122         tmp.ctx.LogD(
123                 "tmp-rename",
124                 LEs{{"Src", tmp.Fd.Name()}, {"Dst", checksum}},
125                 func(les LEs) string {
126                         return fmt.Sprintf("Temporary file: %s -> %s", tmp.Fd.Name(), checksum)
127                 },
128         )
129         if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil {
130                 return err
131         }
132         return DirSync(dir)
133 }