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

Function ReadDir

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

Source from the content-addressed store, hash-verified

36}
37
38func ReadDir(path string, maxEntries int) (*ReadDirResult, error) {
39 expandedPath, err := wavebase.ExpandHomeDir(path)
40 if err != nil {
41 return nil, fmt.Errorf("failed to expand path: %w", err)
42 }
43
44 fileInfo, err := os.Stat(expandedPath)
45 if err != nil {
46 return nil, fmt.Errorf("failed to stat path: %w", err)
47 }
48
49 if !fileInfo.IsDir() {
50 return nil, fmt.Errorf("path is not a directory")
51 }
52
53 entries, err := os.ReadDir(expandedPath)
54 if err != nil {
55 return nil, fmt.Errorf("failed to read directory: %w", err)
56 }
57
58 totalEntries := len(entries)
59
60 isDirMap := make(map[string]bool)
61 symlinkCount := 0
62 for _, entry := range entries {
63 name := entry.Name()
64 if entry.Type()&fs.ModeSymlink != 0 {
65 if symlinkCount < 1000 {
66 symlinkCount++
67 fullPath := filepath.Join(expandedPath, name)
68 if info, err := os.Stat(fullPath); err == nil {
69 isDirMap[name] = info.IsDir()
70 } else {
71 isDirMap[name] = entry.IsDir()
72 }
73 } else {
74 isDirMap[name] = entry.IsDir()
75 }
76 } else {
77 isDirMap[name] = entry.IsDir()
78 }
79 }
80
81 sort.Slice(entries, func(i, j int) bool {
82 iIsDir := isDirMap[entries[i].Name()]
83 jIsDir := isDirMap[entries[j].Name()]
84 if iIsDir != jIsDir {
85 return iIsDir
86 }
87 return entries[i].Name() < entries[j].Name()
88 })
89
90 var truncated bool
91 if len(entries) > maxEntries {
92 entries = entries[:maxEntries]
93 truncated = true
94 }
95

Callers 2

readDirCallbackFunction · 0.92
aiRunFunction · 0.92

Calls 11

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

Tested by

no test coverage detected