| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Text; |
| | 7 | |
|
| | 8 | | namespace Azure.Storage.Files.DataLake.Models |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Represents POSIX-style permissions on a given resource. Each resource specifies permissions for the owner, the o |
| | 12 | | /// group, and everyone else. Permissions for users or groups not included here can be set using an Access Control L |
| | 13 | | /// Manipulating resource permissions is only supported when ADLS interop is enabled. |
| | 14 | | /// </summary> |
| | 15 | | public class PathPermissions |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// The <see cref="RolePermissions"/> for the owner of the resource. |
| | 19 | | /// </summary> |
| 492 | 20 | | public RolePermissions Owner { get; set; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// The <see cref="RolePermissions"/> for the owning group of the resource. |
| | 24 | | /// </summary> |
| 492 | 25 | | public RolePermissions Group { get; set; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The <see cref="RolePermissions"/> for the other users. |
| | 29 | | /// </summary> |
| 492 | 30 | | public RolePermissions Other { get; set; } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// If the sticky bit has been set. The sticky bit may be set on directories, the files in that |
| | 34 | | /// directory may only be renamed or deleted by the file's owner, the directory's owner, or the root user. |
| | 35 | | /// </summary> |
| 492 | 36 | | public bool StickyBit { get; set; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Whether or not there is more permissions information in the ACLs. The permissions string only returns |
| | 40 | | /// information on the owner, owning group, and other, but the ACLs may contain more permissions for specific us |
| | 41 | | /// or groups. |
| | 42 | | /// </summary> |
| 484 | 43 | | public bool ExtendedAcls { get; set; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Internal empty constructor. |
| | 47 | | /// </summary> |
| 532 | 48 | | public PathPermissions() { } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Public constructor. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="owner">The path owner's permissions.</param> |
| | 54 | | /// <param name="group">The path group's permissions.</param> |
| | 55 | | /// <param name="other">Permissions for other users.</param> |
| | 56 | | /// <param name="stickyBit">If sticky bit is enabled</param> |
| | 57 | | /// <param name="extendedInfoInAcl">If there is extended info in the ACL</param> |
| 24 | 58 | | public PathPermissions( |
| 24 | 59 | | RolePermissions owner, |
| 24 | 60 | | RolePermissions group, |
| 24 | 61 | | RolePermissions other, |
| 24 | 62 | | bool stickyBit = false, |
| 24 | 63 | | bool extendedInfoInAcl = false) |
| | 64 | | { |
| 24 | 65 | | Owner = owner; |
| 24 | 66 | | Group = group; |
| 24 | 67 | | Other = other; |
| 24 | 68 | | StickyBit = stickyBit; |
| 24 | 69 | | ExtendedAcls = extendedInfoInAcl; |
| 24 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Parses a string in octal format to PathPermissions. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="s">Octal string to parse.</param> |
| | 76 | | /// <returns><see cref="PathPermissions"/>.</returns> |
| | 77 | | public static PathPermissions ParseOctalPermissions(string s) |
| | 78 | | { |
| 8 | 79 | | if (s == null) |
| | 80 | | { |
| 0 | 81 | | return null; |
| | 82 | | } |
| | 83 | |
|
| 8 | 84 | | if (s.Length != 4) |
| | 85 | | { |
| 2 | 86 | | throw DataLakeErrors.PathPermissionsOctalInvalidLength(s); |
| | 87 | | } |
| | 88 | |
|
| 6 | 89 | | var pathPermissions = new PathPermissions(); |
| | 90 | |
|
| 6 | 91 | | if (s[0] == '0') |
| | 92 | | { |
| 2 | 93 | | pathPermissions.StickyBit = false; |
| | 94 | | } |
| 4 | 95 | | else if (s[0] == '1') |
| | 96 | | { |
| 2 | 97 | | pathPermissions.StickyBit = true; |
| | 98 | | } |
| | 99 | | else |
| | 100 | | { |
| 2 | 101 | | throw DataLakeErrors.PathPermissionsOctalInvalidFirstDigit(s); |
| | 102 | | } |
| | 103 | |
|
| 4 | 104 | | pathPermissions.Owner = PathAccessControlExtensions.ParseOctalRolePermissions(s[1]); |
| 4 | 105 | | pathPermissions.Group = PathAccessControlExtensions.ParseOctalRolePermissions(s[2]); |
| 4 | 106 | | pathPermissions.Other = PathAccessControlExtensions.ParseOctalRolePermissions(s[3]); |
| | 107 | |
|
| 4 | 108 | | return pathPermissions; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Parses a symbolic string to PathPermissions. |
| | 113 | | /// </summary> |
| | 114 | | /// <param name="s">String to parse.</param> |
| | 115 | | /// <returns><see cref="PathPermissions"/>.</returns> |
| | 116 | | public static PathPermissions ParseSymbolicPermissions(string s) |
| | 117 | | { |
| 262 | 118 | | if (s == null) |
| | 119 | | { |
| 0 | 120 | | return null; |
| | 121 | | } |
| | 122 | |
|
| 262 | 123 | | if (s.Length != 9 && s.Length != 10) |
| | 124 | | { |
| 2 | 125 | | throw DataLakeErrors.PathPermissionsSymbolicInvalidLength(s); |
| | 126 | | } |
| | 127 | |
|
| 260 | 128 | | var pathPermissions = new PathPermissions(); |
| | 129 | |
|
| | 130 | | // Set sticky bit |
| 260 | 131 | | if (char.ToLower(s[8], CultureInfo.InvariantCulture) == 't') |
| | 132 | | { |
| 4 | 133 | | pathPermissions.StickyBit = true; |
| | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 256 | 137 | | pathPermissions.StickyBit = false; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | // Set extended info in ACL |
| 260 | 141 | | if (s.Length == 10) |
| | 142 | | { |
| 4 | 143 | | if (s[9] == '+') |
| | 144 | | { |
| 4 | 145 | | pathPermissions.ExtendedAcls = true; |
| | 146 | | } |
| | 147 | | else |
| | 148 | | { |
| 0 | 149 | | throw Errors.InvalidFormat(nameof(s)); |
| | 150 | | } |
| | 151 | | } |
| | 152 | | else |
| | 153 | | { |
| 256 | 154 | | pathPermissions.ExtendedAcls = false; |
| | 155 | | } |
| | 156 | |
|
| 260 | 157 | | pathPermissions.Owner = PathAccessControlExtensions.ParseSymbolicRolePermissions(s.Substring(0, 3), allowSti |
| 260 | 158 | | pathPermissions.Group = PathAccessControlExtensions.ParseSymbolicRolePermissions(s.Substring(3, 3), allowSti |
| 260 | 159 | | pathPermissions.Other = PathAccessControlExtensions.ParseSymbolicRolePermissions(s.Substring(6, 3), allowSti |
| | 160 | |
|
| 260 | 161 | | return pathPermissions; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// Returns the octal representation of this PathPermissions as a string. |
| | 166 | | /// </summary> |
| | 167 | | /// <returns>string</returns> |
| | 168 | | public string ToOctalPermissions() |
| | 169 | | { |
| 4 | 170 | | var sb = new StringBuilder(); |
| | 171 | |
|
| 4 | 172 | | if (StickyBit) |
| | 173 | | { |
| 2 | 174 | | sb.Append(1); |
| | 175 | | } |
| | 176 | | else |
| | 177 | | { |
| 2 | 178 | | sb.Append(0); |
| | 179 | | } |
| | 180 | |
|
| 4 | 181 | | sb.Append(Owner.ToOctalRolePermissions()); |
| 4 | 182 | | sb.Append(Group.ToOctalRolePermissions()); |
| 4 | 183 | | sb.Append(Other.ToOctalRolePermissions()); |
| | 184 | |
|
| 4 | 185 | | return sb.ToString(); |
| | 186 | | } |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Returns the symbolic represenation of this PathPermissions as a string. |
| | 190 | | /// </summary> |
| | 191 | | /// <returns>string.</returns> |
| | 192 | | public string ToSymbolicPermissions() |
| | 193 | | { |
| 112 | 194 | | var sb = new StringBuilder(); |
| 112 | 195 | | sb.Append(Owner.ToSymbolicRolePermissions()); |
| 112 | 196 | | sb.Append(Group.ToSymbolicRolePermissions()); |
| 112 | 197 | | sb.Append(Other.ToSymbolicRolePermissions()); |
| | 198 | |
|
| 112 | 199 | | if (StickyBit) |
| | 200 | | { |
| 4 | 201 | | sb.Remove(8, 1); |
| 4 | 202 | | sb.Append("t"); |
| | 203 | | } |
| | 204 | |
|
| 112 | 205 | | if (ExtendedAcls) |
| | 206 | | { |
| 4 | 207 | | sb.Append("+"); |
| | 208 | | } |
| | 209 | |
|
| 112 | 210 | | return sb.ToString(); |
| | 211 | | } |
| | 212 | | } |
| | 213 | | } |