This repository was archived by the owner on Jan 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_system.go
More file actions
58 lines (51 loc) · 1.59 KB
/
Copy pathfile_system.go
File metadata and controls
58 lines (51 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"log"
"fmt"
"os"
"path/filepath"
)
// createDirectoriesTree fillings the tree structure with data from the system, returning the resulting values
func createDirectoriesTree(dirPath string, node *Node) (root *Node, countEdges int, countFiles int, countBytes int64) {
fmt.Println("Building a file system tree...")
root = fillRecursiveDirectoriesTree(dirPath, node, &countEdges, &countFiles, &countBytes)
fmt.Print("\n")
return
}
// fillRecursiveDirectoriesTree recursively traverses all files starting from the top directory, adding them to the structure
func fillRecursiveDirectoriesTree(dirPath string, node *Node, countEdges *int, countFiles *int, countBytes *int64) *Node {
var newDir *Node
if node == nil {
newDir = NewNode(dirPath, nil)
} else {
*countEdges++
newDir = node.Insert(dirPath)
}
entries, _ := os.ReadDir(dirPath)
for _, entry := range entries {
isDir := entry.Type() & os.ModeDir != 0
isSymlink := entry.Type() & os.ModeSymlink != 0
elemName := entry.Name()
if isDir {
dirName := dirPath + "/" + elemName
fillRecursiveDirectoriesTree(dirName, newDir, countEdges, countFiles, countBytes)
} else if isSymlink {
continue // TODO
} else {
newDir.AddWeightElem(elemName)
*countFiles++
// Next we add the file size to the final size
info, err := entry.Info()
if err != nil {
filePath := filepath.Join(dirPath, elemName)
log.Println("Не удалось получить информацию о файле: " + filePath)
} else {
*countBytes += info.Size()
}
}
}
if node == nil {
return newDir
}
return nil
}