]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-rm/main.go
Intermediate .nock packets step
[nncp.git] / src / cmd / nncp-rm / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Remove packet from the queue.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "os"
26         "path/filepath"
27         "regexp"
28         "strconv"
29         "strings"
30         "time"
31
32         "go.cypherpunks.ru/nncp/v5"
33 )
34
35 func usage() {
36         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
37         fmt.Fprintf(os.Stderr, "nncp-rm -- remove packet\n\n")
38         fmt.Fprintf(os.Stderr, "Usage: %s [options] -tmp\n", os.Args[0])
39         fmt.Fprintf(os.Stderr, "       %s [options] -lock\n", os.Args[0])
40         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -part\n", os.Args[0])
41         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -seen\n", os.Args[0])
42         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -nock\n", os.Args[0])
43         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE {-rx|-tx}\n", os.Args[0])
44         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -pkt PKT\n", os.Args[0])
45         fmt.Fprintln(os.Stderr, "-older option's time units are: (s)econds, (m)inutes, (h)ours, (d)ays")
46         fmt.Fprintln(os.Stderr, "Options:")
47         flag.PrintDefaults()
48 }
49
50 func main() {
51         var (
52                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
53                 doTmp     = flag.Bool("tmp", false, "Remove all temporary files")
54                 doLock    = flag.Bool("lock", false, "Remove all lock files")
55                 nodeRaw   = flag.String("node", "", "Node to remove files in")
56                 doRx      = flag.Bool("rx", false, "Process received packets")
57                 doTx      = flag.Bool("tx", false, "Process transfered packets")
58                 doPart    = flag.Bool("part", false, "Remove only .part files")
59                 doSeen    = flag.Bool("seen", false, "Remove only .seen files")
60                 doNoCK    = flag.Bool("nock", false, "Remove only .nock files")
61                 older     = flag.String("older", "", "XXX{smhd}: only older than XXX number of time units")
62                 dryRun    = flag.Bool("dryrun", false, "Do not actually remove files")
63                 pktRaw    = flag.String("pkt", "", "Packet to remove")
64                 spoolPath = flag.String("spool", "", "Override path to spool")
65                 quiet     = flag.Bool("quiet", false, "Print only errors")
66                 debug     = flag.Bool("debug", false, "Print debug messages")
67                 version   = flag.Bool("version", false, "Print version information")
68                 warranty  = flag.Bool("warranty", false, "Print warranty information")
69         )
70         flag.Usage = usage
71         flag.Parse()
72         if *warranty {
73                 fmt.Println(nncp.Warranty)
74                 return
75         }
76         if *version {
77                 fmt.Println(nncp.VersionGet())
78                 return
79         }
80
81         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
82         if err != nil {
83                 log.Fatalln("Error during initialization:", err)
84         }
85         ctx.Umask()
86
87         var oldBoundaryRaw int
88         if *older != "" {
89                 olderRe := regexp.MustCompile(`^(\d+)([smhd])$`)
90                 matches := olderRe.FindStringSubmatch(*older)
91                 if len(matches) != 1+2 {
92                         log.Fatalln("can not parse -older")
93                 }
94                 oldBoundaryRaw, err = strconv.Atoi(matches[1])
95                 if err != nil {
96                         log.Fatalln("can not parse -older:", err)
97                 }
98                 switch matches[2] {
99                 case "s":
100                         break
101                 case "m":
102                         oldBoundaryRaw *= 60
103                 case "h":
104                         oldBoundaryRaw *= 60 * 60
105                 case "d":
106                         oldBoundaryRaw *= 60 * 60 * 24
107                 }
108         }
109         oldBoundary := time.Second * time.Duration(oldBoundaryRaw)
110
111         now := time.Now()
112         if *doTmp {
113                 err = filepath.Walk(
114                         filepath.Join(ctx.Spool, "tmp"),
115                         func(path string, info os.FileInfo, err error) error {
116                                 if err != nil {
117                                         return err
118                                 }
119                                 if info.IsDir() {
120                                         return nil
121                                 }
122                                 if now.Sub(info.ModTime()) < oldBoundary {
123                                         ctx.LogD("nncp-rm", nncp.LEs{{K: "File", V: path}}, "too fresh, skipping")
124                                         return nil
125                                 }
126                                 ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
127                                 if *dryRun {
128                                         return nil
129                                 }
130                                 return os.Remove(path)
131                         })
132                 if err != nil {
133                         log.Fatalln("Error during walking:", err)
134                 }
135                 return
136         }
137         if *doLock {
138                 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
139                         if err != nil {
140                                 return err
141                         }
142                         if info.IsDir() {
143                                 return nil
144                         }
145                         if strings.HasSuffix(info.Name(), ".lock") {
146                                 ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
147                                 if *dryRun {
148                                         return nil
149                                 }
150                                 return os.Remove(path)
151                         }
152                         return nil
153                 })
154                 if err != nil {
155                         log.Fatalln("Error during walking:", err)
156                 }
157                 return
158         }
159         if *nodeRaw == "" {
160                 usage()
161                 os.Exit(1)
162         }
163         node, err := ctx.FindNode(*nodeRaw)
164         if err != nil {
165                 log.Fatalln("Invalid -node specified:", err)
166         }
167         remove := func(xx nncp.TRxTx) error {
168                 return filepath.Walk(
169                         filepath.Join(ctx.Spool, node.Id.String(), string(xx)),
170                         func(path string, info os.FileInfo, err error) error {
171                                 if err != nil {
172                                         return err
173                                 }
174                                 if info.IsDir() {
175                                         return nil
176                                 }
177                                 if now.Sub(info.ModTime()) < oldBoundary {
178                                         ctx.LogD("nncp-rm", nncp.LEs{{K: "File", V: path}}, "too fresh, skipping")
179                                         return nil
180                                 }
181                                 if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
182                                         ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
183                                         if *dryRun {
184                                                 return nil
185                                         }
186                                         return os.Remove(path)
187                                 }
188                                 if *doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix) {
189                                         ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
190                                         if *dryRun {
191                                                 return nil
192                                         }
193                                         return os.Remove(path)
194                                 }
195                                 if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
196                                         ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
197                                         if *dryRun {
198                                                 return nil
199                                         }
200                                         return os.Remove(path)
201                                 }
202                                 if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
203                                         ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
204                                         if *dryRun {
205                                                 return nil
206                                         }
207                                         return os.Remove(path)
208                                 }
209                                 if !*doSeen &&
210                                         !*doNoCK &&
211                                         !*doPart &&
212                                         (*doRx || *doTx) &&
213                                         ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
214                                         ctx.LogI("nncp-rm", nncp.LEs{{K: "File", V: path}}, "")
215                                         if *dryRun {
216                                                 return nil
217                                         }
218                                         return os.Remove(path)
219                                 }
220                                 return nil
221                         })
222         }
223         if *pktRaw != "" || *doRx || *doSeen || *doNoCK || *doPart {
224                 if err = remove(nncp.TRx); err != nil {
225                         log.Fatalln("Can not remove:", err)
226                 }
227         }
228         if *pktRaw != "" || *doTx {
229                 if err = remove(nncp.TTx); err != nil {
230                         log.Fatalln("Can not remove:", err)
231                 }
232         }
233 }