From f0332c8a7f663407a636628b4226f81b3b1c4b9d Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Fri, 5 Nov 2021 14:38:56 +0300 Subject: [PATCH] [fsys] correctly calculate directory size --- src/fsys/dir.go | 31 ++++++++++++++++++------- src/fsys/dir_test.go | 12 +++++++--- src/node/node.go | 3 ++- src/protocol/headers.go | 16 +++++++------ src/testfiles/testDownload/testfile.txt | 11 --------- 5 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/fsys/dir.go b/src/fsys/dir.go index 3b7ff86..7e0d43d 100644 --- a/src/fsys/dir.go +++ b/src/fsys/dir.go @@ -11,12 +11,18 @@ type Directory struct { Name string Path string ParentPath string + Size uint64 Files []*File Directories []*Directory } var ErrorNotDirectory error = fmt.Errorf("not a directory") +// // gets a child directory +// func getDirChild(path string, parentDir *Directory, recursive bool) (*Directory, error) { + +// } + func GetDir(path string, recursive bool) (*Directory, error) { absPath, err := filepath.Abs(path) if err != nil { @@ -33,12 +39,21 @@ func GetDir(path string, recursive bool) (*Directory, error) { return nil, ErrorNotDirectory } + directory := Directory{ + Name: stats.Name(), + Path: absPath, + ParentPath: filepath.Dir(absPath), + Directories: nil, + Files: nil, + } + // loop through each entry in the directory entries, err := os.ReadDir(absPath) if err != nil { return nil, err } + // var totalSize uint64 = 0 var innerDirs []*Directory var innerFiles []*File for _, entry := range entries { @@ -58,8 +73,10 @@ func GetDir(path string, recursive bool) (*Directory, error) { } innerDirs = append(innerDirs, innerDir) + + directory.Size += innerDir.Size } - // if not - skip the directory + // if not - skip the directory and only work with the files } else { innerFilePath := filepath.Join(absPath, entryInfo.Name()) @@ -70,16 +87,14 @@ func GetDir(path string, recursive bool) (*Directory, error) { } innerFiles = append(innerFiles, innerFile) + + directory.Size += innerFile.Size } } - directory := Directory{ - Name: stats.Name(), - Path: absPath, - ParentPath: filepath.Dir(absPath), - Directories: innerDirs, - Files: innerFiles, - } + directory.Directories = innerDirs + directory.Files = innerFiles + // directory.Size return &directory, nil } diff --git a/src/fsys/dir_test.go b/src/fsys/dir_test.go index 61a62c0..70747d9 100644 --- a/src/fsys/dir_test.go +++ b/src/fsys/dir_test.go @@ -7,7 +7,7 @@ func Test_GetDir(t *testing.T) { _, err := GetDir(dirpath, false) if err != nil { - t.Fatalf("GetDir error: %s", err) + t.Fatalf("%s", err) } } @@ -16,11 +16,17 @@ func Test_GetDirRecursive(t *testing.T) { dir, err := GetDir(dirpath, true) if err != nil { - t.Fatalf("GetDir error: %s", err) + t.Fatalf("%s", err) } expectedAmountOfUpperDirectories := 3 if len(dir.Directories) != expectedAmountOfUpperDirectories { - t.Fatalf("GetDir error: expected to have %d inner directories; got %d", expectedAmountOfUpperDirectories, len(dir.Directories)) + t.Fatalf("expected to have %d inner directories; got %d", expectedAmountOfUpperDirectories, len(dir.Directories)) + } + + for _, innerDir := range dir.Directories { + if innerDir.Size > dir.Size { + t.Errorf("inner dir cannot have a bigger size (%d B) than its parent`s total size (%d B)", innerDir.Size, dir.Size) + } } } diff --git a/src/node/node.go b/src/node/node.go index 5b39493..c306989 100644 --- a/src/node/node.go +++ b/src/node/node.go @@ -266,7 +266,8 @@ func (node *Node) Start() { // connect to the sending node err := node.connect(node.Net.ConnAddr, node.Net.Port) if err != nil { - panic(err) + fmt.Printf("Could not connect to %s:%d\n", node.Net.ConnAddr, node.Net.Port) + os.Exit(-1) } // listen for incoming packets diff --git a/src/protocol/headers.go b/src/protocol/headers.go index d8d3e68..a882f3d 100644 --- a/src/protocol/headers.go +++ b/src/protocol/headers.go @@ -10,20 +10,20 @@ type Header string // ENCRKEY. // The FIRST header to be sent. Sent immediately after the connection has been established // by sender. Body contains a size of a key and the key itself. -// ie: ENCRKEY~(size)(SUPER SECURE ENCRYPTION KEY) +// ie: ENCRKEY~(size)(encryption key) const HeaderEncryptionKey Header = "ENCRKEY" // REJECT. // Sent only by receiver if the receiver has decided to not download the contents. // The body must contain a file ID in binary. -// ie: REJECT~1111011 +// ie: REJECT~(file id in binary) const HeaderReject Header = "REJECT" // ACCEPT. // The opposite of the previous REJECT. Sent by receiver when // he has agreed to download the file|directory. The body must contain // the ID of a file in binary that is allowed to upload -// ie: ACCEPT~1111011 +// ie: ACCEPT~(file id in binary) const HeaderAccept Header = "ACCEPT" // DONE. @@ -54,20 +54,22 @@ const HeaderDisconnecting Header = "BYE!" // FILE. // Sent by sender, indicating that the file is going to be sent. // The body structure must follow such structure: -// FILE~(idInBinary)(filenameLengthInBinary)(filename)(filesize)(checksumLengthInBinary)checksum +// FILE~(id in binary)(filename length in binary)(filename)(filesize)(checksum length in binary)(checksum) const HeaderFile Header = "FILE" // FILEBYTES. // Sent only by sender. The packet`s body must contain // a file`s Identifier and a portion of its bytes. -// ie: FILEBYTES~(fileIDinBinary)(File`sBinaryData) +// ie: FILEBYTES~(file ID in binary)(file`s binary data) const HeaderFileBytes Header = "FILEBYTES" // ENDFILE // Sent by sender when the file`s contents fully has been sent. // The body must contain a file ID. -// ie: ENDFILE~(fileIDIinBinary) +// ie: ENDFILE~(file ID in binary) const HeaderEndfile Header = "ENDFILE" -// DIRECTORY. (TODO) +// DIRECTORY +// If used the first time +// ie: DIRECTORY~(dirname size in binary)(dirname)(dirsize)(checksumLengthInBinary)(checksum) const HeaderDirectory Header = "DIRECTORY" diff --git a/src/testfiles/testDownload/testfile.txt b/src/testfiles/testDownload/testfile.txt index 99a7032..e69de29 100755 --- a/src/testfiles/testDownload/testfile.txt +++ b/src/testfiles/testDownload/testfile.txt @@ -1,11 +0,0 @@ -727 WYSI Airman Badeu square - - - -doable - - -FCeeeeeeeeeeeeeeeeeeeeeeeee - - -testfile it is \ No newline at end of file