Skip to content

Commit

Permalink
Smarter empty directory cleanup
Browse files Browse the repository at this point in the history
We were not detecting non-empty directies (that contained only subdirectories), and we were also not cleaning up parent directories once the descendent ones were empty.
  • Loading branch information
kzu committed Jul 8, 2024
1 parent c48bb1a commit c6d2da2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/File/FileSpec.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace Devlooped
{
[DebuggerDisplay("{Path}")]
public class FileSpec
{
public static FileSpec WithPath(string path, Uri uri)
Expand Down
16 changes: 13 additions & 3 deletions src/File/SyncCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using DotNetConfig;

Expand All @@ -16,12 +17,21 @@ protected override bool OnRemoteUrlMissing(FileSpec spec)

// Clear empty directories
var dir = new FileInfo(spec.Path).DirectoryName;
if (dir != null && !Directory.EnumerateFiles(dir).Any())
Directory.Delete(dir);
DeleteEmptyDirectories(dir);

Configuration.RemoveSection("file", spec.Path);

return true;
}

void DeleteEmptyDirectories(string? dir)
{
if (dir != null && !Directory.EnumerateFiles(dir).Any() && !Directory.EnumerateDirectories(dir).Any())
{
var parent = new DirectoryInfo(dir).Parent?.FullName;
Directory.Delete(dir);
DeleteEmptyDirectories(parent);
}
}
}
}

0 comments on commit c6d2da2

Please sign in to comment.