fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
ignore
1package analyze 2 3import ( 4"os" 5"path/filepath" 6"sync" 7"time" 8 9"github.com/dundee/gdu/v5/internal/common" 10"github.com/dundee/gdu/v5/pkg/fs" 11 log"github.com/sirupsen/logrus" 12) 13 14var pathSep = string (os .PathSeparator ) 15 16var _ common.Analyzer = (* TopDirAnalyzer )(nil ) 17 18// TopDirAnalyzer implements Analyzer 19// It doesn't return the full directory structure, only the top level directory, 20// thus is suitable only for non-interactive mode. 21// It tries to use only stack for storing state and results. 22type TopDirAnalyzer struct { 23BaseAnalyzer 24linkedItems sync.Map 25} 26 27// CreateTopDirAnalyzer returns Analyzer 28func CreateTopDirAnalyzer ()* TopDirAnalyzer { 29a := & TopDirAnalyzer { 30BaseAnalyzer :BaseAnalyzer { 31ignoreFileType :func (string )bool {return false }, 32matchesTimeFilterFn :func (time.Time )bool {return true }, 33 }, 34 } 35a .Init () 36return a 37} 38 39// AnalyzeDir analyzes given path 40func (a * TopDirAnalyzer )AnalyzeDir ( 41path string ,ignore common.ShouldDirBeIgnored ,fileTypeFilter common.ShouldFileBeIgnored , 42) fs.Item { 43a .ignoreDir = ignore 44if fileTypeFilter != nil { 45a .ignoreFileType = fileTypeFilter 46 } 47 48var subDirChan = make (chan struct {}) 49 50go a .UpdateProgress () 51 52files ,err := os .ReadDir (path ) 53if err != nil { 54log .err .Error ()) 55 } 56 57dir := SimpleDir { 58SimpleFile :SimpleFile { 59Name :filepath .Base (path ), 60Flag :getDirFlag (err ,len (files )), 61IsDir :true , 62ItemCount :1 , 63 }, 64Files :make ([]SimpleFile ,0 ,len (files )), 65 } 66 67var topDirs []* TopDir 68 69for _ ,f := range files { 70if a .IsCancelled () { 71break 72 } 73name := f .Name () 74entryPath := filepath .Join (path ,name ) 75if f .IsDir () { 76if a .shouldSkipDir (name ,entryPath ) { 77continue 78 } 79topDir := & TopDir { 80Name :name , 81Flag :' ' , 82 } 83topDirs = append (topDirs ,topDir ) 84go func (entryPath string ) { 85a .processSubDir (entryPath ,topDir ) 86subDirChan <- struct {}{} 87 }(entryPath ) 88 }else { 89var info os.FileInfo 90// Apply file type filter if set 91if a .ignoreFileType (name ) { 92continue // Skip this file 93 } 94 95info ,err = f .Info () 96if err != nil { 97log .err .Error ()) 98dir .Flag = '!' 99continue 100 } 101 102// Apply time filter if set 103if ! a .matchesTimeFilterFn (info .ModTime ()) { 104continue // Skip this file 105 } 106 107if a .followSymlinks && info .Mode ()& os .ModeSymlink != 0 { 108infoF ,err := followSymlink (entryPath ,a .gitAnnexedSize ) 109if err != nil { 110log .err .Error ()) 111dir .Flag = '!' 112continue 113 } 114if infoF != nil { 115info = infoF 116 } 117 } 118 119file := SimpleFile { 120Name :name , 121Flag :getFlag (info ), 122Size :info .Size (), 123Symlink :readSymlinkTarget (f .Type (),entryPath ), 124 } 125 126usage ,mli := getPlatformSpecificUsageAndMli (info ) 127file .Usage = usage 128 129if mli > 0 { 130file .Flag = 'H' 131 } 132 133dir .Files = append (dir .Files ,file ) 134 } 135 } 136 137for i := 0 ;i < len (topDirs );i ++ { 138<- subDirChan 139 } 140 141a .wait .Wait () 142 143for _ ,topDir := range topDirs { 144size ,usage ,itemCount := topDir .GetUsage () 145dir .Files = append (dir .Files ,SimpleFile { 146Name :topDir .Name , 147Flag :topDir .Flag , 148Size :size , 149Usage :usage , 150ItemCount :itemCount , 151IsDir :true , 152 }) 153 } 154 155dir .BasePath = filepath .Dir (path ) 156 157a .progressDoneChan <- struct {}{} 158a .doneChan .Broadcast () 159 160return & dir 161} 162 163func (a * TopDirAnalyzer )processSubDir (path string ,topDir * TopDir ) { 164var ( 165err error 166totalSize int64 167totalUsage int64 168totalCount int64 169info os.FileInfo 170 ) 171 172files ,err := os .ReadDir (path ) 173if err != nil { 174log .err .Error ()) 175topDir .SetFlag ('.' ) 176 } 177 178for _ ,f := range files { 179if a .IsCancelled () { 180break 181 } 182name := f .Name () 183entryPath := path + pathSep + name 184if f .IsDir () { 185if a .shouldSkipDir (name ,entryPath ) { 186continue 187 } 188 189totalCount ++ 190 191select { 192case concurrencyLimit <- struct {}{}: 193a .wait .Add (1 ) 194go func (entryPath string ) { 195a .processSubDir (entryPath ,topDir ) 196<- concurrencyLimit 197a .wait .Done () 198 }(entryPath ) 199default : 200a .processSubDir (entryPath ,topDir ) 201 } 202 }else { 203// Apply file type filter if set 204if a .ignoreFileType (name ) { 205continue // Skip this file 206 } 207 208info ,err = f .Info () 209if err != nil { 210log .err .Error ()) 211topDir .SetFlag ('.' ) 212continue 213 } 214 215// Apply time filter if set 216if ! a .matchesTimeFilterFn (info .ModTime ()) { 217continue // Skip this file 218 } 219 220totalCount ++ 221 222if a .followSymlinks && info .Mode ()& os .ModeSymlink != 0 { 223infoF ,err := followSymlink (entryPath ,a .gitAnnexedSize ) 224if err != nil { 225log .err .Error ()) 226topDir .SetFlag ('.' ) 227continue 228 } 229if infoF != nil { 230info = infoF 231 } 232 } 233 234usage ,mli := getPlatformSpecificUsageAndMli (info ) 235 236if mli > 0 { 237if _ ,loaded := a .linkedItems .LoadOrStore (mli ,struct {}{});loaded { 238continue 239 } 240 } 241 242totalUsage += usage 243totalSize += info .Size () 244 } 245 } 246 247if len (files )== 0 { 248totalSize = EmptyDirSize 249totalUsage = 0 250 } 251 252a .progressItemCount .Add (totalCount ) 253a .progressTotalUsage .Add (totalUsage ) 254 255topDir .AddUsage (totalSize ,totalUsage ,totalCount ) 256}