|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | + "go.bryk.io/pkg/cli" |
| 14 | + "go.bryk.io/pkg/errors" |
| 15 | + xlog "go.bryk.io/pkg/log" |
| 16 | +) |
| 17 | + |
| 18 | +var shredCmd = &cobra.Command{ |
| 19 | + Use: "shred", |
| 20 | + Aliases: []string{"del", "rm"}, |
| 21 | + Example: "tred shred -ra [INPUT]", |
| 22 | + Short: "Securely delete files/directories while preventing contents recovery", |
| 23 | + RunE: runShred, |
| 24 | +} |
| 25 | + |
| 26 | +func init() { |
| 27 | + params := []cli.Param{ |
| 28 | + { |
| 29 | + Name: "recursive", |
| 30 | + Usage: "recursively process directories", |
| 31 | + FlagKey: "shred.recursive", |
| 32 | + ByDefault: false, |
| 33 | + Short: "r", |
| 34 | + }, |
| 35 | + { |
| 36 | + Name: "all", |
| 37 | + Usage: "include hidden files", |
| 38 | + FlagKey: "shred.all", |
| 39 | + ByDefault: false, |
| 40 | + Short: "a", |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "workers", |
| 44 | + Usage: "number or workers to run for parallel processing", |
| 45 | + FlagKey: "shred.workers", |
| 46 | + ByDefault: runtime.NumCPU(), |
| 47 | + Short: "w", |
| 48 | + }, |
| 49 | + { |
| 50 | + Name: "silent", |
| 51 | + Usage: "suppress all output", |
| 52 | + FlagKey: "shred.silent", |
| 53 | + ByDefault: false, |
| 54 | + Short: "s", |
| 55 | + }, |
| 56 | + } |
| 57 | + if err := cli.SetupCommandParams(shredCmd, params, viper.GetViper()); err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + rootCmd.AddCommand(shredCmd) |
| 61 | +} |
| 62 | + |
| 63 | +func runShred(_ *cobra.Command, args []string) error { |
| 64 | + log := getLogger(viper.GetBool("shred.silent")) |
| 65 | + |
| 66 | + // Get input |
| 67 | + if len(args) == 0 { |
| 68 | + log.Fatal("missing required input") |
| 69 | + return errors.New("missing required input") |
| 70 | + } |
| 71 | + |
| 72 | + // Get input absolute path |
| 73 | + input, err := filepath.Abs(args[0]) |
| 74 | + if err != nil { |
| 75 | + log.WithField("error", err).Fatal("invalid source provided") |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + // Get temporary encryption key |
| 80 | + key := make([]byte, 64) |
| 81 | + if _, err := rand.Read(key); err != nil { |
| 82 | + return errors.Wrap(err, "generate encryption key") |
| 83 | + } |
| 84 | + |
| 85 | + // Start tred workers pool |
| 86 | + wp, err := newPool(viper.GetInt("shred.workers"), key, "chacha", log) |
| 87 | + if err != nil { |
| 88 | + log.WithField("error", err).Fatal("could not initialize TRED workers") |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + // Process input |
| 93 | + cleanUpDirs := []string{} |
| 94 | + start := time.Now() |
| 95 | + if isDir(input) { |
| 96 | + err = filepath.Walk(input, func(f string, i os.FileInfo, err error) error { |
| 97 | + // Unexpected error walking the directory |
| 98 | + if err != nil { |
| 99 | + log.WithFields(xlog.Fields{ |
| 100 | + "location": f, |
| 101 | + "error": err, |
| 102 | + }).Warning("failed to traverse location") |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + // Ignore hidden files |
| 107 | + if strings.HasPrefix(filepath.Base(f), ".") && !viper.GetBool("shred.all") { |
| 108 | + log.WithFields(xlog.Fields{ |
| 109 | + "location": f, |
| 110 | + }).Debug("ignoring hidden file") |
| 111 | + return nil |
| 112 | + } |
| 113 | + |
| 114 | + // Don't go into subdirectories if not required |
| 115 | + if i.IsDir() && !viper.GetBool("shred.recursive") { |
| 116 | + log.WithFields(xlog.Fields{ |
| 117 | + "location": f, |
| 118 | + }).Debug("ignoring directory on non-recursive run") |
| 119 | + return filepath.SkipDir |
| 120 | + } |
| 121 | + |
| 122 | + // Ignore subdir markers |
| 123 | + if i.IsDir() { |
| 124 | + cleanUpDirs = append(cleanUpDirs, f) |
| 125 | + return nil |
| 126 | + } |
| 127 | + |
| 128 | + // Add job to processing pool |
| 129 | + wp.add(job{file: f, showBar: false, shred: true}) |
| 130 | + return nil |
| 131 | + }) |
| 132 | + cleanUpDirs = append(cleanUpDirs, input) |
| 133 | + } else { |
| 134 | + // Process single file |
| 135 | + wp.add(job{file: input, showBar: true, shred: true}) |
| 136 | + } |
| 137 | + |
| 138 | + // Wait for operations to complete |
| 139 | + wp.done() |
| 140 | + for _, td := range cleanUpDirs { |
| 141 | + _ = os.Remove(td) // remove empty directories, ignore errors |
| 142 | + } |
| 143 | + log.WithFields(xlog.Fields{ |
| 144 | + "time": time.Since(start), |
| 145 | + "files": wp.count, |
| 146 | + }).Info("operation completed") |
| 147 | + return err |
| 148 | +} |
0 commit comments