|
4 | 4 | package fs |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "errors" |
| 8 | + iofs "io/fs" |
7 | 9 | "net/http" |
8 | 10 | "os" |
9 | 11 | "path/filepath" |
| 12 | + "strconv" |
| 13 | + "strings" |
10 | 14 |
|
| 15 | + "github.com/daytonaio/daemon/internal/util" |
11 | 16 | "github.com/gin-gonic/gin" |
12 | 17 | ) |
13 | 18 |
|
14 | 19 | // ListFiles godoc |
15 | 20 | // |
16 | 21 | // @Summary List files and directories |
17 | | -// @Description List files and directories in the specified path |
| 22 | +// @Description List files and directories in the specified path. Use the optional depth |
| 23 | +// @Description parameter to list recursively: depth=1 (default) lists the directory's |
| 24 | +// @Description entries, depth=2 also includes their children, and so on. |
18 | 25 | // @Tags file-system |
19 | 26 | // @Produce json |
20 | 27 | // @Param path query string false "Directory path to list (defaults to working directory)" |
| 28 | +// @Param depth query int false "How many levels deep to list (default: 1, must be >= 1)" |
21 | 29 | // @Success 200 {array} FileInfo |
22 | 30 | // @Router /files [get] |
23 | 31 | // |
24 | 32 | // @id ListFiles |
25 | 33 | func ListFiles(c *gin.Context) { |
26 | | - path := c.Query("path") |
27 | | - if path == "" { |
28 | | - path = "." |
| 34 | + root := c.Query("path") |
| 35 | + if root == "" { |
| 36 | + root = "." |
29 | 37 | } |
30 | 38 |
|
31 | | - files, err := os.ReadDir(path) |
32 | | - if err != nil { |
33 | | - if os.IsNotExist(err) { |
34 | | - c.AbortWithError(http.StatusNotFound, err) |
| 39 | + depth := 1 |
| 40 | + if depthStr := c.Query("depth"); depthStr != "" { |
| 41 | + parsed, err := strconv.Atoi(depthStr) |
| 42 | + if err != nil || parsed < 1 { |
| 43 | + c.AbortWithError(http.StatusBadRequest, errors.New("depth must be an integer >= 1")) |
35 | 44 | return |
36 | 45 | } |
37 | | - if os.IsPermission(err) { |
38 | | - c.AbortWithError(http.StatusForbidden, err) |
39 | | - return |
40 | | - } |
41 | | - c.AbortWithError(http.StatusBadRequest, err) |
| 46 | + depth = parsed |
| 47 | + } |
| 48 | + |
| 49 | + stripPath := util.ClientRejectsUnknownResponseFields(c.Request.Header) |
| 50 | + |
| 51 | + // depth=1 uses the original os.ReadDir code path to avoid behavioural |
| 52 | + // regressions for the default (most common) case. |
| 53 | + if depth == 1 { |
| 54 | + listFilesShallow(c, root, stripPath) |
42 | 55 | return |
43 | 56 | } |
| 57 | + listFilesRecursive(c, root, depth, stripPath) |
| 58 | +} |
44 | 59 |
|
45 | | - var fileInfos = make([]FileInfo, 0) |
| 60 | +func listFilesShallow(c *gin.Context, path string, stripPath bool) { |
| 61 | + files, err := os.ReadDir(path) |
| 62 | + if err != nil { |
| 63 | + abortWithFsError(c, err) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + fileInfos := make([]FileInfo, 0) |
46 | 68 | for _, file := range files { |
47 | | - info, err := getFileInfo(filepath.Join(path, file.Name())) |
| 69 | + fullPath := filepath.Join(path, file.Name()) |
| 70 | + info, err := getFileInfo(fullPath) |
48 | 71 | if err != nil { |
49 | 72 | continue |
50 | 73 | } |
| 74 | + if !stripPath { |
| 75 | + info.Path = fullPath |
| 76 | + } |
51 | 77 | fileInfos = append(fileInfos, info) |
52 | 78 | } |
53 | 79 |
|
54 | 80 | c.JSON(http.StatusOK, fileInfos) |
55 | 81 | } |
| 82 | + |
| 83 | +// listFilesRecursive returns a flat listing up to depth levels below root; |
| 84 | +// unreadable subtrees are skipped and symlinks are not followed. |
| 85 | +func listFilesRecursive(c *gin.Context, root string, depth int, stripPath bool) { |
| 86 | + if _, err := os.ReadDir(root); err != nil { |
| 87 | + abortWithFsError(c, err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + fileInfos := make([]FileInfo, 0) |
| 92 | + _ = filepath.WalkDir(root, func(entryPath string, d iofs.DirEntry, err error) error { |
| 93 | + if err != nil { |
| 94 | + if d != nil && d.IsDir() { |
| 95 | + return filepath.SkipDir |
| 96 | + } |
| 97 | + return nil |
| 98 | + } |
| 99 | + if entryPath == root { |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + rel, relErr := filepath.Rel(root, entryPath) |
| 104 | + if relErr != nil { |
| 105 | + return nil |
| 106 | + } |
| 107 | + entryDepth := strings.Count(rel, string(os.PathSeparator)) + 1 |
| 108 | + if entryDepth > depth { |
| 109 | + if d.IsDir() { |
| 110 | + return filepath.SkipDir |
| 111 | + } |
| 112 | + return nil |
| 113 | + } |
| 114 | + |
| 115 | + if info, infoErr := getFileInfo(entryPath); infoErr == nil { |
| 116 | + if !stripPath { |
| 117 | + info.Path = entryPath |
| 118 | + } |
| 119 | + fileInfos = append(fileInfos, info) |
| 120 | + } |
| 121 | + |
| 122 | + if d.IsDir() && entryDepth >= depth { |
| 123 | + return filepath.SkipDir |
| 124 | + } |
| 125 | + return nil |
| 126 | + }) |
| 127 | + |
| 128 | + c.JSON(http.StatusOK, fileInfos) |
| 129 | +} |
| 130 | + |
| 131 | +func abortWithFsError(c *gin.Context, err error) { |
| 132 | + if os.IsNotExist(err) { |
| 133 | + c.AbortWithError(http.StatusNotFound, err) |
| 134 | + return |
| 135 | + } |
| 136 | + if os.IsPermission(err) { |
| 137 | + c.AbortWithError(http.StatusForbidden, err) |
| 138 | + return |
| 139 | + } |
| 140 | + c.AbortWithError(http.StatusBadRequest, err) |
| 141 | +} |
0 commit comments