]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-rm/main.go
MTH
[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/v7"
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         log.SetFlags(log.Lshortfile)
73         flag.Usage = usage
74         flag.Parse()
75         if *warranty {
76                 fmt.Println(nncp.Warranty)
77                 return
78         }
79         if *version {
80                 fmt.Println(nncp.VersionGet())
81                 return
82         }
83
84         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
85         if err != nil {
86                 log.Fatalln("Error during initialization:", err)
87         }
88         ctx.Umask()
89
90         var oldBoundaryRaw int
91         if *older != "" {
92                 olderRe := regexp.MustCompile(`^(\d+)([smhd])$`)
93                 matches := olderRe.FindStringSubmatch(*older)
94                 if len(matches) != 1+2 {
95                         log.Fatalln("can not parse -older")
96                 }
97                 oldBoundaryRaw, err = strconv.Atoi(matches[1])
98                 if err != nil {
99                         log.Fatalln("can not parse -older:", err)
100                 }
101                 switch matches[2] {
102                 case "s":
103                         break
104                 case "m":
105                         oldBoundaryRaw *= 60
106                 case "h":
107                         oldBoundaryRaw *= 60 * 60
108                 case "d":
109                         oldBoundaryRaw *= 60 * 60 * 24
110                 }
111         }
112         oldBoundary := time.Second * time.Duration(oldBoundaryRaw)
113
114         now := time.Now()
115         if *doTmp {
116                 err = filepath.Walk(
117                         filepath.Join(ctx.Spool, "tmp"),
118                         func(path string, info os.FileInfo, err error) error {
119                                 if err != nil {
120                                         return err
121                                 }
122                                 if info.IsDir() {
123                                         return nil
124                                 }
125                                 if now.Sub(info.ModTime()) < oldBoundary {
126                                         ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
127                                                 return fmt.Sprintf("File %s: too fresh, skipping", path)
128                                         })
129                                         return nil
130                                 }
131                                 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
132                                         return fmt.Sprintf("File %s: removed", path)
133                                 })
134                                 if *dryRun {
135                                         return nil
136                                 }
137                                 return os.Remove(path)
138                         })
139                 if err != nil {
140                         log.Fatalln("Error during walking:", err)
141                 }
142                 return
143         }
144         if *doLock {
145                 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
146                         if err != nil {
147                                 return err
148                         }
149                         if info.IsDir() {
150                                 return nil
151                         }
152                         if strings.HasSuffix(info.Name(), ".lock") {
153                                 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
154                                         return fmt.Sprintf("File %s: removed", path)
155                                 })
156                                 if *dryRun {
157                                         return nil
158                                 }
159                                 return os.Remove(path)
160                         }
161                         return nil
162                 })
163                 if err != nil {
164                         log.Fatalln("Error during walking:", err)
165                 }
166                 return
167         }
168         if *nodeRaw == "" {
169                 usage()
170                 os.Exit(1)
171         }
172         node, err := ctx.FindNode(*nodeRaw)
173         if err != nil {
174                 log.Fatalln("Invalid -node specified:", err)
175         }
176         remove := func(xx nncp.TRxTx) error {
177                 return filepath.Walk(
178                         filepath.Join(ctx.Spool, node.Id.String(), string(xx)),
179                         func(path string, info os.FileInfo, err error) error {
180                                 if err != nil {
181                                         return err
182                                 }
183                                 if info.IsDir() {
184                                         return nil
185                                 }
186                                 logMsg := func(les nncp.LEs) string {
187                                         return fmt.Sprintf("File %s: removed", path)
188                                 }
189                                 if now.Sub(info.ModTime()) < oldBoundary {
190                                         ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
191                                                 return fmt.Sprintf("File %s: too fresh, skipping", path)
192                                         })
193                                         return nil
194                                 }
195                                 if (*doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix)) ||
196                                         (*doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix)) ||
197                                         (*doHdr && strings.HasSuffix(info.Name(), nncp.HdrSuffix)) ||
198                                         (*doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix)) {
199                                         ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
200                                         if *dryRun {
201                                                 return nil
202                                         }
203                                         return os.Remove(path)
204                                 }
205                                 if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
206                                         ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
207                                         if *dryRun {
208                                                 return nil
209                                         }
210                                         return os.Remove(path)
211                                 }
212                                 if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
213                                         (*doRx || *doTx) &&
214                                         ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
215                                         ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
216                                         if *dryRun {
217                                                 return nil
218                                         }
219                                         return os.Remove(path)
220                                 }
221                                 return nil
222                         })
223         }
224         if *pktRaw != "" || *doRx || *doSeen || *doNoCK || *doHdr || *doPart {
225                 if err = remove(nncp.TRx); err != nil {
226                         log.Fatalln("Can not remove:", err)
227                 }
228         }
229         if *pktRaw != "" || *doTx || *doHdr {
230                 if err = remove(nncp.TTx); err != nil {
231                         log.Fatalln("Can not remove:", err)
232                 }
233         }
234 }