From 2dca004f5189d0fc292e31bd04a70de4a3699cc5 Mon Sep 17 00:00:00 2001 From: fabiokleis Date: Thu, 11 Apr 2024 00:27:03 -0300 Subject: [PATCH] fix checksum error --- pkg/client/main.go | 14 +++++++++----- pkg/file/file.go | 12 ++++++------ pkg/worker/worker.go | 11 ++++++----- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pkg/client/main.go b/pkg/client/main.go index cc06866..6f3089c 100644 --- a/pkg/client/main.go +++ b/pkg/client/main.go @@ -60,12 +60,16 @@ func (c *Client) readFileChunkPacket(buffer []byte) (*msg.FileChunk, error) { return nil, err } - idx, err := strconv.Atoi(strings.Split(fileChunk.Token, "_")[2]) + idx, err := strconv.ParseUint(strings.Split(fileChunk.Token, "_")[2], 10, 32) if err != nil { return nil, err } + if c.File.FindToken(fileChunk.Token) != nil { + c.sendConfirmationPacket(msg.Result_OK, fileChunk.Token) + return &fileChunk, fmt.Errorf("already wrote file chunk, skipping") + } c.File.PushToken(idx, true) c.File.WriteChunk(fileChunk.Chunk) @@ -93,12 +97,12 @@ func (c *Client) verifyConfirmation(confirm *msg.Confirmation) { case msg.Result_VALID_CHECKSUM: if err := c.validateCheckSum(confirm.Token); err != nil { fmt.Println(err) - if err := c.File.DeleteFile(); err != nil { - fmt.Println(err) - } + //if err := c.File.DeleteFile(); err != nil { + // fmt.Println(err) + //} + c.Transfering = false // stop reading packets return } - c.Transfering = false // stop reading packets fmt.Println("sha256 checksum", c.File.CheckSum) fmt.Println("file transfering succeed") diff --git a/pkg/file/file.go b/pkg/file/file.go index 3001bb9..dc71bbe 100644 --- a/pkg/file/file.go +++ b/pkg/file/file.go @@ -5,19 +5,19 @@ import ( "os" ) -func TokenHash(idx int) string { +func TokenHash(idx uint64) string { return fmt.Sprintf("token_idx_%d", idx) } type Token struct { - Index int + Index uint64 Received bool } type TokenizableFile struct { FileName string File *os.File // file - Size int // file content amount of bytes + Size uint64 // file content amount of bytes Tokens []*Token // tokenized indexes CheckSum string // file checksum } @@ -51,7 +51,7 @@ func (tF *TokenizableFile) Path() string { return tF.File.Name() } -func (tF *TokenizableFile) ReadChunk(offset int, chunkSize int) []byte { +func (tF *TokenizableFile) ReadChunk(offset uint64, chunkSize uint64) []byte { chunk := make([]byte, chunkSize) n, _ := tF.File.ReadAt(chunk, int64(offset)) @@ -60,7 +60,7 @@ func (tF *TokenizableFile) ReadChunk(offset int, chunkSize int) []byte { } func (tF *TokenizableFile) WriteChunk(chunk []byte) { - tF.Size += len(chunk) + tF.Size += uint64(len(chunk)) tF.File.Write(chunk) } @@ -93,6 +93,6 @@ func (tF *TokenizableFile) LastReceivedToken() *Token { return nil } -func (tF *TokenizableFile) PushToken(idx int, received bool) { +func (tF *TokenizableFile) PushToken(idx uint64, received bool) { tF.Tokens = append(tF.Tokens, &Token{Index: idx, Received: received}) } diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index d88e33d..e924107 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -15,8 +15,8 @@ import ( ) const ( - TIMEOUT = 10 // secs - CHUNK_SIZE = 1024 // 1k file chunk bytes + TIMEOUT = 10 // secs + CHUNK_SIZE uint64 = 1024 // 1k file chunk bytes ) type Worker struct { @@ -106,10 +106,11 @@ func (w *Worker) tokenizeFile(file *os.File) { stat, _ := file.Stat() w.File.File = file - w.File.Size = int(stat.Size()) + w.File.Size = uint64(stat.Size()) w.File.Tokens = []*f.Token{} - for i := 0; i < w.File.Size; i += CHUNK_SIZE { + var i uint64 + for i = 0; i <= w.File.Size; i += CHUNK_SIZE { w.File.PushToken(i, false) } } @@ -197,7 +198,7 @@ func (w *Worker) Execute() { case msg.Result_OK: w.Waiting = false // stop waiting confirmation packet if err := w.confirmPacket(confirm.Token); err != nil { - //slog.Error("cannot confirm packet, failed to find token hash", "address", w.Addr.String(), "token", confirm.Token, "error", err) + slog.Error("cannot confirm packet, failed to find token hash", "address", w.Addr.String(), "token", confirm.Token, "error", err) w.sendConfirmationPacket(msg.Result_INVALID_TOKEN, confirm.Token) break }