MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / ListContainerFiles

Method ListContainerFiles

agent/app/service/container.go:1178–1242  ·  view source on GitHub ↗
(req dto.ContainerFileReq)

Source from the content-addressed store, hash-verified

1176}
1177
1178func (u *ContainerService) ListContainerFiles(req dto.ContainerFileReq) ([]dto.ContainerFileInfo, error) {
1179 if len(req.Path) == 0 {
1180 req.Path = "/"
1181 }
1182 cli, err := docker.NewDockerClient()
1183 if err != nil {
1184 return nil, err
1185 }
1186 defer cli.Close()
1187
1188 ctx := context.Background()
1189 stat, err := cli.ContainerStatPath(ctx, req.ContainerID, req.Path)
1190 if err != nil {
1191 return nil, normalizeContainerFileError(err)
1192 }
1193 isDir := stat.Mode.IsDir()
1194 isLink := stat.Mode&os.ModeSymlink != 0
1195 if isLink && !isDir {
1196 linkDir, linkErr := isContainerDir(cli, req.ContainerID, req.Path)
1197 if linkErr == nil {
1198 isDir = linkDir
1199 }
1200 }
1201 if !isDir {
1202 return []dto.ContainerFileInfo{toContainerFileInfo(req.Path, stat, isDir)}, nil
1203 }
1204
1205 output, err := runContainerCommand(cli, req.ContainerID, []string{"ls", "-1A", "--", req.Path})
1206 if err != nil {
1207 return nil, normalizeContainerFileError(err)
1208 }
1209 lines := strings.Split(strings.TrimSpace(output), "\n")
1210 files := make([]dto.ContainerFileInfo, 0, len(lines))
1211 for _, line := range lines {
1212 name := strings.TrimSpace(line)
1213 if len(name) == 0 || name == "." || name == ".." {
1214 continue
1215 }
1216 childPath := req.Path
1217 if childPath == "/" {
1218 childPath = "/" + name
1219 } else {
1220 childPath = strings.TrimSuffix(childPath, "/") + "/" + name
1221 }
1222 childStat, statErr := cli.ContainerStatPath(ctx, req.ContainerID, childPath)
1223 if statErr != nil {
1224 continue
1225 }
1226 childIsDir := childStat.Mode.IsDir()
1227 if childStat.Mode&os.ModeSymlink != 0 && !childIsDir {
1228 linkDir, linkErr := isContainerDir(cli, req.ContainerID, childPath)
1229 if linkErr == nil {
1230 childIsDir = linkDir
1231 }
1232 }
1233 files = append(files, toContainerFileInfo(childPath, childStat, childIsDir))
1234 }
1235 sort.Slice(files, func(i, j int) bool {

Callers

nothing calls this directly

Calls 6

isContainerDirFunction · 0.85
toContainerFileInfoFunction · 0.85
runContainerCommandFunction · 0.85
IsDirMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected