From: Sergey Matveev Date: Wed, 6 Jan 2021 18:50:42 +0000 (+0300) Subject: nncp-rm's -dryrun and -older options X-Git-Tag: v5.5.0^2~7 X-Git-Url: http://www.git.cypherpunks.ru/?a=commitdiff_plain;h=8b7e18bdaf59d075158056c42daa072ad566b32e;p=nncp.git nncp-rm's -dryrun and -older options --- diff --git a/doc/cmds.texi b/doc/cmds.texi index 9fff9e4..abaa60a 100644 --- a/doc/cmds.texi +++ b/doc/cmds.texi @@ -483,19 +483,30 @@ $ nncp-rm [options] -node NODE -pkt PKT 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. + +@item @option{-dryrun} option just prints what will be deleted. + +@item You can also select files that only have modification date older +than specified @option{-older} time units (@code{10s} (10 seconds), +@code{5m} (5 minutes), @code{12h} (12 hours), @code{2d} (2 days)). + @end itemize @node nncp-stat diff --git a/doc/news.ru.texi b/doc/news.ru.texi index 6f9c98d..70bbe2f 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -1,6 +1,22 @@ @node Новости @section Новости +@node Релиз 5.5.0 +@subsection Релиз 5.5.0 +@itemize + +@item +Исправления ошибок в @command{nncp-call(er)}/@command{nncp-daemon} и +@command{nncp-bundle} командах. + +@item +У команды @command{nncp-rm} появились @option{-dryrun} и @option{-older} опции. + +@item +Обновлены зависимые библиотеки. + +@end itemize + @node Релиз 5.4.1 @subsection Релиз 5.4.1 @itemize diff --git a/doc/news.texi b/doc/news.texi index 37edcc0..b7c4e53 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -3,6 +3,22 @@ See also this page @ref{Новости, on russian}. +@node Release 5.5.0 +@section Release 5.5.0 +@itemize + +@item +Bugfixes in @command{nncp-call(er)}/@command{nncp-daemon} and +@command{nncp-bundle}. + +@item +@command{nncp-rm} has @option{-dryrun} and @option{-older} options now. + +@item +Updated dependencies. + +@end itemize + @node Release 5.4.1 @section Release 5.4.1 @itemize diff --git a/src/cmd/nncp-rm/main.go b/src/cmd/nncp-rm/main.go index 1581560..7afb24b 100644 --- a/src/cmd/nncp-rm/main.go +++ b/src/cmd/nncp-rm/main.go @@ -24,7 +24,10 @@ import ( "log" "os" "path/filepath" + "regexp" + "strconv" "strings" + "time" "go.cypherpunks.ru/nncp/v5" ) @@ -38,6 +41,7 @@ func usage() { fmt.Fprintf(os.Stderr, " %s [options] -node NODE -seen\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s [options] -node NODE {-rx|-tx}\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s [options] -node NODE -pkt PKT\n", os.Args[0]) + fmt.Fprintln(os.Stderr, "-older option's time units are: (s)econds, (m)inutes, (h)ours, (d)ays") fmt.Fprintln(os.Stderr, "Options:") flag.PrintDefaults() } @@ -52,6 +56,8 @@ func main() { 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") + older = flag.String("older", "", "XXX{smhd}: only older than XXX number of time units") + dryRun = flag.Bool("dryrun", false, "Do not actually remove files") pktRaw = flag.String("pkt", "", "Packet to remove") spoolPath = flag.String("spool", "", "Override path to spool") quiet = flag.Bool("quiet", false, "Print only errors") @@ -76,6 +82,31 @@ func main() { } ctx.Umask() + var oldBoundaryRaw int + if *older != "" { + olderRe := regexp.MustCompile(`^(\d+)([smhd])$`) + matches := olderRe.FindStringSubmatch(*older) + if len(matches) != 1+2 { + log.Fatalln("can not parse -older") + } + oldBoundaryRaw, err = strconv.Atoi(matches[1]) + if err != nil { + log.Fatalln("can not parse -older:", err) + } + switch matches[2] { + case "s": + break + case "m": + oldBoundaryRaw *= 60 + case "h": + oldBoundaryRaw *= 60 * 60 + case "d": + oldBoundaryRaw *= 60 * 60 * 24 + } + } + oldBoundary := time.Second * time.Duration(oldBoundaryRaw) + + now := time.Now() if *doTmp { err = filepath.Walk( filepath.Join(ctx.Spool, "tmp"), @@ -86,7 +117,14 @@ func main() { if info.IsDir() { return nil } + if now.Sub(info.ModTime()) < oldBoundary { + ctx.LogD("nncp-rm", nncp.SDS{"file": path}, "too fresh, skipping") + return nil + } ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "") + if *dryRun { + return nil + } return os.Remove(path) }) if err != nil { @@ -104,6 +142,9 @@ func main() { } if strings.HasSuffix(info.Name(), ".lock") { ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "") + if *dryRun { + return nil + } return os.Remove(path) } return nil @@ -131,16 +172,29 @@ func main() { if info.IsDir() { return nil } + if now.Sub(info.ModTime()) < oldBoundary { + ctx.LogD("nncp-rm", nncp.SDS{"file": path}, "too fresh, skipping") + return nil + } if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) { ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "") + if *dryRun { + return nil + } return os.Remove(path) } if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) { ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "") + if *dryRun { + return nil + } return os.Remove(path) } if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw { ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "") + if *dryRun { + return nil + } return os.Remove(path) } if !*doSeen && @@ -148,6 +202,9 @@ func main() { (*doRx || *doTx) && ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) { ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "") + if *dryRun { + return nil + } return os.Remove(path) } return nil