@@ -16,6 +16,8 @@ import (
16
16
"github.com/coder/coder/v2/codersdk"
17
17
)
18
18
19
+ var WindowsDriveRegex = regexp .MustCompile (`^[a-zA-Z]:\\$` )
20
+
19
21
func (* agent ) HandleLS (rw http.ResponseWriter , r * http.Request ) {
20
22
ctx := r .Context ()
21
23
@@ -57,8 +59,7 @@ func listFiles(query LSRequest) (LSResponse, error) {
57
59
if len (query .Path ) == 0 {
58
60
return listDrives ()
59
61
}
60
- re := regexp .MustCompile (`^[a-zA-Z]:\\$` )
61
- if ! re .MatchString (query .Path [0 ]) {
62
+ if ! WindowsDriveRegex .MatchString (query .Path [0 ]) {
62
63
return LSResponse {}, xerrors .Errorf ("invalid drive letter %q" , query .Path [0 ])
63
64
}
64
65
} else {
@@ -74,6 +75,7 @@ func listFiles(query LSRequest) (LSResponse, error) {
74
75
if err != nil {
75
76
return LSResponse {}, xerrors .Errorf ("failed to get absolute path of %q: %w" , fullPathRelative , err )
76
77
}
78
+ absolutePath := pathToArray (absolutePathString )
77
79
78
80
f , err := os .Open (absolutePathString )
79
81
if err != nil {
@@ -87,7 +89,18 @@ func listFiles(query LSRequest) (LSResponse, error) {
87
89
}
88
90
89
91
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
91
104
}
92
105
93
106
// `contents` may be partially populated even if the operation fails midway.
@@ -101,8 +114,6 @@ func listFiles(query LSRequest) (LSResponse, error) {
101
114
})
102
115
}
103
116
104
- absolutePath := pathToArray (absolutePathString )
105
-
106
117
return LSResponse {
107
118
AbsolutePath : absolutePath ,
108
119
AbsolutePathString : absolutePathString ,
@@ -111,12 +122,12 @@ func listFiles(query LSRequest) (LSResponse, error) {
111
122
}
112
123
113
124
func listDrives () (LSResponse , error ) {
114
- aa , err := disk .Partitions (true )
125
+ partitionStats , err := disk .Partitions (true )
115
126
if err != nil {
116
127
return LSResponse {}, xerrors .Errorf ("failed to get partitions: %w" , err )
117
128
}
118
- contents := make ([]LSFile , 0 , len (aa ))
119
- for _ , a := range aa {
129
+ contents := make ([]LSFile , 0 , len (partitionStats ))
130
+ for _ , a := range partitionStats {
120
131
name := a .Mountpoint + string (os .PathSeparator )
121
132
contents = append (contents , LSFile {
122
133
Name : name ,
0 commit comments