Skip to content

Commit 33af4ec

Browse files
committed
Use GetVolumeInfo
1 parent c74fdf9 commit 33af4ec

File tree

11 files changed

+68
-27
lines changed

11 files changed

+68
-27
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,6 @@ QueryDosDevice
138138
DeviceIoControl
139139
GetLastError
140140
CreateFile
141-
GetVolumeInformation
141+
GetVolumeInformation
142+
COMPRESSION_FORMAT
143+
FILE_ACCESS_RIGHTS

src/Files.App/Data/Models/RemovableDevice.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.Threading.Tasks;
7+
using Windows.Win32.Storage.FileSystem;
78
using static Files.App.Helpers.Win32PInvoke;
89

910
namespace Files.App.Data.Models
@@ -20,7 +21,7 @@ public RemovableDevice(string letter)
2021
string filename = @"\\.\" + driveLetter + ":";
2122

2223
handle = CreateFileFromAppW(filename,
23-
GENERIC_READ | GENERIC_WRITE,
24+
(uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE),
2425
FILE_SHARE_READ | FILE_SHARE_WRITE,
2526
nint.Zero, OPEN_EXISTING, 0, nint.Zero);
2627
}

src/Files.App/Data/Models/SelectedItemsPropertiesViewModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,13 @@ public bool? IsContentCompressedEditedValue
736736
set => SetProperty(ref isContentCompressedEditedValue, value);
737737
}
738738

739+
private bool? canCompressContent;
740+
public bool? CanCompressContent
741+
{
742+
get => canCompressContent;
743+
set => SetProperty(ref canCompressContent, value);
744+
}
745+
739746
private bool runAsAdmin;
740747
public bool RunAsAdmin
741748
{

src/Files.App/Helpers/Win32/Win32Helper.Storage.cs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
using System.Runtime.InteropServices.ComTypes;
1212
using System.Text;
1313
using System.Windows.Forms;
14+
using Vanara;
1415
using Vanara.PInvoke;
1516
using Windows.System;
1617
using Windows.Win32;
18+
using Windows.Win32.Foundation;
1719
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;
1824

1925
namespace Files.App.Helpers
2026
{
@@ -886,24 +892,24 @@ private static Process CreatePowershellProcess(string command, PowerShellExecuti
886892
public static SafeFileHandle CreateFileForWrite(string filePath, bool overwrite = true)
887893
{
888894
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);
890896
}
891897

892898
public static SafeFileHandle OpenFileForRead(string filePath, bool readWrite = false, uint flags = 0)
893899
{
894900
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);
896902
}
897903

898904
public static bool GetFileDateModified(string filePath, out FILETIME dateModified)
899905
{
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);
901907
return Win32PInvoke.GetFileTime(hFile.DangerousGetHandle(), out _, out _, out dateModified);
902908
}
903909

904910
public static bool SetFileDateModified(string filePath, FILETIME dateModified)
905911
{
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);
907913
return Win32PInvoke.SetFileTime(hFile.DangerousGetHandle(), new(), new(), dateModified);
908914
}
909915

@@ -937,13 +943,42 @@ public static bool UnsetFileAttribute(string lpFileName, FileAttributes dwAttrs)
937943
return Win32PInvoke.SetFileAttributesFromApp(lpFileName, lpFileInfo.dwFileAttributes & ~dwAttrs);
938944
}
939945

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+
940975
public static unsafe bool SetCompressionAttributeIoctl(string lpFileName, bool isCompressed)
941976
{
942977
// GENERIC_READ | GENERIC_WRITE flags are needed here
943978
// FILE_FLAG_BACKUP_SEMANTICS is used to open directories
944979
using var hFile = PInvoke.CreateFile(
945980
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),
947982
FILE_SHARE_MODE.FILE_SHARE_READ | FILE_SHARE_MODE.FILE_SHARE_WRITE,
948983
lpSecurityAttributes: null,
949984
FILE_CREATION_DISPOSITION.OPEN_EXISTING,
@@ -973,7 +1008,7 @@ public static unsafe bool SetCompressionAttributeIoctl(string lpFileName, bool i
9731008
public static string ReadStringFromFile(string filePath)
9741009
{
9751010
IntPtr hFile = Win32PInvoke.CreateFileFromApp(filePath,
976-
Win32PInvoke.GENERIC_READ,
1011+
(uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_READ,
9771012
Win32PInvoke.FILE_SHARE_READ,
9781013
IntPtr.Zero,
9791014
Win32PInvoke.OPEN_EXISTING,
@@ -1022,7 +1057,7 @@ public static string ReadStringFromFile(string filePath)
10221057
public static bool WriteStringToFile(string filePath, string str, Win32PInvoke.File_Attributes flags = 0)
10231058
{
10241059
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);
10261061
if (hStream.ToInt64() == -1)
10271062
{
10281063
return false;

src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public static partial class Win32PInvoke
2222
public const int FILE_NOTIFY_CHANGE_SECURITY = 256;
2323

2424
public const int INVALID_HANDLE_VALUE = -1;
25-
public const uint GENERIC_READ = 0x80000000;
26-
public const uint GENERIC_WRITE = 0x40000000;
2725
public const int FILE_SHARE_READ = 0x00000001;
2826
public const int FILE_SHARE_WRITE = 0x00000002;
2927
public const uint FILE_SHARE_DELETE = 0x00000004;
@@ -32,7 +30,6 @@ public static partial class Win32PInvoke
3230
public const int IOCTL_STORAGE_MEDIA_REMOVAL = 0x002D4804;
3331

3432
public const uint FILE_APPEND_DATA = 0x0004;
35-
public const uint FILE_WRITE_ATTRIBUTES = 0x100;
3633

3734

3835
public const uint FILE_BEGIN = 0;

src/Files.App/Utils/Serialization/Implementation/DefaultSettingsSerializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.IO;
55
using Windows.Win32;
6+
using Windows.Win32.Storage.FileSystem;
67
using static Files.App.Helpers.Win32Helper;
78
using static Files.App.Helpers.Win32PInvoke;
89

@@ -16,7 +17,7 @@ public bool CreateFile(string path)
1617
{
1718
PInvoke.CreateDirectoryFromApp(Path.GetDirectoryName(path), null);
1819

19-
var hFile = CreateFileFromApp(path, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_ALWAYS, (uint)File_Attributes.BackupSemantics, IntPtr.Zero);
20+
var hFile = CreateFileFromApp(path, (uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_ALWAYS, (uint)File_Attributes.BackupSemantics, IntPtr.Zero);
2021
if (hFile.IsHandleInvalid())
2122
{
2223
return false;

src/Files.App/ViewModels/Properties/Items/FileProperties.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(
9393

9494
public override async Task GetSpecialPropertiesAsync()
9595
{
96-
ViewModel.IsReadOnly = Win32Helper.HasFileAttribute(
97-
Item.ItemPath, System.IO.FileAttributes.ReadOnly);
98-
ViewModel.IsHidden = Win32Helper.HasFileAttribute(
99-
Item.ItemPath, System.IO.FileAttributes.Hidden);
100-
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(
101-
Item.ItemPath, System.IO.FileAttributes.Compressed);
96+
ViewModel.IsReadOnly = Win32Helper.HasFileAttribute(Item.ItemPath, System.IO.FileAttributes.ReadOnly);
97+
ViewModel.IsHidden = Win32Helper.HasFileAttribute(Item.ItemPath, System.IO.FileAttributes.Hidden);
98+
ViewModel.CanCompressContent = Win32Helper.CanCompressContent(Item.ItemPath);
99+
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(Item.ItemPath, System.IO.FileAttributes.Compressed);
102100

103101
ViewModel.ItemSizeVisibility = true;
104102
ViewModel.ItemSize = Item.FileSizeBytes.ToLongSizeString();

src/Files.App/ViewModels/Properties/Items/FolderProperties.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(
7373

7474
public async override Task GetSpecialPropertiesAsync()
7575
{
76-
ViewModel.IsHidden = Win32Helper.HasFileAttribute(
77-
Item.ItemPath, System.IO.FileAttributes.Hidden);
78-
79-
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(
80-
Item.ItemPath, System.IO.FileAttributes.Compressed);
76+
ViewModel.IsHidden = Win32Helper.HasFileAttribute(Item.ItemPath, System.IO.FileAttributes.Hidden);
77+
ViewModel.CanCompressContent = Win32Helper.CanCompressContent(Item.ItemPath);
78+
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(Item.ItemPath, System.IO.FileAttributes.Compressed);
8179

8280
var result = await FileThumbnailHelper.GetIconAsync(
8381
Item.ItemPath,

src/Files.App/ViewModels/Properties/Items/LibraryProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async override Task GetSpecialPropertiesAsync()
4848
{
4949
ViewModel.IsReadOnly = Win32Helper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.ReadOnly);
5050
ViewModel.IsHidden = Win32Helper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.Hidden);
51-
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.Compressed);
51+
ViewModel.CanCompressContent = false;
5252

5353
var result = await FileThumbnailHelper.GetIconAsync(
5454
Library.ItemPath,

src/Files.App/ViewModels/Settings/AdvancedViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Windows.Storage;
1313
using Windows.Storage.Pickers;
1414
using Windows.System;
15+
using Windows.Win32.Storage.FileSystem;
1516

1617
namespace Files.App.ViewModels.Settings
1718
{
@@ -218,7 +219,7 @@ private async Task ExportSettingsAsync()
218219
{
219220
var handle = Win32PInvoke.CreateFileFromAppW(
220221
filePath,
221-
Win32PInvoke.GENERIC_READ | Win32PInvoke.GENERIC_WRITE,
222+
(uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE),
222223
Win32PInvoke.FILE_SHARE_READ | Win32PInvoke.FILE_SHARE_WRITE,
223224
nint.Zero,
224225
Win32PInvoke.CREATE_NEW,

0 commit comments

Comments
 (0)