Skip to content

Commit a5b8cd4

Browse files
committed
Fixed permission issue.
1 parent d4059b6 commit a5b8cd4

File tree

9 files changed

+241
-149
lines changed

9 files changed

+241
-149
lines changed

Hexa.NET.ImGui.Widgets/Dialogs/FileSystemHelper.cs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
namespace Hexa.NET.ImGui.Widgets.Dialogs
22
{
33
using Hexa.NET.ImGui.Widgets.Extensions;
4+
using Microsoft.CodeAnalysis;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Linq;
89
using System.Runtime.InteropServices;
910
using System.Text.Json.Serialization;
11+
using System.Xml.Linq;
1012

1113
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
1214
[JsonSerializable(typeof(Dictionary<string, string>))]
@@ -212,7 +214,7 @@ public static IEnumerable<FileSystemItem> Refresh(string folder, RefreshFlags re
212214

213215
var itemName = option == SearchOption.AllDirectories ? $"{name}##{id++}" : name.ToString();
214216
var decorator = isDir ? $"{folderDecorator}" : fileDecorator(metadata);
215-
FileSystemItem item = new(metadata, decorator, itemName, isDir ? FileSystemItemFlags.Folder : FileSystemItemFlags.None);
217+
FileSystemItem item = new(metadata, itemName, decorator, isDir ? FileSystemItemFlags.Folder : FileSystemItemFlags.None);
216218
yield return item;
217219
}
218220
}
@@ -238,41 +240,52 @@ public static List<FileSystemItem> GetFileSystemEntries(string folder, RefreshFl
238240
throw new ArgumentNullException(nameof(allowedExtensions));
239241
}
240242

241-
foreach (var fse in Directory.GetFileSystemEntries(folder, string.Empty))
243+
try
242244
{
243-
var flags = File.GetAttributes(fse);
244-
if ((flags & FileAttributes.System) != 0)
245-
continue;
246-
if ((flags & FileAttributes.Hidden) != 0)
247-
continue;
248-
if ((flags & FileAttributes.Device) != 0)
249-
continue;
250-
251-
if ((flags & FileAttributes.Directory) != 0)
245+
foreach (var metadata in FileUtilities.EnumerateEntries(folder, string.Empty, SearchOption.TopDirectoryOnly))
252246
{
253-
if (folders)
254-
{
255-
items.Add(new(fse, $"{MaterialIcons.Folder}", FileSystemItemFlags.Folder));
256-
}
247+
var flags = metadata.Attributes;
248+
if ((flags & FileAttributes.System) != 0)
249+
continue;
250+
if ((flags & FileAttributes.Hidden) != 0)
251+
continue;
252+
if ((flags & FileAttributes.Device) != 0)
253+
continue;
257254

258-
continue;
259-
}
260-
else if (files)
261-
{
262-
if (onlyAllowFilteredExtensions)
255+
var span = metadata.Path.AsSpan();
256+
var name = Path.GetFileName(span);
257+
258+
var itemName = name.ToString();
259+
260+
if ((flags & FileAttributes.Directory) != 0)
263261
{
264-
var ext = Path.GetExtension(fse.AsSpan());
265-
if (allowedExtensions!.Contains(ext, StringComparison.OrdinalIgnoreCase))
262+
if (folders)
266263
{
267-
items.Add(new FileSystemItem(fse, string.Empty, FileSystemItemFlags.None));
264+
items.Add(new(metadata, itemName, $"{MaterialIcons.Folder}", FileSystemItemFlags.Folder));
268265
}
266+
267+
continue;
269268
}
270-
else
269+
else if (files)
271270
{
272-
items.Add(new FileSystemItem(fse, string.Empty, FileSystemItemFlags.None));
271+
if (onlyAllowFilteredExtensions)
272+
{
273+
var ext = Path.GetExtension(span);
274+
if (allowedExtensions!.Contains(ext, StringComparison.OrdinalIgnoreCase))
275+
{
276+
items.Add(new FileSystemItem(metadata, itemName, string.Empty, FileSystemItemFlags.None));
277+
}
278+
}
279+
else
280+
{
281+
items.Add(new FileSystemItem(metadata, itemName, string.Empty, FileSystemItemFlags.None));
282+
}
273283
}
274284
}
275285
}
286+
catch
287+
{
288+
}
276289

277290
cache.Add(folder, items);
278291
return items;

Hexa.NET.ImGui.Widgets/Dialogs/FileSystemItem.cs

Lines changed: 41 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,61 @@ public struct FileSystemItem : IEquatable<FileSystemItem>, IFileSystemItem
1919
private long size;
2020
private CommonFilePermissions permissions;
2121

22-
public FileSystemItem(string path, string icon, string name, string type, FileSystemItemFlags flags)
22+
public FileSystemItem(string path, string icon, string name, FileSystemItemFlags flags)
2323
{
2424
this.path = path;
2525
this.icon = icon;
2626
this.name = name;
2727
this.flags = flags;
28-
this.type = type;
28+
dateModified = path.TryReturn(File.GetLastWriteTime);
29+
30+
if (IsFile)
31+
{
32+
size = path.TryReturn(FileUtilities.GetFileSize);
33+
type = DetermineFileType(System.IO.Path.GetExtension(path.AsSpan()));
34+
}
35+
else
36+
{
37+
type = "File Folder";
38+
}
39+
}
2940

30-
this.dateModified = File.GetLastWriteTime(path);
41+
public FileSystemItem(FileMetadata metadata, string name, string icon, FileSystemItemFlags flags)
42+
{
43+
path = metadata.Path.ToString();
44+
this.icon = icon;
45+
this.name = name;
46+
this.flags = flags;
47+
dateModified = metadata.LastWriteTime;
3148

3249
if (IsFile)
3350
{
34-
size = new FileInfo(path).Length;
51+
size = metadata.Size;
52+
type = DetermineFileType(System.IO.Path.GetExtension(path.AsSpan()));
3553
}
36-
/*
37-
if (!OperatingSystem.IsWindows())
54+
else
3855
{
39-
var access = File.GetUnixFileMode(path);
40-
if ((access & UnixFileMode.UserExecute) != 0 || (access & UnixFileMode.GroupExecute) != 0 || (access & UnixFileMode.OtherExecute) != 0)
41-
{
42-
}
56+
type = "File Folder";
57+
}
58+
}
59+
60+
public FileSystemItem(string path, string icon, FileSystemItemFlags flags)
61+
{
62+
this.path = path;
63+
this.icon = icon;
64+
name = System.IO.Path.GetFileName(path);
65+
this.flags = flags;
66+
67+
dateModified = path.TryReturn(File.GetLastWriteTime);
68+
if (IsFile)
69+
{
70+
size = path.TryReturn(FileUtilities.GetFileSize);
71+
type = DetermineFileType(System.IO.Path.GetExtension(path.AsSpan()));
4372
}
4473
else
4574
{
46-
FileSecurity security = new(path, AccessControlSections.All);
47-
}*/
75+
type = "File Folder";
76+
}
4877
}
4978

5079
private static CommonFilePermissions ConvertUnixPermissions(UnixFileMode permissions)
@@ -124,65 +153,6 @@ private static CommonFilePermissions ConvertWindowsPermissions(FileSecurity secu
124153
return result;
125154
}
126155

127-
public FileSystemItem(string path, string icon, string name, FileSystemItemFlags flags)
128-
{
129-
this.path = path;
130-
this.icon = icon;
131-
this.name = name;
132-
this.flags = flags;
133-
this.dateModified = File.GetLastWriteTime(path);
134-
135-
if (IsFile)
136-
{
137-
this.size = FileUtilities.GetFileSize(path);
138-
this.type = DetermineFileType(System.IO.Path.GetExtension(path.AsSpan()));
139-
}
140-
else
141-
{
142-
this.type = "File Folder";
143-
}
144-
}
145-
146-
public FileSystemItem(FileMetadata metadata, string icon, string name, FileSystemItemFlags flags)
147-
{
148-
this.path = metadata.Path.ToString();
149-
this.icon = icon;
150-
this.name = name;
151-
this.flags = flags;
152-
this.dateModified = metadata.LastWriteTime;
153-
154-
if (IsFile)
155-
{
156-
this.size = metadata.Size;
157-
this.type = DetermineFileType(System.IO.Path.GetExtension(path.AsSpan()));
158-
}
159-
else
160-
{
161-
this.type = "File Folder";
162-
}
163-
}
164-
165-
public FileSystemItem(string path, string icon, FileSystemItemFlags flags)
166-
{
167-
this.path = path;
168-
this.icon = icon;
169-
this.name = System.IO.Path.GetFileName(path);
170-
this.flags = flags;
171-
172-
var mode = File.GetAttributes(path);
173-
174-
dateModified = File.GetLastWriteTime(path);
175-
if (IsFile)
176-
{
177-
size = FileUtilities.GetFileSize(path);
178-
type = DetermineFileType(System.IO.Path.GetExtension(path.AsSpan()));
179-
}
180-
else
181-
{
182-
type = "File Folder";
183-
}
184-
}
185-
186156
public readonly bool IsFile => (flags & FileSystemItemFlags.Folder) == 0;
187157

188158
public readonly bool IsFolder => (flags & FileSystemItemFlags.Folder) != 0;

0 commit comments

Comments
 (0)