MCPcopy Index your code
hub / github.com/coder/coder / listFiles

Function listFiles

agent/agentfiles/ls.go:65–149  ·  view source on GitHub ↗
(fs afero.Fs, path string, query workspacesdk.LSRequest)

Source from the content-addressed store, hash-verified

63}
64
65func listFiles(fs afero.Fs, path string, query workspacesdk.LSRequest) (workspacesdk.LSResponse, error) {
66 absolutePathString := path
67 if absolutePathString != "" {
68 if !filepath.IsAbs(path) {
69 return workspacesdk.LSResponse{}, xerrors.Errorf("path must be absolute: %q", path)
70 }
71 } else {
72 var fullPath []string
73 switch query.Relativity {
74 case workspacesdk.LSRelativityHome:
75 home, err := os.UserHomeDir()
76 if err != nil {
77 return workspacesdk.LSResponse{}, xerrors.Errorf("failed to get user home directory: %w", err)
78 }
79 fullPath = []string{home}
80 case workspacesdk.LSRelativityRoot:
81 if runtime.GOOS == "windows" {
82 if len(query.Path) == 0 {
83 return listDrives()
84 }
85 if !WindowsDriveRegex.MatchString(query.Path[0]) {
86 return workspacesdk.LSResponse{}, xerrors.Errorf("invalid drive letter %q", query.Path[0])
87 }
88 } else {
89 fullPath = []string{"/"}
90 }
91 default:
92 return workspacesdk.LSResponse{}, xerrors.Errorf("unsupported relativity type %q", query.Relativity)
93 }
94
95 fullPath = append(fullPath, query.Path...)
96 fullPathRelative := filepath.Join(fullPath...)
97 var err error
98 absolutePathString, err = filepath.Abs(fullPathRelative)
99 if err != nil {
100 return workspacesdk.LSResponse{}, xerrors.Errorf("failed to get absolute path of %q: %w", fullPathRelative, err)
101 }
102 }
103
104 // codeql[go/path-injection] - The intent is to allow the user to navigate to any directory in their workspace.
105 f, err := fs.Open(absolutePathString)
106 if err != nil {
107 return workspacesdk.LSResponse{}, xerrors.Errorf("failed to open directory %q: %w", absolutePathString, err)
108 }
109 defer f.Close()
110
111 stat, err := f.Stat()
112 if err != nil {
113 return workspacesdk.LSResponse{}, xerrors.Errorf("failed to stat directory %q: %w", absolutePathString, err)
114 }
115
116 if !stat.IsDir() {
117 return workspacesdk.LSResponse{}, xerrors.Errorf("path %q is not a directory", absolutePathString)
118 }
119
120 // `contents` may be partially populated even if the operation fails midway.
121 contents, _ := f.Readdir(-1)
122 respContents := make([]workspacesdk.LSFile, 0, len(contents))

Callers 7

TestListFilesSuccessFunction · 0.85
TestListFilesListDrivesFunction · 0.85
HandleLSMethod · 0.85

Calls 7

listDrivesFunction · 0.85
pathToArrayFunction · 0.85
CompareMethod · 0.80
CloseMethod · 0.65
NameMethod · 0.65
ErrorfMethod · 0.45
OpenMethod · 0.45

Tested by 6

TestListFilesSuccessFunction · 0.68
TestListFilesListDrivesFunction · 0.68