/*
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
"path/filepath"
"github.com/dustin/go-humanize"
- "go.cypherpunks.ru/nncp/v7"
+ "go.cypherpunks.ru/nncp/v8"
)
func usage() {
goto Tx
}
fis, err = dir.Readdir(0)
- dir.Close() // #nosec G104
+ dir.Close()
if err != nil {
ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
return logMsg(les) + ": reading"
continue
}
fisInt, err := dir.Readdir(0)
- dir.Close() // #nosec G104
+ dir.Close()
if err != nil {
ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
return logMsg(les) + ": reading"
ctx.NodeName(nodeId), filename,
)
}
+ if _, err = os.Stat(filepath.Join(
+ ctx.Spool,
+ nodeId.String(),
+ string(nncp.TRx),
+ nncp.SeenDir,
+ fiInt.Name(),
+ )); err == nil || !os.IsNotExist(err) {
+ ctx.LogI("xfer-rx-seen", les, func(les nncp.LEs) string {
+ return logMsg(les) + ": packet already seen"
+ })
+ if !*keep {
+ if err = os.Remove(filename); err != nil {
+ ctx.LogE("xfer-rx-remove", les, err, logMsg)
+ isBad = true
+ }
+ }
+ continue
+ }
fd, err := os.Open(filename)
if err != nil {
ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
continue
}
pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
- if err != nil || pktEnc.Magic != nncp.MagicNNCPEv5 {
- ctx.LogD("xfer-rx-not-packet", les, func(les nncp.LEs) string {
- return logMsg(les) + ": is not a packet"
- })
- fd.Close() // #nosec G104
+ if err == nil {
+ switch pktEnc.Magic {
+ case nncp.MagicNNCPEv1.B:
+ err = nncp.MagicNNCPEv1.TooOld()
+ case nncp.MagicNNCPEv2.B:
+ err = nncp.MagicNNCPEv2.TooOld()
+ case nncp.MagicNNCPEv3.B:
+ err = nncp.MagicNNCPEv3.TooOld()
+ case nncp.MagicNNCPEv4.B:
+ err = nncp.MagicNNCPEv4.TooOld()
+ case nncp.MagicNNCPEv5.B:
+ err = nncp.MagicNNCPEv5.TooOld()
+ case nncp.MagicNNCPEv6.B:
+ default:
+ err = errors.New("is not an encrypted packet")
+ }
+ }
+ if err != nil {
+ ctx.LogD(
+ "xfer-rx-not-packet",
+ append(les, nncp.LE{K: "Err", V: err}),
+ func(les nncp.LEs) string {
+ return logMsg(les) + ": not valid packet: " + err.Error()
+ },
+ )
+ fd.Close()
continue
}
if pktEnc.Nice > nice {
ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
return logMsg(les) + ": too nice"
})
- fd.Close() // #nosec G104
+ fd.Close()
continue
}
les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
}
if !ctx.IsEnoughSpace(fiInt.Size()) {
ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
- fd.Close() // #nosec G104
+ fd.Close()
continue
}
- if _, err = fd.Seek(0, 0); err != nil {
+ if _, err = fd.Seek(0, io.SeekStart); err != nil {
log.Fatalln(err)
}
tmp, err := ctx.NewTmpFileWHash()
}
r, w := io.Pipe()
go func() {
- _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
+ _, err := io.CopyN(
+ w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), fiInt.Size(),
+ )
if err == nil {
err = w.Close()
}
if err != nil {
ctx.LogE("xfer-rx", les, err, logMsg)
- w.CloseWithError(err) // #nosec G104
+ w.CloseWithError(err)
}
}()
- if _, err = nncp.CopyProgressed(
+ _, err = nncp.CopyProgressed(
tmp.W, r, "Rx",
append(
les,
nncp.LE{K: "FullSize", V: fiInt.Size()},
),
ctx.ShowPrgrs,
- ); err != nil {
+ )
+ fd.Close()
+ if err != nil {
ctx.LogE("xfer-rx", les, err, logMsg)
+ tmp.Cancel()
isBad = true
+ continue
}
- fd.Close() // #nosec G104
- if isBad {
+ if err = tmp.W.Flush(); err != nil {
+ ctx.LogE("xfer-rx", les, err, logMsg)
+ tmp.Cancel()
+ isBad = true
+ continue
+ }
+ if tmp.Checksum() != fiInt.Name() {
+ ctx.LogE("xfer-rx", les, errors.New("checksum mismatch"), logMsg)
tmp.Cancel()
+ isBad = true
continue
}
if err = tmp.Commit(filepath.Join(
})
continue
}
- if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
- ctx.LogD("xfer-tx-seen", les, func(les nncp.LEs) string {
- return logMsg(les) + ": already seen"
- })
- continue
- }
tmp, err := nncp.TempFile(dstPath, "xfer")
if err != nil {
ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string {
ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
return logMsg(les) + ": opening"
})
- tmp.Close() // #nosec G104
+ tmp.Close()
isBad = true
continue
}
bufW := bufio.NewWriter(tmp)
copied, err := nncp.CopyProgressed(
- bufW, bufio.NewReader(fd), "Tx",
+ bufW, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "Tx",
append(les, nncp.LE{K: "FullSize", V: job.Size}),
ctx.ShowPrgrs,
)
- fd.Close() // #nosec G104
+ fd.Close()
if err != nil {
ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
return logMsg(les) + ": copying"
})
- tmp.Close() // #nosec G104
+ tmp.Close()
isBad = true
continue
}
if err = bufW.Flush(); err != nil {
- tmp.Close() // #nosec G104
+ tmp.Close()
ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
return logMsg(les) + ": flushing"
})
isBad = true
continue
}
- if err = tmp.Sync(); err != nil {
- tmp.Close() // #nosec G104
- ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
- return logMsg(les) + ": syncing"
- })
- isBad = true
- continue
+ if !nncp.NoSync {
+ if err = tmp.Sync(); err != nil {
+ tmp.Close()
+ ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
+ return logMsg(les) + ": syncing"
+ })
+ isBad = true
+ continue
+ }
}
if err = tmp.Close(); err != nil {
ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string {
isBad = true
continue
}
- os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
+ os.Remove(filepath.Join(dstPath, pktName+".part"))
les = les[:len(les)-1]
ctx.LogI(
"xfer-tx",
})
isBad = true
} else if ctx.HdrUsage {
- os.Remove(job.Path + nncp.HdrSuffix)
+ os.Remove(nncp.JobPath2Hdr(job.Path))
}
}
}