]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tmp.go
MTH
[nncp.git] / src / tmp.go
index 95e45ee4a6162a3eb01ea084ecce9c6606f804e6..88a7ec3847801a9d9fdbddcdf7ae3d67bc2098f4 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -19,14 +19,13 @@ package nncp
 
 import (
        "bufio"
+       "fmt"
        "hash"
        "io"
        "os"
        "path/filepath"
        "strconv"
        "time"
-
-       "golang.org/x/crypto/blake2b"
 )
 
 func TempFile(dir, prefix string) (*os.File, error) {
@@ -44,7 +43,9 @@ func (ctx *Ctx) NewTmpFile() (*os.File, error) {
        }
        fd, err := TempFile(jobsPath, "")
        if err == nil {
-               ctx.LogD("tmp", SDS{"src": fd.Name()}, "created")
+               ctx.LogD("tmp", LEs{{"Src", fd.Name()}}, func(les LEs) string {
+                       return "Temporary file created: " + fd.Name()
+               })
        }
        return fd, err
 }
@@ -61,10 +62,7 @@ func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
        if err != nil {
                return nil, err
        }
-       hsh, err := blake2b.New256(nil)
-       if err != nil {
-               return nil, err
-       }
+       hsh := MTHNew(0, 0)
        return &TmpFileWHash{
                W:   bufio.NewWriter(io.MultiWriter(hsh, tmp)),
                Fd:  tmp,
@@ -74,9 +72,26 @@ func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
 }
 
 func (tmp *TmpFileWHash) Cancel() {
-       tmp.Fd.Truncate(0)
-       tmp.Fd.Close()
-       os.Remove(tmp.Fd.Name())
+       tmp.Fd.Truncate(0)       // #nosec G104
+       tmp.Fd.Close()           // #nosec G104
+       os.Remove(tmp.Fd.Name()) // #nosec G104
+}
+
+func DirSync(dirPath string) error {
+       fd, err := os.Open(dirPath)
+       if err != nil {
+               return err
+       }
+       err = fd.Sync()
+       if err != nil {
+               fd.Close() // #nosec G104
+               return err
+       }
+       return fd.Close()
+}
+
+func (tmp *TmpFileWHash) Checksum() string {
+       return Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
 }
 
 func (tmp *TmpFileWHash) Commit(dir string) error {
@@ -85,15 +100,26 @@ func (tmp *TmpFileWHash) Commit(dir string) error {
                return err
        }
        if err = tmp.W.Flush(); err != nil {
-               tmp.Fd.Close()
+               tmp.Fd.Close() // #nosec G104
                return err
        }
        if err = tmp.Fd.Sync(); err != nil {
-               tmp.Fd.Close()
+               tmp.Fd.Close() // #nosec G104
+               return err
+       }
+       if err = tmp.Fd.Close(); err != nil {
+               return err
+       }
+       checksum := tmp.Checksum()
+       tmp.ctx.LogD(
+               "tmp-rename",
+               LEs{{"Src", tmp.Fd.Name()}, {"Dst", checksum}},
+               func(les LEs) string {
+                       return fmt.Sprintf("Temporary file: %s -> %s", tmp.Fd.Name(), checksum)
+               },
+       )
+       if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil {
                return err
        }
-       tmp.Fd.Close()
-       checksum := ToBase32(tmp.Hsh.Sum(nil))
-       tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit")
-       return os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum))
+       return DirSync(dir)
 }