Skip to content

Commit

Permalink
fix: check if subtitle file is a symbolic link
Browse files Browse the repository at this point in the history
When attempting to move a subtitle file, check if it's a symbolic link and abort if the link target doesn't exist, or create a copy to use if it exists but is pointing to the wrong place.
  • Loading branch information
revam committed Oct 29, 2024
1 parent 52ee4c4 commit 152963a
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Shokofin/Resolvers/VirtualFileSystemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ private LinkGenerationResult CleanupStructure(string vfsPath, string directoryTo
return result;
}

private static bool TryMoveSubtitleFile(IReadOnlyList<string> allKnownPaths, string subtitlePath, bool preview)
private bool TryMoveSubtitleFile(IReadOnlyList<string> allKnownPaths, string subtitlePath, bool preview)
{
if (!TryGetIdsForPath(subtitlePath, out var seriesId, out var fileId))
return false;
Expand All @@ -1161,6 +1161,28 @@ private static bool TryMoveSubtitleFile(IReadOnlyList<string> allKnownPaths, str
if (preview)
return true;

try {
var currentTarget = File.ResolveLinkTarget(subtitlePath, false)?.FullName;
if (!string.IsNullOrEmpty(currentTarget))
{
// Just remove the link if the target doesn't exist.
if (!File.Exists(currentTarget))
return false;

// // This statement will never be true. Because it would never had hit this path if it were true.
// if (currentTarget == realTarget)
// return true;

// Copy the link so we can move it to where it should be.
File.Delete(subtitlePath);
File.Copy(currentTarget, subtitlePath);
}
}
catch (Exception ex) {
Logger.LogWarning(ex, "Unable to check if {Path} is a symbolic link", subtitlePath);
return false;
}

var realSubtitlePath = realTarget[..^Path.GetExtension(realTarget).Length] + extName;
if (!File.Exists(realSubtitlePath))
File.Move(subtitlePath, realSubtitlePath);
Expand Down

0 comments on commit 152963a

Please sign in to comment.