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] -tmp\n", os.Args[0])
40 fmt.Fprintf(os.Stderr, " %s [options] -lock\n", os.Args[0])
41 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -part\n", os.Args[0])
42 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -seen\n", os.Args[0])
43 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -nock\n", os.Args[0])
44 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -hdr\n", os.Args[0])
45 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} -area\n", os.Args[0])
46 fmt.Fprintf(os.Stderr, " %s [options] {-all|-node NODE} {-rx|-tx}\n", os.Args[0])
47 fmt.Fprintf(os.Stderr, " %s [options] {-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 doAll = flag.Bool("all", false, "Apply remove rules to all nodes")
57 doTmp = flag.Bool("tmp", false, "Remove all temporary files")
58 doHdr = flag.Bool("hdr", false, "Remove all hdr/ files")
59 doLock = flag.Bool("lock", false, "Remove all lock files")
60 nodeRaw = flag.String("node", "", "Node to remove files in")
61 doRx = flag.Bool("rx", false, "Process received packets")
62 doTx = flag.Bool("tx", false, "Process transfered 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 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, "Packet to remove TODO")
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.Split(string(raw), "\n") {
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)
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)
187 var nodeId *nncp.NodeId
194 node, err := ctx.FindNode(*nodeRaw)
196 log.Fatalln("Invalid -node specified:", err)
200 for _, node := range ctx.Neigh {
201 if nodeId != nil && node.Id != nodeId {
204 remove := func(xx nncp.TRxTx) error {
205 p := filepath.Join(ctx.Spool, node.Id.String(), string(xx))
206 if _, err := os.Stat(p); err != nil && os.IsNotExist(err) {
209 return filepath.Walk(p,
210 func(path string, info os.FileInfo, err error) error {
217 logMsg := func(les nncp.LEs) string {
218 return fmt.Sprintf("File %s: removed", path)
220 if now.Sub(info.ModTime()) < oldBoundary {
221 ctx.LogD("rm-skip", nncp.LEs{{K: "File", V: path}}, func(les nncp.LEs) string {
222 return fmt.Sprintf("File %s: too fresh, skipping", path)
226 if (*doNoCK && strings.HasSuffix(info.Name(), nncp.NoCKSuffix)) ||
227 (*doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix)) {
228 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
232 return os.Remove(path)
235 if _, exists := pkts[filepath.Base(info.Name())]; exists {
236 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
240 return os.Remove(path)
243 if !*doSeen && !*doNoCK && !*doHdr && !*doPart &&
245 ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
246 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
250 return os.Remove(path)
255 if len(pkts) > 0 || *doRx || *doNoCK || *doPart {
256 if err = remove(nncp.TRx); err != nil {
257 log.Fatalln("Can not remove:", err)
260 if len(pkts) > 0 || *doTx || *doHdr {
261 if err = remove(nncp.TTx); err != nil {
262 log.Fatalln("Can not remove:", err)
265 removeSub := func(p string, everything bool) error {
266 return filepath.Walk(
267 p, func(path string, info os.FileInfo, err error) error {
269 if os.IsNotExist(err) {
277 logMsg := func(les nncp.LEs) string {
278 return fmt.Sprintf("File %s: removed", path)
281 ctx.LogI("rm", nncp.LEs{{K: "File", V: path}}, logMsg)
285 return os.Remove(path)
287 if now.Sub(info.ModTime()) < oldBoundary {
289 "rm-skip", nncp.LEs{{K: "File", V: path}},
290 func(les nncp.LEs) string {
291 return fmt.Sprintf("File %s: too fresh, skipping", path)
295 return os.Remove(path)
301 if *doRx || *doSeen {
302 if err = removeSub(filepath.Join(
303 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.SeenDir,
304 ), *doSeen); err != nil {
305 log.Fatalln("Can not remove:", err)
309 if err = removeSub(filepath.Join(
310 ctx.Spool, node.Id.String(), string(nncp.TRx), nncp.HdrDir,
311 ), *doHdr); err != nil {
312 log.Fatalln("Can not remove:", err)
316 if err = removeSub(filepath.Join(
317 ctx.Spool, node.Id.String(), string(nncp.TTx), nncp.HdrDir,
318 ), *doHdr); err != nil {
319 log.Fatalln("Can not remove:", err)
323 if err = filepath.Walk(
324 filepath.Join(ctx.Spool, node.Id.String(), nncp.AreaDir),
325 func(path string, info os.FileInfo, err error) error {
327 if os.IsNotExist(err) {
335 if now.Sub(info.ModTime()) < oldBoundary {
337 "rm-skip", nncp.LEs{{K: "File", V: path}},
338 func(les nncp.LEs) string {
339 return fmt.Sprintf("File %s: too fresh, skipping", path)
346 nncp.LEs{{K: "File", V: path}},
347 func(les nncp.LEs) string {
348 return fmt.Sprintf("File %s: removed", path)
354 return os.Remove(path)
356 log.Fatalln("Can not remove:", err)