/
readdir.go
191 lines (157 loc) · 3.8 KB
/
readdir.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"errors"
"os"
pathutil "path"
"strings"
)
func readDir() (*dir_t, error) {
name := "."
fileInfo, err := os.Lstat(name)
if err != nil {
return nil, err
}
if !fileInfo.IsDir() {
return nil, errors.New("not a directory: " + name)
}
dir := new_dir(new_entry(name, fileInfo), /*parent_orNil*/ nil)
err = readDir_internal(dir)
if err != nil {
return nil, err
}
return dir, nil
}
func readDir_internal(dir *dir_t) error {
if *flag_debug {
println("read dir:", dir.path)
}
f, err := os.Open(dir.path)
if err != nil {
return err
}
var list []os.FileInfo
list, err = f.Readdir(-1)
f.Close()
if err != nil {
return err
}
listSize := len(list)
objects := make([]object_t, listSize)
subdirs := make([]*dir_t, listSize)
numObjects := 0
numSubdirs := 0
numTemporarySubdirs := 0
for i := 0; i < listSize; i++ {
var entry os.FileInfo = list[i]
// Ignore all entries starting with '.'
if strings.HasPrefix(entry.Name(), ".") {
if *flag_debug {
println("ignore:", pathutil.Join(dir.path, entry.Name()))
}
continue
}
var object object_t
var err error
switch {
case entry.IsDir():
object, err = new_dir(new_entry(pathutil.Join(dir.path, entry.Name()), entry), dir), nil
default:
object, err = identifyFile(pathutil.Join(dir.path, entry.Name()), entry, dir)
}
if err != nil {
return err
}
if object != nil {
objects[numObjects] = object
numObjects++
if dir, isDir := object.(*dir_t); isDir {
subdirs[numSubdirs] = dir
numSubdirs++
if dir.isTemporary() {
numTemporarySubdirs++
}
}
}
}
objects = objects[0:numObjects]
subdirs = subdirs[0:numSubdirs]
dir.objects = objects
if dir.parent_orNil == nil {
// The presence of sub-directories requires a config file or a Makefile
if ((numSubdirs - numTemporarySubdirs) > 0) && (dir.config_orNil == nil) && (dir.makefile_orNil == nil) {
return errors.New("the root directory has one or more user sub-directories," +
" therefore it requires a " + configFileName + " file or a Makefile")
}
}
// Evaluate the config file (if any)
if dir.config_orNil != nil {
err := readConfig(dir.config_orNil)
if err != nil {
return err
}
}
// Dive into sub-directories
for _, subdir := range subdirs {
if _, ignore := ignoredDirs[pathutil.Clean(subdir.path)]; ignore {
if *flag_debug {
println("do not dive into:", subdir.path)
}
continue
}
err = readDir_internal(subdir)
if err != nil {
return err
}
}
return nil
}
func identifyFile(path string, fi os.FileInfo, parent *dir_t) (object_t, error) {
var entry entry_t = new_entry(path, fi)
if strings.HasSuffix(fi.Name(), "_test.go") {
if *flag_debug {
println("go test:", path)
}
return new_go_test(entry, parent), nil
}
if strings.HasSuffix(fi.Name(), ".go") {
if *flag_debug {
println("go source code:", path)
}
return new_go_file(entry, parent), nil
}
if (len(fi.Name()) == len(configFileName)) && (strings.ToLower(fi.Name()) == strings.ToLower(configFileName)) {
if *flag_debug {
println("config file:", path)
}
return new_config_file(entry, parent)
}
if fi.Name() == "Makefile" {
if *flag_debug {
println("makefile:", path)
}
return new_makefile(entry, parent)
}
if isCompilationUnit(fi.Name()) {
if *flag_debug {
println("compilation unit:", path)
}
return new_compilation_unit(entry, parent), nil
}
if strings.HasSuffix(fi.Name(), ".a") {
if *flag_debug {
println("library:", path)
}
return new_library(entry, parent), nil
}
if (fi.Mode().Perm() & 0100) != 0 {
if *flag_debug {
println("executable:", path)
}
return new_executable(entry, parent), nil
}
return nil, nil
}
func isCompilationUnit(path string) bool {
ext := pathutil.Ext(path)
return (ext == ".o") || (ext == ".5") || (ext == ".6") || (ext == ".8")
}