]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-rm/main.go
Operations progress
[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-2019 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         "strings"
28
29         "go.cypherpunks.ru/nncp/v5"
30 )
31
32 func usage() {
33         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
34         fmt.Fprintf(os.Stderr, "nncp-rm -- remove packet\n\n")
35         fmt.Fprintf(os.Stderr, "Usage: %s [options] -tmp\n", os.Args[0])
36         fmt.Fprintf(os.Stderr, "       %s [options] -lock\n", os.Args[0])
37         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -part\n", os.Args[0])
38         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -seen\n", os.Args[0])
39         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE {-rx|-tx}\n", os.Args[0])
40         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -pkt PKT\n", os.Args[0])
41         fmt.Fprintln(os.Stderr, "Options:")
42         flag.PrintDefaults()
43 }
44
45 func main() {
46         var (
47                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
48                 doTmp     = flag.Bool("tmp", false, "Remove all temporary files")
49                 doLock    = flag.Bool("lock", false, "Remove all lock files")
50                 nodeRaw   = flag.String("node", "", "Node to remove files in")
51                 doRx      = flag.Bool("rx", false, "Process received packets")
52                 doTx      = flag.Bool("tx", false, "Process transfered packets")
53                 doPart    = flag.Bool("part", false, "Remove only .part files")
54                 doSeen    = flag.Bool("seen", false, "Remove only .seen files")
55                 pktRaw    = flag.String("pkt", "", "Packet to remove")
56                 spoolPath = flag.String("spool", "", "Override path to spool")
57                 quiet     = flag.Bool("quiet", false, "Print only errors")
58                 debug     = flag.Bool("debug", false, "Print debug messages")
59                 version   = flag.Bool("version", false, "Print version information")
60                 warranty  = flag.Bool("warranty", false, "Print warranty information")
61         )
62         flag.Usage = usage
63         flag.Parse()
64         if *warranty {
65                 fmt.Println(nncp.Warranty)
66                 return
67         }
68         if *version {
69                 fmt.Println(nncp.VersionGet())
70                 return
71         }
72
73         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
74         if err != nil {
75                 log.Fatalln("Error during initialization:", err)
76         }
77         ctx.Umask()
78
79         if *doTmp {
80                 err = filepath.Walk(
81                         filepath.Join(ctx.Spool, "tmp"),
82                         func(path string, info os.FileInfo, err error) error {
83                                 if err != nil {
84                                         return err
85                                 }
86                                 if info.IsDir() {
87                                         return nil
88                                 }
89                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
90                                 return os.Remove(path)
91                         })
92                 if err != nil {
93                         log.Fatalln("Error during walking:", err)
94                 }
95                 return
96         }
97         if *doLock {
98                 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
99                         if err != nil {
100                                 return err
101                         }
102                         if info.IsDir() {
103                                 return nil
104                         }
105                         if strings.HasSuffix(info.Name(), ".lock") {
106                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
107                                 return os.Remove(path)
108                         }
109                         return nil
110                 })
111                 if err != nil {
112                         log.Fatalln("Error during walking:", err)
113                 }
114                 return
115         }
116         if *nodeRaw == "" {
117                 usage()
118                 os.Exit(1)
119         }
120         node, err := ctx.FindNode(*nodeRaw)
121         if err != nil {
122                 log.Fatalln("Invalid -node specified:", err)
123         }
124         remove := func(xx nncp.TRxTx) error {
125                 return filepath.Walk(
126                         filepath.Join(ctx.Spool, node.Id.String(), string(xx)),
127                         func(path string, info os.FileInfo, err error) error {
128                                 if err != nil {
129                                         return err
130                                 }
131                                 if info.IsDir() {
132                                         return nil
133                                 }
134                                 if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
135                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
136                                         return os.Remove(path)
137                                 }
138                                 if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
139                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
140                                         return os.Remove(path)
141                                 }
142                                 if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
143                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
144                                         return os.Remove(path)
145                                 }
146                                 if !*doSeen &&
147                                         !*doPart &&
148                                         (*doRx || *doTx) &&
149                                         ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
150                                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
151                                         return os.Remove(path)
152                                 }
153                                 return nil
154                         })
155         }
156         if *pktRaw != "" || *doRx || *doSeen || *doPart {
157                 if err = remove(nncp.TRx); err != nil {
158                         log.Fatalln("Can not remove:", err)
159                 }
160         }
161         if *pktRaw != "" || *doTx {
162                 if err = remove(nncp.TTx); err != nil {
163                         log.Fatalln("Can not remove:", err)
164                 }
165         }
166 }