gitlab.com/evatix-go/core@v1.3.55/chmodhelper/newRwxWrapperCreator.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 "gitlab.com/evatix-go/core/internal/messages" 10 ) 11 12 type newRwxWrapperCreator struct{} 13 14 // CreatePtr 15 // 16 // mode length needs to 3, not more not less 17 // mode chars should be digits only (0-7) 18 // example "777", "755", "655" 19 func (it newRwxWrapperCreator) CreatePtr(mode string) (*RwxWrapper, error) { 20 rwx, err := it.Create(mode) 21 22 if err != nil { 23 return nil, err 24 } 25 26 return &rwx, err 27 } 28 29 // Create 30 // 31 // mode length needs to 3, not more not less 32 // mode chars should be digits only (0-7) 33 // example "777", "755", "655" 34 func (it newRwxWrapperCreator) Create(mode string) (RwxWrapper, error) { 35 length := len(mode) 36 37 if length != SingleRwxLength { 38 panic(errcore.OutOfRangeLengthType.Combine( 39 "mode length should be "+SingleRwxLengthString, 40 length)) 41 } 42 43 allBytes := []byte(mode) 44 45 for i, allByte := range allBytes { 46 n := allByte - constants.ZeroChar 47 48 if n > 7 || n < 0 { 49 err := errcore. 50 InvalidCharType. 51 Error( 52 messages.ModeCharShouldBeAllNumbersAndWithin0To7, 53 n+constants.ZeroChar) 54 55 return RwxWrapper{}, err 56 } 57 58 allBytes[i] = n 59 } 60 61 return it.UsingSpecificByte( 62 allBytes[OwnerIndex], 63 allBytes[GroupIndex], 64 allBytes[OtherIndex]), nil 65 } 66 67 // UsingBytes 68 // 69 // each byte should not be more than 7 70 func (it newRwxWrapperCreator) UsingBytes(allBytes [3]byte) RwxWrapper { 71 return it.UsingSpecificByte( 72 allBytes[OwnerIndex], 73 allBytes[GroupIndex], 74 allBytes[OtherIndex]) 75 } 76 77 func (it newRwxWrapperCreator) Invalid() RwxWrapper { 78 return RwxWrapper{} 79 } 80 81 func (it newRwxWrapperCreator) InvalidPtr() *RwxWrapper { 82 return &RwxWrapper{} 83 } 84 85 func (it newRwxWrapperCreator) UsingChmod( 86 fileMode os.FileMode, 87 ) *RwxWrapper { 88 return it.UsingChmod(fileMode) 89 } 90 91 // UsingFileModePtr 92 // 93 // Hint. os.FileMode.String() returns "-rwxrwxrwx" full rwx 94 // https://go.dev/play/p/Qq_rKl_pAqe 95 // 96 // Reference: 97 // - Chmod examples : https://ss64.com/bash/chmod.html 98 // - FileMode to RwxFull : https://go.dev/play/p/Qq_rKl_pAqe 99 func (it newRwxWrapperCreator) UsingFileModePtr( 100 fileMode os.FileMode, 101 ) *RwxWrapper { 102 if fileMode == 0 { 103 return it.Empty() 104 } 105 106 str := fileMode.String() 107 // Reference : https://play.golang.org/p/Qq_rKl_pAqe 108 owner := str[1:4] 109 group := str[4:7] 110 other := str[7:10] 111 112 return &RwxWrapper{ 113 Owner: New.Attribute.UsingRwxString(owner), 114 Group: New.Attribute.UsingRwxString(group), 115 Other: New.Attribute.UsingRwxString(other), 116 } 117 } 118 119 // UsingFileMode 120 // 121 // Hint. os.FileMode String() returns "-rwxrwxrwx" full rwx 122 // https://go.dev/play/p/Qq_rKl_pAqe 123 // 124 // Reference: 125 // - Chmod examples : https://ss64.com/bash/chmod.html 126 // - FileMode to RwxFull : https://go.dev/play/p/Qq_rKl_pAqe 127 func (it newRwxWrapperCreator) UsingFileMode( 128 fileMode os.FileMode, 129 ) RwxWrapper { 130 if fileMode == 0 { 131 return it.Empty().ToNonPtr() 132 } 133 134 str := fileMode.String() 135 // Reference : https://play.golang.org/p/Qq_rKl_pAqe 136 owner := str[1:4] 137 group := str[4:7] 138 other := str[7:10] 139 140 return RwxWrapper{ 141 Owner: New.Attribute.UsingRwxString(owner), 142 Group: New.Attribute.UsingRwxString(group), 143 Other: New.Attribute.UsingRwxString(other), 144 } 145 } 146 147 func (it newRwxWrapperCreator) UsingRwxOwnerGroupOther( 148 rwxOwnerGroupOther *chmodins.RwxOwnerGroupOther, 149 ) (RwxWrapper, error) { 150 return it.RwxFullStringWtHyphen( 151 rwxOwnerGroupOther.ToString( 152 false)) 153 } 154 155 // UsingSpecificByte 156 // 157 // each byte should not be more than 7 158 func (it newRwxWrapperCreator) UsingSpecificByte( 159 owner, group, other byte, 160 ) RwxWrapper { 161 wrapper := RwxWrapper{ 162 Owner: New.Attribute.UsingByteMust(owner), 163 Group: New.Attribute.UsingByteMust(group), 164 Other: New.Attribute.UsingByteMust(other), 165 } 166 167 return wrapper 168 } 169 170 func (it newRwxWrapperCreator) UsingAttrVariants( 171 owner, group, other AttrVariant, 172 ) RwxWrapper { 173 wrapper := RwxWrapper{ 174 Owner: New.Attribute.UsingVariantMust(owner), 175 Group: New.Attribute.UsingVariantMust(group), 176 Other: New.Attribute.UsingVariantMust(other), 177 } 178 179 return wrapper 180 } 181 182 func (it newRwxWrapperCreator) UsingAttrs( 183 owner, group, other Attribute, 184 ) RwxWrapper { 185 wrapper := RwxWrapper{ 186 Owner: owner, 187 Group: group, 188 Other: other, 189 } 190 191 return wrapper 192 } 193 194 // Rwx10 195 // 196 // alias for RwxFullString 197 // 198 // Format (length must be 10) 199 // "-rwxrwxrwx" 200 // 201 // Example: 202 // - owner all enabled only "-rwx------" 203 // - group all enabled only "----rwx---" 204 // 205 // Must have or restrictions: 206 // - string length must be 10. 207 // 208 // Reference: 209 // - Chmod examples : https://ss64.com/bash/chmod.html 210 // - FileMode to RwxFull : https://go.dev/play/p/Qq_rKl_pAqe 211 func (it newRwxWrapperCreator) Rwx10( 212 hyphenedRwxRwxRwx string, 213 ) (RwxWrapper, error) { 214 return it.RwxFullString(hyphenedRwxRwxRwx) 215 } 216 217 // Rwx9 218 // 219 // alias of RwxFullStringWtHyphen 220 // 221 // Format (length must be 9) 222 // "rwxrwxrwx" 223 // 224 // Example: 225 // - owner all enabled only "rwx------" 226 // - group all enabled only "---rwx---" 227 // 228 // Must have or restrictions: 229 // - string length must be 9. 230 // 231 // Reference: 232 // - Chmod examples : https://ss64.com/bash/chmod.html 233 // - FileMode to RwxFull : https://go.dev/play/p/Qq_rKl_pAqe 234 func (it newRwxWrapperCreator) Rwx9( 235 rwxRwxRwx string, 236 ) (RwxWrapper, error) { 237 return it.RwxFullStringWtHyphen( 238 rwxRwxRwx) 239 } 240 241 // RwxFullString 242 // 243 // Format (length must be 10) 244 // "-rwxrwxrwx" 245 // 246 // Example: 247 // - owner all enabled only "-rwx------" 248 // - group all enabled only "----rwx---" 249 // 250 // Must have or restrictions: 251 // - string length must be 10. 252 // 253 // Reference: 254 // - Chmod examples : https://ss64.com/bash/chmod.html 255 // - FileMode to RwxFull : https://go.dev/play/p/Qq_rKl_pAqe 256 func (it newRwxWrapperCreator) RwxFullString( 257 hyphenedRwxRwxRwx string, 258 ) (RwxWrapper, error) { 259 length := len(hyphenedRwxRwxRwx) 260 261 if length != HyphenedRwxLength { 262 return it.Invalid(), errHyphenedRwxLength 263 } 264 265 return it.RwxFullStringWtHyphen( 266 hyphenedRwxRwxRwx[constants.One:]) 267 } 268 269 // RwxFullStringWtHyphen 270 // 271 // Format (length must be 9) 272 // "rwxrwxrwx" 273 // 274 // Example: 275 // - owner all enabled only "rwx------" 276 // - group all enabled only "---rwx---" 277 // 278 // Must have or restrictions: 279 // - string length must be 9. 280 // 281 // Reference: 282 // - Chmod examples : https://ss64.com/bash/chmod.html 283 // - FileMode to RwxFull : https://go.dev/play/p/Qq_rKl_pAqe 284 func (it newRwxWrapperCreator) RwxFullStringWtHyphen( 285 rwxFullStringWithoutHyphen string, 286 ) (RwxWrapper, error) { 287 length := len(rwxFullStringWithoutHyphen) 288 289 if length != FullRwxLengthWithoutHyphen { 290 return RwxWrapper{}, errFullRwxLengthWithoutHyphen 291 } 292 293 owner := rwxFullStringWithoutHyphen[0:3] 294 group := rwxFullStringWithoutHyphen[3:6] 295 other := rwxFullStringWithoutHyphen[6:9] 296 297 wrapper := RwxWrapper{ 298 Owner: New.Attribute.UsingRwxString(owner), 299 Group: New.Attribute.UsingRwxString(group), 300 Other: New.Attribute.UsingRwxString(other), 301 } 302 303 return wrapper, nil 304 } 305 306 func (it newRwxWrapperCreator) UsingVariant(variant Variant) (RwxWrapper, error) { 307 return it.Create(variant.String()) 308 } 309 310 func (it newRwxWrapperCreator) UsingVariantPtr( 311 variant Variant, 312 ) (*RwxWrapper, error) { 313 rwxWrapper, err := it.Create(variant.String()) 314 315 if err != nil { 316 return nil, err 317 } 318 319 return &rwxWrapper, nil 320 } 321 322 // Instruction 323 // 324 // rwxFullString must be 10 chars in "-rwxrwxrwx" 325 // 326 // Format (length must be 10) 327 // "-rwxrwxrwx" 328 // 329 // Example: 330 // - owner all enabled only "-rwx------" 331 // - group all enabled only "----rwx---" 332 // 333 // Must have or restrictions: 334 // - string length must be 10. 335 // 336 // Reference: 337 // - Chmod examples : https://ss64.com/bash/chmod.html 338 // - FileMode to RwxFull : https://go.dev/play/p/Qq_rKl_pAqe 339 func (it newRwxWrapperCreator) Instruction( 340 rwxFullString string, 341 condition chmodins.Condition, 342 ) (*chmodins.RwxInstruction, error) { 343 rwxWrapper, err := it.RwxFullString(rwxFullString) 344 345 if err != nil { 346 return nil, err 347 } 348 349 return rwxWrapper.ToRwxInstruction(&condition), nil 350 } 351 352 func (it newRwxWrapperCreator) UsingExistingFile( 353 filePath string, 354 ) (*RwxWrapper, error) { 355 existingChmod, err := GetExistingChmodRwxWrapper(filePath) 356 357 return existingChmod.ToPtr(), err 358 } 359 360 // UsingExistingFileSkipInvalidFile 361 // 362 // Warning: 363 // swallows the error and invalid file 364 func (it newRwxWrapperCreator) UsingExistingFileSkipInvalidFile( 365 filePath string, 366 ) (rwxWrapper *RwxWrapper, isInvalidFile bool) { 367 existingChmod, isInvalidFile := GetExistingChmodOfValidFile(filePath) 368 369 if !isInvalidFile { 370 // valid 371 return it.UsingFileModePtr(existingChmod), isInvalidFile 372 } 373 374 return it.Empty(), isInvalidFile 375 } 376 377 // UsingExistingFileOption 378 // 379 // Warning: 380 // swallows the error and invalid file 381 func (it newRwxWrapperCreator) UsingExistingFileOption( 382 isSkipInvalidFile bool, 383 filePath string, 384 ) (rwxWrapper *RwxWrapper, err error, isInvalidFile bool) { 385 if isSkipInvalidFile { 386 rwxWrapper, isInvalidFile = it.UsingExistingFileSkipInvalidFile( 387 filePath) 388 389 return rwxWrapper, nil, isInvalidFile 390 } 391 392 existingChmod, err := GetExistingChmod(filePath) 393 394 if err != nil || existingChmod == 0 { 395 return it.Empty(), err, true 396 } 397 398 return it.UsingFileModePtr(existingChmod), 399 err, 400 err != nil && existingChmod != 0 401 } 402 403 func (it newRwxWrapperCreator) Empty() *RwxWrapper { 404 return &RwxWrapper{} 405 }