gitlab.com/evatix-go/core@v1.3.55/chmodhelper/fwChmodApplier.go (about)

     1  package chmodhelper
     2  
     3  import (
     4  	"os"
     5  
     6  	"gitlab.com/evatix-go/core/errcore"
     7  )
     8  
     9  type fwChmodApplier struct {
    10  	rw *SimpleFileReaderWriter
    11  }
    12  
    13  func (it fwChmodApplier) OnParent() error {
    14  	return it.OnDir(it.rw.ParentDir)
    15  }
    16  
    17  func (it fwChmodApplier) OnDir(dir string) error {
    18  	return it.Apply(
    19  		it.rw.ChmodDir,
    20  		dir)
    21  }
    22  
    23  func (it fwChmodApplier) OnFile() error {
    24  	return it.Apply(
    25  		it.rw.ChmodFile,
    26  		it.rw.FilePath)
    27  }
    28  
    29  func (it fwChmodApplier) Apply(
    30  	fileMode os.FileMode,
    31  	location string,
    32  ) error {
    33  	err := os.Chmod(
    34  		location,
    35  		fileMode)
    36  
    37  	if err == nil {
    38  		return nil
    39  	}
    40  
    41  	// has error
    42  	return pathError(
    43  		"applying chmod failed",
    44  		fileMode,
    45  		location,
    46  		err)
    47  }
    48  
    49  // OnDiffFile
    50  //
    51  //  apply chmod on file if file doesn't have the save chmod
    52  func (it fwChmodApplier) OnDiffFile(
    53  	isSkipOnInvalidFile bool,
    54  	filePath string,
    55  ) error {
    56  	if ChmodVerify.IsEqual(filePath, it.rw.ChmodFile) {
    57  		return nil
    58  	}
    59  
    60  	if isSkipOnInvalidFile && IsPathInvalid(filePath) {
    61  		return nil
    62  	}
    63  
    64  	return it.Apply(it.rw.ChmodFile, filePath)
    65  }
    66  
    67  // OnDiffDir
    68  //
    69  //  apply chmod on file if file doesn't have the save chmod
    70  func (it fwChmodApplier) OnDiffDir(
    71  	isSkipOnInvalidDir bool,
    72  	dirPath string,
    73  ) error {
    74  	if ChmodVerify.IsEqual(dirPath, it.rw.ChmodDir) {
    75  		return nil
    76  	}
    77  
    78  	if isSkipOnInvalidDir && IsPathInvalid(dirPath) {
    79  		return nil
    80  	}
    81  
    82  	return it.Apply(it.rw.ChmodDir, dirPath)
    83  }
    84  
    85  // OnAll
    86  //
    87  //  both file, parent dir
    88  func (it fwChmodApplier) OnAll() error {
    89  	err := it.OnParent()
    90  
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	return it.OnFile()
    96  }
    97  
    98  func (it fwChmodApplier) DirRecursive(
    99  	isSkipOnInvalid bool,
   100  	dir string,
   101  ) error {
   102  	rwx := New.RwxWrapper.UsingFileMode(it.rw.ChmodDir)
   103  
   104  	return rwx.ApplyRecursive(isSkipOnInvalid, dir)
   105  }
   106  
   107  func (it fwChmodApplier) OnParentRecursive() error {
   108  	return it.DirRecursive(
   109  		false,
   110  		it.rw.ParentDir)
   111  }
   112  
   113  func (it fwChmodApplier) OnMismatch(
   114  	isFile,
   115  	isParentDir bool,
   116  ) error {
   117  	if !isFile && !isParentDir {
   118  		return nil
   119  	}
   120  
   121  	verifier := it.rw.ChmodVerifier()
   122  	var fileErr, dirErr error
   123  
   124  	if isFile && verifier.HasMismatchFile() {
   125  		fileErr = it.OnFile()
   126  	}
   127  
   128  	if isParentDir && verifier.HasMismatchParentDir() {
   129  		dirErr = it.OnParent()
   130  	}
   131  
   132  	return errcore.MergeErrors(fileErr, dirErr)
   133  }