|
| 1 | +using Files.App.Helpers; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Runtime.InteropServices.WindowsRuntime; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Windows.Foundation; |
| 7 | +using Windows.Storage; |
| 8 | +using Windows.Storage.FileProperties; |
| 9 | +using static Files.Backend.Helpers.NativeFindStorageItemHelper; |
| 10 | + |
| 11 | +namespace Files.App.Filesystem.StorageItems |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Implements IStorageItem, allowing us to get an instance of IStorageItem for a ListedItem |
| 15 | + /// representing a standard filesystem item. As such, VirtualStorageItem does not support hidden, |
| 16 | + /// shortcut, or link items. |
| 17 | + /// </summary> |
| 18 | + public class VirtualStorageItem : IStorageItem |
| 19 | + { |
| 20 | + private static BasicProperties props; |
| 21 | + |
| 22 | + public Windows.Storage.FileAttributes Attributes { get; init; } |
| 23 | + |
| 24 | + public DateTimeOffset DateCreated { get; init; } |
| 25 | + |
| 26 | + public string Name { get; init; } |
| 27 | + |
| 28 | + public string Path { get; init; } |
| 29 | + |
| 30 | + private VirtualStorageItem() { } |
| 31 | + |
| 32 | + public static VirtualStorageItem FromListedItem(ListedItem item) |
| 33 | + { |
| 34 | + return new VirtualStorageItem() |
| 35 | + { |
| 36 | + Name = item.ItemNameRaw, |
| 37 | + Path = item.ItemPath, |
| 38 | + DateCreated = item.ItemDateCreatedReal, |
| 39 | + Attributes = item.IsArchive || item.PrimaryItemAttribute == StorageItemTypes.File ? Windows.Storage.FileAttributes.Normal : Windows.Storage.FileAttributes.Directory |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + public static VirtualStorageItem FromPath(string path) |
| 44 | + { |
| 45 | + FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic; |
| 46 | + int additionalFlags = FIND_FIRST_EX_LARGE_FETCH; |
| 47 | + IntPtr hFile = FindFirstFileExFromApp(path, findInfoLevel, out WIN32_FIND_DATA findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags); |
| 48 | + if (hFile.ToInt64() != -1) |
| 49 | + { |
| 50 | + // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/c8e77b37-3909-4fe6-a4ea-2b9d423b1ee4 |
| 51 | + bool isReparsePoint = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.ReparsePoint) == System.IO.FileAttributes.ReparsePoint; |
| 52 | + bool isSymlink = isReparsePoint && findData.dwReserved0 == NativeFileOperationsHelper.IO_REPARSE_TAG_SYMLINK; |
| 53 | + bool isHidden = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden; |
| 54 | + bool isDirectory = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory; |
| 55 | + |
| 56 | + if (!(isHidden && isSymlink)) |
| 57 | + { |
| 58 | + DateTime itemCreatedDate; |
| 59 | + |
| 60 | + try |
| 61 | + { |
| 62 | + FileTimeToSystemTime(ref findData.ftCreationTime, out SYSTEMTIME systemCreatedDateOutput); |
| 63 | + itemCreatedDate = systemCreatedDateOutput.ToDateTime(); |
| 64 | + } |
| 65 | + catch (ArgumentException) |
| 66 | + { |
| 67 | + // Invalid date means invalid findData, do not add to list |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + return new VirtualStorageItem() |
| 72 | + { |
| 73 | + Name = findData.cFileName, |
| 74 | + Path = path, |
| 75 | + DateCreated = itemCreatedDate, |
| 76 | + Attributes = isDirectory ? Windows.Storage.FileAttributes.Directory : Windows.Storage.FileAttributes.Normal |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + FindClose(hFile); |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + private async void StreamedFileWriter(StreamedFileDataRequest request) |
| 87 | + { |
| 88 | + try |
| 89 | + { |
| 90 | + using (var stream = request.AsStreamForWrite()) |
| 91 | + { |
| 92 | + await stream.FlushAsync(); |
| 93 | + } |
| 94 | + request.Dispose(); |
| 95 | + } |
| 96 | + catch (Exception) |
| 97 | + { |
| 98 | + request.FailAndClose(StreamedFileFailureMode.Incomplete); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public IAsyncAction RenameAsync(string desiredName) |
| 103 | + { |
| 104 | + throw new NotImplementedException(); |
| 105 | + } |
| 106 | + |
| 107 | + public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option) |
| 108 | + { |
| 109 | + throw new NotImplementedException(); |
| 110 | + } |
| 111 | + |
| 112 | + public IAsyncAction DeleteAsync() |
| 113 | + { |
| 114 | + throw new NotImplementedException(); |
| 115 | + } |
| 116 | + |
| 117 | + public IAsyncAction DeleteAsync(StorageDeleteOption option) |
| 118 | + { |
| 119 | + throw new NotImplementedException(); |
| 120 | + } |
| 121 | + |
| 122 | + public IAsyncOperation<BasicProperties> GetBasicPropertiesAsync() |
| 123 | + { |
| 124 | + return AsyncInfo.Run(async (cancellationToken) => |
| 125 | + { |
| 126 | + async Task<BasicProperties> GetFakeBasicProperties() |
| 127 | + { |
| 128 | + var streamedFile = await StorageFile.CreateStreamedFileAsync(Name, StreamedFileWriter, null); |
| 129 | + return await streamedFile.GetBasicPropertiesAsync(); |
| 130 | + } |
| 131 | + return props ?? (props = await GetFakeBasicProperties()); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + public bool IsOfType(StorageItemTypes type) |
| 136 | + { |
| 137 | + return Attributes.HasFlag(Windows.Storage.FileAttributes.Directory) ? type == StorageItemTypes.Folder : type == StorageItemTypes.File; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments