Skip to content

Commit

Permalink
[checksum] Micro optimisations; [node] Renamed nodes to receiving and…
Browse files Browse the repository at this point in the history
… sending; [fsys] ftu now DOES NOT die on 'permission denied's
  • Loading branch information
Unbewohnte committed Jan 14, 2022
1 parent 21d16c5 commit 700811b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions src/checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package checksum

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"io"
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/fsys/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package fsys

import (
"fmt"
"io/fs"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/fsys/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -122,19 +122,19 @@ 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,
},
}

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)
}

Expand Down
23 changes: 13 additions & 10 deletions src/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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,
},
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/node/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

package node

type ServerSideNodeOptions struct {
type SenderNodeOptions struct {
ServingPath string
Recursive bool
}

type ClientSideNodeOptions struct {
type ReceiverNodeOptions struct {
ConnectionAddr string
DownloadsFolderPath string
}
Expand All @@ -35,6 +35,6 @@ type NodeOptions struct {
IsSending bool
WorkingPort uint
VerboseOutput bool
ServerSide *ServerSideNodeOptions
ClientSide *ClientSideNodeOptions
SenderSide *SenderNodeOptions
ReceiverSide *ReceiverNodeOptions
}
2 changes: 1 addition & 1 deletion src/protocol/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 700811b

Please sign in to comment.