]> Cypherpunks.ru repositories - nncp.git/blob - src/tmp.go
Do Mkdir syscall only if directory does not exist
[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 := os.Stat(jobsPath); err != nil {
41                 if !os.IsNotExist(err) {
42                         return nil, err
43                 }
44                 if err := os.MkdirAll(jobsPath, os.FileMode(0777)); err != nil {
45                         return nil, err
46                 }
47         }
48         fd, err := TempFile(jobsPath, "")
49         if err == nil {
50                 ctx.LogD("tmp", LEs{{"Src", fd.Name()}}, func(les LEs) string {
51                         return "Temporary file created: " + fd.Name()
52                 })
53         }
54         return fd, err
55 }
56
57 type TmpFileWHash struct {
58         W   *bufio.Writer
59         Fd  *os.File
60         Hsh hash.Hash
61         ctx *Ctx
62 }
63
64 func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
65         tmp, err := ctx.NewTmpFile()
66         if err != nil {
67                 return nil, err
68         }
69         hsh := MTHNew(0, 0)
70         return &TmpFileWHash{
71                 W:   bufio.NewWriter(io.MultiWriter(hsh, tmp)),
72                 Fd:  tmp,
73                 Hsh: hsh,
74                 ctx: ctx,
75         }, nil
76 }
77
78 func (tmp *TmpFileWHash) Cancel() {
79         tmp.Fd.Truncate(0)
80         tmp.Fd.Close()
81         os.Remove(tmp.Fd.Name())
82 }
83
84 func DirSync(dirPath string) error {
85         fd, err := os.Open(dirPath)
86         if err != nil {
87                 return err
88         }
89         err = fd.Sync()
90         if err != nil {
91                 fd.Close()
92                 return err
93         }
94         return fd.Close()
95 }
96
97 func (tmp *TmpFileWHash) Checksum() string {
98         return Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
99 }
100
101 func (tmp *TmpFileWHash) Commit(dir string) error {
102         var err error
103         if _, err = os.Stat(dir); err != nil {
104                 if !os.IsNotExist(err) {
105                         return err
106                 }
107                 if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
108                         return err
109                 }
110         }
111         if err = tmp.W.Flush(); err != nil {
112                 tmp.Fd.Close()
113                 return err
114         }
115         if err = tmp.Fd.Sync(); err != nil {
116                 tmp.Fd.Close()
117                 return err
118         }
119         if err = tmp.Fd.Close(); err != nil {
120                 return err
121         }
122         checksum := tmp.Checksum()
123         tmp.ctx.LogD(
124                 "tmp-rename",
125                 LEs{{"Src", tmp.Fd.Name()}, {"Dst", checksum}},
126                 func(les LEs) string {
127                         return fmt.Sprintf("Temporary file: %s -> %s", tmp.Fd.Name(), checksum)
128                 },
129         )
130         if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil {
131                 return err
132         }
133         return DirSync(dir)
134 }