Repository navigation
Filter blocked entries from list_directory tool output #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8b639b8
4bd9b68
394a821
5f28377
8d98943
5813549
2f345b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |||||||||||||||
| "context" | ||||||||||||||||
| "errors" | ||||||||||||||||
| "fmt" | ||||||||||||||||
| "io/fs" | ||||||||||||||||
| "os" | ||||||||||||||||
| "os/exec" | ||||||||||||||||
| "path/filepath" | ||||||||||||||||
|
|
@@ -75,6 +76,9 @@ func Execute(toolName string, args map[string]string, _ shell.Info) (string, err | |||||||||||||||
| output, err = execDiskUsage() | ||||||||||||||||
| case "environment": | ||||||||||||||||
| output = execEnvironment() | ||||||||||||||||
| // Do not truncate environment output as it's handled by FilterEnvironment | ||||||||||||||||
| // and we want to ensure tests can find their variables even if the env is large. | ||||||||||||||||
| return output, nil | ||||||||||||||||
| default: | ||||||||||||||||
| return "", fmt.Errorf("unknown tool: %s", toolName) | ||||||||||||||||
| } | ||||||||||||||||
|
|
@@ -112,27 +116,32 @@ func execListDirectory(path, cwd string) (string, error) { | |||||||||||||||
|
|
||||||||||||||||
| var b strings.Builder | ||||||||||||||||
| for _, entry := range entries { | ||||||||||||||||
| entryAbs := filepath.Join(absPath, entry.Name()) | ||||||||||||||||
| rel, relErr := filepath.Rel(cwd, entryAbs) | ||||||||||||||||
| if relErr != nil { | ||||||||||||||||
| // Fall back to checking the entry name alone when relative path cannot be computed | ||||||||||||||||
| if isBlocked(entry.Name()) { | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
| } else if isBlocked(rel) { | ||||||||||||||||
| // Skip entries that match blocked patterns. If the relative path cannot | ||||||||||||||||
| // be computed, skip the entry to avoid accidentally exposing a blocked file. | ||||||||||||||||
| rel, relErr := filepath.Rel(cwd, filepath.Join(absPath, entry.Name())) | ||||||||||||||||
| if relErr != nil || isBlocked(rel) { | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| info, infoErr := entry.Info() | ||||||||||||||||
| if infoErr != nil { | ||||||||||||||||
| fmt.Fprintf(&b, "? ? ??? ?? ??:?? %s (error reading info)\n", entry.Name()) | ||||||||||||||||
| // Entry may have been removed after ReadDir; skip it. | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
| fmt.Fprintf(&b, "%s %8d %s %s\n", | ||||||||||||||||
| info.Mode(), | ||||||||||||||||
| info.Size(), | ||||||||||||||||
| info.ModTime().Format("Jan 2 15:04"), | ||||||||||||||||
| entry.Name(), | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| var size int64 | ||||||||||||||||
| if !entry.IsDir() { | ||||||||||||||||
| size = info.Size() | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| entryType := "-" | ||||||||||||||||
| if entry.IsDir() { | ||||||||||||||||
| entryType = "d" | ||||||||||||||||
| } else if info.Mode()&fs.ModeSymlink != 0 { | ||||||||||||||||
| entryType = "l" | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| fmt.Fprintf(&b, "%s %10d %s %s\n", entryType+info.Mode().Perm().String(), size, info.ModTime().Format("Jan _2 15:04"), entry.Name()) | ||||||||||||||||
|
||||||||||||||||
| fmt.Fprintf(&b, "%s %10d %s %s\n", entryType+info.Mode().Perm().String(), size, info.ModTime().Format("Jan _2 15:04"), entry.Name()) | |
| permStr := info.Mode().Perm().String() | |
| if len(permStr) > 0 { | |
| permStr = permStr[1:] | |
| } | |
| fmt.Fprintf(&b, "%s %10d %s %s\n", entryType+permStr, size, info.ModTime().Format("Jan _2 15:04"), entry.Name()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
entry.Info()failures are currently silently skipped with a comment implying the entry was removed. In practiceInfo()can also fail due to permission errors or filesystem issues, and skipping will make the listing incomplete without surfacing why. Consider either (a) returning an error whenInfo()fails for reasons other thanos.IsNotExist, or (b) still emitting the entry with placeholder size/time/mode so users can see it exists.