Skip to content

Commit 01c2e72

Browse files
committed
review
1 parent e0089bd commit 01c2e72

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

agent/ls.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/coder/coder/v2/codersdk"
1717
)
1818

19+
var WindowsDriveRegex = regexp.MustCompile(`^[a-zA-Z]:\\$`)
20+
1921
func (*agent) HandleLS(rw http.ResponseWriter, r *http.Request) {
2022
ctx := r.Context()
2123

@@ -57,8 +59,7 @@ func listFiles(query LSRequest) (LSResponse, error) {
5759
if len(query.Path) == 0 {
5860
return listDrives()
5961
}
60-
re := regexp.MustCompile(`^[a-zA-Z]:\\$`)
61-
if !re.MatchString(query.Path[0]) {
62+
if !WindowsDriveRegex.MatchString(query.Path[0]) {
6263
return LSResponse{}, xerrors.Errorf("invalid drive letter %q", query.Path[0])
6364
}
6465
} else {
@@ -74,6 +75,7 @@ func listFiles(query LSRequest) (LSResponse, error) {
7475
if err != nil {
7576
return LSResponse{}, xerrors.Errorf("failed to get absolute path of %q: %w", fullPathRelative, err)
7677
}
78+
absolutePath := pathToArray(absolutePathString)
7779

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

8991
if !stat.IsDir() {
90-
return LSResponse{}, xerrors.Errorf("path %q is not a directory", absolutePathString)
92+
// `ls` on a file should return just that file.
93+
return LSResponse{
94+
AbsolutePath: absolutePath,
95+
AbsolutePathString: absolutePathString,
96+
Contents: []LSFile{
97+
{
98+
Name: f.Name(),
99+
AbsolutePathString: absolutePathString,
100+
IsDir: false,
101+
},
102+
},
103+
}, nil
91104
}
92105

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

104-
absolutePath := pathToArray(absolutePathString)
105-
106117
return LSResponse{
107118
AbsolutePath: absolutePath,
108119
AbsolutePathString: absolutePathString,
@@ -111,12 +122,12 @@ func listFiles(query LSRequest) (LSResponse, error) {
111122
}
112123

113124
func listDrives() (LSResponse, error) {
114-
aa, err := disk.Partitions(true)
125+
partitionStats, err := disk.Partitions(true)
115126
if err != nil {
116127
return LSResponse{}, xerrors.Errorf("failed to get partitions: %w", err)
117128
}
118-
contents := make([]LSFile, 0, len(aa))
119-
for _, a := range aa {
129+
contents := make([]LSFile, 0, len(partitionStats))
130+
for _, a := range partitionStats {
120131
name := a.Mountpoint + string(os.PathSeparator)
121132
contents = append(contents, LSFile{
122133
Name: name,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy