/*
NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2022 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
"errors"
"fmt"
"io"
- "io/ioutil"
+ "io/fs"
"log"
"mime"
"os"
SeenDir = "seen"
)
+type TossOpts struct {
+ Nice uint8
+ DryRun bool
+ DoSeen bool
+ NoFile bool
+ NoFreq bool
+ NoExec bool
+ NoTrns bool
+ NoArea bool
+ NoACK bool
+}
+
func jobPath2Seen(jobPath string) string {
return filepath.Join(filepath.Dir(jobPath), SeenDir, filepath.Base(jobPath))
}
pktSize uint64,
jobPath string,
decompressor *zstd.Decoder,
- dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK bool,
+ opts *TossOpts,
) error {
defer pipeR.Close()
sendmail := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
})
switch pkt.Type {
case PktTypeExec, PktTypeExecFat:
- if noExec {
+ if opts.NoExec {
return nil
}
path := bytes.Split(pkt.Path[:int(pkt.PathLen)], []byte{0})
log.Fatalln(err)
}
}
- if !dryRun {
+ if !opts.DryRun {
cmd := exec.Command(cmdline[0], append(cmdline[1:], args...)...)
cmd.Env = append(
cmd.Env,
humanize.IBytes(pktSize),
)
})
- if !dryRun && jobPath != "" {
- if doSeen {
+ if !opts.DryRun && jobPath != "" {
+ if opts.DoSeen {
if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
return err
}
}
case PktTypeFile:
- if noFile {
+ if opts.NoFile {
return nil
}
dst := string(pkt.Path[:int(pkt.PathLen)])
})
return err
}
- if !dryRun {
+ if !opts.DryRun {
tmp, err := TempFile(dir, "file")
if err != nil {
ctx.LogE("rx-mktemp", les, err, func(les LEs) string {
dstPathCtr := 0
for {
if _, err = os.Stat(dstPath); err != nil {
- if os.IsNotExist(err) {
+ if errors.Is(err, fs.ErrNotExist) {
break
}
ctx.LogE("rx-stat", les, err, func(les LEs) string {
dst, humanize.IBytes(pktSize), sender.Name,
)
})
- if !dryRun {
+ if !opts.DryRun {
if jobPath != "" {
- if doSeen {
+ if opts.DoSeen {
if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
return err
}
}
case PktTypeFreq:
- if noFreq {
+ if opts.NoFreq {
return nil
}
src := string(pkt.Path[:int(pkt.PathLen)])
)
return err
}
- dstRaw, err := ioutil.ReadAll(pipeR)
+ dstRaw, err := io.ReadAll(pipeR)
if err != nil {
ctx.LogE("rx-read", les, err, func(les LEs) string {
return fmt.Sprintf(
)
return err
}
- if !dryRun {
+ if !opts.DryRun {
err = ctx.TxFile(
sender,
pkt.Nice,
ctx.LogI("rx", les, func(les LEs) string {
return fmt.Sprintf("Got file request %s to %s", src, sender.Name)
})
- if !dryRun {
+ if !opts.DryRun {
if jobPath != "" {
- if doSeen {
+ if opts.DoSeen {
if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
return err
}
}
case PktTypeTrns:
- if noTrns {
+ if opts.NoTrns {
return nil
}
dst := new([MTHSize]byte)
return err
}
ctx.LogD("rx-tx", les, logMsg)
- if !dryRun {
+ if !opts.DryRun {
if len(node.Via) == 0 {
if err = ctx.TxTrns(node, nice, int64(pktSize), pipeR); err != nil {
ctx.LogE("rx", les, err, func(les LEs) string {
humanize.IBytes(pktSize),
)
})
- if !dryRun && jobPath != "" {
- if doSeen {
+ if !opts.DryRun && jobPath != "" {
+ if opts.DoSeen {
if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
return err
}
}
case PktTypeArea:
- if noArea {
+ if opts.NoArea {
return nil
}
areaId := new(AreaId)
les = append(les, LE{"AreaMsg", msgHash})
ctx.LogD("rx-area", les, logMsg)
- if dryRun {
+ if opts.DryRun {
for _, nodeId := range area.Subs {
node := ctx.Neigh[*nodeId]
lesEcho := append(les, LE{"Echo", nodeId})
ctx.LogD("rx-area-seen", les, func(les LEs) string {
return logMsg(les) + ": already seen"
})
- if !dryRun && jobPath != "" {
+ if !opts.DryRun && jobPath != "" {
if err = os.Remove(jobPath); err != nil {
ctx.LogE("rx-area-remove", les, err, func(les LEs) string {
return fmt.Sprintf(
uint64(pktSizeWithoutEnc(int64(pktSize))),
"",
decompressor,
- dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK,
+ opts,
)
}()
_, _, _, err = PktEncRead(
}
}
- if !dryRun && jobPath != "" {
+ if !opts.DryRun && jobPath != "" {
if err = os.MkdirAll(seenDir, os.FileMode(0777)); err != nil {
ctx.LogE("rx-area-mkdir", les, err, logMsg)
return err
}
case PktTypeACK:
- if noACK {
+ if opts.NoACK {
return nil
}
hsh := Base32Codec.EncodeToString(pkt.Path[:MTHSize])
ctx.LogD("rx-ack", les, logMsg)
pktPath := filepath.Join(ctx.Spool, sender.Id.String(), string(TTx), hsh)
if _, err := os.Stat(pktPath); err == nil {
- if !dryRun {
+ if !opts.DryRun {
if err = os.Remove(pktPath); err != nil {
ctx.LogE("rx-ack", les, err, func(les LEs) string {
return logMsg(les) + ": removing packet"
return logMsg(les) + ": already disappeared"
})
}
- if !dryRun && doSeen {
+ if !opts.DryRun && opts.DoSeen {
if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
return err
}
}
}
}
- if !dryRun {
+ if !opts.DryRun {
if err = os.Remove(jobPath); err != nil {
ctx.LogE("rx", les, err, func(les LEs) string {
return logMsg(les) + ": removing job"
return nil
}
-func (ctx *Ctx) Toss(
- nodeId *NodeId,
- xx TRxTx,
- nice uint8,
- dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK bool,
-) bool {
+func (ctx *Ctx) Toss(nodeId *NodeId, xx TRxTx, opts *TossOpts) bool {
dirLock, err := ctx.LockDir(nodeId, "toss")
if err != nil {
return false
{"Pkt", pktName},
{"Nice", int(job.PktEnc.Nice)},
}
- if job.PktEnc.Nice > nice {
+ if job.PktEnc.Nice > opts.Nice {
ctx.LogD("rx-too-nice", les, func(les LEs) string {
return fmt.Sprintf(
"Tossing %s/%s: too nice: %s",
uint64(pktSizeWithoutEnc(job.Size)),
job.Path,
decompressor,
- dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK,
+ opts,
)
}()
pipeWB := bufio.NewWriter(pipeW)
sharedKey, _, _, err = PktEncRead(
ctx.Self,
ctx.Neigh,
- bufio.NewReader(fd),
+ bufio.NewReaderSize(fd, MTHBlockSize),
pipeWB,
sharedKey == nil,
sharedKey,
return isBad
}
-func (ctx *Ctx) AutoToss(
- nodeId *NodeId,
- nice uint8,
- doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK bool,
-) (chan struct{}, chan bool) {
+func (ctx *Ctx) AutoToss(nodeId *NodeId, opts *TossOpts) (chan struct{}, chan bool) {
dw, err := ctx.NewDirWatcher(
filepath.Join(ctx.Spool, nodeId.String(), string(TRx)),
time.Second,
badCode <- bad
return
case <-dw.C:
- bad = !ctx.Toss(
- nodeId, TRx, nice, false,
- doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK) || bad
+ bad = !ctx.Toss(nodeId, TRx, opts) || bad
}
}
}()