Browse Source

Add exists check to IsSymbolic link

Fix issue where IsSymbolicLink can be true when the directory doesn't exist
pull/165/head
Ionite 1 year ago
parent
commit
388f36dcbd
No known key found for this signature in database
  1. 45
      StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs

45
StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs

@ -9,6 +9,7 @@ namespace StabilityMatrix.Core.Models.FileInterfaces;
public class DirectoryPath : FileSystemPath, IPathObject
{
private DirectoryInfo? info;
// ReSharper disable once MemberCanBePrivate.Global
[JsonIgnore]
public DirectoryInfo Info => info ??= new DirectoryInfo(FullPath);
@ -19,7 +20,7 @@ public class DirectoryPath : FileSystemPath, IPathObject
get
{
Info.Refresh();
return Info.Attributes.HasFlag(FileAttributes.ReparsePoint);
return Info.Exists && Info.Attributes.HasFlag(FileAttributes.ReparsePoint);
}
}
@ -37,33 +38,29 @@ public class DirectoryPath : FileSystemPath, IPathObject
/// Get the parent directory.
/// </summary>
[JsonIgnore]
public DirectoryPath? Parent => Info.Parent == null
? null : new DirectoryPath(Info.Parent);
public DirectoryPath? Parent => Info.Parent == null ? null : new DirectoryPath(Info.Parent);
public DirectoryPath(string path) : base(path)
{
}
public DirectoryPath(string path)
: base(path) { }
public DirectoryPath(FileSystemPath path) : base(path)
{
}
public DirectoryPath(FileSystemPath path)
: base(path) { }
public DirectoryPath(DirectoryInfo info) : base(info.FullName)
public DirectoryPath(DirectoryInfo info)
: base(info.FullName)
{
// Additionally set the info field
this.info = info;
}
public DirectoryPath(params string[] paths) : base(paths)
{
}
public DirectoryPath(params string[] paths)
: base(paths) { }
/// <inheritdoc />
public long GetSize()
{
Info.Refresh();
return Info.EnumerateFiles("*", SearchOption.AllDirectories)
.Sum(file => file.Length);
return Info.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length);
}
/// <summary>
@ -74,7 +71,8 @@ public class DirectoryPath : FileSystemPath, IPathObject
/// </param>
public long GetSize(bool includeSymbolicLinks)
{
if (includeSymbolicLinks) return GetSize();
if (includeSymbolicLinks)
return GetSize();
Info.Refresh();
var files = Info.GetFiles()
@ -82,7 +80,9 @@ public class DirectoryPath : FileSystemPath, IPathObject
.Sum(file => file.Length);
var subDirs = Info.GetDirectories()
.Where(dir => !dir.Attributes.HasFlag(FileAttributes.ReparsePoint))
.Sum(dir => dir.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length));
.Sum(
dir => dir.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length)
);
return files + subDirs;
}
@ -136,22 +136,27 @@ public class DirectoryPath : FileSystemPath, IPathObject
public override string ToString() => FullPath;
// DirectoryPath + DirectoryPath = DirectoryPath
public static DirectoryPath operator +(DirectoryPath path, DirectoryPath other) => new(Path.Combine(path, other.FullPath));
public static DirectoryPath operator +(DirectoryPath path, DirectoryPath other) =>
new(Path.Combine(path, other.FullPath));
// DirectoryPath + FilePath = FilePath
public static FilePath operator +(DirectoryPath path, FilePath other) => new(Path.Combine(path, other.FullPath));
public static FilePath operator +(DirectoryPath path, FilePath other) =>
new(Path.Combine(path, other.FullPath));
// DirectoryPath + FileInfo = FilePath
public static FilePath operator +(DirectoryPath path, FileInfo other) => new(Path.Combine(path, other.FullName));
public static FilePath operator +(DirectoryPath path, FileInfo other) =>
new(Path.Combine(path, other.FullName));
// DirectoryPath + string = string
public static string operator +(DirectoryPath path, string other) => Path.Combine(path, other);
// Implicit conversions to and from string
public static implicit operator string(DirectoryPath path) => path.FullPath;
public static implicit operator DirectoryPath(string path) => new(path);
// Implicit conversions to and from DirectoryInfo
public static implicit operator DirectoryInfo(DirectoryPath path) => path.Info;
public static implicit operator DirectoryPath(DirectoryInfo path) => new(path);
}

Loading…
Cancel
Save