fork/gdu
git clone https://git.julia.blue/fork/gdu
34f3996
master
1package webui 2 3import ( 4"errors" 5"os" 6"path/filepath" 7"strings" 8 9"github.com/dundee/gdu/v5/pkg/analyze" 10"github.com/dundee/gdu/v5/pkg/fs" 11) 12 13// errNotFound is returned when a requested path is not present in the tree. 14var errNotFound = errors .New ("node not found" ) 15 16// errOutsideRoot is returned when a requested path escapes the scanned root. 17var errOutsideRoot = errors .New ("path is outside the scanned root" ) 18 19// nodeJSON is the wire representation of a single tree item. Sizes are raw 20// byte counts; the browser is responsible for human-readable formatting. 21type nodeJSON struct { 22Name string `json:"name"` 23Path string `json:"path"` 24IsDir bool `json:"isDir"` 25Size int64 `json:"size"` 26Usage int64 `json:"usage"` 27ItemCount int64 `json:"itemCount"` 28Mtime int64 `json:"mtime"` 29Flag string `json:"flag,omitempty"` 30} 31 32// nodeResponse is the payload of GET /api/v1/nodes: the node itself, its 33// breadcrumb ancestry, and only its immediate children. 34type nodeResponse struct { 35Node nodeJSON `json:"node"` 36Breadcrumbs []nodeJSON `json:"breadcrumbs"` 37Children []nodeJSON `json:"children"` 38} 39 40func toNodeJSON (it fs.Item )nodeJSON { 41flag := "" 42if f := it .GetFlag ();f != ' ' && f != 0 { 43flag = string (f ) 44 } 45return nodeJSON { 46// scanned roots are labelled with their absolute path, since their base 47// names collide when the roots come from different parent directories 48Name :analyze .ItemDisplayName (it .GetParent (),it ), 49Path :it .GetPath (), 50IsDir :it .IsDir (), 51Size :it .GetSize (), 52Usage :it .GetUsage (), 53ItemCount :fs .DisplayedItemCount (it ), 54Mtime :it .GetMtime ().Unix (), 55Flag :flag , 56 } 57} 58 59// findNode locates an item by absolute path, descending from the scanned root. 60// It guards against path traversal outside the root. 61func (ui * UI )findNode (path string ) (fs.Item ,error ) { 62ui .mu .RLock () 63root := ui .topDir 64rootPath := ui .topDirPath 65ui .mu .RUnlock () 66 67if root == nil { 68return nil ,errNotFound 69 } 70if rootPath == "" { 71rootPath = root .GetPath () 72 } 73 74cleanRoot := filepath .Clean (rootPath ) 75if path == "" { 76return root ,nil 77 } 78cleanPath := filepath .Clean (path ) 79if cleanPath == cleanRoot { 80return root ,nil 81 } 82 83// The virtual top level dir has no path of its own, so paths are resolved 84// against each of the scanned roots it holds instead. 85if analyze .IsVirtualRootDir (root ) { 86for child := range root .GetFiles (fs .SortByName ,fs .SortAsc ) { 87if node ,err := descendFrom (child ,child .GetPath (),cleanPath );err == nil { 88return node ,nil 89 } 90 } 91return nil ,errOutsideRoot 92 } 93 94return descendFrom (root ,cleanRoot ,cleanPath ) 95} 96 97// descendFrom walks from root, which lives at cleanRoot, down to cleanPath. 98// It guards against path traversal outside the root. 99func descendFrom (root fs.Item ,cleanRoot ,cleanPath string ) (fs.Item ,error ) { 100if cleanPath == cleanRoot { 101return root ,nil 102 } 103 104rel ,err := filepath .Rel (cleanRoot ,cleanPath ) 105if err != nil { 106return nil ,errOutsideRoot 107 } 108if rel == ".." || strings .HasPrefix (rel ,".." + string (os .PathSeparator )) { 109return nil ,errOutsideRoot 110 } 111 112current := root 113for _ ,segment := range strings .Split (rel ,string (os .PathSeparator )) { 114if segment == "" || segment == "." { 115continue 116 } 117next ,found := childByName (current ,segment ) 118if ! found { 119return nil ,errNotFound 120 } 121current = next 122 } 123return current ,nil 124} 125 126func childByName (parent fs.Item ,name string ) (fs.Item ,bool ) { 127if ! parent .IsDir () { 128return nil ,false 129 } 130for child := range parent .GetFilesLocked (fs .SortByName ,fs .SortAsc ) { 131if child .GetName ()== name { 132return child ,true 133 } 134 } 135return nil ,false 136} 137 138// buildNodeResponse assembles a node, its breadcrumbs, and immediate children, 139// sorted as requested. 140func buildNodeResponse (it fs.Item ,sortBy fs.SortBy ,order fs.SortOrder )nodeResponse { 141resp := nodeResponse { 142Node :toNodeJSON (it ), 143Breadcrumbs :breadcrumbs (it ), 144Children : []nodeJSON {}, 145 } 146if it .IsDir () { 147for child := range it .GetFilesLocked (sortBy ,order ) { 148resp .Children = append (resp .Children ,toNodeJSON (child )) 149 } 150 } 151return resp 152} 153 154// breadcrumbs walks parents from the item up to the root and returns them 155// root-first. 156func breadcrumbs (it fs.Item ) []nodeJSON { 157var chain []nodeJSON 158for cur := it ;cur != nil ;cur = cur .GetParent () { 159chain = append (chain ,toNodeJSON (cur )) 160 } 161// reverse so the root comes first 162for i ,j := 0 ,len (chain )- 1 ;i < j ;i ,j = i + 1 ,j - 1 { 163chain [i ],chain [j ]= chain [j ],chain [i ] 164 } 165return chain 166} 167 168// parseSort maps query parameters to fs sort enums, defaulting to size 169// descending (largest first), which matches Gdu's usual presentation. 170func parseSort (sortParam ,orderParam string ) (fs.SortBy , fs.SortOrder ) { 171sortBy := fs .SortBySize 172switch sortParam { 173case "name" : 174sortBy = fs .SortByName 175case "itemCount" ,"itemcount" : 176sortBy = fs .SortByItemCount 177case "mtime" : 178sortBy = fs .SortByMtime 179case "size" ,"" : 180sortBy = fs .SortBySize 181 } 182 183order := fs .SortDesc 184if orderParam == "asc" { 185order = fs .SortAsc 186 } 187return sortBy ,order 188}