fork/gdu
git clone https://git.julia.blue/fork/gdu
4d478cf
subvolume
1// Package common contains commong logic and interfaces used across Gdu 2// nolint: revive //Why: this is common package 3package common 4 5import ( 6"bufio" 7"os" 8"path/filepath" 9"regexp" 10"strings" 11"syscall" 12 13 log"github.com/sirupsen/logrus" 14) 15 16// CreateIgnorePattern creates one pattern from all path patterns 17func CreateIgnorePattern (paths []string ) (compiled * regexp.Regexp ,err error ) { 18for i ,path := range paths { 19if _ ,err = regexp .Compile (path );err != nil { 20return nil ,err 21 } 22if ! filepath .IsAbs (path ) { 23absPath ,err := filepath .Abs (path ) 24if err == nil { 25paths = append (paths ,absPath ) 26 } 27 }else { 28relPath ,err := filepath .Rel ("/" ,path ) 29if err == nil { 30paths = append (paths ,relPath ) 31 } 32 } 33paths [i ]= "(" + path + ")" 34 } 35 36ignore := `^(?:` + strings .Join (paths ,"|" )+ `)$` 37return regexp .Compile (ignore ) 38} 39 40// SetIgnoreDirPaths sets paths to ignore 41func (ui * UI )SetIgnoreDirPaths (paths []string ) { 42log .Printf ("Ignoring dirs %s" ,strings .Join (paths ,", " )) 43ui .IgnoreDirPaths = make (map [string ]struct {},len (paths )* 2 ) 44for _ ,path := range paths { 45ui .IgnoreDirPaths [path ]= struct {}{} 46if ! filepath .IsAbs (path ) { 47if absPath ,err := filepath .Abs (path );err == nil { 48ui .IgnoreDirPaths [absPath ]= struct {}{} 49 } 50 }else { 51if relPath ,err := filepath .Rel ("/" ,path );err == nil { 52ui .IgnoreDirPaths [relPath ]= struct {}{} 53 } 54 } 55 } 56} 57 58// SetIgnoreDirPatterns sets regular patterns of dirs to ignore 59func (ui * UI )SetIgnoreDirPatterns (paths []string )error { 60var err error 61log .Printf ("Ignoring dir patterns %s" ,strings .Join (paths ,", " )) 62ui .IgnoreDirPathPatterns ,err = CreateIgnorePattern (paths ) 63return err 64} 65 66// SetIgnoreFromFile sets regular patterns of dirs to ignore 67func (ui * UI )SetIgnoreFromFile (ignoreFile string )error { 68var err error 69var paths []string 70log .Printf ("Reading ignoring dir patterns from file '%s'" ,ignoreFile ) 71 72file ,err := os .Open (ignoreFile ) 73if err != nil { 74return err 75 } 76defer file .Close () 77 78scanner := bufio .NewScanner (file ) 79for scanner .Scan () { 80paths = append (paths ,scanner .Text ()) 81 } 82 83if err := scanner .Err ();err != nil { 84return err 85 } 86 87ui .IgnoreDirPathPatterns ,err = CreateIgnorePattern (paths ) 88return err 89} 90 91// SetIgnoreTypes sets file types to ignore 92func (ui * UI )SetIgnoreTypes (types []string ) { 93log .Printf ("Ignoring file types: %s" ,strings .Join (types ,", " )) 94ui .IgnoreTypes = types 95} 96 97// SetIncludeTypes sets file types to include (whitelist) 98func (ui * UI )SetIncludeTypes (types []string ) { 99log .Printf ("Including only file types: %s" ,strings .Join (types ,", " )) 100ui .IncludeTypes = types 101} 102 103// SetIgnoreHidden sets flags if hidden dirs should be ignored 104func (ui * UI )SetIgnoreHidden (value bool ) { 105log .Printf ("Ignoring hidden dirs" ) 106ui .IgnoreHidden = value 107} 108 109// ShouldDirBeIgnored returns true if given path should be ignored 110func (ui * UI )ShouldDirBeIgnored (name ,path string )bool { 111_ ,shouldIgnore := ui .IgnoreDirPaths [path ] 112if shouldIgnore { 113log .Printf ("Directory %s ignored" ,path ) 114 } 115return shouldIgnore 116} 117 118// ShouldDirBeIgnoredUsingPattern returns true if given path should be ignored 119func (ui * UI )ShouldDirBeIgnoredUsingPattern (name ,path string )bool { 120shouldIgnore := ui .IgnoreDirPathPatterns .MatchString (path ) 121if shouldIgnore { 122log .Printf ("Directory %s ignored" ,path ) 123 } 124return shouldIgnore 125} 126 127// IsHiddenDir returns if the dir name begins with dot 128func (ui * UI )IsHiddenDir (name ,path string )bool { 129shouldIgnore := name [0 ]== '.' 130if shouldIgnore { 131log .Printf ("Directory %s ignored" ,path ) 132 } 133return shouldIgnore 134} 135 136// ShouldFileBeIgnoredByType returns true if file should be ignored based on its extension 137func (ui * UI )ShouldFileBeIgnoredByType (name string )bool { 138if len (ui .IgnoreTypes )== 0 { 139return false 140 } 141 142ext := strings .ToLower (filepath .Ext (name )) 143if ext == "" { 144return false // No extension, don't ignore 145 } 146 147// Remove leading dot from extension 148ext = strings .TrimPrefix (ext ,"." ) 149 150for _ ,ignoreType := range ui .IgnoreTypes { 151// Remove leading dot from ignoreType 152cleanIgnoreType := strings .TrimPrefix (strings .ToLower (ignoreType ),"." ) 153if cleanIgnoreType == ext { 154log .Printf ("File %s ignored by type" ,name ) 155return true 156 } 157 } 158return false 159} 160 161// ShouldFileBeIncludedByType returns true if file should be included based on its extension 162func (ui * UI )ShouldFileBeIncludedByType (name string )bool { 163if len (ui .IncludeTypes )== 0 { 164return true // No include filter, include all 165 } 166 167ext := strings .ToLower (filepath .Ext (name )) 168if ext == "" { 169return false // No extension, don't include if we have include filter 170 } 171 172// Remove leading dot from extension 173ext = strings .TrimPrefix (ext ,"." ) 174 175for _ ,includeType := range ui .IncludeTypes { 176// Remove leading dot from includeType 177cleanIncludeType := strings .TrimPrefix (strings .ToLower (includeType ),"." ) 178if cleanIncludeType == ext { 179return true 180 } 181 } 182 183log .Printf ("File %s excluded by type filter" ,name ) 184return false 185} 186 187// CreateIgnoreFunc returns function for detecting if dir should be ignored 188// nolint: gocyclo // Why: This function is a switch statement that is not too complex 189func (ui * UI )CreateIgnoreFunc ()ShouldDirBeIgnored { 190switch { 191case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns == nil && ! ui .IgnoreHidden : 192return ui .ShouldDirBeIgnored 193case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns != nil && ! ui .IgnoreHidden : 194return func (name ,path string )bool { 195return ui .ShouldDirBeIgnored (name ,path )|| ui .ShouldDirBeIgnoredUsingPattern (name ,path ) 196 } 197case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns != nil && ui .IgnoreHidden : 198return func (name ,path string )bool { 199return ui .ShouldDirBeIgnored (name ,path )|| ui .ShouldDirBeIgnoredUsingPattern (name ,path )|| ui .IsHiddenDir (name ,path ) 200 } 201case len (ui .IgnoreDirPaths )== 0 && ui .IgnoreDirPathPatterns != nil && ui .IgnoreHidden : 202return func (name ,path string )bool { 203return ui .ShouldDirBeIgnoredUsingPattern (name ,path )|| ui .IsHiddenDir (name ,path ) 204 } 205case len (ui .IgnoreDirPaths )== 0 && ui .IgnoreDirPathPatterns != nil && ! ui .IgnoreHidden : 206return ui .ShouldDirBeIgnoredUsingPattern 207case len (ui .IgnoreDirPaths )== 0 && ui .IgnoreDirPathPatterns == nil && ui .IgnoreHidden : 208return ui .IsHiddenDir 209case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns == nil && ui .IgnoreHidden : 210return func (name ,path string )bool { 211return ui .ShouldDirBeIgnored (name ,path )|| ui .IsHiddenDir (name ,path ) 212 } 213default : 214return func (name ,path string )bool {return false } 215 } 216} 217 218// CreateFileTypeFilter returns function for detecting if file should be ignored based on type 219func (ui * UI )CreateFileTypeFilter ()ShouldFileBeIgnored { 220// If we have include types, use whitelist mode 221if len (ui .IncludeTypes )> 0 { 222return func (name string )bool { 223return ! ui .ShouldFileBeIncludedByType (name ) 224 } 225 } 226 227// If we have ignore types, use blacklist mode 228if len (ui .IgnoreTypes )> 0 { 229return func (name string )bool { 230return ui .ShouldFileBeIgnoredByType (name ) 231 } 232 } 233 234// No type filtering - return nil to indicate no filtering is needed 235return nil 236} 237 238// IsFilteringFiles returns true if we have any file type filters set 239func (ui * UI )IsFilteringFiles ()bool { 240return len (ui .IgnoreTypes )> 0 || len (ui .IncludeTypes )> 0 || ui .FilteringFiles 241} 242 243func (ui * UI )IsCrossFS (name ,path string )bool { 244parent := filepath .Dir (path ) 245var a ,b syscall.Stat_t 246err1 := syscall .Stat (parent ,& a ) 247err2 := syscall .Stat (path ,& b ) 248if err1 != nil || err2 != nil { 249return true 250 } 251return a .Dev != b .Dev 252} 253 254// CreateIgnoreFunc returns function for detecting if dir should be ignored 255func (ui * UI )ShouldIgnore (name ,path string )bool { 256if len (ui .IgnoreDirPaths )> 0 && ui .ShouldDirBeIgnored (name ,path ) { 257return true 258 } 259if ui .IgnoreDirPathPatterns != nil && ui .ShouldDirBeIgnoredUsingPattern (name ,path ) { 260return true 261 } 262if ui .IgnoreHidden && ui .IsHiddenDir (name ,path ) { 263return true 264 } 265if ui .IgnoreCrossFS && ui .IsCrossFS (name ,path ) { 266return true 267 } 268return false 269}