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