-
Notifications
You must be signed in to change notification settings - Fork 92
/
find_largest_files.go
64 lines (55 loc) · 1.36 KB
/
find_largest_files.go
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
59
60
61
62
63
64
package main
import (
"container/list"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
type FileNode struct {
FullPath string
Info os.FileInfo
}
func insertSorted(fileList *list.List, fileNode FileNode) {
if fileList.Len() == 0 { // If list is empty, just insert and return
fileList.PushFront(fileNode)
return
}
for element := fileList.Front(); element != nil; element = element.Next() {
if fileNode.Info.Size() < element.Value.(FileNode).Info.Size() {
fileList.InsertBefore(fileNode, element)
return
}
}
fileList.PushBack(fileNode)
}
func getFilesInDirRecursivelyBySize(fileList *list.List, path string) {
dirFiles, err := ioutil.ReadDir(path)
if err != nil {
log.Println("Error reading directory: " + err.Error())
}
for _, dirFile := range dirFiles {
fullpath := filepath.Join(path, dirFile.Name())
if dirFile.IsDir() {
getFilesInDirRecursivelyBySize(
fileList,
filepath.Join(path, dirFile.Name()),
)
} else if dirFile.Mode().IsRegular() {
insertSorted(
fileList,
FileNode{FullPath: fullpath, Info: dirFile},
)
}
}
}
func main() {
fileList := list.New()
getFilesInDirRecursivelyBySize(fileList, "/home")
for element := fileList.Front(); element != nil; element =
element.Next() {
fmt.Printf("%d ", element.Value.(FileNode).Info.Size())
fmt.Printf("%s\n", element.Value.(FileNode).FullPath)
}
}