Browse Source

[fsys] correctly calculate directory size

main
Unbewohnte 3 years ago
parent
commit
f0332c8a7f
  1. 31
      src/fsys/dir.go
  2. 12
      src/fsys/dir_test.go
  3. 3
      src/node/node.go
  4. 16
      src/protocol/headers.go
  5. 11
      src/testfiles/testDownload/testfile.txt

31
src/fsys/dir.go

@ -11,12 +11,18 @@ type Directory struct {
Name string Name string
Path string Path string
ParentPath string ParentPath string
Size uint64
Files []*File Files []*File
Directories []*Directory Directories []*Directory
} }
var ErrorNotDirectory error = fmt.Errorf("not a 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) { func GetDir(path string, recursive bool) (*Directory, error) {
absPath, err := filepath.Abs(path) absPath, err := filepath.Abs(path)
if err != nil { if err != nil {
@ -33,12 +39,21 @@ func GetDir(path string, recursive bool) (*Directory, error) {
return nil, ErrorNotDirectory 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 // loop through each entry in the directory
entries, err := os.ReadDir(absPath) entries, err := os.ReadDir(absPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// var totalSize uint64 = 0
var innerDirs []*Directory var innerDirs []*Directory
var innerFiles []*File var innerFiles []*File
for _, entry := range entries { for _, entry := range entries {
@ -58,8 +73,10 @@ func GetDir(path string, recursive bool) (*Directory, error) {
} }
innerDirs = append(innerDirs, innerDir) 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 { } else {
innerFilePath := filepath.Join(absPath, entryInfo.Name()) innerFilePath := filepath.Join(absPath, entryInfo.Name())
@ -70,16 +87,14 @@ func GetDir(path string, recursive bool) (*Directory, error) {
} }
innerFiles = append(innerFiles, innerFile) innerFiles = append(innerFiles, innerFile)
directory.Size += innerFile.Size
} }
} }
directory := Directory{ directory.Directories = innerDirs
Name: stats.Name(), directory.Files = innerFiles
Path: absPath, // directory.Size
ParentPath: filepath.Dir(absPath),
Directories: innerDirs,
Files: innerFiles,
}
return &directory, nil return &directory, nil
} }

12
src/fsys/dir_test.go

@ -7,7 +7,7 @@ func Test_GetDir(t *testing.T) {
_, err := GetDir(dirpath, false) _, err := GetDir(dirpath, false)
if err != nil { 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) dir, err := GetDir(dirpath, true)
if err != nil { if err != nil {
t.Fatalf("GetDir error: %s", err) t.Fatalf("%s", err)
} }
expectedAmountOfUpperDirectories := 3 expectedAmountOfUpperDirectories := 3
if len(dir.Directories) != expectedAmountOfUpperDirectories { 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)
}
} }
} }

3
src/node/node.go

@ -266,7 +266,8 @@ func (node *Node) Start() {
// connect to the sending node // connect to the sending node
err := node.connect(node.Net.ConnAddr, node.Net.Port) err := node.connect(node.Net.ConnAddr, node.Net.Port)
if err != nil { 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 // listen for incoming packets

16
src/protocol/headers.go

@ -10,20 +10,20 @@ type Header string
// ENCRKEY. // ENCRKEY.
// The FIRST header to be sent. Sent immediately after the connection has been established // 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. // 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" const HeaderEncryptionKey Header = "ENCRKEY"
// REJECT. // REJECT.
// Sent only by receiver if the receiver has decided to not download the contents. // Sent only by receiver if the receiver has decided to not download the contents.
// The body must contain a file ID in binary. // The body must contain a file ID in binary.
// ie: REJECT~1111011 // ie: REJECT~(file id in binary)
const HeaderReject Header = "REJECT" const HeaderReject Header = "REJECT"
// ACCEPT. // ACCEPT.
// The opposite of the previous REJECT. Sent by receiver when // The opposite of the previous REJECT. Sent by receiver when
// he has agreed to download the file|directory. The body must contain // he has agreed to download the file|directory. The body must contain
// the ID of a file in binary that is allowed to upload // 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" const HeaderAccept Header = "ACCEPT"
// DONE. // DONE.
@ -54,20 +54,22 @@ const HeaderDisconnecting Header = "BYE!"
// FILE. // FILE.
// Sent by sender, indicating that the file is going to be sent. // Sent by sender, indicating that the file is going to be sent.
// The body structure must follow such structure: // 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" const HeaderFile Header = "FILE"
// FILEBYTES. // FILEBYTES.
// Sent only by sender. The packet`s body must contain // Sent only by sender. The packet`s body must contain
// a file`s Identifier and a portion of its bytes. // 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" const HeaderFileBytes Header = "FILEBYTES"
// ENDFILE // ENDFILE
// Sent by sender when the file`s contents fully has been sent. // Sent by sender when the file`s contents fully has been sent.
// The body must contain a file ID. // The body must contain a file ID.
// ie: ENDFILE~(fileIDIinBinary) // ie: ENDFILE~(file ID in binary)
const HeaderEndfile Header = "ENDFILE" 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" const HeaderDirectory Header = "DIRECTORY"

11
src/testfiles/testDownload/testfile.txt

@ -1,11 +0,0 @@
727 WYSI Airman Badeu square
doable
FCeeeeeeeeeeeeeeeeeeeeeeeee
testfile it is
Loading…
Cancel
Save