/*
NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2023 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
import (
"bytes"
+ "errors"
"fmt"
"io"
+ "io/fs"
"os"
"path/filepath"
"strings"
TRx TRxTx = "rx"
TTx TRxTx = "tx"
- HdrSuffix = ".hdr"
+ HdrDir = "hdr"
)
type Job struct {
HshValue *[MTHSize]byte
}
+func JobPath2Hdr(jobPath string) string {
+ return filepath.Join(filepath.Dir(jobPath), HdrDir, filepath.Base(jobPath))
+}
+
func (ctx *Ctx) HdrRead(r io.Reader) (*PktEnc, []byte, error) {
var pktEnc PktEnc
_, err := xdr.Unmarshal(r, &pktEnc)
os.Remove(tmpHdr.Name())
return err
}
- if err = os.Rename(tmpHdr.Name(), tgt+HdrSuffix); err != nil {
+ if err = ensureDir(filepath.Dir(tgt), HdrDir); err != nil {
+ ctx.LogE("hdr-write-ensure-mkdir", nil, err, func(les LEs) string {
+ return "Header writing: ensuring directory"
+ })
+ return err
+ }
+ if err = os.Rename(tmpHdr.Name(), JobPath2Hdr(tgt)); err != nil {
ctx.LogE("hdr-write-rename", nil, err, func(les LEs) string {
return "Header writing: renaming"
})
if nock || part {
fd, err = os.Open(pth)
} else {
- fd, err = os.Open(pth + HdrSuffix)
- if err != nil && os.IsNotExist(err) {
+ fd, err = os.Open(JobPath2Hdr(pth))
+ if err != nil && errors.Is(err, fs.ErrNotExist) {
hdrExists = false
fd, err = os.Open(pth)
}
case MagicNNCPEv4.B:
err = MagicNNCPEv4.TooOld()
case MagicNNCPEv5.B:
+ err = MagicNNCPEv5.TooOld()
+ case MagicNNCPEv6.B:
default:
err = BadMagic
}