X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Ftmp.go;h=aee58677518a562c55d7be7c56931061b2dbf83c;hb=0367cce2741e1ce6a89a49fd5c4e9df6005c9744;hp=272fbb3ec4bcac76a4bfc0f7556d2aa8ed1525af;hpb=9cf466a3b95f9b95a7bce1c1cc2b5c970f9fa6a6;p=nncp.git diff --git a/src/tmp.go b/src/tmp.go index 272fbb3..aee5867 100644 --- a/src/tmp.go +++ b/src/tmp.go @@ -1,6 +1,6 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2020 Sergey Matveev +Copyright (C) 2016-2022 Sergey Matveev 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) { @@ -38,13 +37,14 @@ func TempFile(dir, prefix string) (*os.File, error) { func (ctx *Ctx) NewTmpFile() (*os.File, error) { jobsPath := filepath.Join(ctx.Spool, "tmp") - var err error - if err = os.MkdirAll(jobsPath, os.FileMode(0777)); err != nil { + if err := ensureDir(jobsPath); err != nil { return nil, err } 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 +61,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, @@ -92,9 +89,13 @@ 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 { + if err = ensureDir(dir); err != nil { return err } if err = tmp.W.Flush(); err != nil { @@ -105,9 +106,17 @@ func (tmp *TmpFileWHash) Commit(dir string) error { tmp.Fd.Close() return err } - tmp.Fd.Close() - checksum := Base32Codec.EncodeToString(tmp.Hsh.Sum(nil)) - tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit") + 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 }