Skip to content

Commit 22c55c3

Browse files
authored
Implemented preemptive caching (#2944)
1 parent 824aa11 commit 22c55c3

File tree

16 files changed

+1081
-662
lines changed

16 files changed

+1081
-662
lines changed

Files/DataModels/SidebarPinnedModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public async Task AddItemToSidebarAsync(string path)
190190
{
191191
var item = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
192192
var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item));
193-
if (res || (FilesystemResult)ItemViewModel.CheckFolderAccessWithWin32(path))
193+
if (res || (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(path))
194194
{
195195
int insertIndex = MainPage.SideBarItems.IndexOf(MainPage.SideBarItems.Last(x => x.ItemType == NavigationControlItemType.Location
196196
&& !x.Path.Equals(App.AppSettings.RecycleBinPath))) + 1;

Files/Extensions/EnumerableExtensions.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using System.Threading.Tasks.Dataflow;
25

36
namespace Files.Extensions
47
{
@@ -12,5 +15,29 @@ internal static class EnumerableExtensions
1215
/// <returns><see cref="IEnumerable{T}"/> with <paramref name="item"/></returns>
1316
internal static IEnumerable<T> CreateEnumerable<T>(this T item) =>
1417
new List<T>() { item };
18+
19+
/// <summary>
20+
/// Executes given lambda parallely on given data set with max degree of parallelism set up
21+
/// </summary>
22+
/// <typeparam name="T">The item type</typeparam>
23+
/// <param name="source">Data to process</param>
24+
/// <param name="body">Lambda to execute on all items</param>
25+
/// <param name="maxDegreeOfParallelism">Max degree of parallelism (-1 for unbounded execution)</param>
26+
/// <returns></returns>
27+
internal static Task AsyncParallelForEach<T>(this IEnumerable<T> source, Func<T, Task> body, int maxDegreeOfParallelism)
28+
{
29+
var options = new ExecutionDataflowBlockOptions
30+
{
31+
MaxDegreeOfParallelism = maxDegreeOfParallelism
32+
};
33+
34+
var block = new ActionBlock<T>(body, options);
35+
36+
foreach (var item in source)
37+
block.Post(item);
38+
39+
block.Complete();
40+
return block.Completion;
41+
}
1542
}
1643
}

Files/Extensions/TaskExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Files.Extensions
4+
{
5+
internal static class TaskExtensions
6+
{
7+
#pragma warning disable RCS1175 // Unused this parameter.
8+
#pragma warning disable IDE0060 // Remove unused parameter
9+
/// <summary>
10+
/// This function is to explicitly state that we know that we're runnign task without awaiting.
11+
/// This makes visual studio to drop warning, but the programmer intent is still clearly stated.
12+
/// </summary>
13+
/// <param name="task"></param>
14+
internal static void Forget(this Task task)
15+
{
16+
// do nothing, just forget about the task
17+
}
18+
#pragma warning restore IDE0060 // Remove unused parameter
19+
#pragma warning restore RCS1175 // Unused this parameter.
20+
}
21+
}

Files/Files.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@
178178
<Compile Include="Enums\DynamicDialogButtons.cs" />
179179
<Compile Include="Enums\DynamicDialogResult.cs" />
180180
<Compile Include="Enums\FolderLayoutModes.cs" />
181+
<Compile Include="Extensions\TaskExtensions.cs" />
182+
<Compile Include="Filesystem\FolderHelpers.cs" />
183+
<Compile Include="Filesystem\StorageEnumerators\UniversalStorageEnumerator.cs" />
184+
<Compile Include="Filesystem\StorageEnumerators\Win32StorageEnumerator.cs" />
181185
<Compile Include="Enums\FileSystemStatusCode.cs" />
182186
<Compile Include="Filesystem\CloudDrivesManager.cs" />
183187
<Compile Include="Filesystem\Cloud\Providers\OneDriveSharePointCloudProvider.cs" />

Files/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ await DialogDisplayHelper.ShowDialogAsync(
206206
}
207207
if (fsCopyResult)
208208
{
209-
if (associatedInstance.FilesystemViewModel.CheckFolderForHiddenAttribute(source.Path))
209+
if (FolderHelpers.CheckFolderForHiddenAttribute(source.Path))
210210
{
211211
// The source folder was hidden, apply hidden attribute to destination
212212
NativeFileOperationsHelper.SetFileAttribute(fsCopyResult.Result.Path, FileAttributes.Hidden);
@@ -434,7 +434,7 @@ await DialogDisplayHelper.ShowDialogAsync(
434434
}
435435
if (fsResultMove)
436436
{
437-
if (associatedInstance.FilesystemViewModel.CheckFolderForHiddenAttribute(source.Path))
437+
if (FolderHelpers.CheckFolderForHiddenAttribute(source.Path))
438438
{
439439
// The source folder was hidden, apply hidden attribute to destination
440440
NativeFileOperationsHelper.SetFileAttribute(fsResultMove.Result.Path, FileAttributes.Hidden);

Files/Filesystem/FolderHelpers.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using Windows.Storage;
6+
using static Files.Helpers.NativeFindStorageItemHelper;
7+
using FileAttributes = System.IO.FileAttributes;
8+
9+
namespace Files.Filesystem
10+
{
11+
public static class FolderHelpers
12+
{
13+
public static bool CheckFolderAccessWithWin32(string path)
14+
{
15+
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
16+
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
17+
IntPtr hFileTsk = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findDataTsk, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
18+
additionalFlags);
19+
if (hFileTsk.ToInt64() != -1)
20+
{
21+
FindClose(hFileTsk);
22+
return true;
23+
}
24+
return false;
25+
}
26+
27+
public static bool CheckFolderForHiddenAttribute(string path)
28+
{
29+
if (string.IsNullOrEmpty(path))
30+
{
31+
return false;
32+
}
33+
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
34+
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
35+
IntPtr hFileTsk = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findDataTsk, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
36+
additionalFlags);
37+
if (hFileTsk.ToInt64() == -1)
38+
{
39+
return false;
40+
}
41+
var isHidden = ((FileAttributes)findDataTsk.dwFileAttributes & FileAttributes.Hidden) == FileAttributes.Hidden;
42+
FindClose(hFileTsk);
43+
return isHidden;
44+
}
45+
46+
public static async Task<bool> CheckBitlockerStatusAsync(StorageFolder rootFolder, string path)
47+
{
48+
if (rootFolder == null || rootFolder.Properties == null)
49+
{
50+
return false;
51+
}
52+
if (Path.IsPathRooted(path) && Path.GetPathRoot(path) == path)
53+
{
54+
IDictionary<string, object> extraProperties = await rootFolder.Properties.RetrievePropertiesAsync(new string[] { "System.Volume.BitLockerProtection" });
55+
return (int?)extraProperties["System.Volume.BitLockerProtection"] == 6; // Drive is bitlocker protected and locked
56+
}
57+
return false;
58+
}
59+
60+
/// <summary>
61+
/// This function is used to determine whether or not a folder has any contents.
62+
/// </summary>
63+
/// <param name="targetPath">The path to the target folder</param>
64+
///
65+
public static bool CheckForFilesFolders(string targetPath)
66+
{
67+
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
68+
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
69+
70+
IntPtr hFile = FindFirstFileExFromApp(targetPath + "\\*.*", findInfoLevel, out WIN32_FIND_DATA _, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags);
71+
FindNextFile(hFile, out _);
72+
var result = FindNextFile(hFile, out _);
73+
FindClose(hFile);
74+
return result;
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)