-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootCmd.go
138 lines (117 loc) · 2.64 KB
/
rootCmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cmd
import (
"fmt"
"io"
"io/fs"
"log"
"math/rand"
"os"
"time"
"github.com/cheggaaa/pb"
"github.com/spf13/cobra"
)
const (
stepSize int = 512 //bytes
)
var (
targetDrive string
randomizeDelete bool
deleteCycles int
shutdown bool
)
func init() {
rootCmd.Flags().StringVarP(&targetDrive, "drive", "d", "", "drive to wipe")
rootCmd.Flags().BoolVarP(&randomizeDelete, "randomize", "r", false, "enable randomization of delete output")
rootCmd.Flags().IntVarP(&deleteCycles, "cycles", "c", 3, "number of delete cycles")
rootCmd.MarkFlagRequired("drive")
}
var rootCmd = &cobra.Command{
Use: "diskwipe",
Short: "Wipe a disk provided a directory",
Run: func(cmd *cobra.Command, args []string) {
config, err := parseFlags()
if err != nil {
panic(err)
}
r := newDeleteRunner(config)
if err := r.run(); err != nil {
panic(err)
}
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
func parseFlags() (*config, error) {
info, err := os.Stat(targetDrive)
if err != nil {
log.Println(err)
return nil, fmt.Errorf("invalid target drive: %s", targetDrive)
}
deviceSize := info.Size()
if (info.Mode() & os.ModeDevice) != 0 {
f, err := os.Open(targetDrive)
deviceSize, err = f.Seek(0, io.SeekEnd)
if err != nil {
return nil, fmt.Errorf("failed to get device size")
}
} else {
return nil, fmt.Errorf("device not found at: %s", targetDrive)
}
if deleteCycles <= 0 {
return nil, fmt.Errorf("cycles must be greater than 1")
}
return &config{
Target: targetDrive,
TargetInfo: info,
TargetSize: deviceSize,
Randomize: randomizeDelete,
Cycles: deleteCycles,
Shutdown: shutdown,
}, nil
}
type config struct {
Target string
TargetInfo fs.FileInfo
TargetSize int64
Randomize bool
Cycles int
Shutdown bool
}
type deleteRunner struct {
Config *config
}
func newDeleteRunner(c *config) *deleteRunner {
return &deleteRunner{Config: c}
}
func (d *deleteRunner) run() error {
f, err := os.OpenFile(d.Config.Target, os.O_WRONLY, fs.ModeDevice)
if err != nil {
return err
}
offset := int64(0)
buffer := make([]byte, stepSize)
s := rand.NewSource(time.Now().UnixNano())
rng := rand.New(s)
totalProgress := (int(d.Config.TargetSize) * d.Config.Cycles)
bar := pb.StartNew(totalProgress)
bar.SetUnits(pb.U_BYTES)
bar.SetRefreshRate(time.Duration(time.Second * 5))
for i := 0; i < d.Config.Cycles; i++ {
for offset < d.Config.TargetSize {
if d.Config.Randomize {
rng.Read(buffer)
}
n, err := f.WriteAt(buffer, int64(offset))
if err != nil {
return err
}
offset += int64(n)
bar.Add(n)
}
}
bar.Finish()
return nil
}