Create backup file before bricking
Bypass with -nosafe
This commit is contained in:
31
main.go
31
main.go
@@ -6,6 +6,8 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -31,6 +33,7 @@ func init() {
|
||||
|
||||
func main() {
|
||||
percent := flag.Int("p", 20, "Percent to mangle")
|
||||
unsafe := flag.Bool("unsafe", false, "Unsafe mode")
|
||||
w := flag.Int("w", 8, "Workers")
|
||||
flag.Parse()
|
||||
|
||||
@@ -48,6 +51,9 @@ func main() {
|
||||
defer wg.Done()
|
||||
defer func() { <-workers }()
|
||||
|
||||
file = filepath.Clean(file)
|
||||
file = filepath.ToSlash(file)
|
||||
|
||||
log.Printf("Corrupting %s", file)
|
||||
filehandle, err := os.OpenFile(file, os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
@@ -75,6 +81,14 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if !*unsafe {
|
||||
err := backup(file)
|
||||
if err != nil {
|
||||
Error.Printf("Error backing up %v: %v", file, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
filesize := info.Size()
|
||||
log.Printf("File size is %d bytes", filesize)
|
||||
|
||||
@@ -119,3 +133,20 @@ func selectRandomIndices(size, n int64) []int64 {
|
||||
|
||||
return indices
|
||||
}
|
||||
|
||||
func backup(file string) error {
|
||||
backupfile := file + ".bak"
|
||||
|
||||
_, err := os.Stat(backupfile)
|
||||
if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("backup file %s already exists %s.bak", backupfile)
|
||||
}
|
||||
|
||||
cmd := exec.Command("cp", "-a", file, backupfile)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed creating backup file with return %d and out %s", err, string(out))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user