-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhackbackup.go
206 lines (171 loc) · 3.96 KB
/
hackbackup.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"crypto/md5"
"fmt"
"log"
"os"
"time"
"gopkg.in/yaml.v2"
)
type HackFile struct {
name string
path string
modified time.Time
size int
hash string
}
type ConfigLoc struct {
Dir string
}
type Config struct {
Desc string
Server struct {
Name string
Dir string
}
Locations []ConfigLoc
}
func testmd5() {
buff := []byte("abcdefg")
//printhex(checksum)
fmt.Printf("%x", md5.Sum(buff))
}
func printbytes(data []byte, length int) {
for i := 0; i <= length; i++ {
//convert and print byte to ascii
fmt.Print(fmt.Sprintf("%c", data[i]))
}
}
func loadyml(filename string) (Config, error) {
var config Config
dir_err := os.Chdir("C:\\Users\\Felix\\programing\\go\\src\\github.com\\user\\hackbackup\\")
if dir_err != nil {
fmt.Println(dir_err)
}
fileinfo, err := os.Stat(filename)
if err != nil {
fmt.Println("error couldnt get file info", filename)
return config, err
}
file, err := os.Open(filename)
if err != nil {
fmt.Println("error couldnt open file", filename)
return config, err
}
buff := make([]byte, fileinfo.Size())
var n int
n, err = file.Read(buff)
if err != nil {
fmt.Println("error reading the file")
}
file.Close()
if n > 1 {
err := yaml.Unmarshal(buff, &config)
if err != nil {
fmt.Println("couldnt do the yml", err)
}
}
//fmt.Print(config)
return config, nil
}
// Return servername and the directory
func get_config(filename string) (string, string) {
var n int
dir_err := os.Chdir("C:\\Users\\Felix\\programing\\go\\src\\github.com\\user\\hackbackup\\")
if dir_err != nil {
fileinfo, fileinfoerr := os.Stat(filename)
if fileinfoerr != nil {
fmt.Println("error couldnt get file info", filename)
return "", ""
}
file, err := os.Open("hack.yml")
fmt.Println(err)
buff := make([]byte, fileinfo.Size())
file.Close()
n, err = file.Read(buff)
for i := 0; i < n; i++ {
if buff[i] == 66 {
fmt.Println("error")
}
}
}
return "1.2.3.4", "C:\\User\\Felix"
}
func get_files(dir string) ([]HackFile , error) {
result := make([]HackFile, 0)
var hfile HackFile
// look at a dir and get data
dirdata, err := os.Open("C:\\Users\\felix")
if err != nil {
fmt.Println(err)
fmt.Println("couldnt read directory")
}
ls, err := dirdata.Readdir(0)
if err != nil {
fmt.Println("couldnt read directory listing")
}
for i := 0; i < len(ls); i++ {
// fmt.Println(ls[i].Name())
// fmt.Println(ls[i].ModTime())
hfile.name = ls[i].Name()
hfile.modified = ls[i].ModTime()
result = append(result, hfile)
}
// find all directories in this dir
//fmt.Println(os.ModeDir)
for i := 0; i < len(ls); i++ {
if ls[i].Mode().IsDir() {
//fmt.Println(ls[i].Name(), "%s is a directory ")
}
}
hfile.name = "filename"
hfile.modified = time.Now()
result = append(result, hfile)
return result, nil
}
func get_recent_backup() ([]HackFile, error) {
return make([]HackFile, 1), nil
}
func main() {
// open a yml file!
//server, dir := get_config("hack.yml")
//fmt.Println(server, dir)
// look at that dir and list file names and dates
//printbytes(buff, n)
config, err := loadyml("hack.yml")
if err != nil {
log.Println("Couldnt load config")
log.Println("err")
panic("Couldnt load config")
}
log.Printf("config = %+v\n", config)
file_list, err := get_files(config.Server.Dir)
if err != nil {
panic("counldnt get file list")
}
// open current file list
backup_set, err := get_recent_backup()
if err != nil {
panic("couldnt get recent backup")
}
compare := testable_make_list(backup_set)
compare_list := testable_make_list(file_list)
result, err := compare_string_file_elements(compare, compare_list)
if err != nil {
panic(err)
}
fmt.Printf("result = %+v\n", result)
// test connect to server
// hash paths and names and sizes
// (encrypt) send files
// save backupset to disk
}
func server() {
// initalise
// open sockets
// wait
// receieve backupset
// detect full disk
// loop recieve files
// calculate and update backupsets
}