(path string, maxEntries int)
| 137 | } |
| 138 | |
| 139 | func 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 |
no test coverage detected