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

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

3
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

16
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"

11
src/testfiles/testDownload/testfile.txt

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