From a62ea6902fb8751501a724bd20db4baddadc7dba Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 13 Mar 2022 11:38:22 +0300 Subject: [PATCH] Refactored and tested nncp-rm --- doc/cmd/nncp-rm.texi | 54 ++++---- doc/news.ru.texi | 13 ++ doc/news.texi | 13 ++ src/cmd/nncp-rm/main.go | 194 ++++++++++++++++------------ src/nncp.go | 2 +- t/nncp-rm.t | 278 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 448 insertions(+), 106 deletions(-) create mode 100755 t/nncp-rm.t diff --git a/doc/cmd/nncp-rm.texi b/doc/cmd/nncp-rm.texi index 0267673..4444c33 100644 --- a/doc/cmd/nncp-rm.texi +++ b/doc/cmd/nncp-rm.texi @@ -3,15 +3,14 @@ @section nncp-rm @example -$ nncp-rm [options] -tmp +$ nncp-rm [options] [-older X] -tmp $ nncp-rm [options] -lock -$ nncp-rm [options] @{-all|-node NODE@} -part -$ nncp-rm [options] @{-all|-node NODE@} -seen -$ nncp-rm [options] @{-all|-node NODE@} -nock -$ nncp-rm [options] @{-all|-node NODE@} -hdr -$ nncp-rm [options] @{-all|-node NODE@} -area -$ nncp-rm [options] @{-all|-node NODE@} [-rx] [-tx] -$ nncp-rm [options] @{-all|-node NODE@} -pkt < 0 { + if _, exists := pkts[filepath.Base(entry.Name())]; exists { + ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg) + if !*dryRun { + os.Remove(nncp.JobPath2Hdr(pth)) + if err = os.Remove(pth); err != nil { + return err + } + } + } + continue + } + info, err := entry.Info() + if err != nil { + return err } if now.Sub(info.ModTime()) < oldBoundary { - ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string { - return fmt.Sprintf("File %s: too fresh, skipping", path) + ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: pth}}, func(les nncp.LEs) string { + return fmt.Sprintf("File %s: too fresh, skipping", pth) }) - return nil + continue } - if (*doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix)) || - (*doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix)) { - ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) + if (*doNoCK && strings.HasSuffix(entry.Name(), nncp.NoCKSuffix)) || + (*doPart && strings.HasSuffix(entry.Name(), nncp.PartSuffix)) { + ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg) if *dryRun { - return nil + continue } - return os.Remove(path) - } - if len(pkts) > 0 { - if _, exists := pkts[filepath.Base(info.Name())]; exists { - ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) - if *dryRun { - return nil - } - return os.Remove(path) + if err = os.Remove(pth); err != nil { + return err } } if !*doSeen && !*doNoCK && !*doHdr && !*doPart && (*doRx || *doTx) && ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) { - ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) + ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg) if *dryRun { - return nil + continue + } + os.Remove(nncp.JobPath2Hdr(pth)) + if err = os.Remove(pth); err != nil { + return err } - return os.Remove(path) } - return nil - }) + } + } + return nil } if len(pkts) > 0 || *doRx || *doNoCK || *doPart { if err = remove(nncp.TRx); err != nil { log.Fatalln("Can not remove:", err) } } - if len(pkts) > 0 || *doTx || *doHdr { + if len(pkts) > 0 || *doTx { if err = remove(nncp.TTx); err != nil { log.Fatalln("Can not remove:", err) } } - removeSub := func(p string, everything bool) error { - return filepath.Walk( - p, func(path string, info os.FileInfo, err error) error { - if err != nil { - if os.IsNotExist(err) { - return nil - } - return err - } - if info.IsDir() { + removeSub := func(p string) error { + return filepath.Walk(p, func(path string, info os.FileInfo, err error) error { + if err != nil { + if os.IsNotExist(err) { return nil } - logMsg := func(les nncp.LEs) string { - return fmt.Sprintf("File %s: removed", path) - } - if everything { - ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) - if *dryRun { - return nil - } - return os.Remove(path) + return err + } + if info.IsDir() { + return nil + } + logMsg := func(les nncp.LEs) string { + return fmt.Sprintf("File %s: removed", path) + } + if len(pkts) > 0 { + if _, exists := pkts[filepath.Base(info.Name())]; !exists { + return nil } - if now.Sub(info.ModTime()) < oldBoundary { - ctx.LogD( - "rm-skip", nncp.LEs{{K: "File", V: path}}, - func(les nncp.LEs) string { - return fmt.Sprintf("File %s: too fresh, skipping", path) - }, - ) - } else if !*dryRun { - return os.Remove(path) + ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) + if *dryRun { + return nil } + return os.Remove(path) + } + if now.Sub(info.ModTime()) < oldBoundary { + ctx.LogD( + "rm-skip", nncp.LEs{{K: "File", V: path}}, + func(les nncp.LEs) string { + return fmt.Sprintf("File %s: too fresh, skipping", path) + }, + ) return nil - }, - ) + } + ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) + if *dryRun { + return nil + } + return os.Remove(path) + }) } - if *doRx || *doSeen { + if len(pkts) > 0 || *doSeen { if err = removeSub(filepath.Join( ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir, - ), *doSeen); err != nil { + )); err != nil { log.Fatalln("Can not remove:", err) } } - if *doRx || *doHdr { + if *doRx && *doHdr { if err = removeSub(filepath.Join( ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir, - ), *doHdr); err != nil { + )); err != nil { log.Fatalln("Can not remove:", err) } } - if *doTx || *doHdr { + if *doTx && *doHdr { if err = removeSub(filepath.Join( ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir, - ), *doHdr); err != nil { + )); err != nil { log.Fatalln("Can not remove:", err) } } diff --git a/src/nncp.go b/src/nncp.go index 2647e4c..e51f925 100644 --- a/src/nncp.go +++ b/src/nncp.go @@ -40,7 +40,7 @@ along with this program. If not, see .` const Base32Encoded32Len = 52 var ( - Version string = "8.7.1" + Version string = "8.7.2" Base32Codec *base32.Encoding = base32.StdEncoding.WithPadding(base32.NoPadding) ) diff --git a/t/nncp-rm.t b/t/nncp-rm.t new file mode 100755 index 0000000..aa7cc83 --- /dev/null +++ b/t/nncp-rm.t @@ -0,0 +1,278 @@ +#!/bin/sh + +testname=`basename "$0"` +test_description="nncp-rm's behaviour" +. $SHARNESS_TEST_SRCDIR/sharness.sh + +rand32() { + perl <&2 + return 1 + fi + done + local pkts="" + for pkt in `pkts_remove $@` ; do + if ! [ -e $pkt ] ; then + echo unexpectedly removed: $pkt >&2 + return 1 + fi + pkts="$pkts $pkt" + done + PKTS="$pkts" +} + +now=`date +%s` +nncp-cfgnew > cfg +nncp-cfgdir -cfg cfg -dump cfgdir +echo "$PWD/spool" > cfgdir/spool +echo "$PWD/log" > cfgdir/log +neigh=`rand32` +mkdir -p cfgdir/neigh/neigh +for w in id exchpub signpub ; do echo $neigh > cfgdir/neigh/neigh/$w ; done +mkdir -p spool/tmp spool/$neigh/rx/hdr spool/$neigh/rx/seen spool/$neigh/tx/hdr +date_old=$(date -j -f %s +%FT%T $(( $now - (3600 * 24 * 7) ))) + +pkts_old_rx="" +pkts_old_rx_hdr="" +for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/rx/$pkt spool/$neigh/rx/hdr/$pkt + pkts_old_rx="$pkts_old_rx spool/$neigh/rx/$pkt" + pkts_old_rx_hdr="$pkts_old_rx_hdr spool/$neigh/rx/hdr/$pkt" +done + +pkts_old_tx="" +pkts_old_tx_hdr="" +for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/tx/$pkt spool/$neigh/tx/hdr/$pkt + pkts_old_tx="$pkts_old_tx spool/$neigh/tx/$pkt" + pkts_old_tx_hdr="$pkts_old_tx_hdr spool/$neigh/tx/hdr/$pkt" +done + +pkts_old_part="" +for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/rx/$pkt.part + pkts_old_part="$pkts_old_part spool/$neigh/rx/$pkt.part" +done + +pkts_old_nock="" +for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/rx/$pkt.nock + pkts_old_nock="$pkts_old_nock spool/$neigh/rx/$pkt.nock" +done + +pkts_old_seen="" +for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/rx/seen/$pkt + pkts_old_seen="$pkts_old_seen spool/$neigh/rx/seen/$pkt" +done + +pkts_new_rx="" +pkts_new_rx_hdr="" +for pkt in `randpkts` ; do + touch spool/$neigh/rx/$pkt spool/$neigh/rx/hdr/$pkt + pkts_new_rx="$pkts_new_rx spool/$neigh/rx/$pkt" + pkts_new_rx_hdr="$pkts_new_rx_hdr spool/$neigh/rx/hdr/$pkt" +done + +pkts_new_tx="" +pkts_new_tx_hdr="" +for pkt in `randpkts` ; do + touch spool/$neigh/tx/$pkt spool/$neigh/tx/hdr/$pkt + pkts_new_tx="$pkts_new_tx spool/$neigh/tx/$pkt" + pkts_new_tx_hdr="$pkts_new_tx_hdr spool/$neigh/tx/hdr/$pkt" +done + +pkts_new_part="" +for pkt in `randpkts` ; do + touch spool/$neigh/rx/$pkt.part + pkts_new_part="$pkts_new_part spool/$neigh/rx/$pkt.part" +done + +pkts_new_nock="" +for pkt in `randpkts` ; do + touch spool/$neigh/rx/$pkt.nock + pkts_new_nock="$pkts_new_nock spool/$neigh/rx/$pkt.nock" +done + +pkts_new_seen="" +for pkt in `randpkts` ; do + touch spool/$neigh/rx/seen/$pkt + pkts_new_seen="$pkts_new_seen spool/$neigh/rx/seen/$pkt" +done + +pkts_tmp_old="" +for pkt in `randpkts` ; do + touch -d $date_old spool/tmp/$pkt + pkts_tmp_old="$pkts_tmp_old spool/tmp/$pkt" +done + +pkts_tmp_new="" +for pkt in `randpkts` ; do + touch spool/tmp/$pkt + pkts_tmp_new="$pkts_tmp_new spool/tmp/$pkt" +done + +pkts_hdr_excess_old_rx="" +for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/rx/hdr/$pkt + pkts_hdr_excess_old_rx="$pkts_hdr_excess_old_rx spool/$neigh/rx/hdr/$pkt" +done + +pkts_hdr_excess_old_tx="" +for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/tx/hdr/$pkt + pkts_hdr_excess_old_tx="$pkts_hdr_excess_old_tx spool/$neigh/tx/hdr/$pkt" +done + +pkts_hdr_excess_new_rx="" +for pkt in `randpkts` ; do + touch spool/$neigh/rx/hdr/$pkt + pkts_hdr_excess_new_rx="$pkts_hdr_excess_new_rx spool/$neigh/rx/hdr/$pkt" +done + +pkts_hdr_excess_new_tx="" +for pkt in `randpkts` ; do + touch spool/$neigh/tx/hdr/$pkt + pkts_hdr_excess_new_tx="$pkts_hdr_excess_new_tx spool/$neigh/tx/hdr/$pkt" +done + +pkts_area_old="" +pkts_area_new="" +for area in `randpkts` ; do + mkdir -p spool/$neigh/area/$area + for pkt in `randpkts` ; do + touch -d $date_old spool/$neigh/area/$area/$pkt + pkts_area_old="$pkts_area_old spool/$neigh/area/$area/$pkt" + done + for pkt in `randpkts` ; do + touch spool/$neigh/area/$area/$pkt + pkts_area_new="$pkts_area_new spool/$neigh/area/$area/$pkt" + done +done + +pkts_specified="" +for pkt in `randpkts` ; do + touch spool/$neigh/rx/$pkt + pkts_specified="$pkts_specified spool/$neigh/rx/$pkt" +done +for pkt in `randpkts` ; do + touch spool/$neigh/tx/$pkt + pkts_specified="$pkts_specified spool/$neigh/tx/$pkt" +done +for pkt in `randpkts` ; do + touch spool/$neigh/rx/seen/$pkt + pkts_specified="$pkts_specified spool/$neigh/rx/seen/$pkt" +done + +PKTS=" +$pkts_old_rx +$pkts_old_rx_hdr +$pkts_old_tx +$pkts_old_tx_hdr +$pkts_old_part +$pkts_old_nock +$pkts_old_seen +$pkts_new_rx +$pkts_new_rx_hdr +$pkts_new_tx +$pkts_new_tx_hdr +$pkts_new_part +$pkts_new_nock +$pkts_new_seen +$pkts_tmp_old +$pkts_tmp_new +$pkts_hdr_excess_old_rx +$pkts_hdr_excess_old_tx +$pkts_hdr_excess_new_rx +$pkts_hdr_excess_new_tx +$pkts_area_old +$pkts_area_new +$pkts_specified +" +rmcmd="nncp-rm -quiet -cfg cfgdir -all" +older="-older 6d" + +$rmcmd -pkt <