X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcmd%2Fnncp-rm%2Fmain.go;h=67f66574f1a9c5d48ced22f41c7a263c1861e306;hb=0367cce2741e1ce6a89a49fd5c4e9df6005c9744;hp=39cd0c644e5171c5e0b0af37e1721f460ec74088;hpb=d0647df298815e8b948838760ea8ac1ad00cb6c5;p=nncp.git diff --git a/src/cmd/nncp-rm/main.go b/src/cmd/nncp-rm/main.go index 39cd0c6..67f6657 100644 --- a/src/cmd/nncp-rm/main.go +++ b/src/cmd/nncp-rm/main.go @@ -1,6 +1,6 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2021 Sergey Matveev +Copyright (C) 2016-2022 Sergey Matveev 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 @@ -29,7 +29,7 @@ import ( "strings" "time" - "go.cypherpunks.ru/nncp/v7" + "go.cypherpunks.ru/nncp/v8" ) func usage() { @@ -54,15 +54,15 @@ func main() { cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file") doAll = flag.Bool("all", false, "Apply remove rules to all nodes") doTmp = flag.Bool("tmp", false, "Remove all temporary files") - doHdr = flag.Bool("hdr", false, "Remove all .hdr 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") + doSeen = flag.Bool("seen", false, "Remove only seen/ files") doNoCK = flag.Bool("nock", false, "Remove only .nock files") - doArea = flag.Bool("area", false, "Remove only area/*.seen files") + doArea = flag.Bool("area", false, "Remove only area/* 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") @@ -186,8 +186,11 @@ func main() { continue } remove := func(xx nncp.TRxTx) error { - return filepath.Walk( - filepath.Join(ctx.Spool, node.Id.String(), string(xx)), + p := filepath.Join(ctx.Spool, node.Id.String(), string(xx)) + if _, err := os.Stat(p); err != nil && os.IsNotExist(err) { + return nil + } + return filepath.Walk(p, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -204,9 +207,7 @@ func main() { }) return nil } - if (*doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix)) || - (*doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix)) || - (*doHdr && strings.HasSuffix(info.Name(), nncp.HdrSuffix)) || + if (*doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix)) || (*doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix)) { ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) if *dryRun { @@ -233,7 +234,7 @@ func main() { return nil }) } - if *pktRaw != "" || *doRx || *doSeen || *doNoCK || *doHdr || *doPart { + if *pktRaw != "" || *doRx || *doNoCK || *doPart { if err = remove(nncp.TRx); err != nil { log.Fatalln("Can not remove:", err) } @@ -243,6 +244,63 @@ func main() { log.Fatalln("Can not remove:", err) } } + removeSub := func(p string, everything bool) error { + return filepath.Walk( + p, func(path string, info os.FileInfo, err error) error { + if err != nil { + if os.IsNotExist(err) { + return nil + } + return err + } + if info.IsDir() { + return nil + } + logMsg := func(les nncp.LEs) string { + return fmt.Sprintf("File %s: removed", path) + } + if everything { + ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg) + if *dryRun { + return nil + } + return os.Remove(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) + }, + ) + } else if !*dryRun { + return os.Remove(path) + } + return nil + }, + ) + } + if *doRx || *doSeen { + if err = removeSub(filepath.Join( + ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir, + ), *doSeen); err != nil { + log.Fatalln("Can not remove:", err) + } + } + if *doRx || *doHdr { + if err = removeSub(filepath.Join( + ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir, + ), *doHdr); err != nil { + log.Fatalln("Can not remove:", err) + } + } + if *doTx || *doHdr { + if err = removeSub(filepath.Join( + ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir, + ), *doHdr); err != nil { + log.Fatalln("Can not remove:", err) + } + } if *doArea { if err = filepath.Walk( filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir), @@ -262,20 +320,17 @@ func main() { }) return nil } - if strings.HasSuffix(info.Name(), nncp.SeenSuffix) { - 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) + 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 nil + return os.Remove(path) }); err != nil { log.Fatalln("Can not remove:", err) }