2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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.
33 "go.cypherpunks.ru/nncp/v8"
37 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
38 fmt.Fprintf(os.Stderr, "nncp-rm -- remove packet\n\n")
39 fmt.Fprintf(os.Stderr, "Usage: %s [options] [-older X] -tmp\n", os.Args[0])
40 fmt.Fprintf(os.Stderr, " %s [options] -lock\n", os.Args[0])
41 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -part\n", os.Args[0])
42 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -seen\n", os.Args[0])
43 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -nock\n", os.Args[0])
44 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -area\n", os.Args[0])
45 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} {-rx|-tx} [-hdr]\n", os.Args[0])
46 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -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 doTmp = flag.Bool("tmp", false, "Remove temporary files")
56 doLock = flag.Bool("lock", false, "Remove all lock files")
57 doAll = flag.Bool("all", false, "Process all nodes")
58 nodeRaw = flag.String("node", "", "Process only that node")
59 doRx = flag.Bool("rx", false, "Process inbound packets")
60 doTx = flag.Bool("tx", false, "Process outbound packets")
61 doPart = flag.Bool("part", false, "Remove only .part files")
62 doSeen = flag.Bool("seen", false, "Remove only seen/ files")
63 doNoCK = flag.Bool("nock", false, "Remove only .nock files")
64 doHdr = flag.Bool("hdr", false, "Remove only hdr/ 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 doPkt = flag.Bool("pkt", false, "Remove only that packets")
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)
117 pkts := make(map[string]struct{})
119 raw, err := io.ReadAll(os.Stdin)
121 log.Fatalln("can not read -pkt from stdin:", err)
123 for _, line := range strings.Fields(string(raw)) {
127 cols := strings.Split(line, "/")
128 pkts[cols[len(cols)-1]] = struct{}{}
135 filepath.Join(ctx.Spool, "tmp"),
136 func(path string, info os.FileInfo, err error) error {
143 if now.Sub(info.ModTime()) < oldBoundary {
144 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
145 return fmt.Sprintf("File %s: too fresh, skipping", path)
149 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
150 return fmt.Sprintf("File %s: removed", path)
155 return os.Remove(path)
158 log.Fatalln("Error during walking:", err)
164 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
171 if strings.HasSuffix(info.Name(), ".lock") {
172 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
173 return fmt.Sprintf("File %s: removed", path)
178 return os.Remove(path)
183 log.Fatalln("Error during walking:", err)
188 var nodeId *nncp.NodeId
195 node, err := ctx.FindNode(*nodeRaw)
197 log.Fatalln("Invalid -node specified:", err)
202 for _, node := range ctx.Neigh {
203 if nodeId != nil && node.Id != nodeId {
206 remove := func(xx nncp.TRxTx) error {
207 p := filepath.Join(ctx.Spool, node.Id.String(), string(xx))
208 if _, err := os.Stat(p); err != nil && os.IsNotExist(err) {
211 dir, err := os.Open(p)
213 if os.IsNotExist(err) {
220 entries, err := dir.ReadDir(1 << 10)
227 for _, entry := range entries {
231 pth := filepath.Join(p, entry.Name())
232 logMsg := func(les nncp.LEs) string {
233 return fmt.Sprintf("File %s: removed", pth)
236 if _, exists := pkts[filepath.Base(entry.Name())]; exists {
237 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
239 os.Remove(nncp.JobPath2Hdr(pth))
240 if err = os.Remove(pth); err != nil {
247 info, err := entry.Info()
251 if now.Sub(info.ModTime()) < oldBoundary {
252 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: pth}}, func(les nncp.LEs) string {
253 return fmt.Sprintf("File %s: too fresh, skipping", pth)
257 if (*doNoCK && strings.HasSuffix(entry.Name(), nncp.NoCKSuffix)) ||
258 (*doPart && strings.HasSuffix(entry.Name(), nncp.PartSuffix)) {
259 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
263 if err = os.Remove(pth); err != nil {
267 if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
269 ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
270 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
274 os.Remove(nncp.JobPath2Hdr(pth))
275 if err = os.Remove(pth); err != nil {
283 if len(pkts) > 0 || *doRx || *doNoCK || *doPart {
284 if err = remove(nncp.TRx); err != nil {
285 log.Fatalln("Can not remove:", err)
288 if len(pkts) > 0 || *doTx {
289 if err = remove(nncp.TTx); err != nil {
290 log.Fatalln("Can not remove:", err)
293 removeSub := func(p string) error {
294 return filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
296 if os.IsNotExist(err) {
304 logMsg := func(les nncp.LEs) string {
305 return fmt.Sprintf("File %s: removed", path)
308 if _, exists := pkts[filepath.Base(info.Name())]; !exists {
311 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
315 return os.Remove(path)
317 if now.Sub(info.ModTime()) < oldBoundary {
319 "rm-skip", nncp.LEs{{K: "File", V: path}},
320 func(les nncp.LEs) string {
321 return fmt.Sprintf("File %s: too fresh, skipping", path)
326 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
330 return os.Remove(path)
333 if len(pkts) > 0 || *doSeen {
334 if err = removeSub(filepath.Join(
335 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir,
337 log.Fatalln("Can not remove:", err)
341 if err = removeSub(filepath.Join(
342 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir,
344 log.Fatalln("Can not remove:", err)
348 if err = removeSub(filepath.Join(
349 ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir,
351 log.Fatalln("Can not remove:", err)
355 if err = filepath.Walk(
356 filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir),
357 func(path string, info os.FileInfo, err error) error {
359 if os.IsNotExist(err) {
367 if now.Sub(info.ModTime()) < oldBoundary {
369 "rm-skip", nncp.LEs{{K: "File", V: path}},
370 func(les nncp.LEs) string {
371 return fmt.Sprintf("File %s: too fresh, skipping", path)
378 nncp.LEs{{K: "File", V: path}},
379 func(les nncp.LEs) string {
380 return fmt.Sprintf("File %s: removed", path)
386 return os.Remove(path)
388 log.Fatalln("Can not remove:", err)