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"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -31,6 +33,7 @@ func init() {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
percent := flag.Int("p", 20, "Percent to mangle")
|
percent := flag.Int("p", 20, "Percent to mangle")
|
||||||
|
unsafe := flag.Bool("unsafe", false, "Unsafe mode")
|
||||||
w := flag.Int("w", 8, "Workers")
|
w := flag.Int("w", 8, "Workers")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@@ -48,6 +51,9 @@ func main() {
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer func() { <-workers }()
|
defer func() { <-workers }()
|
||||||
|
|
||||||
|
file = filepath.Clean(file)
|
||||||
|
file = filepath.ToSlash(file)
|
||||||
|
|
||||||
log.Printf("Corrupting %s", file)
|
log.Printf("Corrupting %s", file)
|
||||||
filehandle, err := os.OpenFile(file, os.O_RDWR, 0644)
|
filehandle, err := os.OpenFile(file, os.O_RDWR, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -75,6 +81,14 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !*unsafe {
|
||||||
|
err := backup(file)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error backing up %v: %v", file, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
filesize := info.Size()
|
filesize := info.Size()
|
||||||
log.Printf("File size is %d bytes", filesize)
|
log.Printf("File size is %d bytes", filesize)
|
||||||
|
|
||||||
@@ -119,3 +133,20 @@ func selectRandomIndices(size, n int64) []int64 {
|
|||||||
|
|
||||||
return indices
|
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