]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-rm/main.go
MTH
[nncp.git] / src / cmd / nncp-rm / main.go
index 0802f51dcebc99ac06bd96301dcb243976039d31..f8f4289639c0f685cd3a777471eec40cada25b7b 100644 (file)
@@ -29,7 +29,7 @@ import (
        "strings"
        "time"
 
-       "go.cypherpunks.ru/nncp/v5"
+       "go.cypherpunks.ru/nncp/v7"
 )
 
 func usage() {
@@ -40,6 +40,7 @@ func usage() {
        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")
@@ -51,6 +52,7 @@ 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")
@@ -67,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 {
@@ -120,10 +123,14 @@ func main() {
                                        return nil
                                }
                                if now.Sub(info.ModTime()) < oldBoundary {
-                                       ctx.LogD("nncp-rm", nncp.LEs{{K: "File", V: path}}, "too fresh, skipping")
+                                       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("nncp-rm", nncp.LEs{{K: "File", V: 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
                                }
@@ -143,7 +150,9 @@ func main() {
                                return nil
                        }
                        if strings.HasSuffix(info.Name(), ".lock") {
-                               ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: 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
                                }
@@ -174,44 +183,36 @@ func main() {
                                if info.IsDir() {
                                        return nil
                                }
+                               logMsg := func(les nncp.LEs) string {
+                                       return fmt.Sprintf("File %s: removed", path)
+                               }
                                if now.Sub(info.ModTime()) < oldBoundary {
-                                       ctx.LogD("nncp-rm", nncp.LEs{{K: "File", V: path}}, "too fresh, skipping")
+                                       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 *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
-                                       ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
-                                       if *dryRun {
-                                               return nil
-                                       }
-                                       return os.Remove(path)
-                               }
-                               if *doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix) {
-                                       ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
-                                       if *dryRun {
-                                               return nil
-                                       }
-                                       return os.Remove(path)
-                               }
-                               if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
-                                       ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: 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.LEs{{K: "File", V: path}}, "")
+                                       ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
                                        if *dryRun {
                                                return nil
                                        }
                                        return os.Remove(path)
                                }
-                               if !*doSeen &&
-                                       !*doNoCK &&
-                                       !*doPart &&
+                               if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
                                        (*doRx || *doTx) &&
                                        ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
-                                       ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
+                                       ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
                                        if *dryRun {
                                                return nil
                                        }
@@ -220,12 +221,12 @@ func main() {
                                return nil
                        })
        }
-       if *pktRaw != "" || *doRx || *doSeen || *doNoCK || *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)
                }