Fully implement bricker
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
bricker.exe
|
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
||||
module bricker
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
|
||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
|
104
main.go
104
main.go
@@ -1,34 +1,114 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/rand"
|
||||
)
|
||||
|
||||
var Error *log.Logger
|
||||
var Warning *log.Logger
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||
logFile, err := os.Create("main.log")
|
||||
if err != nil {
|
||||
log.Printf("Error creating log file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logger := io.MultiWriter(os.Stdout, logFile)
|
||||
logger := io.MultiWriter(os.Stdout)
|
||||
log.SetOutput(logger)
|
||||
|
||||
Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
|
||||
Error = log.New(io.MultiWriter(os.Stderr, os.Stdout),
|
||||
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
|
||||
log.Lmicroseconds|log.Lshortfile)
|
||||
Warning = log.New(io.MultiWriter(logFile, os.Stdout),
|
||||
Warning = log.New(io.MultiWriter(os.Stdout),
|
||||
fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
|
||||
log.Lmicroseconds|log.Lshortfile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Println("Hello, World!")
|
||||
Warning.Println("Hello, World!")
|
||||
Error.Println("Hello, World!")
|
||||
}
|
||||
percent := flag.Int("p", 20, "Percent to mangle")
|
||||
w := flag.Int("w", 8, "Workers")
|
||||
flag.Parse()
|
||||
|
||||
workers := make(chan struct{}, *w)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
log.Printf("Corrupting %d%% of each input file", *percent)
|
||||
log.Printf("Using %d workers", *w)
|
||||
|
||||
for _, file := range flag.Args() {
|
||||
wg.Add(1)
|
||||
workers <- struct{}{}
|
||||
|
||||
go func(file string) {
|
||||
defer wg.Done()
|
||||
defer func() { <-workers }()
|
||||
|
||||
log.Printf("Corrupting %s", file)
|
||||
filehandle, err := os.OpenFile(file, os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
Error.Printf("Error opening %v: %v", file, err)
|
||||
return
|
||||
}
|
||||
defer filehandle.Close()
|
||||
|
||||
// data, err := io.ReadAll(filehandle)
|
||||
// if err != nil {
|
||||
// Error.Printf("Error reading %v: %v", file, err)
|
||||
// return
|
||||
// }
|
||||
// log.Printf("Read %d bytes from %s", len(data), file)
|
||||
// filehandle.Seek(0, io.SeekStart)
|
||||
|
||||
info, err := os.Stat(file)
|
||||
if err != nil {
|
||||
Error.Printf("Error getting file info for %v: %v", file, err)
|
||||
return
|
||||
}
|
||||
filesize := info.Size()
|
||||
log.Printf("File size is %d bytes", filesize)
|
||||
|
||||
toCorrupt := int64(float32(filesize) * (float32(*percent) / 100))
|
||||
indicesToCorrupt := selectRandomIndices(filesize, toCorrupt)
|
||||
log.Printf("Corrupting %d bytes in %s", toCorrupt, file)
|
||||
log.Printf("%#v", indicesToCorrupt)
|
||||
|
||||
for _, index := range indicesToCorrupt {
|
||||
rbyte := byte(rand.Intn(256))
|
||||
filehandle.Seek(int64(index), io.SeekStart)
|
||||
filehandle.Write([]byte{rbyte})
|
||||
}
|
||||
|
||||
log.Printf("Corrupted %d bytes in %s", toCorrupt, file)
|
||||
|
||||
// n, err := filehandle.Write(data)
|
||||
// if err != nil {
|
||||
// Error.Printf("Error writing %v: %v", file, err)
|
||||
// return
|
||||
// }
|
||||
// log.Printf("Wrote %d bytes to %s", n, file)
|
||||
}(file)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func selectRandomIndices(size, n int64) []int64 {
|
||||
indices := make([]int64, n)
|
||||
for i := range indices {
|
||||
indices[i] = int64(i)
|
||||
}
|
||||
|
||||
rand.Seed(uint64(time.Now().UnixNano()))
|
||||
|
||||
for i := n; i < size; i++ {
|
||||
r := rand.Int63n(i + 1)
|
||||
if r < n {
|
||||
indices[r] = i
|
||||
}
|
||||
}
|
||||
|
||||
return indices
|
||||
}
|
||||
|
Reference in New Issue
Block a user