2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 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.
35 "go.cypherpunks.ru/nncp/v8"
39 fmt.Fprint(os.Stderr, "nncp-rm -- remove packet\n\n")
40 fmt.Fprintf(os.Stderr, "Usage: %s [options] [-older X] -tmp\n", os.Args[0])
41 fmt.Fprintf(os.Stderr, " %s [options] -lock\n", os.Args[0])
42 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -part\n", os.Args[0])
43 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -seen\n", os.Args[0])
44 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -nock\n", os.Args[0])
45 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -area\n", os.Args[0])
46 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} {-rx|-tx} [-hdr]\n", os.Args[0])
47 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -pkt < ...\n", os.Args[0])
48 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -ack\n", os.Args[0])
49 fmt.Fprintln(os.Stderr, "-older option's time units are: (s)econds, (m)inutes, (h)ours, (d)ays")
50 fmt.Fprintln(os.Stderr, "Options:")
56 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
57 doTmp = flag.Bool("tmp", false, "Remove temporary files")
58 doLock = flag.Bool("lock", false, "Remove all lock files")
59 doAll = flag.Bool("all", false, "Process all nodes")
60 nodeRaw = flag.String("node", "", "Process only that node")
61 doRx = flag.Bool("rx", false, "Process inbound packets")
62 doTx = flag.Bool("tx", false, "Process outbound packets")
63 doPart = flag.Bool("part", false, "Remove only .part files")
64 doSeen = flag.Bool("seen", false, "Remove only seen/ files")
65 doNoCK = flag.Bool("nock", false, "Remove only .nock files")
66 doHdr = flag.Bool("hdr", false, "Remove only hdr/ files")
67 doArea = flag.Bool("area", false, "Remove only area/* seen files")
68 older = flag.String("older", "", "XXX{smhd}: only older than XXX number of time units")
69 dryRun = flag.Bool("dryrun", false, "Do not actually remove files")
70 doPkt = flag.Bool("pkt", false, "Remove only that packets")
71 doACK = flag.Bool("ack", false, "Remove ACK packets from outbound")
72 spoolPath = flag.String("spool", "", "Override path to spool")
73 quiet = flag.Bool("quiet", false, "Print only errors")
74 debug = flag.Bool("debug", false, "Print debug messages")
75 version = flag.Bool("version", false, "Print version information")
76 warranty = flag.Bool("warranty", false, "Print warranty information")
78 log.SetFlags(log.Lshortfile)
82 fmt.Println(nncp.Warranty)
86 fmt.Println(nncp.VersionGet())
90 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
92 log.Fatalln("Error during initialization:", err)
96 var oldBoundaryRaw int
98 olderRe := regexp.MustCompile(`^(\d+)([smhd])$`)
99 matches := olderRe.FindStringSubmatch(*older)
100 if len(matches) != 1+2 {
101 log.Fatalln("can not parse -older")
103 oldBoundaryRaw, err = strconv.Atoi(matches[1])
105 log.Fatalln("can not parse -older:", err)
113 oldBoundaryRaw *= 60 * 60
115 oldBoundaryRaw *= 60 * 60 * 24
118 oldBoundary := time.Second * time.Duration(oldBoundaryRaw)
120 pkts := make(map[string]struct{})
122 raw, err := io.ReadAll(os.Stdin)
124 log.Fatalln("can not read -pkt from stdin:", err)
126 for _, line := range strings.Fields(string(raw)) {
130 cols := strings.Split(line, "/")
131 pkts[cols[len(cols)-1]] = struct{}{}
138 filepath.Join(ctx.Spool, "tmp"),
139 func(path string, info os.FileInfo, err error) error {
146 if now.Sub(info.ModTime()) < oldBoundary {
147 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
148 return fmt.Sprintf("File %s: too fresh, skipping", path)
152 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
153 return fmt.Sprintf("File %s: removed", path)
158 return os.Remove(path)
161 log.Fatalln("Error during walking:", err)
167 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
174 if strings.HasSuffix(info.Name(), ".lock") {
175 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
176 return fmt.Sprintf("File %s: removed", path)
181 return os.Remove(path)
186 log.Fatalln("Error during walking:", err)
191 var nodeId *nncp.NodeId
198 node, err := ctx.FindNode(*nodeRaw)
200 log.Fatalln("Invalid -node specified:", err)
205 for _, node := range ctx.Neigh {
206 if nodeId != nil && node.Id != nodeId {
209 remove := func(xx nncp.TRxTx) error {
210 p := filepath.Join(ctx.Spool, node.Id.String(), string(xx))
211 if _, err := os.Stat(p); err != nil && errors.Is(err, fs.ErrNotExist) {
214 dir, err := os.Open(p)
216 if errors.Is(err, fs.ErrNotExist) {
223 entries, err := dir.ReadDir(1 << 10)
230 for _, entry := range entries {
234 pth := filepath.Join(p, entry.Name())
235 logMsg := func(les nncp.LEs) string {
236 return fmt.Sprintf("File %s: removed", pth)
239 if _, exists := pkts[filepath.Base(entry.Name())]; exists {
240 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
242 os.Remove(nncp.JobPath2Hdr(pth))
243 if err = os.Remove(pth); err != nil {
250 info, err := entry.Info()
254 if now.Sub(info.ModTime()) < oldBoundary {
255 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: pth}},
256 func(les nncp.LEs) string {
257 return fmt.Sprintf("File %s: too fresh, skipping", pth)
262 if (*doNoCK && strings.HasSuffix(entry.Name(), nncp.NoCKSuffix)) ||
263 (*doPart && strings.HasSuffix(entry.Name(), nncp.PartSuffix)) {
264 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
268 if err = os.Remove(pth); err != nil {
272 if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
274 ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
275 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
279 os.Remove(nncp.JobPath2Hdr(pth))
280 if err = os.Remove(pth); err != nil {
288 if len(pkts) > 0 || *doRx || *doNoCK || *doPart {
289 if err = remove(nncp.TRx); err != nil {
290 log.Fatalln("Can not remove:", err)
293 if len(pkts) > 0 || *doTx {
294 if err = remove(nncp.TTx); err != nil {
295 log.Fatalln("Can not remove:", err)
298 removeSub := func(p string) error {
299 return filepath.Walk(p, func(
300 path string, info os.FileInfo, err error,
303 if errors.Is(err, fs.ErrNotExist) {
311 logMsg := func(les nncp.LEs) string {
312 return fmt.Sprintf("File %s: removed", path)
315 if _, exists := pkts[filepath.Base(info.Name())]; !exists {
318 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
322 return os.Remove(path)
324 if now.Sub(info.ModTime()) < oldBoundary {
326 "rm-skip", nncp.LEs{{K: "File", V: path}},
327 func(les nncp.LEs) string {
328 return fmt.Sprintf("File %s: too fresh, skipping", path)
333 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
337 return os.Remove(path)
340 if len(pkts) > 0 || *doSeen {
341 if err = removeSub(filepath.Join(
342 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir,
344 log.Fatalln("Can not remove:", err)
348 if err = removeSub(filepath.Join(
349 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir,
351 log.Fatalln("Can not remove:", err)
355 if err = removeSub(filepath.Join(
356 ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir,
358 log.Fatalln("Can not remove:", err)
362 if err = filepath.Walk(
363 filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir),
364 func(path string, info os.FileInfo, err error) error {
366 if errors.Is(err, fs.ErrNotExist) {
374 if now.Sub(info.ModTime()) < oldBoundary {
376 "rm-skip", nncp.LEs{{K: "File", V: path}},
377 func(les nncp.LEs) string {
378 return fmt.Sprintf("File %s: too fresh, skipping", path)
385 nncp.LEs{{K: "File", V: path}},
386 func(les nncp.LEs) string {
387 return fmt.Sprintf("File %s: removed", path)
393 return os.Remove(path)
395 log.Fatalln("Can not remove:", err)
399 dirPath := filepath.Join(
400 ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.ACKDir)
401 dir, err := os.Open(dirPath)
406 fis, err := dir.ReadDir(1 << 10)
411 log.Fatalln("Can not read directory:", err)
413 for _, fi := range fis {
414 for _, pth := range []string{
421 filepath.Join(dirPath, fi.Name()),
423 if err = os.Remove(pth); err == nil {
426 nncp.LEs{{K: "File", V: pth}},
427 func(les nncp.LEs) string {
428 return fmt.Sprintf("File %s: removed", pth)
431 } else if !errors.Is(err, fs.ErrNotExist) {
432 log.Fatalln("Can not remove:", pth, ":", err)