-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileServer.go
651 lines (545 loc) · 16.5 KB
/
fileServer.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* ThreadedIPEchoServer
*/
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"path"
"runtime"
"strconv"
"syscall"
"time"
)
const msgBufferLength uint32 = 65546
// random number seeding for fileId creation
var r = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
type File struct {
FileId uint32
FileSize uint64
RelativePathLength uint16
RelativePath string
AbsolutePath string
Chunks []Chunk
}
type Chunk struct {
Start uint64
End uint64
Length uint64
}
var files = make(chan map[string]File, 1)
var filesLoaded = make(chan bool, 1)
type Request struct {
cmd uint8
msgId uint32
length uint32
payload []byte
}
type User struct {
LOGGED_IN bool
}
func main() {
service := ":1200"
listener, err := net.Listen("tcp", service)
checkError(err)
filesLoaded <- false
files <- make(map[string]File)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleClient(conn)
}
}
func handleClient(conn net.Conn) {
defer conn.Close()
var buf [msgBufferLength]byte
var bufRes []byte
user := new(User)
user.LOGGED_IN = false
for {
n, err := conn.Read(buf[0:])
if err != nil {
return
}
fmt.Fprintf(os.Stderr, "Received: % x\n", buf[0:n])
request := new(Request)
parserCommon(request, buf, n)
fmt.Fprintf(os.Stderr, "cmd: %d\n", request.cmd)
fmt.Fprintf(os.Stderr, "msgId: %d\n", request.msgId)
fmt.Fprintf(os.Stderr, "payload: % x\n", request.payload)
switch request.cmd {
case 1:
// LOGIN
fmt.Fprintf(os.Stderr, "Login!\n")
var loginSuccess = login(request)
if loginSuccess {
user.LOGGED_IN = true
fmt.Fprintf(os.Stderr, "Logged in!\n")
loadFiles()
bufRes = respondLogin(request, loginSuccess)
} else {
bufRes = respondLogin(request, loginSuccess)
}
sendBuffer(conn, bufRes)
case 2:
// EXIT
fmt.Fprintf(os.Stderr, "Exit!\n")
storeFiles()
bufRes = wrapCommonHeaders(request, []byte{0x12}, []byte{})
sendBuffer(conn, bufRes)
return
case 3:
// GET_STORAGE_DATA
fmt.Fprintf(os.Stderr, "GetStorageData\n")
bufRes = respondGetStorageData(request)
sendBuffer(conn, bufRes)
case 4:
// GET_FILE_IDS
fmt.Fprintf(os.Stderr, "GetFileIds\n")
bufRes = respondGetFileIds(request)
sendBuffer(conn, bufRes)
case 5:
// GET_FILE_CHUNKS
fmt.Fprintf(os.Stderr, "GetFileChunks\n")
bufRes = respondGetFileChunks(request)
sendBuffer(conn, bufRes)
case 6:
// ALLOCATE_FILE
fileId, success := allocateFile(request)
fmt.Fprintf(os.Stderr, "fileId %v success %v\n", fileId, success)
bufRes = respondAllocateFile(request, fileId, success)
sendBuffer(conn, bufRes)
case 7:
// WRITE_FILE_CHUNK
bufRes = respondWriteFileChunk(request)
sendBuffer(conn, bufRes)
case 8:
// READ_FILE_CHUNK
bufRes = respondReadFileChunk(request)
sendBuffer(conn, bufRes)
case 9:
// DELETE_FILE
fmt.Fprintf(os.Stderr, "DeleteFile\n")
fileId, success := deleteFile(request)
fileIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(fileIdBytes, fileId)
if success {
bufRes = wrapCommonHeaders(request, []byte{0x19}, []byte{0x01}, fileIdBytes)
} else {
bufRes = wrapCommonHeaders(request, []byte{0x19}, []byte{0x00}, fileIdBytes)
}
sendBuffer(conn, bufRes)
}
}
}
func storeFiles() {
f := <-files
b, err := json.Marshal(f)
check(err)
fmt.Fprintf(os.Stderr, "filesStruct: %+v\n", f)
fmt.Fprintf(os.Stderr, "filesStruct marshalled: %v\n", b)
err = ioutil.WriteFile("filesDb.json", b, 0777)
check(err)
files <- f
}
func loadFiles() {
// loads files json struct from file
if <-filesLoaded {
// if files have already been loaded
// release lock and return
filesLoaded <- true
return
}
b, err := ioutil.ReadFile("filesDb.json")
if os.IsNotExist(err) {
// if file does not exist create file
_, err := os.Create("filesDb.json")
if err != nil {
panic(err)
}
b = []byte{}
}
<-files
var f map[string]File
json.Unmarshal(b, &f)
if f == nil {
f = make(map[string]File)
}
fmt.Fprintf(os.Stderr, "filesStruct: %+v\n", f)
files <- f
filesLoaded <- true
}
func sendBuffer(conn net.Conn, bufRes []byte) {
fmt.Fprintf(os.Stderr, "bufRes: % x\n", bufRes)
_, err2 := conn.Write(bufRes)
if err2 != nil {
return
}
}
func login(request *Request) bool {
var usernameLength, passwordLength uint8
var username, password uint64
// convert from bytes to uints
binary.Read(bytes.NewReader(request.payload[0:1]), binary.BigEndian, &usernameLength)
binary.Read(bytes.NewReader(request.payload[5:6]), binary.BigEndian, &passwordLength)
// convert ascii bytes to string to uint
username, _ = strconv.ParseUint(string(request.payload[1:5]), 10, 64)
password, _ = strconv.ParseUint(string(request.payload[6:10]), 10, 64)
// fmt.Fprintf(os.Stderr, "username length: %d\n", usernameLength)
// fmt.Fprintf(os.Stderr, "username: %v\n", password)
// fmt.Fprintf(os.Stderr, "password length: %d\n", passwordLength)
// fmt.Fprintf(os.Stderr, "password: %v\n", password)
if username >= 1001 && username <= 9999 && username == password {
return true
}
return false
}
func check(e error) {
if e != nil {
panic(e)
}
}
func respondLogin(request *Request, success bool) []byte {
// make payload
var payload []byte
if success {
payload = []byte{0x01}
} else {
payload = []byte{0x00}
}
// add common headers
response := wrapCommonHeaders(request, []byte{0x11}, payload)
//return
return response
}
func allocateFile(request *Request) (uint32, bool) {
var relativePathLength uint16
var relativePath string
var fileSize uint64
var fileId uint32
// convert from bytes to appropriate types
binary.Read(bytes.NewReader(request.payload[0:2]), binary.BigEndian, &relativePathLength)
relativePath = string(request.payload[2 : 2+relativePathLength])
binary.Read(bytes.NewReader(request.payload[2+relativePathLength:]), binary.BigEndian, &fileSize)
// fmt.Fprintf(os.Stderr, "relative path length: %d\n", relativePathLength)
// fmt.Fprintf(os.Stderr, "relativePath: %v\n", relativePath)
// fmt.Fprintf(os.Stderr, "fileSize: %v\n", fileSize)
var empty = make([]byte, fileSize)
var err error
// create directory in relative path to where program is running
_, currentPath, _, _ := runtime.Caller(1)
basepath := path.Join(path.Dir(currentPath), path.Dir(relativePath))
fmt.Fprintf(os.Stderr, "basepath: %v\n", basepath)
err = os.MkdirAll(basepath, 0777)
check(err)
// write file
absolutePath := path.Join(basepath, path.Base(relativePath))
err = ioutil.WriteFile(absolutePath, empty, 0777)
check(err)
// create file id
fileId = r.Uint32()
file := File{fileId, fileSize, relativePathLength, relativePath, absolutePath, []Chunk{}}
fmt.Println(file)
filesTmp := <-files
filesTmp[strconv.Itoa(int(fileId))] = file
fmt.Println(filesTmp)
files <- filesTmp
return fileId, true
}
func respondAllocateFile(request *Request, fileId uint32, success bool) []byte {
// make payload
var payload []byte
if success {
payload = []byte{0x01}
} else {
payload = []byte{0x00}
}
fileIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(fileIdBytes, fileId)
payload = append(payload, fileIdBytes...)
// add common headers
response := wrapCommonHeaders(request, []byte{0x16}, payload)
//return
return response
}
func respondGetStorageData(request *Request) []byte {
// make payload
var payload []byte
payloadBuf := new(bytes.Buffer)
// from http://stackoverflow.com/questions/20108520/get-amount-of-free-disk-space-using-go
var stat syscall.Statfs_t
wd, err := os.Getwd()
syscall.Statfs(wd, &stat)
// Available blocks * size per block = available space in bytes
freeSpace := stat.Bavail * uint64(stat.Bsize)
totalSpace := stat.Blocks * uint64(stat.Bsize)
fmt.Println("freeSpace %v", freeSpace)
fmt.Println("totalSpace %v", totalSpace)
err = binary.Write(payloadBuf, binary.BigEndian, totalSpace)
if err != nil {
fmt.Println("totalSpace binary.Write failed:", err)
}
err = binary.Write(payloadBuf, binary.BigEndian, freeSpace)
if err != nil {
fmt.Println("freeSpace binary.Write failed:", err)
}
err = binary.Write(payloadBuf, binary.BigEndian, totalSpace-freeSpace)
if err != nil {
fmt.Println("usedSpace binary.Write failed:", err)
}
payload = payloadBuf.Bytes()
// add common headers
response := wrapCommonHeaders(request, []byte{0x13}, payload)
//return
return response
}
func respondGetFileIds(request *Request) []byte {
// make payload
var payload []byte
f := <-files
// release files data
// in case something wrong here doesn't
// block whole program
files <- f
payloadBuf := new(bytes.Buffer)
// numberOfFiles
err := binary.Write(payloadBuf, binary.BigEndian, uint32(len(f)))
if err != nil {
fmt.Println("binary.Write failed:", err)
}
// FILEDESCRIPTOR_STRUCT per file
for _, file := range f {
err = binary.Write(payloadBuf, binary.BigEndian, file.FileId)
if err != nil {
fmt.Println("FileId binary.Write failed:", err)
}
err = binary.Write(payloadBuf, binary.BigEndian, file.FileSize)
if err != nil {
fmt.Println("FileSize binary.Write failed:", err)
}
err = binary.Write(payloadBuf, binary.BigEndian, file.RelativePathLength)
if err != nil {
fmt.Println("RelativePathLength binary.Write failed:", err)
}
_, err = payloadBuf.WriteString(file.RelativePath)
if err != nil {
fmt.Println("RelativePath binary.Write failed:", err)
}
}
// // prepend total length data
totalLength := make([]byte, 4)
binary.BigEndian.PutUint32(totalLength, uint32(len(payload)))
payload = append(totalLength, payloadBuf.Bytes()...)
// add common headers
response := wrapCommonHeaders(request, []byte{0x14}, payload)
//return
return response
}
func respondGetFileChunks(request *Request) []byte {
var response []byte
var err error
var fileId uint32
binary.Read(bytes.NewReader(request.payload[0:4]), binary.BigEndian, &fileId)
f := <-files
// release files data
// in case something wrong here doesn't
// block whole program
files <- f
fileInfo, ok := f[strconv.Itoa(int(fileId))]
if !ok {
// fileId not found in db
emptyPayload := make([]byte, 12)
response = wrapCommonHeaders(request, []byte{0x15}, emptyPayload)
return response
}
chunkBuf := new(bytes.Buffer)
// CHUNK_DESCRIPTOP per chunk
for _, chunk := range fileInfo.Chunks {
err = binary.Write(chunkBuf, binary.BigEndian, chunk.Start)
if err != nil {
fmt.Println("Chunk Start binary.Write failed:", err)
}
err = binary.Write(chunkBuf, binary.BigEndian, chunk.End)
if err != nil {
fmt.Println("Chunk End binary.Write failed:", err)
}
err = binary.Write(chunkBuf, binary.BigEndian, chunk.Length)
if err != nil {
fmt.Println("Chunk Length binary.Write failed:", err)
}
}
// convert to bytes
chunkBytes := chunkBuf.Bytes()
payloadBuf := new(bytes.Buffer)
// success
_, err = payloadBuf.Write([]byte{0x01})
check(err)
// fileId
err = binary.Write(payloadBuf, binary.BigEndian, fileId)
check(err)
// totalLength
err = binary.Write(payloadBuf, binary.BigEndian, uint32(4+len(chunkBytes)))
check(err)
// numberOfChunks
err = binary.Write(payloadBuf, binary.BigEndian, uint32(len(fileInfo.Chunks)))
check(err)
// chunk_descriptor per file
_, err = payloadBuf.Write(chunkBytes)
check(err)
var payload []byte
payload = payloadBuf.Bytes()
// add common headers
response = wrapCommonHeaders(request, []byte{0x15}, payload)
//return
return response
}
func respondWriteFileChunk(request *Request) []byte {
var fileId uint32
var start, end, length uint64
var data []byte
var response []byte
binary.Read(bytes.NewReader(request.payload[0:4]), binary.BigEndian, &fileId)
binary.Read(bytes.NewReader(request.payload[4:12]), binary.BigEndian, &start)
binary.Read(bytes.NewReader(request.payload[12:20]), binary.BigEndian, &end)
binary.Read(bytes.NewReader(request.payload[20:28]), binary.BigEndian, &length)
data = request.payload[28:]
// fmt.Println("file id: ", fileId)
// fmt.Println("start: ", start)
// fmt.Println("end: ", end)
// fmt.Println("length: ", length)
fmt.Println("data: ", data)
f := <-files
fileInfo, ok := f[strconv.Itoa(int(fileId))]
if !ok {
// fileId not found in db
files <- f
response = wrapCommonHeaders(request, []byte{0x17}, []byte{0x00}, request.payload[0:28])
return response
}
// TODO : check that chunk writing to hasn't already been written
// Open the file for read and write (O_RDRW), append to it if it has
// content, create it if it does not exit, use 0666 for permissions
// on creation.
file, err := os.OpenFile(fileInfo.AbsolutePath, os.O_RDWR, 0777)
check(err)
file.WriteAt(data, int64(start))
file.Close()
// Store chunk in file info
newChunk := Chunk{start, end, length}
fileInfo.Chunks = append(fileInfo.Chunks, newChunk)
f[strconv.Itoa(int(fileId))] = fileInfo
fmt.Println("new files struct: ", f)
files <- f
response = wrapCommonHeaders(request, []byte{0x17}, []byte{0x01}, request.payload[0:28])
return response
}
func respondReadFileChunk(request *Request) []byte {
var fileId uint32
var start, end, length uint64
var response []byte
binary.Read(bytes.NewReader(request.payload[0:4]), binary.BigEndian, &fileId)
binary.Read(bytes.NewReader(request.payload[4:12]), binary.BigEndian, &start)
binary.Read(bytes.NewReader(request.payload[12:20]), binary.BigEndian, &end)
binary.Read(bytes.NewReader(request.payload[20:28]), binary.BigEndian, &length)
fmt.Println("file id: ", fileId)
fmt.Println("start: ", start)
fmt.Println("end: ", end)
fmt.Println("length: ", length)
f := <-files
// doesn't need to block
files <- f
fileInfo, ok := f[strconv.Itoa(int(fileId))]
if !ok {
// fileId not found in db
response = wrapCommonHeaders(request, []byte{0x18}, []byte{0x00}, request.payload[0:28])
return response
}
// Check if chunk has been written
for _, chunk := range fileInfo.Chunks {
if chunk.Start == start && length <= chunk.Length {
// Chunk has been written to
fileData, err := ioutil.ReadFile(fileInfo.AbsolutePath)
if err != nil {
fmt.Println("Reading file for chunk failed:", err)
}
var payload []byte
payload = append(request.payload[0:28], fileData[start:start+length]...)
fmt.Println("Chunk Requested: ", fileData[start:start+length])
fmt.Println("DataLength: ", len(fileData[start:start+length]))
fmt.Println("Payload: ", payload)
fmt.Println("Payload length: ", len(payload))
response = wrapCommonHeaders(request, []byte{0x18}, []byte{0x01}, payload)
return response
}
}
// if chunk wasn't found above
response = wrapCommonHeaders(request, []byte{0x18}, []byte{0x00}, request.payload[0:28])
return response
}
func deleteFile(request *Request) (uint32, bool) {
var fileId uint32
binary.Read(bytes.NewReader(request.payload[0:]), binary.BigEndian, &fileId)
f := <-files
file, ok := f[strconv.Itoa(int(fileId))]
if !ok {
// fileId not found in db
files <- f
return fileId, false
}
err := os.Remove(file.AbsolutePath)
if err != nil {
fmt.Println("Delete file error: ", err)
delete(f, strconv.Itoa(int(fileId)))
files <- f
return fileId, false
}
delete(f, strconv.Itoa(int(fileId)))
files <- f
return fileId, true
}
func wrapCommonHeaders(request *Request, cmd []byte, payloads ...[]byte) []byte {
// fmt.Fprintf(os.Stderr, "payload: %v\n", payload)
// find length of payload to attach to header
// and convert to bytes
var payload []byte
var payloadLen uint32 = 0
for _, payloadItem := range payloads {
payloadLen = payloadLen + uint32(len(payloadItem))
payload = append(payload, payloadItem...)
}
var length uint32 = uint32(payloadLen) + 5
lengthBytes := make([]byte, 4)
binary.BigEndian.PutUint32(lengthBytes, length)
response := []byte{0x01, 0x11}
response = append(response, lengthBytes...)
response = append(response, cmd...)
msgIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(msgIdBytes, request.msgId)
response = append(response, msgIdBytes...)
response = append(response, payload...)
// fmt.Fprintf(os.Stderr, "response: % x\n", response)
return response
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}
func parserCommon(request *Request, buf [msgBufferLength]byte, n int) {
//ProtocolTag Length cmd MsgId Payload
//2 Bytes 4 Bytes 1 Byte 4 Bytes Var (defined by command)
//0x0111 5 to 65535 Cmd specific Random Cmd specific (max 65530)
binary.Read(bytes.NewReader(buf[2:6]), binary.BigEndian, &request.length)
binary.Read(bytes.NewReader(buf[6:7]), binary.BigEndian, &request.cmd)
binary.Read(bytes.NewReader(buf[7:11]), binary.BigEndian, &request.msgId)
request.payload = buf[11:n]
return
}