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