10000 chore: add agent endpoint for querying file system by ethanndickson · Pull Request #16736 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

chore: add agent endpoint for querying file system #16736

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

Merged
merged 11 commits into from
Mar 7, 2025
Prev Previous commit
Next Next commit
review
  • Loading branch information
ethanndickson committed Mar 7, 2025
commit 01c2e72c1099dcb54e4e329ae100eeb5ac442416
27 changes: 19 additions & 8 deletions agent/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/coder/coder/v2/codersdk"
)

var WindowsDriveRegex = regexp.MustCompile(`^[a-zA-Z]:\\$`)

func (*agent) HandleLS(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()

Expand Down Expand Up @@ -57,8 +59,7 @@ func listFiles(query LSRequest) (LSResponse, error) {
if len(query.Path) == 0 {
return listDrives()
}
re := regexp.MustCompile(`^[a-zA-Z]:\\$`)
if !re.MatchString(query.Path[0]) {
if !WindowsDriveRegex.MatchString(query.Path[0]) {
return LSResponse{}, xerrors.Errorf("invalid drive letter %q", query.Path[0])
}
} else {
Expand All @@ -74,6 +75,7 @@ func listFiles(query LSRequest) (LSResponse, error) {
if err != nil {
return LSResponse{}, xerrors.Errorf("failed to get absolute path of %q: %w", fullPathRelative, err)
}
absolutePath := pathToArray(absolutePathString)

f, err := os.Open(absolutePathString)
if err != nil {
Expand All @@ -87,7 +89,18 @@ func listFiles(query LSRequest) (LSResponse, error) {
}

if !stat.IsDir() {
return LSResponse{}, xerrors.Errorf("path %q is not a directory", absolutePathString)
// `ls` on a file should return just that file.
return LSResponse{
AbsolutePath: absolutePath,
AbsolutePathString: absolutePathString,
Contents: []LSFile{
{
Name: f.Name(),
AbsolutePathString: absolutePathString,
IsDir: false,
},
},
}, nil
}

// `contents` may be partially populated even if the operation fails midway.
Expand All @@ -101,8 +114,6 @@ func listFiles(query LSRequest) (LSResponse, error) {
})
}

absolutePath := pathToArray(absolutePathString)

return LSResponse{
AbsolutePath: absolutePath,
AbsolutePathString: absolutePathString,
Expand All @@ -111,12 +122,12 @@ func listFiles(query LSRequest) (LSResponse, error) {
}

func listDrives() (LSResponse, error) {
aa, err := disk.Partitions(true)
partitionStats, err := disk.Partitions(true)
if err != nil {
return LSResponse{}, xerrors.Errorf("failed to get partitions: %w", err)
}
contents := make([]LSFile, 0, len(aa))
for _, a := range aa {
contents := make([]LSFile, 0, len(partitionStats))
for _, a := range partitionStats {
name := a.Mountpoint + string(os.PathSeparator)
contents = append(contents, LSFile{
Name: name,
Expand Down
0