forked from haf/DotNetZip.Semverd
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strips invalid file attributes, leaving only those valid for the curr…
…ent operating system before calling File.SetAttributes
- Loading branch information
René Mihula
committed
Nov 22, 2024
1 parent
439dc46
commit dd726f2
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.IO; | ||
using Interop = System.Runtime.InteropServices; | ||
|
||
namespace Ionic.Zip | ||
{ | ||
/// <summary> | ||
/// Helper class for file attribute operations. | ||
/// </summary> | ||
/// <example> | ||
/// FileAttributes attributesToCheck = (FileAttributes)0x27; // Example attributes | ||
/// FileAttributes validAttributes = FileAttributeHelper.StripInvalidAttributes(attributesToCheck); | ||
/// <para/> | ||
/// Console.WriteLine($"Original Attributes: {attributesToCheck}"); | ||
/// Console.WriteLine($"Valid Attributes: {validAttributes}"); | ||
/// </example> | ||
public static class FileAttributeHelper | ||
{ | ||
// Define a mask of valid attributes for Windows and non-Windows systems | ||
private static readonly FileAttributes ValidAttributesWindows = | ||
FileAttributes.ReadOnly | | ||
FileAttributes.Hidden | | ||
FileAttributes.System | | ||
FileAttributes.Directory | | ||
FileAttributes.Archive | | ||
FileAttributes.Device | | ||
FileAttributes.Normal | | ||
FileAttributes.Temporary | | ||
FileAttributes.SparseFile | | ||
FileAttributes.ReparsePoint | | ||
FileAttributes.Compressed | | ||
FileAttributes.Offline | | ||
FileAttributes.NotContentIndexed | | ||
FileAttributes.Encrypted | | ||
FileAttributes.IntegrityStream | | ||
FileAttributes.NoScrubData; | ||
|
||
private static readonly FileAttributes ValidAttributesNonWindows = | ||
FileAttributes.ReadOnly | | ||
FileAttributes.Hidden | | ||
FileAttributes.System | | ||
FileAttributes.Directory | | ||
FileAttributes.Archive | | ||
FileAttributes.Normal | | ||
FileAttributes.Temporary; | ||
|
||
/// <summary> | ||
/// Strips invalid file attributes, leaving only those valid for the current operating system. | ||
/// </summary> | ||
/// <param name="attributes">The file attributes to be validated.</param> | ||
/// <returns>A set of valid file attributes.</returns> | ||
public static FileAttributes StripInvalidAttributes(FileAttributes attributes) | ||
{ | ||
var validAttributes = Interop.RuntimeInformation.IsOSPlatform(Interop.OSPlatform.Windows) ? ValidAttributesWindows : ValidAttributesNonWindows; | ||
return attributes & validAttributes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters