Skip to content

Commit

Permalink
Limit recursion when searching for conflict files
Browse files Browse the repository at this point in the history
Fixes #195
  • Loading branch information
canton7 committed Jan 13, 2016
1 parent 7f77d58 commit 5e9f275
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/SyncTrayzor/Services/Conflicts/ConflictFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class ConflictFileManager : IConflictFileManager
private static readonly Regex conflictRegex =
new Regex(@"^(?<prefix>.*).sync-conflict-(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})-(?<hours>\d{2})(?<mins>\d{2})(?<secs>\d{2})(?<suffix>.*)(?<extension>\..*)$");
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private const int maxSearchDepth = 255; // Loosely based on the max path length (a bit over)

private readonly IFilesystemProvider filesystemProvider;

Expand Down Expand Up @@ -128,14 +129,15 @@ private void FindConflictsImpl(string basePath, SlimObservable<ConflictSet> subj
logger.Debug("Looking for conflicts in {0}", basePath);

var conflictLookup = new Dictionary<string, List<ParsedConflictFileInfo>>();
var stack = new Stack<string>();
stack.Push(basePath);
var stack = new Stack<SearchDirectory>();
stack.Push(new SearchDirectory(basePath, 0));
while (stack.Count > 0)
{
cancellationToken.ThrowIfCancellationRequested();

conflictLookup.Clear();
var directory = stack.Pop();
var searchDirectory = stack.Pop();
var directory = searchDirectory.Directory;

foreach (var fileName in this.TryGetFiles(directory, conflictPattern, System.IO.SearchOption.TopDirectoryOnly))
{
Expand Down Expand Up @@ -164,14 +166,21 @@ private void FindConflictsImpl(string basePath, SlimObservable<ConflictSet> subj
subject.Next(new ConflictSet(file, conflicts));
}

foreach (var subDirectory in this.TryGetDirectories(directory, "*", System.IO.SearchOption.TopDirectoryOnly))
if (searchDirectory.Depth < maxSearchDepth)
{
if (subDirectory == stVersionsFolder)
continue;
foreach (var subDirectory in this.TryGetDirectories(directory, "*", System.IO.SearchOption.TopDirectoryOnly))
{
if (subDirectory == stVersionsFolder)
continue;

stack.Push(Path.Combine(directory, subDirectory));
stack.Push(new SearchDirectory(Path.Combine(directory, subDirectory), searchDirectory.Depth + 1));

cancellationToken.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
}
}
else
{
logger.Warn($"Max search depth of {maxSearchDepth} exceeded with path {directory}. Not proceeding further.");
}
}
}
Expand Down Expand Up @@ -311,5 +320,17 @@ public void ResolveConflict(ConflictSet conflictSet, string chosenFilePath)
this.filesystemProvider.MoveFile(chosenFilePath, conflictSet.File.FilePath);
}
}

private struct SearchDirectory
{
public readonly string Directory;
public readonly int Depth;

public SearchDirectory(string directory, int depth)
{
this.Directory = directory;
this.Depth = depth;
}
}
}
}

0 comments on commit 5e9f275

Please sign in to comment.