]> Cypherpunks.ru repositories - nncp.git/commitdiff
nncp-rm's -dryrun and -older options
authorSergey Matveev <stargrave@stargrave.org>
Wed, 6 Jan 2021 18:50:42 +0000 (21:50 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 6 Jan 2021 18:50:42 +0000 (21:50 +0300)
doc/cmds.texi
doc/news.ru.texi
doc/news.texi
src/cmd/nncp-rm/main.go

index 9fff9e4679f77d1bc951d619b18fe0e8705ff6e3..abaa60afe89aea83934a1808a1c82ab3cad314dc 100644 (file)
@@ -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
index 6f9c98dea3bfb3b4878d05715238ec4687689d01..70bbe2f404509e79e6550f061576b650d831ac49 100644 (file)
@@ -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
index 37edcc0442f0669246fe4730644871211cbd67cf..b7c4e538c5687e92cc959d767dad5a3daafb08ca 100644 (file)
@@ -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
index 1581560f104441c3e3ba20865b35e0025d9ef1b9..7afb24be8e0551bab1133a0a26dda6a3f9b75452 100644 (file)
@@ -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