import (
"bufio"
+ "fmt"
"hash"
"io"
"os"
"path/filepath"
"strconv"
"time"
-
- "golang.org/x/crypto/blake2b"
)
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", LEs{{"Src", fd.Name()}}, "created")
+ ctx.LogD("tmp", LEs{{"Src", fd.Name()}}, func(les LEs) string {
+ return "Temporary file created: " + fd.Name()
+ })
}
return fd, err
}
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,
}
func (tmp *TmpFileWHash) Cancel() {
- tmp.Fd.Truncate(0) // #nosec G104
- tmp.Fd.Close() // #nosec G104
- os.Remove(tmp.Fd.Name()) // #nosec G104
+ tmp.Fd.Truncate(0)
+ tmp.Fd.Close()
+ os.Remove(tmp.Fd.Name())
}
func DirSync(dirPath string) error {
}
err = fd.Sync()
if err != nil {
- fd.Close() // #nosec G104
+ fd.Close()
return err
}
return fd.Close()
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 {
- tmp.Fd.Close() // #nosec G104
+ tmp.Fd.Close()
return err
}
if err = tmp.Fd.Sync(); err != nil {
- tmp.Fd.Close() // #nosec G104
+ tmp.Fd.Close()
return err
}
if err = tmp.Fd.Close(); err != nil {
return err
}
checksum := tmp.Checksum()
- tmp.ctx.LogD("tmp", LEs{{"Src", tmp.Fd.Name()}, {"Dst", checksum}}, "commit")
+ 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
}