Skip to content

Commit

Permalink
Update FileSizeCalculator.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa authored Nov 12, 2024
1 parent fdfb696 commit 9dabb75
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Files.App/Utils/Storage/Operations/FileSizeCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ await Parallel.ForEachAsync(
unsafe void ComputeSizeRecursively(string path, CancellationToken token)
{
var queue = new Queue<string>();

if (!Win32Helper.HasFileAttribute(path, FileAttributes.Directory))
{
ComputeFileSize(path);
Expand All @@ -49,14 +50,14 @@ unsafe void ComputeSizeRecursively(string path, CancellationToken token)

while (queue.TryDequeue(out var directory))
{
WIN32_FIND_DATAW* findData = default;
WIN32_FIND_DATAW findData = default;

fixed (char* pszFilePath = directory + "\\*.*")
{
var hFile = PInvoke.FindFirstFileEx(
pszFilePath,
FINDEX_INFO_LEVELS.FindExInfoBasic,
findData,
&findData,
FINDEX_SEARCH_OPS.FindExSearchNameMatch,
null,
FIND_FIRST_EX_FLAGS.FIND_FIRST_EX_LARGE_FETCH);
Expand All @@ -65,19 +66,20 @@ unsafe void ComputeSizeRecursively(string path, CancellationToken token)
{
do
{
FILE_FLAGS_AND_ATTRIBUTES attributes = (FILE_FLAGS_AND_ATTRIBUTES)findData->dwFileAttributes;
FILE_FLAGS_AND_ATTRIBUTES attributes = (FILE_FLAGS_AND_ATTRIBUTES)findData.dwFileAttributes;

if (attributes.HasFlag(FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_REPARSE_POINT))
// Skip symbolic links and junctions
continue;

var itemPath = Path.Combine(directory, findData->cFileName.ToString());
var itemPath = Path.Combine(directory, findData.cFileName.ToString());

if (attributes.HasFlag(FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_DIRECTORY))
{
// Recurse
ComputeFileSize(itemPath);
}
else if (findData->cFileName.ToString() is string fileName &&
else if (findData.cFileName.ToString() is string fileName &&
fileName.Equals(".", StringComparison.OrdinalIgnoreCase) &&
fileName.Equals("..", StringComparison.OrdinalIgnoreCase))
{
Expand All @@ -87,7 +89,7 @@ unsafe void ComputeSizeRecursively(string path, CancellationToken token)
if (token.IsCancellationRequested)
break;
}
while (PInvoke.FindNextFile(hFile, findData));
while (PInvoke.FindNextFile(hFile, &findData));
}

PInvoke.CloseHandle(hFile);
Expand Down

0 comments on commit 9dabb75

Please sign in to comment.