]> Cypherpunks.ru repositories - nncp.git/blob - src/tmp.go
163acb366d50873c3a484548604f0050eba92d3a
[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         if err := ensureDir(jobsPath); err != nil {
41                 return nil, err
42         }
43         fd, err := TempFile(jobsPath, "")
44         if err == nil {
45                 ctx.LogD("tmp", LEs{{"Src", fd.Name()}}, func(les LEs) string {
46                         return "Temporary file created: " + fd.Name()
47                 })
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 := MTHNew(0, 0)
65         return &TmpFileWHash{
66                 W:   bufio.NewWriter(io.MultiWriter(hsh, tmp)),
67                 Fd:  tmp,
68                 Hsh: hsh,
69                 ctx: ctx,
70         }, nil
71 }
72
73 func (tmp *TmpFileWHash) Cancel() {
74         tmp.Fd.Truncate(0)
75         tmp.Fd.Close()
76         os.Remove(tmp.Fd.Name())
77 }
78
79 func DirSync(dirPath string) error {
80         fd, err := os.Open(dirPath)
81         if err != nil {
82                 return err
83         }
84         err = fd.Sync()
85         if err != nil {
86                 fd.Close()
87                 return err
88         }
89         return fd.Close()
90 }
91
92 func (tmp *TmpFileWHash) Checksum() string {
93         return Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
94 }
95
96 func (tmp *TmpFileWHash) Commit(dir string) error {
97         var err error
98         if err = ensureDir(dir); err != nil {
99                 return err
100         }
101         if err = tmp.W.Flush(); err != nil {
102                 tmp.Fd.Close()
103                 return err
104         }
105         if err = tmp.Fd.Sync(); err != nil {
106                 tmp.Fd.Close()
107                 return err
108         }
109         if err = tmp.Fd.Close(); err != nil {
110                 return err
111         }
112         checksum := tmp.Checksum()
113         tmp.ctx.LogD(
114                 "tmp-rename",
115                 LEs{{"Src", tmp.Fd.Name()}, {"Dst", checksum}},
116                 func(les LEs) string {
117                         return fmt.Sprintf("Temporary file: %s -> %s", tmp.Fd.Name(), checksum)
118                 },
119         )
120         if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil {
121                 return err
122         }
123         return DirSync(dir)
124 }