Skip to content

Commit

Permalink
Implemented preemptive caching (#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakoss authored Feb 14, 2021
1 parent 824aa11 commit 22c55c3
Show file tree
Hide file tree
Showing 16 changed files with 1,081 additions and 662 deletions.
2 changes: 1 addition & 1 deletion Files/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public async Task AddItemToSidebarAsync(string path)
{
var item = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item));
if (res || (FilesystemResult)ItemViewModel.CheckFolderAccessWithWin32(path))
if (res || (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(path))
{
int insertIndex = MainPage.SideBarItems.IndexOf(MainPage.SideBarItems.Last(x => x.ItemType == NavigationControlItemType.Location
&& !x.Path.Equals(App.AppSettings.RecycleBinPath))) + 1;
Expand Down
29 changes: 28 additions & 1 deletion Files/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace Files.Extensions
{
Expand All @@ -12,5 +15,29 @@ internal static class EnumerableExtensions
/// <returns><see cref="IEnumerable{T}"/> with <paramref name="item"/></returns>
internal static IEnumerable<T> CreateEnumerable<T>(this T item) =>
new List<T>() { item };

/// <summary>
/// Executes given lambda parallely on given data set with max degree of parallelism set up
/// </summary>
/// <typeparam name="T">The item type</typeparam>
/// <param name="source">Data to process</param>
/// <param name="body">Lambda to execute on all items</param>
/// <param name="maxDegreeOfParallelism">Max degree of parallelism (-1 for unbounded execution)</param>
/// <returns></returns>
internal static Task AsyncParallelForEach<T>(this IEnumerable<T> source, Func<T, Task> body, int maxDegreeOfParallelism)
{
var options = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = maxDegreeOfParallelism
};

var block = new ActionBlock<T>(body, options);

foreach (var item in source)
block.Post(item);

block.Complete();
return block.Completion;
}
}
}
21 changes: 21 additions & 0 deletions Files/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;

namespace Files.Extensions
{
internal static class TaskExtensions
{
#pragma warning disable RCS1175 // Unused this parameter.
#pragma warning disable IDE0060 // Remove unused parameter
/// <summary>
/// This function is to explicitly state that we know that we're runnign task without awaiting.
/// This makes visual studio to drop warning, but the programmer intent is still clearly stated.
/// </summary>
/// <param name="task"></param>
internal static void Forget(this Task task)
{
// do nothing, just forget about the task
}
#pragma warning restore IDE0060 // Remove unused parameter
#pragma warning restore RCS1175 // Unused this parameter.
}
}
4 changes: 4 additions & 0 deletions Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@
<Compile Include="Enums\DynamicDialogButtons.cs" />
<Compile Include="Enums\DynamicDialogResult.cs" />
<Compile Include="Enums\FolderLayoutModes.cs" />
<Compile Include="Extensions\TaskExtensions.cs" />
<Compile Include="Filesystem\FolderHelpers.cs" />
<Compile Include="Filesystem\StorageEnumerators\UniversalStorageEnumerator.cs" />
<Compile Include="Filesystem\StorageEnumerators\Win32StorageEnumerator.cs" />
<Compile Include="Enums\FileSystemStatusCode.cs" />
<Compile Include="Filesystem\CloudDrivesManager.cs" />
<Compile Include="Filesystem\Cloud\Providers\OneDriveSharePointCloudProvider.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Files/Filesystem/FilesystemOperations/FilesystemOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ await DialogDisplayHelper.ShowDialogAsync(
}
if (fsCopyResult)
{
if (associatedInstance.FilesystemViewModel.CheckFolderForHiddenAttribute(source.Path))
if (FolderHelpers.CheckFolderForHiddenAttribute(source.Path))
{
// The source folder was hidden, apply hidden attribute to destination
NativeFileOperationsHelper.SetFileAttribute(fsCopyResult.Result.Path, FileAttributes.Hidden);
Expand Down Expand Up @@ -434,7 +434,7 @@ await DialogDisplayHelper.ShowDialogAsync(
}
if (fsResultMove)
{
if (associatedInstance.FilesystemViewModel.CheckFolderForHiddenAttribute(source.Path))
if (FolderHelpers.CheckFolderForHiddenAttribute(source.Path))
{
// The source folder was hidden, apply hidden attribute to destination
NativeFileOperationsHelper.SetFileAttribute(fsResultMove.Result.Path, FileAttributes.Hidden);
Expand Down
77 changes: 77 additions & 0 deletions Files/Filesystem/FolderHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using static Files.Helpers.NativeFindStorageItemHelper;
using FileAttributes = System.IO.FileAttributes;

namespace Files.Filesystem
{
public static class FolderHelpers
{
public static bool CheckFolderAccessWithWin32(string path)
{
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
IntPtr hFileTsk = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findDataTsk, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
additionalFlags);
if (hFileTsk.ToInt64() != -1)
{
FindClose(hFileTsk);
return true;
}
return false;
}

public static bool CheckFolderForHiddenAttribute(string path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
IntPtr hFileTsk = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findDataTsk, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
additionalFlags);
if (hFileTsk.ToInt64() == -1)
{
return false;
}
var isHidden = ((FileAttributes)findDataTsk.dwFileAttributes & FileAttributes.Hidden) == FileAttributes.Hidden;
FindClose(hFileTsk);
return isHidden;
}

public static async Task<bool> CheckBitlockerStatusAsync(StorageFolder rootFolder, string path)
{
if (rootFolder == null || rootFolder.Properties == null)
{
return false;
}
if (Path.IsPathRooted(path) && Path.GetPathRoot(path) == path)
{
IDictionary<string, object> extraProperties = await rootFolder.Properties.RetrievePropertiesAsync(new string[] { "System.Volume.BitLockerProtection" });
return (int?)extraProperties["System.Volume.BitLockerProtection"] == 6; // Drive is bitlocker protected and locked
}
return false;
}

/// <summary>
/// This function is used to determine whether or not a folder has any contents.
/// </summary>
/// <param name="targetPath">The path to the target folder</param>
///
public static bool CheckForFilesFolders(string targetPath)
{
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;

IntPtr hFile = FindFirstFileExFromApp(targetPath + "\\*.*", findInfoLevel, out WIN32_FIND_DATA _, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags);
FindNextFile(hFile, out _);
var result = FindNextFile(hFile, out _);
FindClose(hFile);
return result;
}
}
}
Loading

0 comments on commit 22c55c3

Please sign in to comment.