]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-rm/main.go
Generate ACKs during tossing
[nncp.git] / src / cmd / nncp-rm / main.go
index 9589792daf6e0e58885790f19c4a29c4da42384c..18931ebbcbe3fd3f9ad0196fcbd561e538eb38e1 100644 (file)
@@ -19,9 +19,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "errors"
        "flag"
        "fmt"
        "io"
+       "io/fs"
        "log"
        "os"
        "path/filepath"
@@ -34,8 +36,7 @@ import (
 )
 
 func usage() {
-       fmt.Fprintf(os.Stderr, nncp.UsageHeader())
-       fmt.Fprintf(os.Stderr, "nncp-rm -- remove packet\n\n")
+       fmt.Fprint(os.Stderr, "nncp-rm -- remove packet\n\n")
        fmt.Fprintf(os.Stderr, "Usage: %s [options] [-older X] -tmp\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] -lock\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] [-older X] {-all|-node NODE} -part\n", os.Args[0])
@@ -43,7 +44,8 @@ func usage() {
        fmt.Fprintf(os.Stderr, "       %s [options] [-older X] {-all|-node NODE} -nock\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] [-older X] {-all|-node NODE} -area\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] [-older X] {-all|-node NODE} {-rx|-tx} [-hdr]\n", os.Args[0])
-       fmt.Fprintf(os.Stderr, "       %s [options] [-older X] {-all|-node NODE} -pkt < ...\n", os.Args[0])
+       fmt.Fprintf(os.Stderr, "       %s [options] {-all|-node NODE} -pkt < ...\n", os.Args[0])
+       fmt.Fprintf(os.Stderr, "       %s [options] {-all|-node NODE} -ack\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()
@@ -66,6 +68,7 @@ func main() {
                older     = flag.String("older", "", "XXX{smhd}: only older than XXX number of time units")
                dryRun    = flag.Bool("dryrun", false, "Do not actually remove files")
                doPkt     = flag.Bool("pkt", false, "Remove only that packets")
+               doACK     = flag.Bool("ack", false, "Remove ACK packets from outbound")
                spoolPath = flag.String("spool", "", "Override path to spool")
                quiet     = flag.Bool("quiet", false, "Print only errors")
                debug     = flag.Bool("debug", false, "Print debug messages")
@@ -205,12 +208,12 @@ func main() {
                }
                remove := func(xx nncp.TRxTx) error {
                        p := filepath.Join(ctx.Spool, node.Id.String(), string(xx))
-                       if _, err := os.Stat(p); err != nil && os.IsNotExist(err) {
+                       if _, err := os.Stat(p); err != nil && errors.Is(err, fs.ErrNotExist) {
                                return nil
                        }
                        dir, err := os.Open(p)
                        if err != nil {
-                               if os.IsNotExist(err) {
+                               if errors.Is(err, fs.ErrNotExist) {
                                        return nil
                                }
                                return err
@@ -249,9 +252,11 @@ func main() {
                                                return err
                                        }
                                        if now.Sub(info.ModTime()) < oldBoundary {
-                                               ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: pth}}, func(les nncp.LEs) string {
-                                                       return fmt.Sprintf("File %s: too fresh, skipping", pth)
-                                               })
+                                               ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: pth}},
+                                                       func(les nncp.LEs) string {
+                                                               return fmt.Sprintf("File %s: too fresh, skipping", pth)
+                                                       },
+                                               )
                                                continue
                                        }
                                        if (*doNoCK && strings.HasSuffix(entry.Name(), nncp.NoCKSuffix)) ||
@@ -291,9 +296,11 @@ func main() {
                        }
                }
                removeSub := func(p string) error {
-                       return filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
+                       return filepath.Walk(p, func(
+                               path string, info os.FileInfo, err error,
+                       ) error {
                                if err != nil {
-                                       if os.IsNotExist(err) {
+                                       if errors.Is(err, fs.ErrNotExist) {
                                                return nil
                                        }
                                        return err
@@ -356,7 +363,7 @@ func main() {
                                filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir),
                                func(path string, info os.FileInfo, err error) error {
                                        if err != nil {
-                                               if os.IsNotExist(err) {
+                                               if errors.Is(err, fs.ErrNotExist) {
                                                        return nil
                                                }
                                                return err
@@ -388,5 +395,46 @@ func main() {
                                log.Fatalln("Can not remove:", err)
                        }
                }
+               if *doACK {
+                       dirPath := filepath.Join(
+                               ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.ACKDir)
+                       dir, err := os.Open(dirPath)
+                       if err != nil {
+                               continue
+                       }
+                       for {
+                               fis, err := dir.ReadDir(1 << 10)
+                               if err != nil {
+                                       if err == io.EOF {
+                                               break
+                                       }
+                                       log.Fatalln("Can not read directory:", err)
+                               }
+                               for _, fi := range fis {
+                                       for _, pth := range []string{
+                                               filepath.Join(
+                                                       ctx.Spool,
+                                                       node.Id.String(),
+                                                       string(nncp.TTx),
+                                                       fi.Name(),
+                                               ),
+                                               filepath.Join(dirPath, fi.Name()),
+                                       } {
+                                               if err = os.Remove(pth); err == nil {
+                                                       ctx.LogI(
+                                                               "rm",
+                                                               nncp.LEs{{K: "File", V: pth}},
+                                                               func(les nncp.LEs) string {
+                                                                       return fmt.Sprintf("File %s: removed", pth)
+                                                               },
+                                                       )
+                                               } else if !errors.Is(err, fs.ErrNotExist) {
+                                                       log.Fatalln("Can not remove:", pth, ":", err)
+                                               }
+                                       }
+                               }
+                       }
+                       dir.Close()
+               }
        }
 }