From bf7d884183c45763efc1d8fb00d808db4d918b63 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 29 May 2022 14:46:58 -0500 Subject: [PATCH] fix: Migration logic now handles symlinks (sort of) Symlinks are difficult to deal with. At this point, it was still failing to migrate the `.config/recyclarr/repo` directory. Even though it still doesn't work 100%, I'm going to leave it as it is and instead simplify what gets migrated later. --- src/Common/Extensions/FileSystemExtensions.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Common/Extensions/FileSystemExtensions.cs b/src/Common/Extensions/FileSystemExtensions.cs index ee9a33b8..6b2678d8 100644 --- a/src/Common/Extensions/FileSystemExtensions.cs +++ b/src/Common/Extensions/FileSystemExtensions.cs @@ -18,16 +18,35 @@ public static class FileSystemExtensions foreach (var dir in directories) { + console?.Output.WriteLine($" - Attributes: {dir.Attributes}"); + + // Is it a symbolic link? + if ((dir.Attributes & FileAttributes.ReparsePoint) != 0) + { + var newPath = RelocatePath(dir.FullName, targetDir, destDir); + fs.Directory.CreateDirectory(fs.Path.GetDirectoryName(newPath)); + console?.Output.WriteLine($" - Symlink: {dir.FullName} :: TO :: {newPath}"); + dir.MoveTo(newPath); + continue; + } + + // For real directories, move all the files inside foreach (var file in dir.EnumerateFiles()) { - var newPath = Regex.Replace(file.FullName, $"^{Regex.Escape(targetDir)}", destDir); + var newPath = RelocatePath(file.FullName, targetDir, destDir); fs.Directory.CreateDirectory(fs.Path.GetDirectoryName(newPath)); console?.Output.WriteLine($" - Moving: {file.FullName} :: TO :: {newPath}"); file.MoveTo(newPath); } + // Delete the directory now that it is empty. console?.Output.WriteLine($" - Deleting: {dir.FullName}"); dir.Delete(); } } + + private static string RelocatePath(string path, string oldDir, string newDir) + { + return Regex.Replace(path, $"^{Regex.Escape(oldDir)}", newDir); + } }