From 495e07e3c84060fd7779364130fc56e30112445b Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Wed, 29 Nov 2017 23:41:06 +0300 Subject: [PATCH] Various file deletion capabilities --- doc/cmds.texi | 25 +++++- doc/news.ru.texi | 3 + doc/news.texi | 3 + src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go | 94 ++++++++++++++++----- 4 files changed, 98 insertions(+), 27 deletions(-) diff --git a/doc/cmds.texi b/doc/cmds.texi index 6113aeb..51ea04f 100644 --- a/doc/cmds.texi +++ b/doc/cmds.texi @@ -405,12 +405,29 @@ Checksums: @section nncp-rm @verbatim -% nncp-rm [options] NODE PKT +% nncp-rm [options] -tmp +% nncp-rm [options] -lock +% nncp-rm [options] -node NODE [-rx] [-tx] [-part] [-seen] +% nncp-rm [options] -node NODE -pkt PKT @end verbatim -Remove specified packet (Base32 name) in @option{NODE}'s queues. This -command is useful when you want to remove the packet that is failing to -be processed. +This command is aimed to delete various files from your spool directory: + +@itemize +@item If @option{-tmp} option is specified, then it will delete all +temporary files in @file{spool/tmp} directory. Files may stay in it when +commands like @ref{nncp-file} fail for some reason. +@item If @option{-lock} option is specified, then all @file{.lock} files +will be deleted in your spool directory. +@item If @option{-pkt} option is specified, then @file{PKT} packet (its +Base32 name) will be deleted. This is useful when you see some packet +failing to be processed. +@item When either @option{-rx} or @option{-tx} options are specified +(maybe both of them), then delete all packets from that given queues. If +@option{-part} is given, then delete only @file{.part}ly downloaded +ones. If @option{-seen} option is specified, then delete only +@file{.seen} files. +@end itemize @node nncp-stat @section nncp-stat diff --git a/doc/news.ru.texi b/doc/news.ru.texi index a8600ad..ce983f3 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -22,6 +22,9 @@ @item Возможность переопределить путь до spool директории и файла журнала через аргумент командной строки или переменную окружения. +@item +@command{nncp-rm} команда может удалять все исходящие/входящие, +@file{.seen}, @file{.part}, @file{.lock} и временные файлы. @end itemize @node Релиз 0.12 diff --git a/doc/news.texi b/doc/news.texi index eae13ba..189311d 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -24,6 +24,9 @@ packets could create many goroutines. @item Ability to override path to spool directory and logfile through either command line argument, or environment variable. +@item +@command{nncp-rm} is able to delete outbound/inbound, @file{.seen}, +@file{.part}, @file{.lock} and temporary files. @end itemize @node Release 0.12 diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go index cfe4bbb..d93a46a 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go @@ -25,6 +25,7 @@ import ( "log" "os" "path/filepath" + "strings" "cypherpunks.ru/nncp" ) @@ -32,15 +33,26 @@ import ( func usage() { fmt.Fprintf(os.Stderr, nncp.UsageHeader()) fmt.Fprintln(os.Stderr, "nncp-rm -- remove packet\n") - fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE PKT\nOptions:\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "Usage: %s [options] -tmp\n", os.Args[0]) + fmt.Fprintf(os.Stderr, " %s [options] -lock\n", os.Args[0]) + fmt.Fprintf(os.Stderr, " %s [options] -node NODE [-rx] [-tx] [-part] [-seen]\n", os.Args[0]) + fmt.Fprintf(os.Stderr, " %s [options] -node NODE -pkt PKT\n", os.Args[0]) + fmt.Fprintln(os.Stderr, "Options:") flag.PrintDefaults() } func main() { var ( cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") + doTmp = flag.Bool("tmp", false, "Remove all temporary files") + doLock = flag.Bool("lock", false, "Remove all lock files") + nodeRaw = flag.String("node", "", "Node to remove files in") + doRx = flag.Bool("rx", false, "Process received packets") + doTx = flag.Bool("tx", false, "Process transfered packets") + doPart = flag.Bool("part", false, "Remove only .part files") + doSeen = flag.Bool("seen", false, "Remove only .seen files") + pktRaw = flag.String("pkt", "", "Packet to remove") spoolPath = flag.String("spool", "", "Override path to spool") - logPath = flag.String("log", "", "Override path to logfile") quiet = flag.Bool("quiet", false, "Print only errors") debug = flag.Bool("debug", false, "Print debug messages") version = flag.Bool("version", false, "Print version information") @@ -56,36 +68,72 @@ func main() { fmt.Println(nncp.VersionGet()) return } - if flag.NArg() != 2 { - usage() - os.Exit(1) - } - ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug) + ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, *debug) if err != nil { log.Fatalln("Error during initialization:", err) } - node, err := ctx.FindNode(flag.Arg(0)) + if *doTmp { + err = filepath.Walk(filepath.Join(ctx.Spool, "tmp"), func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + return os.Remove(info.Name()) + }) + if err != nil { + log.Fatalln("Error during walking:", err) + } + return + } + if *doLock { + err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if strings.HasSuffix(info.Name(), ".lock") { + return os.Remove(info.Name()) + } + return nil + }) + if err != nil { + log.Fatalln("Error during walking:", err) + } + return + } + if *nodeRaw == "" { + usage() + os.Exit(1) + } + node, err := ctx.FindNode(*nodeRaw) if err != nil { - log.Fatalln("Invalid NODE specified:", err) + log.Fatalln("Invalid -node specified:", err) } - - pktName := flag.Arg(1) - remove := func(xx nncp.TRxTx) bool { - for job := range ctx.Jobs(node.Id, xx) { - job.Fd.Close() - if filepath.Base(job.Fd.Name()) == pktName { - if err = os.Remove(job.Fd.Name()); err != nil { - log.Fatalln("Can not remove packet:", err) - } - return true + remove := func(xx nncp.TRxTx) error { + return filepath.Walk(filepath.Join(ctx.Spool, node.Id.String(), string(xx)), func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) { + return os.Remove(info.Name()) } + if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) { + return os.Remove(info.Name()) + } + if *pktRaw == "" || filepath.Base(info.Name()) == *pktRaw { + return os.Remove(info.Name()) + } + return nil + }) + } + if *pktRaw != "" || *doRx { + if err = remove(nncp.TRx); err != nil { + log.Fatalln("Can not remove:", err) } - return false } - - if !(remove(nncp.TRx) || remove(nncp.TTx)) { - log.Fatalln("Have not found specified packet") + if *pktRaw != "" || *doTx { + if err = remove(nncp.TTx); err != nil { + log.Fatalln("Can not remove:", err) + } } } -- 2.44.0