]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go
029837906111e74e202b89fbc8459bf741a4b10d
[nncp.git] / src / cypherpunks.ru / nncp / 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, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Remove packet from the queue.
20 package main
21
22 import (
23         "flag"
24         "fmt"
25         "log"
26         "os"
27         "path/filepath"
28         "strings"
29
30         "cypherpunks.ru/nncp"
31 )
32
33 func usage() {
34         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
35         fmt.Fprintf(os.Stderr, "nncp-rm -- remove packet\n\n")
36         fmt.Fprintf(os.Stderr, "Usage: %s [options] -tmp\n", os.Args[0])
37         fmt.Fprintf(os.Stderr, "       %s [options] -lock\n", os.Args[0])
38         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -part\n", os.Args[0])
39         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -seen\n", os.Args[0])
40         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE {-rx|-tx}\n", os.Args[0])
41         fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -pkt PKT\n", os.Args[0])
42         fmt.Fprintln(os.Stderr, "Options:")
43         flag.PrintDefaults()
44 }
45
46 func main() {
47         var (
48                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
49                 doTmp     = flag.Bool("tmp", false, "Remove all temporary files")
50                 doLock    = flag.Bool("lock", false, "Remove all lock files")
51                 nodeRaw   = flag.String("node", "", "Node to remove files in")
52                 doRx      = flag.Bool("rx", false, "Process received packets")
53                 doTx      = flag.Bool("tx", false, "Process transfered packets")
54                 doPart    = flag.Bool("part", false, "Remove only .part files")
55                 doSeen    = flag.Bool("seen", false, "Remove only .seen files")
56                 pktRaw    = flag.String("pkt", "", "Packet to remove")
57                 spoolPath = flag.String("spool", "", "Override path to spool")
58                 quiet     = flag.Bool("quiet", false, "Print only errors")
59                 debug     = flag.Bool("debug", false, "Print debug messages")
60                 version   = flag.Bool("version", false, "Print version information")
61                 warranty  = flag.Bool("warranty", false, "Print warranty information")
62         )
63         flag.Usage = usage
64         flag.Parse()
65         if *warranty {
66                 fmt.Println(nncp.Warranty)
67                 return
68         }
69         if *version {
70                 fmt.Println(nncp.VersionGet())
71                 return
72         }
73
74         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, *debug)
75         if err != nil {
76                 log.Fatalln("Error during initialization:", err)
77         }
78
79         if *doTmp {
80                 err = filepath.Walk(filepath.Join(ctx.Spool, "tmp"), func(path string, info os.FileInfo, err error) error {
81                         if err != nil {
82                                 return err
83                         }
84                         if info.IsDir() {
85                                 return nil
86                         }
87                         ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
88                         return os.Remove(path)
89                 })
90                 if err != nil {
91                         log.Fatalln("Error during walking:", err)
92                 }
93                 return
94         }
95         if *doLock {
96                 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
97                         if err != nil {
98                                 return err
99                         }
100                         if info.IsDir() {
101                                 return nil
102                         }
103                         if strings.HasSuffix(info.Name(), ".lock") {
104                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
105                                 return os.Remove(path)
106                         }
107                         return nil
108                 })
109                 if err != nil {
110                         log.Fatalln("Error during walking:", err)
111                 }
112                 return
113         }
114         if *nodeRaw == "" {
115                 usage()
116                 os.Exit(1)
117         }
118         node, err := ctx.FindNode(*nodeRaw)
119         if err != nil {
120                 log.Fatalln("Invalid -node specified:", err)
121         }
122         remove := func(xx nncp.TRxTx) error {
123                 return filepath.Walk(filepath.Join(ctx.Spool, node.Id.String(), string(xx)), func(path string, info os.FileInfo, err error) error {
124                         if err != nil {
125                                 return err
126                         }
127                         if info.IsDir() {
128                                 return nil
129                         }
130                         if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
131                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
132                                 return os.Remove(path)
133                         }
134                         if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
135                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
136                                 return os.Remove(path)
137                         }
138                         if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
139                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
140                                 return os.Remove(path)
141                         }
142                         if !*doSeen &&
143                                 !*doPart &&
144                                 (*doRx || *doTx) &&
145                                 ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
146                                 ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
147                                 return os.Remove(path)
148                         }
149                         return nil
150                 })
151         }
152         if *pktRaw != "" || *doRx || *doSeen || *doPart {
153                 if err = remove(nncp.TRx); err != nil {
154                         log.Fatalln("Can not remove:", err)
155                 }
156         }
157         if *pktRaw != "" || *doTx {
158                 if err = remove(nncp.TTx); err != nil {
159                         log.Fatalln("Can not remove:", err)
160                 }
161         }
162 }