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] [-older X] {-all|-node NODE} -pkt < ...\n", os.Args[0])
48 fmt.Fprintln(os.Stderr, "-older option's time units are: (s)econds, (m)inutes, (h)ours, (d)ays")
49 fmt.Fprintln(os.Stderr, "Options:")
55 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
56 doTmp = flag.Bool("tmp", false, "Remove temporary files")
57 doLock = flag.Bool("lock", false, "Remove all lock files")
58 doAll = flag.Bool("all", false, "Process all nodes")
59 nodeRaw = flag.String("node", "", "Process only that node")
60 doRx = flag.Bool("rx", false, "Process inbound packets")
61 doTx = flag.Bool("tx", false, "Process outbound 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 doHdr = flag.Bool("hdr", false, "Remove only hdr/ files")
66 doArea = flag.Bool("area", false, "Remove only area/* seen files")
67 older = flag.String("older", "", "XXX{smhd}: only older than XXX number of time units")
68 dryRun = flag.Bool("dryrun", false, "Do not actually remove files")
69 doPkt = flag.Bool("pkt", false, "Remove only that packets")
70 spoolPath = flag.String("spool", "", "Override path to spool")
71 quiet = flag.Bool("quiet", false, "Print only errors")
72 debug = flag.Bool("debug", false, "Print debug messages")
73 version = flag.Bool("version", false, "Print version information")
74 warranty = flag.Bool("warranty", false, "Print warranty information")
76 log.SetFlags(log.Lshortfile)
80 fmt.Println(nncp.Warranty)
84 fmt.Println(nncp.VersionGet())
88 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", *quiet, false, false, *debug)
90 log.Fatalln("Error during initialization:", err)
94 var oldBoundaryRaw int
96 olderRe := regexp.MustCompile(`^(\d+)([smhd])$`)
97 matches := olderRe.FindStringSubmatch(*older)
98 if len(matches) != 1+2 {
99 log.Fatalln("can not parse -older")
101 oldBoundaryRaw, err = strconv.Atoi(matches[1])
103 log.Fatalln("can not parse -older:", err)
111 oldBoundaryRaw *= 60 * 60
113 oldBoundaryRaw *= 60 * 60 * 24
116 oldBoundary := time.Second * time.Duration(oldBoundaryRaw)
118 pkts := make(map[string]struct{})
120 raw, err := io.ReadAll(os.Stdin)
122 log.Fatalln("can not read -pkt from stdin:", err)
124 for _, line := range strings.Fields(string(raw)) {
128 cols := strings.Split(line, "/")
129 pkts[cols[len(cols)-1]] = struct{}{}
136 filepath.Join(ctx.Spool, "tmp"),
137 func(path string, info os.FileInfo, err error) error {
144 if now.Sub(info.ModTime()) < oldBoundary {
145 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
146 return fmt.Sprintf("File %s: too fresh, skipping", path)
150 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
151 return fmt.Sprintf("File %s: removed", path)
156 return os.Remove(path)
159 log.Fatalln("Error during walking:", err)
165 err = filepath.Walk(ctx.Spool, func(path string, info os.FileInfo, err error) error {
172 if strings.HasSuffix(info.Name(), ".lock") {
173 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
174 return fmt.Sprintf("File %s: removed", path)
179 return os.Remove(path)
184 log.Fatalln("Error during walking:", err)
189 var nodeId *nncp.NodeId
196 node, err := ctx.FindNode(*nodeRaw)
198 log.Fatalln("Invalid -node specified:", err)
203 for _, node := range ctx.Neigh {
204 if nodeId != nil && node.Id != nodeId {
207 remove := func(xx nncp.TRxTx) error {
208 p := filepath.Join(ctx.Spool, node.Id.String(), string(xx))
209 if _, err := os.Stat(p); err != nil && errors.Is(err, fs.ErrNotExist) {
212 dir, err := os.Open(p)
214 if errors.Is(err, fs.ErrNotExist) {
221 entries, err := dir.ReadDir(1 << 10)
228 for _, entry := range entries {
232 pth := filepath.Join(p, entry.Name())
233 logMsg := func(les nncp.LEs) string {
234 return fmt.Sprintf("File %s: removed", pth)
237 if _, exists := pkts[filepath.Base(entry.Name())]; exists {
238 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
240 os.Remove(nncp.JobPath2Hdr(pth))
241 if err = os.Remove(pth); err != nil {
248 info, err := entry.Info()
252 if now.Sub(info.ModTime()) < oldBoundary {
253 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: pth}}, func(les nncp.LEs) string {
254 return fmt.Sprintf("File %s: too fresh, skipping", pth)
258 if (*doNoCK && strings.HasSuffix(entry.Name(), nncp.NoCKSuffix)) ||
259 (*doPart && strings.HasSuffix(entry.Name(), nncp.PartSuffix)) {
260 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
264 if err = os.Remove(pth); err != nil {
268 if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
270 ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
271 ctx.LogI("rm", nncp.LEs{{K: "File", V: pth}}, logMsg)
275 os.Remove(nncp.JobPath2Hdr(pth))
276 if err = os.Remove(pth); err != nil {
284 if len(pkts) > 0 || *doRx || *doNoCK || *doPart {
285 if err = remove(nncp.TRx); err != nil {
286 log.Fatalln("Can not remove:", err)
289 if len(pkts) > 0 || *doTx {
290 if err = remove(nncp.TTx); err != nil {
291 log.Fatalln("Can not remove:", err)
294 removeSub := func(p string) error {
295 return filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
297 if errors.Is(err, fs.ErrNotExist) {
305 logMsg := func(les nncp.LEs) string {
306 return fmt.Sprintf("File %s: removed", path)
309 if _, exists := pkts[filepath.Base(info.Name())]; !exists {
312 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
316 return os.Remove(path)
318 if now.Sub(info.ModTime()) < oldBoundary {
320 "rm-skip", nncp.LEs{{K: "File", V: path}},
321 func(les nncp.LEs) string {
322 return fmt.Sprintf("File %s: too fresh, skipping", path)
327 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
331 return os.Remove(path)
334 if len(pkts) > 0 || *doSeen {
335 if err = removeSub(filepath.Join(
336 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir,
338 log.Fatalln("Can not remove:", err)
342 if err = removeSub(filepath.Join(
343 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir,
345 log.Fatalln("Can not remove:", err)
349 if err = removeSub(filepath.Join(
350 ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir,
352 log.Fatalln("Can not remove:", err)
356 if err = filepath.Walk(
357 filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir),
358 func(path string, info os.FileInfo, err error) error {
360 if errors.Is(err, fs.ErrNotExist) {
368 if now.Sub(info.ModTime()) < oldBoundary {
370 "rm-skip", nncp.LEs{{K: "File", V: path}},
371 func(les nncp.LEs) string {
372 return fmt.Sprintf("File %s: too fresh, skipping", path)
379 nncp.LEs{{K: "File", V: path}},
380 func(les nncp.LEs) string {
381 return fmt.Sprintf("File %s: removed", path)
387 return os.Remove(path)
389 log.Fatalln("Can not remove:", err)