github.com/vmware/go-vcloud-director/v2@v2.24.0/internal/udf/perms.go (about)

     1  package udf
     2  
     3  import "io/fs"
     4  
     5  // Permissions (BP 44)
     6  // Bit 0: Other: If set to ZERO, shall mean that the user may not execute the file; If set to ONE, shall mean that
     7  // the user may execute the file.
     8  // Bit 1: Other: If set to ZERO, shall mean that the user may not write the file; If set to ONE, shall mean that
     9  // the user may write the file.
    10  // Bit 2: Other: If set to ZERO, shall mean that the user may not read the file; If set to ONE, shall mean that the
    11  // user may read the file.
    12  // Bit 3: Other: If set to ZERO, shall mean that the user may not change any attributes of the file; If set to
    13  // ONE, shall mean that the user may change attributes of the file.
    14  // Bit 4: Other: If set to ZERO, shall mean that the user may not delete the file; If set to ONE, shall mean that
    15  // the user may delete the file.
    16  // Bit 5: Group: If set to ZERO, shall mean that the user may not execute the file; If set to ONE, shall mean
    17  // that the user may execute the file.
    18  // Bit 6: Group: If set to ZERO, shall mean that the user may not write the file; If set to ONE, shall mean that
    19  // the user may write the file.
    20  // Bit 7: Group: If set to ZERO, shall mean that the user may not read the file; If set to ONE, shall mean that
    21  // the user may read the file.
    22  // Bit 8: Group: If set to ZERO, shall mean that the user may not change any attributes of the file; If set to
    23  // ONE, shall mean that the user may change attributes of the file.
    24  // Bit 9: Group: If set to ZERO, shall mean that the user may not delete the file; If set to ONE, shall mean that
    25  // the user may delete the file.
    26  // Bit 10: Owner: If set to ZERO, shall mean that the user may not execute the file; If set to ONE, shall mean
    27  // that the user may execute the file.
    28  // Bit 11: Owner: If set to ZERO, shall mean that the user may not write the file; If set to ONE, shall mean that
    29  // the user may write the file.
    30  // Bit 12: Owner: If set to ZERO, shall mean that the user may not read the file; If set to ONE, shall mean that
    31  // the user may read the file.
    32  // Bit 13: Owner: If set to ZERO, shall mean that the user may not change any attributes of the file; If set to
    33  // ONE, shall mean that the user may change attributes of the file.
    34  // Bit 14: Owner: If set to ZERO, shall mean that the user may not delete the file; If set to ONE, shall mean that
    35  // the user may delete the file.
    36  // 15-31 Reserved: Shall be set to ZERO.
    37  
    38  type FilePerm uint32
    39  
    40  const (
    41  	FilePermExecute FilePerm = 1 << iota
    42  	FilePermWrite
    43  	FilePermRead
    44  	FilePermChange
    45  	FilePermDelete
    46  )
    47  
    48  const (
    49  	FileModeOtherOffset = 5 * iota
    50  	FileModeGroupOffset
    51  	FileModeOwnerOffset
    52  )
    53  
    54  const (
    55  	FileModeOtherMask = ((1 << 5) - 1) << (5 * iota)
    56  	FileModeGroupMask
    57  	FileModeOwnerMask
    58  )
    59  
    60  type FileMode uint32
    61  
    62  func ToFileMode(mode fs.FileMode) FileMode {
    63  	mode = mode.Perm()
    64  	var r FileMode
    65  	r |= FileMode(mode & 7)              // Other
    66  	r |= FileMode((mode >> 3) & 7 << 5)  // Group
    67  	r |= FileMode((mode >> 6) & 7 << 10) // Owner
    68  	return r
    69  }
    70  
    71  func FromFileMode(mode FileMode) fs.FileMode {
    72  	var r fs.FileMode
    73  	r |= fs.FileMode(mode & 7)                // Other
    74  	r |= fs.FileMode(((mode >> 5) & 7) << 3)  // Group
    75  	r |= fs.FileMode(((mode >> 10) & 7) << 6) // Owner
    76  	return r
    77  }
    78  
    79  func (m FileMode) Other() FileMode {
    80  	return m & FileModeOtherMask
    81  }
    82  
    83  func (m FileMode) HasOther(perms FilePerm) bool {
    84  	return (m>>FileModeOtherOffset)&FileMode(perms) == FileMode(perms)
    85  }
    86  
    87  func (m FileMode) SetOther(perms FilePerm) FileMode {
    88  	return m | (FileMode(perms) << FileModeOtherOffset)
    89  }
    90  
    91  func (m FileMode) UnsetOther(perms FilePerm) FileMode {
    92  	return m &^ (FileMode(perms) << FileModeOtherOffset)
    93  }
    94  
    95  func (m FileMode) Group() FileMode {
    96  	return m & FileModeGroupMask
    97  }
    98  
    99  func (m FileMode) HasGroup(perms FilePerm) bool {
   100  	return (m>>FileModeGroupOffset)&FileMode(perms) == FileMode(perms)
   101  }
   102  
   103  func (m FileMode) SetGroup(perms FilePerm) FileMode {
   104  	return m | (FileMode(perms) << FileModeGroupOffset)
   105  }
   106  
   107  func (m FileMode) UnsetGroup(perms FilePerm) FileMode {
   108  	return m &^ (FileMode(perms) << FileModeGroupOffset)
   109  }
   110  func (m FileMode) Owner() FileMode {
   111  	return m & FileModeOwnerMask
   112  }
   113  
   114  func (m FileMode) HasOwner(perms FilePerm) bool {
   115  	return (m>>FileModeOwnerOffset)&FileMode(perms) == FileMode(perms)
   116  }
   117  
   118  func (m FileMode) SetOwner(perms FilePerm) FileMode {
   119  	return m | (FileMode(perms) << FileModeOwnerOffset)
   120  }
   121  
   122  func (m FileMode) UnsetOwner(perms FilePerm) FileMode {
   123  	return m &^ (FileMode(perms) << FileModeOwnerOffset)
   124  }