gitlab.com/evatix-go/core@v1.3.55/chmodhelper/RwxVariableWrapper.go (about) 1 package chmodhelper 2 3 import ( 4 "os" 5 6 "gitlab.com/evatix-go/core/chmodhelper/chmodins" 7 "gitlab.com/evatix-go/core/constants" 8 "gitlab.com/evatix-go/core/errcore" 9 ) 10 11 type RwxVariableWrapper struct { 12 rawInput string 13 isFixedType bool 14 Owner, Group, Other VarAttribute 15 } 16 17 // NewRwxVariableWrapper 18 // 19 // partialRwx can be any length in 20 // between 1-10 (rest will be fixed by wildcard) 21 // 22 // Hyphened prefix MUST. 23 // 24 // rwxPartial: 25 // - "-rwx" will be "-rwx******" 26 // - "-rwxr-x" will be "-rwxr-x***" 27 // - "-rwxr-x" will be "-rwxr-x***" 28 // 29 // Restrictions: 30 // - cannot have first char other than hyphen(-) or else things will not work 31 func NewRwxVariableWrapper(partialRwx string) (*RwxVariableWrapper, error) { 32 fullRwxWithWildcard := chmodins.FixRwxFullStringWithWildcards(partialRwx) 33 34 owner := fullRwxWithWildcard[1:4] 35 group := fullRwxWithWildcard[4:7] 36 other := fullRwxWithWildcard[7:10] 37 ownerAttr, err := ParseRwxToVarAttribute(owner) 38 groupAttr, err2 := ParseRwxToVarAttribute(group) 39 otherAttr, err3 := ParseRwxToVarAttribute(other) 40 41 mergedErr := errcore.MergeErrors( 42 err, 43 err2, 44 err3) 45 46 if mergedErr != nil { 47 return nil, mergedErr 48 } 49 50 isAllFixedType := ownerAttr.IsFixedType() && 51 groupAttr.IsFixedType() && 52 otherAttr.IsFixedType() 53 54 return &RwxVariableWrapper{ 55 rawInput: fullRwxWithWildcard, 56 isFixedType: isAllFixedType, 57 Owner: *ownerAttr, 58 Group: *groupAttr, 59 Other: *otherAttr, 60 }, nil 61 } 62 63 func (varWrapper *RwxVariableWrapper) IsFixedType() bool { 64 return varWrapper.isFixedType 65 } 66 67 func (varWrapper *RwxVariableWrapper) HasWildcard() bool { 68 return !varWrapper.isFixedType 69 } 70 71 func (varWrapper *RwxVariableWrapper) ToCompileFixedPtr() *RwxWrapper { 72 if varWrapper.IsFixedType() { 73 return varWrapper.ToCompileWrapperPtr(nil) 74 } 75 76 return nil 77 } 78 79 // ToCompileWrapper if Fixed type then fixed input can be nil. 80 func (varWrapper *RwxVariableWrapper) ToCompileWrapper(fixed *RwxWrapper) RwxWrapper { 81 return *varWrapper.ToCompileWrapperPtr(fixed) 82 } 83 84 func (varWrapper *RwxVariableWrapper) ToCompileWrapperUsingLocationPtr(location string) (*RwxWrapper, error) { 85 if varWrapper.IsFixedType() { 86 return varWrapper.ToCompileFixedPtr(), nil 87 } 88 89 existingRwxWrapper, err := GetExistingChmodRwxWrapperPtr(location) 90 91 if err != nil { 92 return nil, err 93 } 94 95 return varWrapper.ToCompileWrapperPtr(existingRwxWrapper), nil 96 } 97 98 // ToCompileWrapperPtr if Fixed type then fixed input can be nil. 99 func (varWrapper *RwxVariableWrapper) ToCompileWrapperPtr(fixed *RwxWrapper) *RwxWrapper { 100 if varWrapper.IsFixedType() { 101 return &RwxWrapper{ 102 Owner: *varWrapper.Owner.ToCompileFixAttr(), 103 Group: *varWrapper.Group.ToCompileFixAttr(), 104 Other: *varWrapper.Other.ToCompileFixAttr(), 105 } 106 } 107 108 return &RwxWrapper{ 109 Owner: varWrapper.Owner.ToCompileAttr(&fixed.Owner), 110 Group: varWrapper.Group.ToCompileAttr(&fixed.Group), 111 Other: varWrapper.Other.ToCompileAttr(&fixed.Other), 112 } 113 } 114 115 func (varWrapper *RwxVariableWrapper) Clone() *RwxVariableWrapper { 116 if varWrapper == nil { 117 return nil 118 } 119 120 return &RwxVariableWrapper{ 121 rawInput: varWrapper.rawInput, 122 isFixedType: varWrapper.IsFixedType(), 123 Owner: *varWrapper.Owner.Clone(), 124 Group: *varWrapper.Group.Clone(), 125 Other: *varWrapper.Other.Clone(), 126 } 127 } 128 129 func (varWrapper *RwxVariableWrapper) IsEqualPtr(next *RwxVariableWrapper) bool { 130 if varWrapper == nil && next == nil { 131 return true 132 } 133 134 if varWrapper == nil || next == nil { 135 return false 136 } 137 138 isOwner := varWrapper.Owner.IsEqualPtr(&next.Owner) 139 isGroup := varWrapper.Group.IsEqualPtr(&next.Group) 140 isOther := varWrapper.Other.IsEqualPtr(&next.Other) 141 142 return isOwner && 143 isGroup && 144 isOther 145 } 146 147 func (varWrapper *RwxVariableWrapper) IsOwnerPartialMatch(rwx string) bool { 148 return IsPartialMatchVariableAttr( 149 &varWrapper.Owner, 150 rwx) 151 } 152 153 func (varWrapper *RwxVariableWrapper) IsGroupPartialMatch(rwx string) bool { 154 return IsPartialMatchVariableAttr( 155 &varWrapper.Group, 156 rwx) 157 } 158 159 func (varWrapper *RwxVariableWrapper) IsOtherPartialMatch(rwx string) bool { 160 return IsPartialMatchVariableAttr( 161 &varWrapper.Other, 162 rwx) 163 } 164 165 func (varWrapper *RwxVariableWrapper) ApplyRwxOnLocations( 166 isContinueOnError, 167 isSkipOnInvalid bool, 168 locations ...string, 169 ) error { 170 existsFilteredPathFileInfoMap := GetExistsFilteredPathFileInfoMap( 171 isSkipOnInvalid, 172 locations...) 173 if !isContinueOnError && existsFilteredPathFileInfoMap.Error != nil { 174 return existsFilteredPathFileInfoMap.Error 175 } 176 177 locationsFileInfoRwx := existsFilteredPathFileInfoMap. 178 LazyValidLocationFileInfoRwxWrappers() 179 180 if isContinueOnError { 181 var sliceErr []string 182 for _, locationFileInfoRwx := range locationsFileInfoRwx { 183 rwx := locationFileInfoRwx. 184 RwxWrapper 185 186 if rwx == nil { 187 continue 188 } 189 190 err := rwx. 191 ApplyChmod( 192 isSkipOnInvalid, 193 locationFileInfoRwx.Location) 194 195 if err != nil { 196 sliceErr = append(sliceErr, err.Error()) 197 } 198 } 199 200 return errcore.SliceToError(sliceErr) 201 } 202 203 for _, locationFileInfoRwx := range locationsFileInfoRwx { 204 rwx := locationFileInfoRwx. 205 RwxWrapper 206 207 if rwx == nil { 208 continue 209 } 210 211 err := rwx. 212 ApplyChmod( 213 isSkipOnInvalid, 214 locationFileInfoRwx.Location) 215 216 if err != nil { 217 return err 218 } 219 } 220 221 return nil 222 } 223 224 func (varWrapper *RwxVariableWrapper) RwxMatchingStatus( 225 isContinueOnError, 226 isSkipOnInvalid bool, 227 locations []string, 228 ) *RwxMatchingStatus { 229 existsFilteredPathFileInfoMap := GetExistsFilteredPathFileInfoMap( 230 isSkipOnInvalid, 231 locations...) 232 if !isContinueOnError && existsFilteredPathFileInfoMap.Error != nil { 233 return InvalidRwxMatchingStatus(existsFilteredPathFileInfoMap.Error) 234 } 235 236 rwxMismatchInfos := make( 237 []*RwxMismatchInfo, 238 0, 239 constants.Capacity1) 240 241 for filePath, fileInfo := range existsFilteredPathFileInfoMap.FilesToInfoMap { 242 fileRwx := fileInfo.Mode().String() 243 244 if varWrapper.IsMismatchPartialFullRwx(fileRwx) { 245 rwxMismatchInfos = append(rwxMismatchInfos, 246 &RwxMismatchInfo{ 247 FilePath: filePath, 248 Expecting: varWrapper.ToString(false), 249 Actual: fileRwx[1:], 250 }) 251 } 252 } 253 254 isAllMatching := len(rwxMismatchInfos) == 0 && 255 len(locations) == len(existsFilteredPathFileInfoMap.FilesToInfoMap) 256 257 return &RwxMatchingStatus{ 258 RwxMismatchInfos: rwxMismatchInfos, 259 MissingOrPathsWithIssues: existsFilteredPathFileInfoMap.MissingOrOtherPathIssues, 260 IsAllMatching: isAllMatching, 261 Error: existsFilteredPathFileInfoMap.Error, 262 } 263 } 264 265 // IsEqualPartialRwxPartial 266 // 267 // will make the partial to full rwx and then calls IsEqualPartialFullRwx 268 // partialRwx can be any length in 269 // between 1-10 (rest will be fixed by wildcard) 270 // 271 // rwxPartial: 272 // - "-rwx" will be "-rwx******" 273 // - "-rwxr-x" will be "-rwxr-x***" 274 // - "-rwxr-x" will be "-rwxr-x***" 275 func (varWrapper *RwxVariableWrapper) IsEqualPartialRwxPartial( 276 rwxPartial string, 277 ) bool { 278 fullRwxWithWildcard := chmodins.FixRwxFullStringWithWildcards(rwxPartial) 279 280 return varWrapper.IsEqualPartialFullRwx(fullRwxWithWildcard) 281 } 282 283 // IsEqualUsingLocation returns by sending to IsEqualPartialFullRwx 284 // 285 // Returns false on non exist. 286 func (varWrapper *RwxVariableWrapper) IsEqualUsingLocation( 287 location string, 288 ) bool { 289 fileInfo, _ := os.Stat(location) 290 291 if fileInfo == nil { 292 return false 293 } 294 295 return varWrapper.IsEqualPartialFullRwx( 296 fileInfo.Mode().String()) 297 } 298 299 // IsEqualUsingFileInfo returns by sending to IsEqualPartialFullRwx 300 // 301 // Returns false on nil. 302 func (varWrapper *RwxVariableWrapper) IsEqualUsingFileInfo( 303 fileInfo os.FileInfo, 304 ) bool { 305 if fileInfo == nil { 306 return false 307 } 308 309 return varWrapper.IsEqualPartialFullRwx( 310 fileInfo.Mode().String()) 311 } 312 313 // IsEqualUsingFileMode returns by sending to IsEqualPartialFullRwx 314 // 315 // Returns false on nil. 316 func (varWrapper *RwxVariableWrapper) IsEqualUsingFileMode( 317 fileMode os.FileMode, 318 ) bool { 319 return varWrapper.IsEqualPartialFullRwx( 320 fileMode.String()) 321 } 322 323 // IsEqualRwxWrapperPtr returns by sending to IsEqualPartialFullRwx 324 // 325 // Returns false on nil. 326 func (varWrapper *RwxVariableWrapper) IsEqualRwxWrapperPtr( 327 rwxWrapper *RwxWrapper, 328 ) bool { 329 if rwxWrapper == nil { 330 return false 331 } 332 333 return varWrapper.IsEqualPartialFullRwx( 334 rwxWrapper.ToFullRwxValueString()) 335 } 336 337 // IsMismatchPartialFullRwx returns revert of IsEqualPartialFullRwx 338 // 339 // fullRwx (10 chars) where wildcard will be ignore during compare 340 func (varWrapper *RwxVariableWrapper) IsMismatchPartialFullRwx( 341 fullRwx string, 342 ) bool { 343 return !varWrapper.IsEqualPartialFullRwx(fullRwx) 344 } 345 346 // IsEqualPartialFullRwx will compare with concrete FullRwx (10 chars) where wildcard will be ignore during compare 347 func (varWrapper *RwxVariableWrapper) IsEqualPartialFullRwx( 348 fullRwx string, 349 ) bool { 350 if len(fullRwx) < HyphenedRwxLength { 351 return false 352 } 353 354 owner := fullRwx[1:4] 355 group := fullRwx[4:7] 356 other := fullRwx[7:10] 357 358 isOwner := varWrapper.IsOwnerPartialMatch(owner) 359 isGroup := varWrapper.IsGroupPartialMatch(group) 360 isOther := varWrapper.IsOtherPartialMatch(other) 361 362 return isOwner && 363 isGroup && 364 isOther 365 } 366 367 // IsEqualPartialUsingFileMode will compare 368 // with concrete FullRwx (10 chars) where wildcard will be ignore during compare 369 func (varWrapper *RwxVariableWrapper) IsEqualPartialUsingFileMode( 370 mode os.FileMode, 371 ) bool { 372 return varWrapper.IsEqualPartialFullRwx( 373 mode.String()) 374 } 375 376 func (varWrapper *RwxVariableWrapper) ToString(isIncludeHyphen bool) string { 377 if isIncludeHyphen { 378 return constants.Hyphen + 379 varWrapper.Owner.String() + 380 varWrapper.Group.String() + 381 varWrapper.Other.String() 382 } 383 384 return varWrapper.Owner.String() + 385 varWrapper.Group.String() + 386 varWrapper.Other.String() 387 } 388 389 func (varWrapper *RwxVariableWrapper) String() string { 390 return varWrapper.rawInput 391 }