]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-rm/main.go
Merge branch 'develop'
[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 {-rx|-tx}\n", os.Args[0])
43         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -pkt PKT\n", os.Args[0])
44         fmt.Fprintln(os.Stderr, "-older option's time units are: (s)econds, (m)inutes, (h)ours, (d)ays")
45         fmt.Fprintln(os.Stderr, "Options:")
46         flag.PrintDefaults()
47 }
48
49 func main() {
50         var (
51                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
52                 doTmp     = flag.Bool("tmp", false, "Remove all temporary files")
53                 doLock    = flag.Bool("lock", false, "Remove all lock files")
54                 nodeRaw   = flag.String("node", "", "Node to remove files in")
55                 doRx      = flag.Bool("rx", false, "Process received packets")
56                 doTx      = flag.Bool("tx", false, "Process transfered packets")
57                 doPart    = flag.Bool("part", false, "Remove only .part files")
58                 doSeen    = flag.Bool("seen", false, "Remove only .seen files")
59                 older     = flag.String("older", "", "XXX{smhd}: only older than XXX number of time units")
60                 dryRun    = flag.Bool("dryrun", false, "Do not actually remove files")
61                 pktRaw    = flag.String("pkt", "", "Packet to remove")
62                 spoolPath = flag.String("spool", "", "Override path to spool")
63                 quiet     = flag.Bool("quiet", false, "Print only errors")
64                 debug     = flag.Bool("debug", false, "Print debug messages")
65                 version   = flag.Bool("version", false, "Print version information")
66                 warranty  = flag.Bool("warranty", false, "Print warranty information")
67         )
68         flag.Usage = usage
69         flag.Parse()
70         if *warranty {
71                 fmt.Println(nncp.Warranty)
72                 return
73         }
74         if *version {
75                 fmt.Println(nncp.VersionGet())
76                 return
77         }
78
79         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
80         if err != nil {
81                 log.Fatalln("Error during initialization:", err)
82         }
83         ctx.Umask()
84
85         var oldBoundaryRaw int
86         if *older != "" {
87                 olderRe := regexp.MustCompile(`^(\d+)([smhd])$`)
88                 matches := olderRe.FindStringSubmatch(*older)
89                 if len(matches) != 1+2 {
90                         log.Fatalln("can not parse -older")
91                 }
92                 oldBoundaryRaw, err = strconv.Atoi(matches[1])
93                 if err != nil {
94                         log.Fatalln("can not parse -older:", err)
95                 }
96                 switch matches[2] {
97                 case "s":
98                         break
99                 case "m":
100                         oldBoundaryRaw *= 60
101                 case "h":
102                         oldBoundaryRaw *= 60 * 60
103                 case "d":
104                         oldBoundaryRaw *= 60 * 60 * 24
105                 }
106         }
107         oldBoundary := time.Second * time.Duration(oldBoundaryRaw)
108
109         now := time.Now()
110         if *doTmp {
111                 err = filepath.Walk(
112                         filepath.Join(ctx.Spool, "tmp"),
113                         func(path string, info os.FileInfo, err error) error {
114                                 if err != nil {
115                                         return err
116                                 }
117                                 if info.IsDir() {
118                                         return nil
119                                 }
120                                 if now.Sub(info.ModTime()) < oldBoundary {
121                                         ctx.LogD("nncp-rm", nncp.SDS{"file": path}, "too fresh, skipping")
122                                         return nil
123                                 }
124                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
125                                 if *dryRun {
126                                         return nil
127                                 }
128                                 return os.Remove(path)
129                         })
130                 if err != nil {
131                         log.Fatalln("Error during walking:", err)
132                 }
133                 return
134         }
135         if *doLock {
136                 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
137                         if err != nil {
138                                 return err
139                         }
140                         if info.IsDir() {
141                                 return nil
142                         }
143                         if strings.HasSuffix(info.Name(), ".lock") {
144                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
145                                 if *dryRun {
146                                         return nil
147                                 }
148                                 return os.Remove(path)
149                         }
150                         return nil
151                 })
152                 if err != nil {
153                         log.Fatalln("Error during walking:", err)
154                 }
155                 return
156         }
157         if *nodeRaw == "" {
158                 usage()
159                 os.Exit(1)
160         }
161         node, err := ctx.FindNode(*nodeRaw)
162         if err != nil {
163                 log.Fatalln("Invalid -node specified:", err)
164         }
165         remove := func(xx nncp.TRxTx) error {
166                 return filepath.Walk(
167                         filepath.Join(ctx.Spool, node.Id.String(), string(xx)),
168                         func(path string, info os.FileInfo, err error) error {
169                                 if err != nil {
170                                         return err
171                                 }
172                                 if info.IsDir() {
173                                         return nil
174                                 }
175                                 if now.Sub(info.ModTime()) < oldBoundary {
176                                         ctx.LogD("nncp-rm", nncp.SDS{"file": path}, "too fresh, skipping")
177                                         return nil
178                                 }
179                                 if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
180                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
181                                         if *dryRun {
182                                                 return nil
183                                         }
184                                         return os.Remove(path)
185                                 }
186                                 if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
187                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
188                                         if *dryRun {
189                                                 return nil
190                                         }
191                                         return os.Remove(path)
192                                 }
193                                 if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
194                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
195                                         if *dryRun {
196                                                 return nil
197                                         }
198                                         return os.Remove(path)
199                                 }
200                                 if !*doSeen &&
201                                         !*doPart &&
202                                         (*doRx || *doTx) &&
203                                         ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
204                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
205                                         if *dryRun {
206                                                 return nil
207                                         }
208                                         return os.Remove(path)
209                                 }
210                                 return nil
211                         })
212         }
213         if *pktRaw != "" || *doRx || *doSeen || *doPart {
214                 if err = remove(nncp.TRx); err != nil {
215                         log.Fatalln("Can not remove:", err)
216                 }
217         }
218         if *pktRaw != "" || *doTx {
219                 if err = remove(nncp.TTx); err != nil {
220                         log.Fatalln("Can not remove:", err)
221                 }
222         }
223 }