fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
ignore
1package analyze 2 3import ( 4"os" 5"path/filepath" 6 7"github.com/dundee/gdu/v5/internal/common" 8"github.com/dundee/gdu/v5/pkg/fs" 9 log"github.com/sirupsen/logrus" 10) 11 12// SequentialAnalyzer implements Analyzer 13type SequentialAnalyzer struct { 14BaseAnalyzer 15} 16 17// CreateSeqAnalyzer returns Analyzer 18func CreateSeqAnalyzer ()* SequentialAnalyzer { 19a := & SequentialAnalyzer {} 20a .Init () 21return a 22} 23 24// AnalyzeDir analyzes given path 25func (a * SequentialAnalyzer )AnalyzeDir ( 26path string ,ignore common.ShouldDirBeIgnored ,fileTypeFilter common.ShouldFileBeIgnored , 27) fs.Item { 28a .ignoreDir = ignore 29a .ignoreFileType = fileTypeFilter 30 31go a .UpdateProgress () 32dir := a .processDir (path ) 33 34dir .BasePath = filepath .Dir (path ) 35a .setCurrentDir (dir ) 36 37a .progressDoneChan <- struct {}{} 38a .doneChan .Broadcast () 39 40return dir 41} 42 43func (a * SequentialAnalyzer )processDir (path string )* Dir { 44var ( 45file fs.Item 46err error 47totalSize int64 48info os.FileInfo 49dirCount int 50 ) 51 52files ,err := os .ReadDir (path ) 53if err != nil { 54log .err .Error ()) 55 } 56 57dir := & Dir { 58File :& File { 59Name :filepath .Base (path ), 60Flag :getDirFlag (err ,len (files )), 61 }, 62ItemCount :1 , 63Files :make (fs.Files ,0 ,len (files )), 64 } 65setDirPlatformSpecificAttrs (dir ,path ) 66 67for _ ,f := range files { 68if a .IsCancelled () { 69break 70 } 71name := f .Name () 72entryPath := filepath .Join (path ,name ) 73if f .IsDir () { 74if a .shouldSkipDir (name ,entryPath ) { 75continue 76 } 77dirCount ++ 78 79subdir := a .processDir (entryPath ) 80subdir .Parent = dir 81dir .AddFile (subdir ) 82 }else { 83// Apply file type filter if set 84if a .ignoreFileType != nil && a .ignoreFileType (name ) { 85continue // Skip this file 86 } 87 88info ,err = f .Info () 89if err != nil { 90log .err .Error ()) 91dir .SetFlag ('!' ) 92continue 93 } 94 95if a .followSymlinks && info .Mode ()& os .ModeSymlink != 0 { 96infoF ,err := followSymlink (entryPath ,a .gitAnnexedSize ) 97if err != nil { 98log .err .Error ()) 99dir .SetFlag ('!' ) 100continue 101 } 102if infoF != nil { 103info = infoF 104 } 105 } 106 107symlinkTarget := readSymlinkTarget (f .Type (),entryPath ) 108 109// Apply time filter if set 110if a .matchesTimeFilterFn != nil && ! a .matchesTimeFilterFn (info .ModTime ()) { 111continue // Skip this file 112 } 113 114switch { 115case a .archiveBrowsing && isZipFile (name ): 116zipDir ,err := processZipFile (entryPath ,info ) 117if err != nil { 118log .Printf ("Failed to process zip file %s: %v" ,entryPath ,err ) 119file = & File { 120Name :name , 121Flag :getFlag (info ), 122Size :info .Size (), 123Parent :dir , 124 } 125 }else { 126uncompressedSize ,compressedSize ,err := getZipFileSize (entryPath ) 127if err == nil { 128zipDir .Size = uncompressedSize 129zipDir .Usage = compressedSize 130 } 131zipDir .Parent = dir 132file = zipDir 133 } 134case a .archiveBrowsing && isTarFile (name ): 135tarDir ,err := processTarFile (entryPath ,info ) 136if err != nil { 137log .Printf ("Failed to process tar file %s: %v" ,entryPath ,err ) 138file = & File { 139Name :name , 140Flag :getFlag (info ), 141Size :info .Size (), 142Parent :dir , 143 } 144 }else { 145tarDir .Parent = dir 146file = tarDir 147 } 148default : 149file = & File { 150Name :name , 151Flag :getFlag (info ), 152Size :info .Size (), 153Parent :dir , 154Symlink :symlinkTarget , 155 } 156 } 157 158if file != nil { 159// Only set platform-specific attributes for regular files 160if regularFile ,ok := file .(* File );ok { 161setPlatformSpecificAttrs (regularFile ,info ) 162 } 163totalSize += file .GetUsage () 164dir .AddFile (file ) 165 } 166 } 167 } 168 169a .progressCurrentItemName .Store (path ) 170a .progressItemCount .Add (int64 (len (files ))) 171a .progressTotalUsage .Add (totalSize ) 172return dir 173}