MCPcopy
hub / github.com/wavetermdev/waveterm / ReadDirRecursive

Function ReadDirRecursive

pkg/util/fileutil/readdir.go:139–234  ·  view source on GitHub ↗
(path string, maxEntries int)

Source from the content-addressed store, hash-verified

137}
138
139func ReadDirRecursive(path string, maxEntries int) (*ReadDirResult, error) {
140 expandedPath, err := wavebase.ExpandHomeDir(path)
141 if err != nil {
142 return nil, fmt.Errorf("failed to expand path: %w", err)
143 }
144
145 fileInfo, err := os.Stat(expandedPath)
146 if err != nil {
147 return nil, fmt.Errorf("failed to stat path: %w", err)
148 }
149
150 if !fileInfo.IsDir() {
151 return nil, fmt.Errorf("path is not a directory")
152 }
153
154 var allEntries []DirEntryOut
155 isDirMap := make(map[string]bool)
156 var truncated bool
157
158 err = filepath.WalkDir(expandedPath, func(fullPath string, d fs.DirEntry, err error) error {
159 if err != nil {
160 return nil
161 }
162
163 if fullPath == expandedPath {
164 return nil
165 }
166
167 if len(allEntries) >= maxEntries {
168 truncated = true
169 return fs.SkipAll
170 }
171
172 relativePath, _ := filepath.Rel(expandedPath, fullPath)
173
174 isSymlink := d.Type()&fs.ModeSymlink != 0
175
176 info, infoErr := d.Info()
177 if infoErr != nil {
178 return nil
179 }
180
181 isDir := d.IsDir()
182 isDirMap[relativePath] = isDir
183
184 entryData := DirEntryOut{
185 Name: relativePath,
186 Dir: isDir,
187 Symlink: isSymlink,
188 Mode: info.Mode().String(),
189 Modified: utilfn.FormatRelativeTime(info.ModTime()),
190 ModifiedTime: info.ModTime().UTC().Format(time.RFC3339),
191 }
192
193 if !isDir {
194 entryData.Size = info.Size()
195 }
196

Callers 1

ListAllAppFilesFunction · 0.92

Calls 10

ExpandHomeDirFunction · 0.92
FormatRelativeTimeFunction · 0.92
StatMethod · 0.80
TypeMethod · 0.80
InfoMethod · 0.80
ModeMethod · 0.80
ModTimeMethod · 0.80
IsDirMethod · 0.45
StringMethod · 0.45
SizeMethod · 0.45

Tested by

no test coverage detected