2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 // Remove packet from the queue.
32 "go.cypherpunks.ru/nncp/v7"
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] {-all|-node NODE} -part\n", os.Args[0])
41 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -seen\n", os.Args[0])
42 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -nock\n", os.Args[0])
43 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -hdr\n", os.Args[0])
44 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -area\n", os.Args[0])
45 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} {-rx|-tx}\n", os.Args[0])
46 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -pkt PKT\n", os.Args[0])
47 fmt.Fprintln(os.Stderr, "-older option's time units are: (s)econds, (m)inutes, (h)ours, (d)ays")
48 fmt.Fprintln(os.Stderr, "Options:")
54 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
55 doAll = flag.Bool("all", false, "Apply remove rules to all nodes")
56 doTmp = flag.Bool("tmp", false, "Remove all temporary files")
57 doHdr = flag.Bool("hdr", false, "Remove all hdr/ files")
58 doLock = flag.Bool("lock", false, "Remove all lock files")
59 nodeRaw = flag.String("node", "", "Node to remove files in")
60 doRx = flag.Bool("rx", false, "Process received packets")
61 doTx = flag.Bool("tx", false, "Process transfered packets")
62 doPart = flag.Bool("part", false, "Remove only .part files")
63 doSeen = flag.Bool("seen", false, "Remove only seen/ files")
64 doNoCK = flag.Bool("nock", false, "Remove only .nock files")
65 doArea = flag.Bool("area", false, "Remove only area/* seen files")
66 older = flag.String("older", "", "XXX{smhd}: only older than XXX number of time units")
67 dryRun = flag.Bool("dryrun", false, "Do not actually remove files")
68 pktRaw = flag.String("pkt", "", "Packet to remove")
69 spoolPath = flag.String("spool", "", "Override path to spool")
70 quiet = flag.Bool("quiet", false, "Print only errors")
71 debug = flag.Bool("debug", false, "Print debug messages")
72 version = flag.Bool("version", false, "Print version information")
73 warranty = flag.Bool("warranty", false, "Print warranty information")
75 log.SetFlags(log.Lshortfile)
79 fmt.Println(nncp.Warranty)
83 fmt.Println(nncp.VersionGet())
87 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
89 log.Fatalln("Error during initialization:", err)
93 var oldBoundaryRaw int
95 olderRe := regexp.MustCompile(`^(\d+)([smhd])$`)
96 matches := olderRe.FindStringSubmatch(*older)
97 if len(matches) != 1+2 {
98 log.Fatalln("can not parse -older")
100 oldBoundaryRaw, err = strconv.Atoi(matches[1])
102 log.Fatalln("can not parse -older:", err)
110 oldBoundaryRaw *= 60 * 60
112 oldBoundaryRaw *= 60 * 60 * 24
115 oldBoundary := time.Second * time.Duration(oldBoundaryRaw)
120 filepath.Join(ctx.Spool, "tmp"),
121 func(path string, info os.FileInfo, err error) error {
128 if now.Sub(info.ModTime()) < oldBoundary {
129 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
130 return fmt.Sprintf("File %s: too fresh, skipping", path)
134 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
135 return fmt.Sprintf("File %s: removed", path)
140 return os.Remove(path)
143 log.Fatalln("Error during walking:", err)
148 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
155 if strings.HasSuffix(info.Name(), ".lock") {
156 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
157 return fmt.Sprintf("File %s: removed", path)
162 return os.Remove(path)
167 log.Fatalln("Error during walking:", err)
171 var nodeId *nncp.NodeId
178 node, err := ctx.FindNode(*nodeRaw)
180 log.Fatalln("Invalid -node specified:", err)
184 for _, node := range ctx.Neigh {
185 if nodeId != nil && node.Id != nodeId {
188 remove := func(xx nncp.TRxTx) error {
189 p := filepath.Join(ctx.Spool, node.Id.String(), string(xx))
190 if _, err := os.Stat(p); err != nil && os.IsNotExist(err) {
193 return filepath.Walk(p,
194 func(path string, info os.FileInfo, err error) error {
201 logMsg := func(les nncp.LEs) string {
202 return fmt.Sprintf("File %s: removed", path)
204 if now.Sub(info.ModTime()) < oldBoundary {
205 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
206 return fmt.Sprintf("File %s: too fresh, skipping", path)
210 if (*doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix)) ||
211 (*doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix)) {
212 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
216 return os.Remove(path)
218 if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
219 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
223 return os.Remove(path)
225 if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
227 ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
228 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
232 return os.Remove(path)
237 if *pktRaw != "" || *doRx || *doNoCK || *doPart {
238 if err = remove(nncp.TRx); err != nil {
239 log.Fatalln("Can not remove:", err)
242 if *pktRaw != "" || *doTx || *doHdr {
243 if err = remove(nncp.TTx); err != nil {
244 log.Fatalln("Can not remove:", err)
247 removeSub := func(p string, everything bool) error {
248 return filepath.Walk(
249 p, func(path string, info os.FileInfo, err error) error {
251 if os.IsNotExist(err) {
259 logMsg := func(les nncp.LEs) string {
260 return fmt.Sprintf("File %s: removed", path)
263 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
267 return os.Remove(path)
269 if now.Sub(info.ModTime()) < oldBoundary {
271 "rm-skip", nncp.LEs{{K: "File", V: path}},
272 func(les nncp.LEs) string {
273 return fmt.Sprintf("File %s: too fresh, skipping", path)
277 return os.Remove(path)
283 if *doRx || *doSeen {
284 if err = removeSub(filepath.Join(
285 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir,
286 ), *doSeen); err != nil {
287 log.Fatalln("Can not remove:", err)
291 if err = removeSub(filepath.Join(
292 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir,
293 ), *doHdr); err != nil {
294 log.Fatalln("Can not remove:", err)
298 if err = removeSub(filepath.Join(
299 ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir,
300 ), *doHdr); err != nil {
301 log.Fatalln("Can not remove:", err)
305 if err = filepath.Walk(
306 filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir),
307 func(path string, info os.FileInfo, err error) error {
309 if os.IsNotExist(err) {
317 if now.Sub(info.ModTime()) < oldBoundary {
318 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
319 return fmt.Sprintf("File %s: too fresh, skipping", path)
325 nncp.LEs{{K: "File", V: path}},
326 func(les nncp.LEs) string {
327 return fmt.Sprintf("File %s: removed", path)
333 return os.Remove(path)
335 log.Fatalln("Can not remove:", err)