From 700811b1794fec86d7018de30ed00241e85f9313 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Fri, 14 Jan 2022 09:49:53 +0300 Subject: [PATCH] [checksum] Micro optimisations; [node] Renamed nodes to receiving and sending; [fsys] ftu now DOES NOT die on 'permission denied's --- README.md | 2 +- src/checksum/checksum.go | 14 ++++++++------ src/fsys/dir.go | 6 ++++-- src/fsys/file.go | 2 +- src/main.go | 12 ++++++------ src/node/node.go | 23 +++++++++++++---------- src/node/options.go | 8 ++++---- src/protocol/send.go | 2 +- 8 files changed, 38 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index ccc02c9..f02bf27 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Now you have ftu installed ! ## ● Usage `ftu -h` - to print a usage message -`ftu [FLAGS]` +`ftu [FLAGs]` ### ● FLAGs - -p [uint] for port diff --git a/src/checksum/checksum.go b/src/checksum/checksum.go index b896d3c..9826e18 100644 --- a/src/checksum/checksum.go +++ b/src/checksum/checksum.go @@ -21,6 +21,7 @@ along with this program. If not, see . package checksum import ( + "bytes" "crypto/sha256" "encoding/hex" "io" @@ -36,8 +37,8 @@ import ( // GetPartialCheckSum is default method used to get a file checksum by sender and receiver func GetPartialCheckSum(file *os.File) (string, error) { // "capturing" CHUNKSIZE bytes and then skipping STEP bytes before the next chunk until the last one - const CHUNKS uint = 100 - const CHUNKSIZE uint = 100 + const CHUNKS uint = 50 + const CHUNKSIZE uint = 50 const STEP uint = 250 fileStats, err := file.Stat() @@ -48,7 +49,7 @@ func GetPartialCheckSum(file *os.File) (string, error) { fileSize := fileStats.Size() if fileSize < int64(CHUNKS*CHUNKSIZE+STEP*(CHUNKS-1)) { - // file is too small to chop it in chunks, so just doing full checksum + // file is too small to chop it in chunks, so just get the full checksum checksum, err := getFullCheckSum(file) if err != nil { @@ -62,19 +63,20 @@ func GetPartialCheckSum(file *os.File) (string, error) { return "", err } - var capturedChunks string + // var capturedChunks string + var capturedChunks bytes.Buffer var read uint64 = 0 for i := 0; uint(i) < CHUNKS; i++ { buffer := make([]byte, CHUNKSIZE) r, _ := file.ReadAt(buffer, int64(read)) - capturedChunks += string(buffer) + capturedChunks.Write(buffer) read += uint64(r) read += uint64(STEP) } - checksumBytes := sha256.Sum256([]byte(capturedChunks)) + checksumBytes := sha256.Sum256(capturedChunks.Bytes()) checksum := hex.EncodeToString(checksumBytes[:]) return checksum, nil diff --git a/src/fsys/dir.go b/src/fsys/dir.go index 81ea4c0..6140aba 100644 --- a/src/fsys/dir.go +++ b/src/fsys/dir.go @@ -22,6 +22,7 @@ package fsys import ( "fmt" + "io/fs" "os" "path/filepath" ) @@ -63,7 +64,7 @@ func GetDir(path string, recursive bool) (*Directory, error) { // loop through each entry in the directory entries, err := os.ReadDir(absPath) - if err != nil { + if err != nil && err != fs.ErrPermission { return nil, err } @@ -96,7 +97,8 @@ func GetDir(path string, recursive bool) (*Directory, error) { innerFile, err := GetFile(innerFilePath) if err != nil { - return nil, err + // skip this file + continue } directory.Size += innerFile.Size diff --git a/src/fsys/file.go b/src/fsys/file.go index edbceed..8220c89 100644 --- a/src/fsys/file.go +++ b/src/fsys/file.go @@ -45,7 +45,7 @@ var ErrorNotFile error = fmt.Errorf("not a file") // Get general information about a file with the // future ability to open it. // NOTE that Handler field is nil BY DEFAULT until you -// manually call a (file *File) Open() function to open it ! +// manually call (file *File).Open() to open it ! func GetFile(path string) (*File, error) { absPath, err := filepath.Abs(path) if err != nil { diff --git a/src/main.go b/src/main.go index 86e4792..bb17ea7 100644 --- a/src/main.go +++ b/src/main.go @@ -30,9 +30,9 @@ import ( ) var ( - VERSION string = "v2.2.2" + VERSION string = "v2.2.3" - versionInformation string = fmt.Sprintf("ftu %s\n\nCopyright (C) 2021,2022 Kasyanov Nikolay Alexeevich (Unbewohnte (https://unbewohnte.xyz/))\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it under certain conditions; type \"ftu -l\" for details.\n", VERSION) + versionInformation string = fmt.Sprintf("ftu %s\nfile transferring utility\n\nCopyright (C) 2021,2022 Kasyanov Nikolay Alexeevich (Unbewohnte (me@unbewohnte.xyz))\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it under certain conditions; type \"ftu -l\" for details.\n", VERSION) //go:embed COPYING licenseInformation string @@ -52,7 +52,7 @@ var ( func init() { flag.Usage = func() { - fmt.Printf("ftu -[FLAG]...\n\n") + fmt.Printf("ftu -[FLAGs]\n\n") fmt.Printf("[FLAGs]\n\n") fmt.Printf("| -p [Uinteger_here] for port\n") @@ -122,11 +122,11 @@ func main() { VerboseOutput: *VERBOSE, IsSending: isSending, WorkingPort: *PORT, - ServerSide: &node.ServerSideNodeOptions{ + SenderSide: &node.SenderNodeOptions{ ServingPath: *SEND, Recursive: *RECUSRIVE, }, - ClientSide: &node.ClientSideNodeOptions{ + ReceiverSide: &node.ReceiverNodeOptions{ ConnectionAddr: *ADDRESS, DownloadsFolderPath: *DOWNLOADS_DIR, }, @@ -134,7 +134,7 @@ func main() { node, err := node.NewNode(&nodeOptions) if err != nil { - fmt.Printf("Error constructing a new node: %s\n", err) + fmt.Printf("[ERROR] Error constructing a new node: %s\n", err) os.Exit(-1) } diff --git a/src/node/node.go b/src/node/node.go index b5974f2..68334fb 100644 --- a/src/node/node.go +++ b/src/node/node.go @@ -91,7 +91,7 @@ func NewNode(options *NodeOptions) (*Node, error) { var isDir bool if options.IsSending { // sending node preparation - sendingPathStats, err := os.Stat(options.ServerSide.ServingPath) + sendingPathStats, err := os.Stat(options.SenderSide.ServingPath) if err != nil { return nil, err } @@ -106,12 +106,12 @@ func NewNode(options *NodeOptions) (*Node, error) { } else { // receiving node preparation var err error - options.ClientSide.DownloadsFolderPath, err = filepath.Abs(options.ClientSide.DownloadsFolderPath) + options.ReceiverSide.DownloadsFolderPath, err = filepath.Abs(options.ReceiverSide.DownloadsFolderPath) if err != nil { return nil, err } - err = os.MkdirAll(options.ClientSide.DownloadsFolderPath, os.ModePerm) + err = os.MkdirAll(options.ReceiverSide.DownloadsFolderPath, os.ModePerm) if err != nil { return nil, err } @@ -125,22 +125,22 @@ func NewNode(options *NodeOptions) (*Node, error) { isSending: options.IsSending, netInfo: &netInfo{ Port: options.WorkingPort, - ConnAddr: options.ClientSide.ConnectionAddr, + ConnAddr: options.ReceiverSide.ConnectionAddr, EncryptionKey: nil, Conn: nil, }, stopped: false, transferInfo: &transferInfo{ Sending: &sending{ - ServingPath: options.ServerSide.ServingPath, - Recursive: options.ServerSide.Recursive, + ServingPath: options.SenderSide.ServingPath, + Recursive: options.SenderSide.Recursive, IsDirectory: isDir, TotalTransferSize: 0, SentBytes: 0, }, Receiving: &receiving{ AcceptedFiles: nil, - DownloadsPath: options.ClientSide.DownloadsFolderPath, + DownloadsPath: options.ReceiverSide.DownloadsFolderPath, ReceivedBytes: 0, TotalDownloadSize: 0, }, @@ -512,7 +512,7 @@ func (node *Node) send() { default: node.stopped = true - fmt.Printf("\nAn error occured while sending a piece of \"%s\": %s", node.transferInfo.Sending.FilesToSend[currentFileIndex].Name, err) + fmt.Printf("\n[ERROR] An error occured while sending a piece of \"%s\": %s", node.transferInfo.Sending.FilesToSend[currentFileIndex].Name, err) panic(err) } } @@ -525,7 +525,7 @@ func (node *Node) receive() { // connect to the sending node err := node.connect() if err != nil { - fmt.Printf("\nCould not connect to %s:%d", node.netInfo.ConnAddr, node.netInfo.Port) + fmt.Printf("\n[ERROR] Could not connect to %s:%d", node.netInfo.ConnAddr, node.netInfo.Port) os.Exit(-1) } @@ -823,7 +823,10 @@ func (node *Node) receive() { } if realChecksum != acceptedFile.Checksum { - fmt.Printf("\n| \"%s\" is corrupted", acceptedFile.Name) + if node.verboseOutput { + fmt.Printf("\n[ERROR] \"%s\" is corrupted", acceptedFile.Name) + } + acceptedFile.Close() break } else { diff --git a/src/node/options.go b/src/node/options.go index 6de7d3a..32f31e1 100644 --- a/src/node/options.go +++ b/src/node/options.go @@ -20,12 +20,12 @@ along with this program. If not, see . package node -type ServerSideNodeOptions struct { +type SenderNodeOptions struct { ServingPath string Recursive bool } -type ClientSideNodeOptions struct { +type ReceiverNodeOptions struct { ConnectionAddr string DownloadsFolderPath string } @@ -35,6 +35,6 @@ type NodeOptions struct { IsSending bool WorkingPort uint VerboseOutput bool - ServerSide *ServerSideNodeOptions - ClientSide *ClientSideNodeOptions + SenderSide *SenderNodeOptions + ReceiverSide *ReceiverNodeOptions } diff --git a/src/protocol/send.go b/src/protocol/send.go index be6c9b5..27dba0e 100644 --- a/src/protocol/send.go +++ b/src/protocol/send.go @@ -174,7 +174,7 @@ func SendPiece(file *fsys.File, connection net.Conn, encrKey []byte) (uint64, er if encrKey != nil { // account for padding - canSendBytes -= 32 + canSendBytes -= 48 } if (file.Size - file.SentBytes) < canSendBytes {