| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 6 | | using Azure.Storage.Files.Shares.Models; |
| | 7 | |
|
| | 8 | | namespace Azure.Storage.Files.Shares.Models |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// NTFS file attributes for Files and Directories. |
| | 12 | | /// </summary> |
| | 13 | | [Flags] |
| | 14 | | public enum NtfsFileAttributes |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// The File or Directory is read-only. |
| | 18 | | /// </summary> |
| | 19 | | ReadOnly = 1, |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// The File or Directory is hidden, and thus is not included in an ordinary directory listing. |
| | 23 | | /// </summary> |
| | 24 | | Hidden = 2, |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// The File or Directory is a systemfile. That is, the file is part of the operating system |
| | 28 | | /// or is used exclusively by the operating system. |
| | 29 | | /// </summary> |
| | 30 | | System = 4, |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The file or directory is a standard file that has no special attributes. This attribute is |
| | 34 | | /// valid only if it is used alone. |
| | 35 | | /// </summary> |
| | 36 | | None = 8, |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The file is a directory. |
| | 40 | | /// </summary> |
| | 41 | | Directory = 16, |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The file is a candidate for backup or removal. |
| | 45 | | /// </summary> |
| | 46 | | Archive = 32, |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// The file or directory is temporary. A temporary file contains data that is needed while an |
| | 50 | | /// application is executing but is not needed after the application is finished. |
| | 51 | | /// File systems try to keep all the data in memory for quicker access rather than |
| | 52 | | /// flushing the data back to mass storage. A temporary file should be deleted by |
| | 53 | | /// the application as soon as it is no longer needed. |
| | 54 | | /// </summary> |
| | 55 | | Temporary = 64, |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// The file or directory is offline. The data of the file is not immediately available. |
| | 59 | | /// </summary> |
| | 60 | | Offline = 128, |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// The file or directory will not be indexed by the operating system's content indexing service. |
| | 64 | | /// </summary> |
| | 65 | | NotContentIndexed = 256, |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// The file or directory is excluded from the data integrity scan. When this value |
| | 69 | | /// is applied to a directory, by default, all new files and subdirectories within |
| | 70 | | /// that directory are excluded from data integrity. |
| | 71 | | /// </summary> |
| | 72 | | NoScrubData = 512 |
| | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| | 76 | | namespace Azure.Storage.Files.Shares |
| | 77 | | { |
| | 78 | | /// <summary> |
| | 79 | | /// File enum extensions. |
| | 80 | | /// </summary> |
| | 81 | | internal static partial class ShareExtensions |
| | 82 | | { |
| | 83 | | /// <summary> |
| | 84 | | /// ToString |
| | 85 | | /// </summary> |
| | 86 | | /// <returns>string</returns> |
| | 87 | | public static string ToAttributesString(this NtfsFileAttributes attributes) |
| | 88 | | { |
| 12 | 89 | | var stringBuilder = new StringBuilder(); |
| | 90 | |
|
| 12 | 91 | | if ((attributes & NtfsFileAttributes.ReadOnly) == NtfsFileAttributes.ReadOnly) |
| | 92 | | { |
| 12 | 93 | | AppendAttribute(nameof(NtfsFileAttributes.ReadOnly), stringBuilder); |
| | 94 | | } |
| 12 | 95 | | if ((attributes & NtfsFileAttributes.Hidden) == NtfsFileAttributes.Hidden) |
| | 96 | | { |
| 0 | 97 | | AppendAttribute(nameof(NtfsFileAttributes.Hidden), stringBuilder); |
| | 98 | | } |
| 12 | 99 | | if ((attributes & NtfsFileAttributes.System) == NtfsFileAttributes.System) |
| | 100 | | { |
| 0 | 101 | | AppendAttribute(nameof(NtfsFileAttributes.System), stringBuilder); |
| | 102 | | } |
| 12 | 103 | | if ((attributes & NtfsFileAttributes.None) == NtfsFileAttributes.None) |
| | 104 | | { |
| 0 | 105 | | AppendAttribute(nameof(NtfsFileAttributes.None), stringBuilder); |
| | 106 | | } |
| 12 | 107 | | if ((attributes & NtfsFileAttributes.Directory) == NtfsFileAttributes.Directory) |
| | 108 | | { |
| 4 | 109 | | AppendAttribute(nameof(NtfsFileAttributes.Directory), stringBuilder); |
| | 110 | | } |
| 12 | 111 | | if ((attributes & NtfsFileAttributes.Archive) == NtfsFileAttributes.Archive) |
| | 112 | | { |
| 8 | 113 | | AppendAttribute(nameof(NtfsFileAttributes.Archive), stringBuilder); |
| | 114 | | } |
| 12 | 115 | | if ((attributes & NtfsFileAttributes.Temporary) == NtfsFileAttributes.Temporary) |
| | 116 | | { |
| 0 | 117 | | AppendAttribute(nameof(NtfsFileAttributes.Temporary), stringBuilder); |
| | 118 | | } |
| 12 | 119 | | if ((attributes & NtfsFileAttributes.Offline) == NtfsFileAttributes.Offline) |
| | 120 | | { |
| 0 | 121 | | AppendAttribute(nameof(NtfsFileAttributes.Offline), stringBuilder); |
| | 122 | | } |
| 12 | 123 | | if ((attributes & NtfsFileAttributes.NotContentIndexed) == NtfsFileAttributes.NotContentIndexed) |
| | 124 | | { |
| 0 | 125 | | AppendAttribute(nameof(NtfsFileAttributes.NotContentIndexed), stringBuilder); |
| | 126 | | } |
| 12 | 127 | | if ((attributes & NtfsFileAttributes.NoScrubData) == NtfsFileAttributes.NoScrubData) |
| | 128 | | { |
| 0 | 129 | | AppendAttribute(nameof(NtfsFileAttributes.NoScrubData), stringBuilder); |
| | 130 | | } |
| 12 | 131 | | if (stringBuilder[stringBuilder.Length - 1] == '|') |
| | 132 | | { |
| 12 | 133 | | stringBuilder.Remove(stringBuilder.Length - 1, 1); |
| | 134 | | } |
| 12 | 135 | | return stringBuilder.ToString(); |
| | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary> |
| | 139 | | /// Helper method to append attribute name to stringbuilder. |
| | 140 | | /// </summary> |
| | 141 | | /// <param name="attributeName">name of attribute</param> |
| | 142 | | /// <param name="stringBuilder">stringbuilder reference</param> |
| | 143 | | private static void AppendAttribute(string attributeName, StringBuilder stringBuilder) |
| | 144 | | { |
| 24 | 145 | | stringBuilder.Append(attributeName); |
| 24 | 146 | | stringBuilder.Append("|"); |
| 24 | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Parses a NTFS attributes string to a nullable FileNtfsAttributes. |
| | 151 | | /// </summary> |
| | 152 | | /// <param name="attributesString">string to parse</param> |
| | 153 | | /// <returns></returns> |
| | 154 | | public static NtfsFileAttributes? ToFileAttributes(string attributesString) |
| | 155 | | { |
| 780 | 156 | | if (attributesString == null) |
| | 157 | | { |
| 7 | 158 | | return null; |
| | 159 | | } |
| 773 | 160 | | var splitString = attributesString.Split('|'); |
| | 161 | |
|
| 773 | 162 | | if (splitString.Length == 0) |
| | 163 | | { |
| 0 | 164 | | throw Errors.InvalidArgument(attributesString); |
| | 165 | | } |
| | 166 | |
|
| 773 | 167 | | NtfsFileAttributes attributes = default; |
| 3140 | 168 | | foreach (var s in splitString) |
| | 169 | | { |
| 797 | 170 | | var trimmed = s.Trim(); |
| | 171 | |
|
| 797 | 172 | | if (trimmed.Equals(nameof(NtfsFileAttributes.ReadOnly), StringComparison.InvariantCultureIgnoreCase)) |
| | 173 | | { |
| 24 | 174 | | attributes |= NtfsFileAttributes.ReadOnly; |
| | 175 | | } |
| 773 | 176 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.Hidden), StringComparison.InvariantCultureIgnoreCase)) |
| | 177 | | { |
| 0 | 178 | | attributes |= NtfsFileAttributes.Hidden; |
| | 179 | | } |
| 773 | 180 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.System), StringComparison.InvariantCultureIgnoreCase)) |
| | 181 | | { |
| 0 | 182 | | attributes |= NtfsFileAttributes.System; |
| | 183 | | } |
| 773 | 184 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.None), StringComparison.InvariantCultureIgnoreCase)) |
| | 185 | | { |
| 0 | 186 | | attributes |= NtfsFileAttributes.None; |
| | 187 | | } |
| 773 | 188 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.Directory), StringComparison.InvariantCultureIgnoreCas |
| | 189 | | { |
| 381 | 190 | | attributes |= NtfsFileAttributes.Directory; |
| | 191 | | } |
| 392 | 192 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.Archive), StringComparison.InvariantCultureIgnoreCase) |
| | 193 | | { |
| 392 | 194 | | attributes |= NtfsFileAttributes.Archive; |
| | 195 | | } |
| 0 | 196 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.Temporary), StringComparison.InvariantCultureIgnoreCas |
| | 197 | | { |
| 0 | 198 | | attributes |= NtfsFileAttributes.Temporary; |
| | 199 | | } |
| 0 | 200 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.Offline), StringComparison.InvariantCultureIgnoreCase) |
| | 201 | | { |
| 0 | 202 | | attributes |= NtfsFileAttributes.Offline; |
| | 203 | | } |
| 0 | 204 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.NotContentIndexed), StringComparison.InvariantCultureI |
| | 205 | | { |
| 0 | 206 | | attributes |= NtfsFileAttributes.NotContentIndexed; |
| | 207 | | } |
| 0 | 208 | | else if (trimmed.Equals(nameof(NtfsFileAttributes.NoScrubData), StringComparison.InvariantCultureIgnoreC |
| | 209 | | { |
| 0 | 210 | | attributes |= NtfsFileAttributes.NoScrubData; |
| | 211 | | } |
| | 212 | | else |
| | 213 | | { |
| 0 | 214 | | throw Errors.InvalidArgument(trimmed); |
| | 215 | | } |
| | 216 | | } |
| 773 | 217 | | return attributes; |
| | 218 | | } |
| | 219 | | } |
| | 220 | | } |