From 9be2f6bf504faee68914d14fef0f8fce46a0a635 Mon Sep 17 00:00:00 2001
From: Evan Su <48808396+HACKERALERT@users.noreply.github.com>
Date: Tue, 28 Sep 2021 20:35:06 -0400
Subject: [PATCH] Revert "Merge pull request #44 from Zacho2/simplify-build"
This reverts commit 2934c6b77d8ebc7827bd7d6de8819cae1a836a5e, reversing
changes made to b2d92146c18bddf86517dee6920299ed2b65c155.
---
src/Picocrypt.go | 159 ++++++++++++++++++++++++++++++++++++++++++---
src/README.md | 15 +++--
src/cmd_unix.go | 82 -----------------------
src/cmd_windows.go | 79 ----------------------
4 files changed, 159 insertions(+), 176 deletions(-)
delete mode 100644 src/cmd_unix.go
delete mode 100644 src/cmd_windows.go
diff --git a/src/Picocrypt.go b/src/Picocrypt.go
index 016ccbf..5b6e34f 100644
--- a/src/Picocrypt.go
+++ b/src/Picocrypt.go
@@ -25,16 +25,20 @@ import (
"image/color"
"image/png"
"io"
+ "io/ioutil"
"math"
"math/big"
"net/http"
"os"
+ "os/exec"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"strings"
+ "sync"
+ "syscall"
"time"
// Cryptography
@@ -73,6 +77,9 @@ var icon []byte
//go:embed font.ttf
var font []byte
+//go:embed sdelete64.exe
+var sdelete64bytes []byte
+
//go:embed strings.json
var localeBytes []byte
@@ -100,6 +107,7 @@ var mode string
var working bool
var recombine bool
var fill float32 = -0.0000001
+var sdelete64path string
// Three variables store the input files
var onlyFiles []string
@@ -949,9 +957,7 @@ func onDrop(names []string) {
return
}
if tab == 2 {
- namesCopy := make([]string, len(names))
- copy(namesCopy, names)
- go doShred(namesCopy, true)
+ go shred(names, true)
return
}
@@ -1888,7 +1894,7 @@ func work() {
if shredTemp {
progressInfo = ""
popupStatus = s("Shredding temporary files...")
- doShred([]string{inputFile + ".pcv"}, false)
+ shred([]string{inputFile + ".pcv"}, false)
} else {
os.Remove(inputFile + ".pcv")
}
@@ -1905,7 +1911,7 @@ func work() {
progressInfo = ""
popupStatus = s("Shredding temporary files...")
giu.Update()
- doShred([]string{inputFile}, false)
+ shred([]string{inputFile}, false)
} else {
os.Remove(inputFile)
}
@@ -2074,7 +2080,7 @@ func generateChecksums(file string) {
}
// Recursively shred all file(s) and folder(s) passed in as 'names'
-func doShred(names []string, separate bool) {
+func shred(names []string, separate bool) {
stopShredding = false
shredTotal = 0
shredDone = 0
@@ -2100,7 +2106,133 @@ func doShred(names []string, separate bool) {
for _, name := range names {
shredding = name
- shred(1, false, name, &shredding)
+
+ // Linux and macOS need a command with similar syntax and usage, so they're combined
+ if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
+ stat, _ := os.Stat(name)
+ if stat.IsDir() {
+ var coming []string
+
+ // Walk the folder recursively
+ filepath.Walk(name, func(path string, _ os.FileInfo, err error) error {
+ if err != nil {
+ return nil
+ }
+ if stopShredding {
+ return nil
+ }
+ stat, _ := os.Stat(path)
+ if !stat.IsDir() {
+ if len(coming) == 128 {
+ // Use a WaitGroup to parallelize shredding
+ var wg sync.WaitGroup
+ for i, j := range coming {
+ wg.Add(1)
+ go func(wg *sync.WaitGroup, id int, j string) {
+ defer wg.Done()
+ shredding = j
+ var cmd *exec.Cmd
+ if runtime.GOOS == "linux" {
+ cmd = exec.Command("shred", "-ufvz", "-n", strconv.Itoa(int(shredPasses)), j)
+ } else {
+ cmd = exec.Command("rm", "-rfP", j)
+ }
+ cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
+ cmd.Run()
+ shredDone++
+ shredUpdate(separate)
+ giu.Update()
+ }(&wg, i, j)
+ }
+ wg.Wait()
+ coming = nil
+ } else {
+ coming = append(coming, path)
+ }
+ }
+ return nil
+ })
+ for _, i := range coming {
+ if stopShredding {
+ break
+ }
+ go func(i string) {
+ shredding = i
+ var cmd *exec.Cmd
+ if runtime.GOOS == "linux" {
+ cmd = exec.Command("shred", "-ufvz", "-n", strconv.Itoa(int(shredPasses)), i)
+ } else {
+ cmd = exec.Command("rm", "-rfP", i)
+ }
+ cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
+ cmd.Run()
+ shredDone++
+ shredUpdate(separate)
+ giu.Update()
+ }(i)
+ }
+ if !stopShredding {
+ os.RemoveAll(name)
+ }
+ } else { // The path is a file, not a directory, so just shred it
+ shredding = name
+ var cmd *exec.Cmd
+ if runtime.GOOS == "linux" {
+ cmd = exec.Command("shred", "-ufvz", "-n", strconv.Itoa(int(shredPasses)), name)
+ } else {
+ cmd = exec.Command("rm", "-rfP", name)
+ }
+ cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
+ cmd.Run()
+ shredDone++
+ shredUpdate(separate)
+ }
+ } else if runtime.GOOS == "windows" {
+ stat, _ := os.Stat(name)
+ if stat.IsDir() {
+ // Walk the folder recursively
+ filepath.Walk(name, func(path string, _ os.FileInfo, err error) error {
+ if err != nil {
+ return nil
+ }
+ stat, _ := os.Stat(path)
+ if stat.IsDir() {
+ if stopShredding {
+ return nil
+ }
+
+ t := 0
+ files, _ := ioutil.ReadDir(path)
+ for _, f := range files {
+ if !f.IsDir() {
+ t++
+ }
+ }
+ shredDone += float32(t)
+ shredUpdate(separate)
+ shredding = strings.ReplaceAll(path, "\\", "/") + "/*"
+ cmd := exec.Command(sdelete64path, "*", "-p", strconv.Itoa(int(shredPasses)))
+ cmd.Dir = path
+ cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
+ cmd.Run()
+ giu.Update()
+ }
+ return nil
+ })
+
+ if !stopShredding {
+ // sdelete64 doesn't delete the empty folder, so I'll do it manually
+ os.RemoveAll(name)
+ }
+ } else {
+ shredding = name
+ cmd := exec.Command(sdelete64path, name, "-p", strconv.Itoa(int(shredPasses)))
+ cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
+ cmd.Run()
+ shredDone++
+ shredUpdate(separate)
+ }
+ }
giu.Update()
if stopShredding {
return
@@ -2289,8 +2421,14 @@ func main() {
}
}
- cleanup := initializeShred()
- defer cleanup()
+ // Create a temporary file to store sdelete64.exe
+ sdelete64, _ := os.CreateTemp("", "sdelete64.*.exe")
+ sdelete64path = sdelete64.Name()
+ sdelete64.Write(sdelete64bytes)
+ sdelete64.Close()
+ cmd := exec.Command(sdelete64path, "/accepteula")
+ cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
+ cmd.Run()
// Set a universal font
giu.SetDefaultFontFromBytes(font, 18)
@@ -2330,4 +2468,7 @@ func main() {
// Start the UI
window.Run(draw)
+
+ // Window closed, clean up
+ os.Remove(sdelete64path)
}
diff --git a/src/README.md b/src/README.md
index b957947..d98d759 100644
--- a/src/README.md
+++ b/src/README.md
@@ -18,11 +18,14 @@ If you don't have Go installed, download the corresponding installer for Go from
# 3. Get the Source Files
Download the source files as a zip from the homepage or `git clone` this repository.
-# 4. Build From Source
-Finally, build Picocrypt from source:
-- Windows: go build -ldflags "-s -w -H=windowsgui -extldflags=-static"
-- macOS: go build -ldflags "-s -w"
-- Linux: go build -ldflags "-s -w"
+# 4. If You're Not on Windows...
+Windows requires a couple of extra lines to hide the command prompt window that shows when shredding a file. If you're not on Windows, however, you'll need to delete all occurrences of this line: `cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow:true}`. You'll also need to remove the import of `syscall`.
-# 5. Done!
+# 5. Build From Source
+Finally, build Picocrypt from source:
+- Windows: go build -ldflags "-s -w -H=windowsgui -extldflags=-static" Picocrypt.go
+- macOS: go build -ldflags "-s -w" Picocrypt.go
+- Linux: go build -ldflags "-s -w" Picocrypt.go
+
+# 6. Done!
You should now see a compiled executable (`Picocrypt.exe`/`Picocrypt`) in your directory. You can run it by double-clicking or executing it in your terminal. That wasn't too hard, right? Enjoy!
diff --git a/src/cmd_unix.go b/src/cmd_unix.go
deleted file mode 100644
index 2d97af5..0000000
--- a/src/cmd_unix.go
+++ /dev/null
@@ -1,82 +0,0 @@
-// +build linux darwin
-
-package main
-
-import (
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strconv"
- "sync"
-
- "github.com/AllenDang/giu"
-)
-
-func shred(passes int, separate bool, name string, shredding *string) {
- stat, _ := os.Stat(name)
- if stat.IsDir() {
- var coming []string
-
- // Walk the folder recursively
- filepath.Walk(name, func(path string, _ os.FileInfo, err error) error {
- if err != nil {
- return nil
- }
- if stopShredding {
- return nil
- }
- stat, _ := os.Stat(path)
- if !stat.IsDir() {
- if len(coming) == 128 {
- // Use a WaitGroup to parallelize shredding
- var wg sync.WaitGroup
- for i, j := range coming {
- wg.Add(1)
- go func(wg *sync.WaitGroup, id int, j string) {
- defer wg.Done()
- runShredCommand(j, separate)
- giu.Update()
- }(&wg, i, j)
- }
- wg.Wait()
- coming = nil
- } else {
- coming = append(coming, path)
- }
- }
- return nil
- })
- for _, i := range coming {
- if stopShredding {
- break
- }
- go func(i string) {
- runShredCommand(i, separate)
- giu.Update()
- }(i)
- }
- if !stopShredding {
- os.RemoveAll(name)
- }
- } else { // The path is a file, not a directory, so just shred it
- runShredCommand(name, separate)
- }
-}
-
-func runShredCommand(name string, separate bool) {
- shredding = name
- var cmd *exec.Cmd
- if runtime.GOOS == "linux" {
- cmd = exec.Command("shred", "-ufvz", "-n", strconv.Itoa(int(shredPasses)), name)
- } else {
- cmd = exec.Command("rm", "-rfP", name)
- }
- cmd.Run()
- shredDone++
- shredUpdate(separate)
-}
-
-func initializeShred() func() {
- return func() {}
-}
diff --git a/src/cmd_windows.go b/src/cmd_windows.go
deleted file mode 100644
index ada18d4..0000000
--- a/src/cmd_windows.go
+++ /dev/null
@@ -1,79 +0,0 @@
-// +build windows
-
-package main
-
-import (
- "io/ioutil"
- "os"
- "os/exec"
- "path/filepath"
- "strconv"
- "strings"
- "syscall"
-
- "github.com/AllenDang/giu"
-)
-
-//go:embed sdelete64.exe
-var sdelete64bytes []byte
-
-var sdelete64path string
-
-func shred(passes int, separate bool, name string, shredding *string) {
- stat, _ := os.Stat(name)
- if stat.IsDir() {
- // Walk the folder recursively
- filepath.Walk(name, func(path string, _ os.FileInfo, err error) error {
- if err != nil {
- return nil
- }
- stat, _ := os.Stat(path)
- if stat.IsDir() {
- if stopShredding {
- return nil
- }
-
- t := 0
- files, _ := ioutil.ReadDir(path)
- for _, f := range files {
- if !f.IsDir() {
- t++
- }
- }
- shredDone += float32(t)
- shredUpdate(separate)
- shredding = strings.ReplaceAll(path, "\\", "/") + "/*"
- cmd := exec.Command(sdelete64path, "*", "-p", strconv.Itoa(passes))
- cmd.Dir = path
- cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
- cmd.Run()
- giu.Update()
- }
- return nil
- })
-
- if !stopShredding {
- // sdelete64 doesn't delete the empty folder, so I'll do it manually
- os.RemoveAll(name)
- }
- } else {
- shredding = name
- cmd := exec.Command(sdelete64path, "*", "-p", strconv.Itoa(passes))
- cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
- cmd.Run()
- shredDone++
- shredUpdate(separate)
- }
-}
-
-func initializeShred() func() {
- // Create a temporary file to store sdelete64.exe
- sdelete64, _ := os.CreateTemp("", "sdelete64.*.exe")
- sdelete64path = sdelete64.Name()
- sdelete64.Write(sdelete64bytes)
- sdelete64.Close()
- cmd := exec.Command(s64deletepath, "/accepteula")
- cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
- cmd.Run()
- return func() { os.Remove(sdelete64path) }
-}