31 | 31 |
return f.IsDir()
|
32 | 32 |
}
|
33 | 33 |
|
34 | |
func statDir(dirPath, recPath string, includeDir, isDirOnly bool) ([]string, error) {
|
|
34 |
func statDir(dirPath, recPath string, includeDir, isDirOnly, followSymlinks bool) ([]string, error) {
|
35 | 35 |
dir, err := os.Open(dirPath)
|
36 | 36 |
if err != nil {
|
37 | 37 |
return nil, err
|
|
55 | 55 |
if includeDir {
|
56 | 56 |
statList = append(statList, relPath+"/")
|
57 | 57 |
}
|
58 | |
s, err := statDir(curPath, relPath, includeDir, isDirOnly)
|
|
58 |
s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks)
|
59 | 59 |
if err != nil {
|
60 | 60 |
return nil, err
|
61 | 61 |
}
|
62 | 62 |
statList = append(statList, s...)
|
63 | 63 |
} else if !isDirOnly {
|
64 | 64 |
statList = append(statList, relPath)
|
|
65 |
} else if followSymlinks && fi.Mode()&os.ModeSymlink != 0 {
|
|
66 |
link, err := os.Readlink(curPath)
|
|
67 |
if err != nil {
|
|
68 |
return nil, err
|
|
69 |
}
|
|
70 |
|
|
71 |
if IsDir(link) {
|
|
72 |
if includeDir {
|
|
73 |
statList = append(statList, relPath+"/")
|
|
74 |
}
|
|
75 |
s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks)
|
|
76 |
if err != nil {
|
|
77 |
return nil, err
|
|
78 |
}
|
|
79 |
statList = append(statList, s...)
|
|
80 |
}
|
65 | 81 |
}
|
66 | 82 |
}
|
67 | 83 |
return statList, nil
|
|
83 | 99 |
if len(includeDir) >= 1 {
|
84 | 100 |
isIncludeDir = includeDir[0]
|
85 | 101 |
}
|
86 | |
return statDir(rootPath, "", isIncludeDir, false)
|
|
102 |
return statDir(rootPath, "", isIncludeDir, false, false)
|
|
103 |
}
|
|
104 |
|
|
105 |
// LstatDir gathers information of given directory by depth-first.
|
|
106 |
// It returns slice of file list, follows symbolic links and includes subdirectories if enabled;
|
|
107 |
// it returns error and nil slice when error occurs in underlying functions,
|
|
108 |
// or given path is not a directory or does not exist.
|
|
109 |
//
|
|
110 |
// Slice does not include given path itself.
|
|
111 |
// If subdirectories is enabled, they will have suffix '/'.
|
|
112 |
func LstatDir(rootPath string, includeDir ...bool) ([]string, error) {
|
|
113 |
if !IsDir(rootPath) {
|
|
114 |
return nil, errors.New("not a directory or does not exist: " + rootPath)
|
|
115 |
}
|
|
116 |
|
|
117 |
isIncludeDir := false
|
|
118 |
if len(includeDir) >= 1 {
|
|
119 |
isIncludeDir = includeDir[0]
|
|
120 |
}
|
|
121 |
return statDir(rootPath, "", isIncludeDir, false, true)
|
87 | 122 |
}
|
88 | 123 |
|
89 | 124 |
// GetAllSubDirs returns all subdirectories of given root path.
|
|
92 | 127 |
if !IsDir(rootPath) {
|
93 | 128 |
return nil, errors.New("not a directory or does not exist: " + rootPath)
|
94 | 129 |
}
|
95 | |
return statDir(rootPath, "", true, true)
|
|
130 |
return statDir(rootPath, "", true, true, false)
|
|
131 |
}
|
|
132 |
|
|
133 |
// LgetAllSubDirs returns all subdirectories of given root path, including
|
|
134 |
// following symbolic links, if any.
|
|
135 |
// Slice does not include given path itself.
|
|
136 |
func LgetAllSubDirs(rootPath string) ([]string, error) {
|
|
137 |
if !IsDir(rootPath) {
|
|
138 |
return nil, errors.New("not a directory or does not exist: " + rootPath)
|
|
139 |
}
|
|
140 |
return statDir(rootPath, "", true, true, true)
|
96 | 141 |
}
|
97 | 142 |
|
98 | 143 |
// GetFileListBySuffix returns an ordered list of file paths.
|