|
11 | 11 | using System.Runtime.InteropServices.ComTypes; |
12 | 12 | using System.Text; |
13 | 13 | using System.Windows.Forms; |
| 14 | +using Vanara; |
14 | 15 | using Vanara.PInvoke; |
15 | 16 | using Windows.System; |
16 | 17 | using Windows.Win32; |
| 18 | +using Windows.Win32.Foundation; |
17 | 19 | using Windows.Win32.Storage.FileSystem; |
| 20 | +using static Files.App.Constants.Widgets; |
| 21 | +using static Vanara.PInvoke.Kernel32; |
| 22 | +using HRESULT = Vanara.PInvoke.HRESULT; |
| 23 | +using HWND = Vanara.PInvoke.HWND; |
18 | 24 |
|
19 | 25 | namespace Files.App.Helpers |
20 | 26 | { |
@@ -886,24 +892,24 @@ private static Process CreatePowershellProcess(string command, PowerShellExecuti |
886 | 892 | public static SafeFileHandle CreateFileForWrite(string filePath, bool overwrite = true) |
887 | 893 | { |
888 | 894 | return new SafeFileHandle(Win32PInvoke.CreateFileFromApp(filePath, |
889 | | - Win32PInvoke.GENERIC_WRITE, 0, IntPtr.Zero, overwrite ? Win32PInvoke.CREATE_ALWAYS : Win32PInvoke.OPEN_ALWAYS, (uint)Win32PInvoke.File_Attributes.BackupSemantics, IntPtr.Zero), true); |
| 895 | + (uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE, 0, IntPtr.Zero, overwrite ? Win32PInvoke.CREATE_ALWAYS : Win32PInvoke.OPEN_ALWAYS, (uint)Win32PInvoke.File_Attributes.BackupSemantics, IntPtr.Zero), true); |
890 | 896 | } |
891 | 897 |
|
892 | 898 | public static SafeFileHandle OpenFileForRead(string filePath, bool readWrite = false, uint flags = 0) |
893 | 899 | { |
894 | 900 | return new SafeFileHandle(Win32PInvoke.CreateFileFromApp(filePath, |
895 | | - Win32PInvoke.GENERIC_READ | (readWrite ? Win32PInvoke.GENERIC_WRITE : 0), (uint)(Win32PInvoke.FILE_SHARE_READ | (readWrite ? 0 : Win32PInvoke.FILE_SHARE_WRITE)), IntPtr.Zero, Win32PInvoke.OPEN_EXISTING, (uint)Win32PInvoke.File_Attributes.BackupSemantics | flags, IntPtr.Zero), true); |
| 901 | + (uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | (uint)(readWrite ? FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE : 0u), (uint)(Win32PInvoke.FILE_SHARE_READ | (readWrite ? 0 : Win32PInvoke.FILE_SHARE_WRITE)), IntPtr.Zero, Win32PInvoke.OPEN_EXISTING, (uint)Win32PInvoke.File_Attributes.BackupSemantics | flags, IntPtr.Zero), true); |
896 | 902 | } |
897 | 903 |
|
898 | 904 | public static bool GetFileDateModified(string filePath, out FILETIME dateModified) |
899 | 905 | { |
900 | | - using var hFile = new SafeFileHandle(Win32PInvoke.CreateFileFromApp(filePath, Win32PInvoke.GENERIC_READ, Win32PInvoke.FILE_SHARE_READ, IntPtr.Zero, Win32PInvoke.OPEN_EXISTING, (uint)Win32PInvoke.File_Attributes.BackupSemantics, IntPtr.Zero), true); |
| 906 | + using var hFile = new SafeFileHandle(Win32PInvoke.CreateFileFromApp(filePath, (uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_READ, Win32PInvoke.FILE_SHARE_READ, IntPtr.Zero, Win32PInvoke.OPEN_EXISTING, (uint)Win32PInvoke.File_Attributes.BackupSemantics, IntPtr.Zero), true); |
901 | 907 | return Win32PInvoke.GetFileTime(hFile.DangerousGetHandle(), out _, out _, out dateModified); |
902 | 908 | } |
903 | 909 |
|
904 | 910 | public static bool SetFileDateModified(string filePath, FILETIME dateModified) |
905 | 911 | { |
906 | | - using var hFile = new SafeFileHandle(Win32PInvoke.CreateFileFromApp(filePath, Win32PInvoke.FILE_WRITE_ATTRIBUTES, 0, IntPtr.Zero, Win32PInvoke.OPEN_EXISTING, (uint)Win32PInvoke.File_Attributes.BackupSemantics, IntPtr.Zero), true); |
| 912 | + using var hFile = new SafeFileHandle(Win32PInvoke.CreateFileFromApp(filePath, (uint)FILE_ACCESS_RIGHTS.FILE_WRITE_ATTRIBUTES, 0, IntPtr.Zero, Win32PInvoke.OPEN_EXISTING, (uint)Win32PInvoke.File_Attributes.BackupSemantics, IntPtr.Zero), true); |
907 | 913 | return Win32PInvoke.SetFileTime(hFile.DangerousGetHandle(), new(), new(), dateModified); |
908 | 914 | } |
909 | 915 |
|
@@ -937,13 +943,42 @@ public static bool UnsetFileAttribute(string lpFileName, FileAttributes dwAttrs) |
937 | 943 | return Win32PInvoke.SetFileAttributesFromApp(lpFileName, lpFileInfo.dwFileAttributes & ~dwAttrs); |
938 | 944 | } |
939 | 945 |
|
| 946 | + public static unsafe bool CanCompressContent(string path) |
| 947 | + { |
| 948 | + path = Path.GetPathRoot(path) ?? string.Empty; |
| 949 | + |
| 950 | + // MAX_PATH = 260 + 1 (null termination) |
| 951 | + var volumeName = new PWSTR(); |
| 952 | + var fileSystemName = new PWSTR(); |
| 953 | + |
| 954 | + uint serialNumber = 0; |
| 955 | + uint maxComponentLength = 0; |
| 956 | + uint fileSystemFlags = 0; |
| 957 | + |
| 958 | + var success = PInvoke.GetVolumeInformation( |
| 959 | + path, |
| 960 | + volumeName, |
| 961 | + 261u, |
| 962 | + &serialNumber, |
| 963 | + &maxComponentLength, |
| 964 | + &fileSystemFlags, |
| 965 | + fileSystemName, |
| 966 | + 261u); |
| 967 | + |
| 968 | + if (!success) |
| 969 | + return false; |
| 970 | + |
| 971 | + // 0x00000010 FILE_FILE_COMPRESSION |
| 972 | + return (fileSystemFlags & 0x00000010) != 0; |
| 973 | + } |
| 974 | + |
940 | 975 | public static unsafe bool SetCompressionAttributeIoctl(string lpFileName, bool isCompressed) |
941 | 976 | { |
942 | 977 | // GENERIC_READ | GENERIC_WRITE flags are needed here |
943 | 978 | // FILE_FLAG_BACKUP_SEMANTICS is used to open directories |
944 | 979 | using var hFile = PInvoke.CreateFile( |
945 | 980 | lpFileName, |
946 | | - Win32PInvoke.GENERIC_READ | Win32PInvoke.GENERIC_WRITE | Win32PInvoke.FILE_WRITE_ATTRIBUTES, |
| 981 | + (uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE | FILE_ACCESS_RIGHTS.FILE_WRITE_ATTRIBUTES), |
947 | 982 | FILE_SHARE_MODE.FILE_SHARE_READ | FILE_SHARE_MODE.FILE_SHARE_WRITE, |
948 | 983 | lpSecurityAttributes: null, |
949 | 984 | FILE_CREATION_DISPOSITION.OPEN_EXISTING, |
@@ -973,7 +1008,7 @@ public static unsafe bool SetCompressionAttributeIoctl(string lpFileName, bool i |
973 | 1008 | public static string ReadStringFromFile(string filePath) |
974 | 1009 | { |
975 | 1010 | IntPtr hFile = Win32PInvoke.CreateFileFromApp(filePath, |
976 | | - Win32PInvoke.GENERIC_READ, |
| 1011 | + (uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_READ, |
977 | 1012 | Win32PInvoke.FILE_SHARE_READ, |
978 | 1013 | IntPtr.Zero, |
979 | 1014 | Win32PInvoke.OPEN_EXISTING, |
@@ -1022,7 +1057,7 @@ public static string ReadStringFromFile(string filePath) |
1022 | 1057 | public static bool WriteStringToFile(string filePath, string str, Win32PInvoke.File_Attributes flags = 0) |
1023 | 1058 | { |
1024 | 1059 | IntPtr hStream = Win32PInvoke.CreateFileFromApp(filePath, |
1025 | | - Win32PInvoke.GENERIC_WRITE, 0, IntPtr.Zero, Win32PInvoke.CREATE_ALWAYS, (uint)(Win32PInvoke.File_Attributes.BackupSemantics | flags), IntPtr.Zero); |
| 1060 | + (uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE, 0, IntPtr.Zero, Win32PInvoke.CREATE_ALWAYS, (uint)(Win32PInvoke.File_Attributes.BackupSemantics | flags), IntPtr.Zero); |
1026 | 1061 | if (hStream.ToInt64() == -1) |
1027 | 1062 | { |
1028 | 1063 | return false; |
|
0 commit comments