]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tmp.go
Correctly show seconds
[nncp.git] / src / tmp.go
index 62ec5bbc9cbdd4d253858666c850168595ab3ba9..da96a25bfd0785f9bdc7b8aa48e523b01c47360f 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2020 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,6 +19,7 @@ package nncp
 
 import (
        "bufio"
+       "fmt"
        "hash"
        "io"
        "os"
@@ -44,7 +45,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
 }
@@ -74,8 +77,8 @@ func (ctx *Ctx) NewTmpFileWHash() (*TmpFileWHash, error) {
 }
 
 func (tmp *TmpFileWHash) Cancel() {
-       tmp.Fd.Truncate(0) // #nosec G104
-       tmp.Fd.Close() // #nosec G104
+       tmp.Fd.Truncate(0)       // #nosec G104
+       tmp.Fd.Close()           // #nosec G104
        os.Remove(tmp.Fd.Name()) // #nosec G104
 }
 
@@ -92,6 +95,10 @@ func DirSync(dirPath string) error {
        return fd.Close()
 }
 
+func (tmp *TmpFileWHash) Checksum() string {
+       return Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
+}
+
 func (tmp *TmpFileWHash) Commit(dir string) error {
        var err error
        if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
@@ -108,8 +115,14 @@ func (tmp *TmpFileWHash) Commit(dir string) error {
        if err = tmp.Fd.Close(); err != nil {
                return err
        }
-       checksum := Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
-       tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit")
+       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
        }