Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow partial enumeration of directory entries #6083

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions osu.Framework/Graphics/UserInterface/DirectorySelector.cs
Expand Up @@ -11,6 +11,7 @@
using osu.Framework.Bindables;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osuTK;

Expand Down Expand Up @@ -201,16 +202,27 @@ private void updateDisplay()
protected virtual bool TryGetEntriesForPath(DirectoryInfo path, out ICollection<DirectorySelectorItem> items)
{
items = new List<DirectorySelectorItem>();
bool gotAllEntries = true;

try
{
foreach (var dir in path.GetDirectories().OrderBy(d => d.Name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into this, I found an EnumerateDirectories(String, EnumerationOptions) overload that can take in EnumerationOptions, which has the option IgnoreInaccessible -- exactly what you're looking for (there's also AttributesToSkip that could be used for hidden items).

The same should also apply to FileSelector.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately EnumerateDirectories, even with IgnoreInaccessible, throws an exception. I've tried many of the other overloads that allow ignoring certain failures with no success; enumerating using a string of the path really was a last-resort.

2023-12-27_04-41-41.mp4

foreach (string directoryName in Directory.GetDirectories(path.FullName).OrderBy(d => d))
{
if (ShowHiddenItems.Value || !dir.Attributes.HasFlagFast(FileAttributes.Hidden))
items.Add(CreateDirectoryItem(dir));
try
{
DirectoryInfo di = new DirectoryInfo(directoryName);
if (ShowHiddenItems.Value || !di.Attributes.HasFlagFast(FileAttributes.Hidden))
items.Add(CreateDirectoryItem(di));
}
catch
{
// Don't fail enumeration if we fail getting attributes for a single entry
Logger.Log($"Directory {directoryName} is inaccessible", LoggingTarget.Information, LogLevel.Debug);
gotAllEntries = false;
}
}

return true;
return items.Count > 0 || gotAllEntries;
}
catch
{
Expand Down
30 changes: 21 additions & 9 deletions osu.Framework/Graphics/UserInterface/FileSelector.cs
Expand Up @@ -11,6 +11,7 @@
using osu.Framework.Bindables;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Input.Events;
using osu.Framework.Logging;

namespace osu.Framework.Graphics.UserInterface
{
Expand All @@ -33,27 +34,38 @@ protected FileSelector(string initialPath = null, string[] validFileExtensions =

protected override bool TryGetEntriesForPath(DirectoryInfo path, out ICollection<DirectorySelectorItem> items)
{
bool gotAllEntries = true;
items = new List<DirectorySelectorItem>();

if (!base.TryGetEntriesForPath(path, out var directories))
return false;
gotAllEntries = false;

items = directories;

try
{
IEnumerable<FileInfo> files = path.GetFiles();

if (validFileExtensions.Length > 0)
files = files.Where(f => validFileExtensions.Contains(f.Extension));
IEnumerable<string> filenames = Directory.GetFiles(path.FullName).OrderBy(f => f);

foreach (var file in files.OrderBy(d => d.Name))
foreach (string filename in filenames)
{
if (ShowHiddenItems.Value || !file.Attributes.HasFlagFast(FileAttributes.Hidden))
items.Add(CreateFileItem(file));
try
{
FileInfo file = new FileInfo(filename);

if (validFileExtensions.Length > 0 && !validFileExtensions.Contains(file.Extension))
continue;

if (ShowHiddenItems.Value || !file.Attributes.HasFlagFast(FileAttributes.Hidden))
items.Add(CreateFileItem(file));
}
catch
{
Logger.Log($"File {filename} is inaccessible", LoggingTarget.Information, LogLevel.Debug);
gotAllEntries = false;
}
}

return true;
return items.Count > 0 || gotAllEntries;
}
catch
{
Expand Down