]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go
d93a46a01bb903c990899a97c00ccac45e4923a3
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-rm / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Remove packet from the queue
20 package main
21
22 import (
23         "flag"
24         "fmt"
25         "log"
26         "os"
27         "path/filepath"
28         "strings"
29
30         "cypherpunks.ru/nncp"
31 )
32
33 func usage() {
34         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
35         fmt.Fprintln(os.Stderr, "nncp-rm -- remove packet\n")
36         fmt.Fprintf(os.Stderr, "Usage: %s [options] -tmp\n", os.Args[0])
37         fmt.Fprintf(os.Stderr, "       %s [options] -lock\n", os.Args[0])
38         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE [-rx] [-tx] [-part] [-seen]\n", os.Args[0])
39         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -pkt PKT\n", os.Args[0])
40         fmt.Fprintln(os.Stderr, "Options:")
41         flag.PrintDefaults()
42 }
43
44 func main() {
45         var (
46                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
47                 doTmp     = flag.Bool("tmp", false, "Remove all temporary files")
48                 doLock    = flag.Bool("lock", false, "Remove all lock files")
49                 nodeRaw   = flag.String("node", "", "Node to remove files in")
50                 doRx      = flag.Bool("rx", false, "Process received packets")
51                 doTx      = flag.Bool("tx", false, "Process transfered packets")
52                 doPart    = flag.Bool("part", false, "Remove only .part files")
53                 doSeen    = flag.Bool("seen", false, "Remove only .seen files")
54                 pktRaw    = flag.String("pkt", "", "Packet to remove")
55                 spoolPath = flag.String("spool", "", "Override path to spool")
56                 quiet     = flag.Bool("quiet", false, "Print only errors")
57                 debug     = flag.Bool("debug", false, "Print debug messages")
58                 version   = flag.Bool("version", false, "Print version information")
59                 warranty  = flag.Bool("warranty", false, "Print warranty information")
60         )
61         flag.Usage = usage
62         flag.Parse()
63         if *warranty {
64                 fmt.Println(nncp.Warranty)
65                 return
66         }
67         if *version {
68                 fmt.Println(nncp.VersionGet())
69                 return
70         }
71
72         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, *debug)
73         if err != nil {
74                 log.Fatalln("Error during initialization:", err)
75         }
76
77         if *doTmp {
78                 err = filepath.Walk(filepath.Join(ctx.Spool, "tmp"), func(path string, info os.FileInfo, err error) error {
79                         if err != nil {
80                                 return err
81                         }
82                         return os.Remove(info.Name())
83                 })
84                 if err != nil {
85                         log.Fatalln("Error during walking:", err)
86                 }
87                 return
88         }
89         if *doLock {
90                 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
91                         if err != nil {
92                                 return err
93                         }
94                         if strings.HasSuffix(info.Name(), ".lock") {
95                                 return os.Remove(info.Name())
96                         }
97                         return nil
98                 })
99                 if err != nil {
100                         log.Fatalln("Error during walking:", err)
101                 }
102                 return
103         }
104         if *nodeRaw == "" {
105                 usage()
106                 os.Exit(1)
107         }
108         node, err := ctx.FindNode(*nodeRaw)
109         if err != nil {
110                 log.Fatalln("Invalid -node specified:", err)
111         }
112         remove := func(xx nncp.TRxTx) error {
113                 return filepath.Walk(filepath.Join(ctx.Spool, node.Id.String(), string(xx)), func(path string, info os.FileInfo, err error) error {
114                         if err != nil {
115                                 return err
116                         }
117                         if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
118                                 return os.Remove(info.Name())
119                         }
120                         if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
121                                 return os.Remove(info.Name())
122                         }
123                         if *pktRaw == "" || filepath.Base(info.Name()) == *pktRaw {
124                                 return os.Remove(info.Name())
125                         }
126                         return nil
127                 })
128         }
129         if *pktRaw != "" || *doRx {
130                 if err = remove(nncp.TRx); err != nil {
131                         log.Fatalln("Can not remove:", err)
132                 }
133         }
134         if *pktRaw != "" || *doTx {
135                 if err = remove(nncp.TTx); err != nil {
136                         log.Fatalln("Can not remove:", err)
137                 }
138         }
139 }