]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-rm/main.go
MTH
[nncp.git] / src / cmd / nncp-rm / main.go
index 911db6370b35ffc0d84ae2deeed65ea889f67562..f8f4289639c0f685cd3a777471eec40cada25b7b 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 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
@@ -24,9 +24,12 @@ import (
        "log"
        "os"
        "path/filepath"
+       "regexp"
+       "strconv"
        "strings"
+       "time"
 
-       "go.cypherpunks.ru/nncp/v5"
+       "go.cypherpunks.ru/nncp/v7"
 )
 
 func usage() {
@@ -36,8 +39,11 @@ func usage() {
        fmt.Fprintf(os.Stderr, "       %s [options] -lock\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -part\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -seen\n", os.Args[0])
+       fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -nock\n", os.Args[0])
+       fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -hdr\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()
 }
@@ -46,12 +52,16 @@ func main() {
        var (
                cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
                doTmp     = flag.Bool("tmp", false, "Remove all temporary files")
+               doHdr     = flag.Bool("hdr", false, "Remove all .hdr 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")
+               doNoCK    = flag.Bool("nock", false, "Remove only .nock 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")
@@ -59,6 +69,7 @@ func main() {
                version   = flag.Bool("version", false, "Print version information")
                warranty  = flag.Bool("warranty", false, "Print warranty information")
        )
+       log.SetFlags(log.Lshortfile)
        flag.Usage = usage
        flag.Parse()
        if *warranty {
@@ -76,6 +87,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 +122,18 @@ func main() {
                                if info.IsDir() {
                                        return nil
                                }
-                               ctx.LogI("nncp-rm", nncp.SDS{"file": 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}}, func(les nncp.LEs) string {
+                                       return fmt.Sprintf("File %s: removed", path)
+                               })
+                               if *dryRun {
+                                       return nil
+                               }
                                return os.Remove(path)
                        })
                if err != nil {
@@ -103,7 +150,12 @@ func main() {
                                return nil
                        }
                        if strings.HasSuffix(info.Name(), ".lock") {
-                               ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                               ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
+                                       return fmt.Sprintf("File %s: removed", path)
+                               })
+                               if *dryRun {
+                                       return nil
+                               }
                                return os.Remove(path)
                        }
                        return nil
@@ -131,34 +183,50 @@ func main() {
                                if info.IsDir() {
                                        return nil
                                }
-                               if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
-                                       ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
-                                       return os.Remove(path)
+                               logMsg := func(les nncp.LEs) string {
+                                       return fmt.Sprintf("File %s: removed", 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
                                }
-                               if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
-                                       ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                               if (*doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix)) ||
+                                       (*doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix)) ||
+                                       (*doHdr && strings.HasSuffix(info.Name(), nncp.HdrSuffix)) ||
+                                       (*doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix)) {
+                                       ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
+                                       if *dryRun {
+                                               return nil
+                                       }
                                        return os.Remove(path)
                                }
                                if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
-                                       ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                                       ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
+                                       if *dryRun {
+                                               return nil
+                                       }
                                        return os.Remove(path)
                                }
-                               if !*doSeen &&
-                                       !*doPart &&
+                               if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
                                        (*doRx || *doTx) &&
                                        ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
-                                       ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                                       ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
+                                       if *dryRun {
+                                               return nil
+                                       }
                                        return os.Remove(path)
                                }
                                return nil
                        })
        }
-       if *pktRaw != "" || *doRx || *doSeen || *doPart {
+       if *pktRaw != "" || *doRx || *doSeen || *doNoCK || *doHdr || *doPart {
                if err = remove(nncp.TRx); err != nil {
                        log.Fatalln("Can not remove:", err)
                }
        }
-       if *pktRaw != "" || *doTx {
+       if *pktRaw != "" || *doTx || *doHdr {
                if err = remove(nncp.TTx); err != nil {
                        log.Fatalln("Can not remove:", err)
                }