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.UsageHeader())
40 fmt.Fprint(os.Stderr, "nncp-rm -- remove packet\n\n")
41 fmt.Fprintf(os.Stderr, "Usage: %s [options] [-older X] -tmp\n", os.Args[0])
42 fmt.Fprintf(os.Stderr, " %s [options] -lock\n", os.Args[0])
43 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -part\n", os.Args[0])
44 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -seen\n", os.Args[0])
45 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -nock\n", os.Args[0])
46 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -area\n", os.Args[0])
47 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} {-rx|-tx} [-hdr]\n", os.Args[0])
48 fmt.Fprintf(os.Stderr, " %s [options] [-older X] {-all|-node NODE} -pkt < ...\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 spoolPath = flag.String("spool", "", "Override path to spool")
72 quiet = flag.Bool("quiet", false, "Print only errors")
73 debug = flag.Bool("debug", false, "Print debug messages")
74 version = flag.Bool("version", false, "Print version information")
75 warranty = flag.Bool("warranty", false, "Print warranty information")
77 log.SetFlags(log.Lshortfile)
81 fmt.Println(nncp.Warranty)
85 fmt.Println(nncp.VersionGet())
89 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
91 log.Fatalln("Error during initialization:", err)
95 var oldBoundaryRaw int
97 olderRe := regexp.MustCompile(`^(\d+)([smhd])$`)
98 matches := olderRe.FindStringSubmatch(*older)
99 if len(matches) != 1+2 {
100 log.Fatalln("can not parse -older")
102 oldBoundaryRaw, err = strconv.Atoi(matches[1])
104 log.Fatalln("can not parse -older:", err)
112 oldBoundaryRaw *= 60 * 60
114 oldBoundaryRaw *= 60 * 60 * 24
117 oldBoundary := time.Second * time.Duration(oldBoundaryRaw)
119 pkts := make(map[string]struct{})
121 raw, err := io.ReadAll(os.Stdin)
123 log.Fatalln("can not read -pkt from stdin:", err)
125 for _, line := range strings.Fields(string(raw)) {
129 cols := strings.Split(line, "/")
130 pkts[cols[len(cols)-1]] = struct{}{}
137 filepath.Join(ctx.Spool, "tmp"),
138 func(path string, info os.FileInfo, err error) error {
145 if now.Sub(info.ModTime()) < oldBoundary {
146 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
147 return fmt.Sprintf("File %s: too fresh, skipping", path)
151 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
152 return fmt.Sprintf("File %s: removed", path)
157 return os.Remove(path)
160 log.Fatalln("Error during walking:", err)
166 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
173 if strings.HasSuffix(info.Name(), ".lock") {
174 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
175 return fmt.Sprintf("File %s: removed", path)
180 return os.Remove(path)
185 log.Fatalln("Error during walking:", err)
190 var nodeId *nncp.NodeId
197 node, err := ctx.FindNode(*nodeRaw)
199 log.Fatalln("Invalid -node specified:", err)
204 for _, node := range ctx.Neigh {
205 if nodeId != nil && node.Id != nodeId {
208 remove := func(xx nncp.TRxTx) error {
209 p := filepath.Join(ctx.Spool, node.Id.String(), string(xx))
210 if _, err := os.Stat(p); err != nil && errors.Is(err, fs.ErrNotExist) {
213 dir, err := os.Open(p)
215 if errors.Is(err, fs.ErrNotExist) {
222 entries, err := dir.ReadDir(1 << 10)
229 for _, entry := range entries {
233 pth := filepath.Join(p, entry.Name())
234 logMsg := func(les nncp.LEs) string {
235 return fmt.Sprintf("File %s: removed", pth)
238 if _, exists := pkts[filepath.Base(entry.Name())]; exists {
239 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
241 os.Remove(nncp.JobPath2Hdr(pth))
242 if err = os.Remove(pth); err != nil {
249 info, err := entry.Info()
253 if now.Sub(info.ModTime()) < oldBoundary {
254 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: pth}}, func(les nncp.LEs) string {
255 return fmt.Sprintf("File %s: too fresh, skipping", pth)
259 if (*doNoCK && strings.HasSuffix(entry.Name(), nncp.NoCKSuffix)) ||
260 (*doPart && strings.HasSuffix(entry.Name(), nncp.PartSuffix)) {
261 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
265 if err = os.Remove(pth); err != nil {
269 if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
271 ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
272 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
276 os.Remove(nncp.JobPath2Hdr(pth))
277 if err = os.Remove(pth); err != nil {
285 if len(pkts) > 0 || *doRx || *doNoCK || *doPart {
286 if err = remove(nncp.TRx); err != nil {
287 log.Fatalln("Can not remove:", err)
290 if len(pkts) > 0 || *doTx {
291 if err = remove(nncp.TTx); err != nil {
292 log.Fatalln("Can not remove:", err)
295 removeSub := func(p string) error {
296 return filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
298 if errors.Is(err, fs.ErrNotExist) {
306 logMsg := func(les nncp.LEs) string {
307 return fmt.Sprintf("File %s: removed", path)
310 if _, exists := pkts[filepath.Base(info.Name())]; !exists {
313 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
317 return os.Remove(path)
319 if now.Sub(info.ModTime()) < oldBoundary {
321 "rm-skip", nncp.LEs{{K: "File", V: path}},
322 func(les nncp.LEs) string {
323 return fmt.Sprintf("File %s: too fresh, skipping", path)
328 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
332 return os.Remove(path)
335 if len(pkts) > 0 || *doSeen {
336 if err = removeSub(filepath.Join(
337 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir,
339 log.Fatalln("Can not remove:", err)
343 if err = removeSub(filepath.Join(
344 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir,
346 log.Fatalln("Can not remove:", err)
350 if err = removeSub(filepath.Join(
351 ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir,
353 log.Fatalln("Can not remove:", err)
357 if err = filepath.Walk(
358 filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir),
359 func(path string, info os.FileInfo, err error) error {
361 if errors.Is(err, fs.ErrNotExist) {
369 if now.Sub(info.ModTime()) < oldBoundary {
371 "rm-skip", nncp.LEs{{K: "File", V: path}},
372 func(les nncp.LEs) string {
373 return fmt.Sprintf("File %s: too fresh, skipping", path)
380 nncp.LEs{{K: "File", V: path}},
381 func(les nncp.LEs) string {
382 return fmt.Sprintf("File %s: removed", path)
388 return os.Remove(path)
390 log.Fatalln("Can not remove:", err)