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

Fix deletion of unmanaged files #3865

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Core/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@

return dllChanged || dlcChanged;
}
return false;

Check warning on line 396 in Core/GameInstance.cs

View workflow job for this annotation

GitHub Actions / build_NetCore (Debug_NetCore)

Unreachable code detected

Check warning on line 396 in Core/GameInstance.cs

View workflow job for this annotation

GitHub Actions / build_NetCore (Debug_NetCore)

Unreachable code detected

Check warning on line 396 in Core/GameInstance.cs

View workflow job for this annotation

GitHub Actions / build_NetCore (Release_NetCore)

Unreachable code detected

Check warning on line 396 in Core/GameInstance.cs

View workflow job for this annotation

GitHub Actions / build_NetCore (Release_NetCore)

Unreachable code detected
}

#endregion
Expand Down Expand Up @@ -466,8 +466,9 @@
/// <returns>true if any descendants of given path were installed by CKAN, false otherwise</returns>
public bool HasManagedFiles(Registry registry, string absPath)
=> registry.FileOwner(ToRelativeGameDir(absPath)) != null
|| Directory.EnumerateFileSystemEntries(absPath, "*", SearchOption.AllDirectories)
.Any(f => registry.FileOwner(ToRelativeGameDir(f)) != null);
|| (Directory.Exists(absPath)
&& Directory.EnumerateFileSystemEntries(absPath, "*", SearchOption.AllDirectories)
.Any(f => registry.FileOwner(ToRelativeGameDir(f)) != null));

public override string ToString()
=> string.Format(Properties.Resources.GameInstanceToString, game.ShortName, gameDir);
Expand Down
25 changes: 19 additions & 6 deletions GUI/Controls/UnmanagedFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.IO;
using System.Threading.Tasks;

using log4net;

using CKAN.Games;

namespace CKAN.GUI
Expand All @@ -19,9 +21,10 @@ public UnmanagedFiles()
GameFolderTree.TreeViewNodeSorter = new DirsFirstSorter();
}

public void LoadFiles(GameInstance inst)
public void LoadFiles(GameInstance inst, IUser user)
{
this.inst = inst;
this.user = user;
this.registry = RegistryManager.Instance(inst).registry;
Util.Invoke(this, _UpdateGameFolderTree);
}
Expand Down Expand Up @@ -151,6 +154,7 @@ private void DeleteButton_Click(object sender, EventArgs e)
{
var relPath = GameFolderTree.SelectedNode?.Name;
var absPath = inst.ToAbsoluteGameDir(relPath);
log.DebugFormat("Trying to delete {0}", absPath);
if (inst.HasManagedFiles(registry, absPath))
{
Main.Instance.ErrorDialog(Properties.Resources.FolderContainsManagedFiles, relPath);
Expand All @@ -161,15 +165,22 @@ private void DeleteButton_Click(object sender, EventArgs e)
Properties.Resources.DeleteUnmanagedFileDelete,
Properties.Resources.DeleteUnmanagedFileCancel))
{
if (File.Exists(absPath))
try
{
File.Delete(absPath);
if (File.Exists(absPath))
{
File.Delete(absPath);
}
else if (Directory.Exists(absPath))
{
Directory.Delete(absPath, true);
}
GameFolderTree.Nodes.Remove(GameFolderTree.SelectedNode);
}
else if (Directory.Exists(absPath))
catch (Exception exc)
{
Directory.Delete(absPath, true);
user.RaiseError(exc.Message);
}
GameFolderTree.Nodes.Remove(GameFolderTree.SelectedNode);
}
}

Expand Down Expand Up @@ -213,7 +224,9 @@ private void OpenFileBrowser(TreeNode node)
}

private GameInstance inst;
private IUser user;
private Registry registry;
private static readonly ILog log = LogManager.GetLogger(typeof(UnmanagedFiles));
}

internal class DirsFirstSorter : IComparer, IComparer<TreeNode>
Expand Down
2 changes: 1 addition & 1 deletion GUI/Main/MainUnmanaged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Main
{
private void viewUnmanagedFilesStripMenuItem_Click(object sender, EventArgs e)
{
UnmanagedFiles.LoadFiles(manager.CurrentInstance);
UnmanagedFiles.LoadFiles(manager.CurrentInstance, currentUser);
tabController.ShowTab("UnmanagedFilesTabPage", 2);
DisableMainWindow();
}
Expand Down
Loading