gitlab.com/evatix-go/core@v1.3.55/chmodhelper/RwxInstructionExecutor.go (about) 1 package chmodhelper 2 3 import ( 4 "errors" 5 "os" 6 7 "gitlab.com/evatix-go/core/chmodhelper/chmodins" 8 "gitlab.com/evatix-go/core/constants" 9 "gitlab.com/evatix-go/core/errcore" 10 "gitlab.com/evatix-go/core/internal/messages" 11 ) 12 13 type RwxInstructionExecutor struct { 14 rwxInstruction *chmodins.RwxInstruction 15 varWrapper *RwxVariableWrapper 16 } 17 18 // IsVarWrapper if it has any wildcard symbol in it 19 func (it *RwxInstructionExecutor) IsVarWrapper() bool { 20 return !it.varWrapper.IsFixedType() 21 } 22 23 // IsFixedWrapper true indicates no wildcard symbol 24 func (it *RwxInstructionExecutor) IsFixedWrapper() bool { 25 return it.varWrapper.IsFixedType() 26 } 27 28 // IsEqualFileInfo fileInfo nil returns false. 29 func (it *RwxInstructionExecutor) IsEqualFileInfo(fileInfo os.FileInfo) bool { 30 return it.varWrapper.IsEqualUsingFileInfo(fileInfo) 31 } 32 33 // IsEqualRwxWrapper nil returns false. 34 func (it *RwxInstructionExecutor) IsEqualRwxWrapper(rwxWrapper *RwxWrapper) bool { 35 return it.varWrapper.IsEqualRwxWrapperPtr(rwxWrapper) 36 } 37 38 // IsEqualRwxPartial 39 // 40 // rwxPartial: 41 // - "-rwx" will be "-rwx******" 42 // - "-rwxr-x" will be "-rwxr-x***" 43 // - "-rwxr-x" will be "-rwxr-x***" 44 func (it *RwxInstructionExecutor) IsEqualRwxPartial(rwxPartial string) bool { 45 return it.varWrapper.IsEqualPartialRwxPartial(rwxPartial) 46 } 47 48 func (it *RwxInstructionExecutor) IsEqualFileMode(mode os.FileMode) bool { 49 return it.varWrapper.IsEqualUsingFileMode(mode) 50 } 51 52 func (it *RwxInstructionExecutor) CompiledWrapper(mode os.FileMode) (*RwxWrapper, error) { 53 if it.IsFixedWrapper() { 54 return it. 55 varWrapper. 56 ToCompileWrapperPtr(nil), nil 57 } 58 59 if it.IsVarWrapper() { 60 fixedWrapper := New.RwxWrapper.UsingFileMode(mode) 61 62 return it. 63 varWrapper. 64 ToCompileWrapperPtr(&fixedWrapper), nil 65 } 66 67 return nil, failedToCompileVarWrapperToWrapper 68 } 69 70 func (it *RwxInstructionExecutor) CompiledRwxWrapperUsingFixedRwxWrapper( 71 wrapper *RwxWrapper, 72 ) (*RwxWrapper, error) { 73 if it.IsFixedWrapper() { 74 return it. 75 varWrapper. 76 ToCompileWrapperPtr(nil), nil 77 } 78 79 if it.IsVarWrapper() { 80 return it. 81 varWrapper. 82 ToCompileWrapperPtr(wrapper), nil 83 } 84 85 return nil, errcore. 86 FailedToExecuteType. 87 Error( 88 messages.FailedToCompileChmodhelperVarWrapperToWrapper, 89 wrapper.String()) 90 } 91 92 // ApplyOnPath 93 // 94 // Warning: 95 // swallows error if chmodins.RwxInstruction. IsSkipOnInvalid or 96 // chmodins.RwxInstruction.IsExitOnInvalid() comes as negative 97 func (it *RwxInstructionExecutor) ApplyOnPath(location string) error { 98 existingRwxFileModWrapper, err := GetExistingChmodRwxWrapperPtr( 99 location) 100 101 if it.rwxInstruction.IsExitOnInvalid() && err != nil { 102 return errcore.PathErrorType.Error(messages.FailedToGetFileModeRwx, location) 103 } else if it.rwxInstruction.IsSkipOnInvalid && err != nil { 104 // nothing apply got an error 105 return nil 106 } 107 108 compiledWrapper, compiledErr := it.CompiledRwxWrapperUsingFixedRwxWrapper(existingRwxFileModWrapper) 109 110 if compiledErr != nil { 111 funcWithLoc := "ApplyOnPath" + constants.HyphenAngelRight + location 112 113 return errcore. 114 MeaningfulError( 115 errcore.PathErrorType, funcWithLoc, compiledErr) 116 } 117 118 if it.rwxInstruction.IsRecursive { 119 return compiledWrapper.ApplyRecursive( 120 it.rwxInstruction.IsSkipOnInvalid, 121 location) 122 } 123 124 return compiledWrapper.ApplyChmod( 125 it.rwxInstruction.IsSkipOnInvalid, 126 location, 127 ) 128 } 129 130 func (it *RwxInstructionExecutor) VerifyRwxModifiersDirect( 131 isRecursiveIgnore bool, 132 locations ...string, 133 ) error { 134 return it.VerifyRwxModifiers(isRecursiveIgnore, locations) 135 } 136 137 func (it *RwxInstructionExecutor) VerifyRwxModifiers( 138 isRecursiveIgnore bool, 139 locations []string, 140 ) error { 141 if len(locations) == 0 { 142 return nil 143 } 144 145 resultsMap, err := it. 146 getVerifyRwxInternalError( 147 isRecursiveIgnore, 148 locations) 149 150 if err != nil { 151 return err 152 } 153 154 if it.rwxInstruction.IsContinueOnError { 155 return it.verifyChmodLocationsContinueOnError(resultsMap) 156 } 157 158 return it.verifyChmodLocationsNoContinue(resultsMap) 159 } 160 161 func (it *RwxInstructionExecutor) getVerifyRwxInternalError( 162 isRecursiveIgnore bool, 163 locations []string, 164 ) ( 165 *FilteredPathFileInfoMap, error, 166 ) { 167 if !isRecursiveIgnore && it.rwxInstruction.Condition.IsRecursive { 168 return nil, errcore.NotSupportedType.Error( 169 "IsRecursive is not supported for Verify chmod.", 170 locations) 171 } 172 173 resultsMap := GetExistsFilteredPathFileInfoMap( 174 it.rwxInstruction.IsSkipOnInvalid, 175 locations...) 176 177 return resultsMap, nil 178 } 179 180 func (it *RwxInstructionExecutor) verifyChmodLocationsContinueOnError( 181 resultsMap *FilteredPathFileInfoMap, 182 ) error { 183 var sliceErr []string 184 185 if resultsMap.Error != nil && it.rwxInstruction.IsCollectErrorOnInvalid() { 186 sliceErr = append( 187 sliceErr, 188 resultsMap.Error.Error()) 189 } 190 191 for filePath, info := range resultsMap.FilesToInfoMap { 192 fileMode := info.Mode() 193 fixedRwxWrapper, err := it.CompiledWrapper(fileMode) 194 195 if err != nil { 196 sliceErr = append( 197 sliceErr, 198 err.Error()+"- failed to verify rwxInstruction for - "+filePath) 199 } 200 201 if fixedRwxWrapper != nil && !fixedRwxWrapper.IsEqualFileMode(fileMode) { 202 sliceErr = append( 203 sliceErr, 204 errcore.ExpectingSimpleNoType( 205 "Path:"+filePath, 206 fixedRwxWrapper.ToFullRwxValueStringExceptHyphen(), 207 fileMode.String()[1:])) 208 } 209 } 210 211 return errcore.SliceToError(sliceErr) 212 } 213 214 func (it *RwxInstructionExecutor) verifyChmodLocationsNoContinue( 215 resultsMap *FilteredPathFileInfoMap, 216 ) error { 217 if resultsMap.Error != nil && !it.rwxInstruction.IsSkipOnInvalid { 218 return resultsMap.Error 219 } 220 221 for filePath, info := range resultsMap.FilesToInfoMap { 222 fileMode := info.Mode() 223 fixedRwxWrapper, err := it.CompiledWrapper( 224 fileMode) 225 226 if err != nil { 227 return errcore.MeaningfulErrorWithData( 228 errcore.ValidataionFailedType, 229 "verifyChmodLocationsNoContinue", 230 err, 231 "failed to verify rwxInstruction for - "+filePath) 232 } 233 234 if fixedRwxWrapper.IsDefined() && fixedRwxWrapper.IsNotEqualFileMode(fileMode) { 235 expectingMsg := errcore.ExpectingSimpleNoType( 236 "Path:"+filePath, 237 fixedRwxWrapper.ToFullRwxValueStringExceptHyphen(), 238 fileMode.String()[1:]) 239 240 return errors.New(expectingMsg) 241 } 242 } 243 244 return nil 245 } 246 247 func (it *RwxInstructionExecutor) ApplyOnPathsDirect(locations ...string) error { 248 if len(locations) == 0 { 249 return nil 250 } 251 252 return it.ApplyOnPathsPtr(&locations) 253 } 254 255 func (it *RwxInstructionExecutor) ApplyOnPaths(locations []string) error { 256 if len(locations) == 0 { 257 return nil 258 } 259 260 return it.ApplyOnPathsPtr(&locations) 261 } 262 263 func (it *RwxInstructionExecutor) ApplyOnPathsPtr(locations *[]string) error { 264 if locations == nil { 265 return nil 266 } 267 268 isContinueOnError := it. 269 rwxInstruction. 270 IsContinueOnError 271 272 if !isContinueOnError { 273 return it.applyOnPaths(locations) 274 } 275 276 return it.applyOnPathsContinueOnError(locations) 277 } 278 279 func (it *RwxInstructionExecutor) applyOnPaths(locations *[]string) error { 280 for _, location := range *locations { 281 err := it.ApplyOnPath(location) 282 283 if err != nil { 284 return err 285 } 286 } 287 288 return nil 289 } 290 291 func (it *RwxInstructionExecutor) applyOnPathsContinueOnError(locations *[]string) error { 292 errorSlice := make([]string, constants.Zero) 293 294 for _, location := range *locations { 295 err := it.ApplyOnPath(location) 296 297 if err != nil { 298 errorSlice = append(errorSlice, err.Error()) 299 } 300 } 301 302 return errcore.SliceToErrorPtr(&errorSlice) 303 }