]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cmd/nncp-rm/main.go
Forbid any later GNU GPL versions autousage
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-rm / main.go
index d93a46a01bb903c990899a97c00ccac45e4923a3..1040b9a3b08c6f6f7a270e92b6a0cb9a77c1d6da 100644 (file)
@@ -1,11 +1,10 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, version 3 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -16,7 +15,7 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-// Remove packet from the queue
+// Remove packet from the queue.
 package main
 
 import (
@@ -32,10 +31,12 @@ import (
 
 func usage() {
        fmt.Fprintf(os.Stderr, nncp.UsageHeader())
-       fmt.Fprintln(os.Stderr, "nncp-rm -- remove packet\n")
+       fmt.Fprintf(os.Stderr, "nncp-rm -- remove packet\n\n")
        fmt.Fprintf(os.Stderr, "Usage: %s [options] -tmp\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] -lock\n", os.Args[0])
-       fmt.Fprintf(os.Stderr, "       %s [options] -node NODE [-rx] [-tx] [-part] [-seen]\n", os.Args[0])
+       fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -part\n", os.Args[0])
+       fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -seen\n", os.Args[0])
+       fmt.Fprintf(os.Stderr, "       %s [options] -node NODE {-rx|-tx}\n", os.Args[0])
        fmt.Fprintf(os.Stderr, "       %s [options] -node NODE -pkt PKT\n", os.Args[0])
        fmt.Fprintln(os.Stderr, "Options:")
        flag.PrintDefaults()
@@ -79,7 +80,11 @@ func main() {
                        if err != nil {
                                return err
                        }
-                       return os.Remove(info.Name())
+                       if info.IsDir() {
+                               return nil
+                       }
+                       ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                       return os.Remove(path)
                })
                if err != nil {
                        log.Fatalln("Error during walking:", err)
@@ -91,8 +96,12 @@ func main() {
                        if err != nil {
                                return err
                        }
+                       if info.IsDir() {
+                               return nil
+                       }
                        if strings.HasSuffix(info.Name(), ".lock") {
-                               return os.Remove(info.Name())
+                               ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                               return os.Remove(path)
                        }
                        return nil
                })
@@ -114,19 +123,32 @@ func main() {
                        if err != nil {
                                return err
                        }
+                       if info.IsDir() {
+                               return nil
+                       }
                        if *doSeen && strings.HasSuffix(info.Name(), nncp.SeenSuffix) {
-                               return os.Remove(info.Name())
+                               ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                               return os.Remove(path)
                        }
                        if *doPart && strings.HasSuffix(info.Name(), nncp.PartSuffix) {
-                               return os.Remove(info.Name())
+                               ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                               return os.Remove(path)
+                       }
+                       if *pktRaw != "" && filepath.Base(info.Name()) == *pktRaw {
+                               ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                               return os.Remove(path)
                        }
-                       if *pktRaw == "" || filepath.Base(info.Name()) == *pktRaw {
-                               return os.Remove(info.Name())
+                       if !*doSeen &&
+                               !*doPart &&
+                               (*doRx || *doTx) &&
+                               ((*doRx && xx == nncp.TRx) || (*doTx && xx == nncp.TTx)) {
+                               ctx.LogI("nncp-rm", nncp.SDS{"file": path}, "")
+                               return os.Remove(path)
                        }
                        return nil
                })
        }
-       if *pktRaw != "" || *doRx {
+       if *pktRaw != "" || *doRx || *doSeen || *doPart {
                if err = remove(nncp.TRx); err != nil {
                        log.Fatalln("Can not remove:", err)
                }