-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilesystem.go
107 lines (90 loc) · 1.9 KB
/
filesystem.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package airlock
import (
"os"
"path/filepath"
)
type ErrDoesNotExist error
func (a *Airlock) ScanFiles(path string) error {
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return ErrDoesNotExist(err)
} else {
return err
}
}
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
a.tree = make(map[string]*File)
if info.IsDir() {
return a.scanDirectory(absPath)
} else {
// uploading only one file, create the file "tree" manually
descriptor, err := os.Open(path)
if err != nil {
return err
}
file := &File{
RelPath: info.Name(),
Name: info.Name(),
IsDir: false,
IsNotRoot: true,
Size: info.Size(),
Reader: descriptor,
}
a.tree["."] = &File{
RelPath: ".",
Name: info.Name(),
IsDir: true,
IsNotRoot: false,
Children: []*File{file},
Size: info.Size(),
Reader: descriptor,
}
a.files = append(a.files, *file)
return nil
}
}
func (a *Airlock) scanDirectory(dirPath string) error {
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
descriptor, err := os.Open(path)
if err != nil {
return err
}
// get relative path
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
relPath, err := filepath.Rel(dirPath, absPath)
if err != nil {
return err
}
// create struct
file := &File{
RelPath: relPath,
Name: info.Name(),
IsDir: info.IsDir(),
IsNotRoot: relPath != ".",
Reader: descriptor,
Size: info.Size(),
}
// insert into tree
a.tree[relPath] = file
// update parent's children
if parent, found := a.tree[filepath.Dir(relPath)]; found {
parent.Children = append(parent.Children, file)
}
// insert only files to a.files
if !info.IsDir() {
a.files = append(a.files, *file)
}
return nil
})
return err
}