-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumps.go
61 lines (58 loc) · 1.57 KB
/
dumps.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
package main
import (
"os"
"fmt"
)
func checkIfFile(fileRoute string) bool{
/*
* This function is used to return true if a file
* is found inside a directory.
*
* Parameters:
* fileRoute -> The file to check
*
* Returns:
* true if found
* */
var val bool
_, err := os.Stat(fileRoute)
if err == nil {
// The file actually exists
val = true
} else if os.IsNotExist(err) {
// THe file doesn't exists
val = false
} else {
// Schrodinger: file may or may not exist. See err for details.
fmt.Println("[FATAL ERROR]: Schrodinger location")
}
return val
}
func dump(fileRoute string, cipherBytes []byte) {
/*
* This function is used to decode a given cipher in a byte(s)
* format to a string, so it can be dumped to a file
*
* Parameters:
* fileRoute -> The file route to dump the information
* cipherBytes -> The cipher to be loaded to string
* */
if checkIfFile(fileRoute) {
// File not empty, so we should remove the file to then write it again
fmt.Printf("[RECREATING]: %s", fileRoute)
os.Remove(fileRoute)
}
// Once we have removed the file, we should write all the cipher bytes to the file itself
file, err := os.Create(fileRoute)
if err != nil {
fmt.Println("[FATAL ERROR]: Can't create a new file at: ", fileRoute)
os.Exit(1)
}
nb, err := file.Write(cipherBytes)
defer file.Close()
if err != nil {
fmt.Println("[FATAL ERROR]: Can't write the file using the os module")
os.Exit(1)
}
fmt.Printf("\n[DUMPS]: Encrl dumped %d bytes\n", nb)
}