Skip to content

Commit

Permalink
Fix #211 cleaning with non-flat folder stucture reports inaccuratly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Mar 16, 2021
1 parent 7637b29 commit af186f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 5 additions & 1 deletion uSync8.BackOffice/Services/SyncFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,18 @@ public void CleanFolder(string folder)
/// Get a list of files from a folder.
/// </summary>
public IEnumerable<string> GetFiles(string folder, string extensions)
=> GetFiles(folder, extensions, false);

public IEnumerable<string> GetFiles(string folder, string extensions, bool allFolders)
{
var localPath = GetAbsPath(folder);
if (DirectoryExists(localPath))
{
return Directory.GetFiles(localPath, extensions);
return Directory.GetFiles(localPath, extensions, allFolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
}

return Enumerable.Empty<string>();

}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public SyncHandlerBase(
protected override IEnumerable<uSyncAction> DeleteMissingItems(TObject parent, IEnumerable<Guid> keys, bool reportOnly)
{
var items = GetChildItems(parent.Id).ToList();

logger.Debug(handlerType, "DeleteMissingItems: {parentId} Checking {itemCount} items for {keyCount} keys", parent.Id, items.Count, keys.Count());

var actions = new List<uSyncAction>();
foreach (var item in items.Where(x => !keys.Contains(x.Key)))
{
Expand Down
23 changes: 8 additions & 15 deletions uSync8.BackOffice/SyncHandlers/SyncHandlerRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,20 +548,6 @@ virtual public SyncAttempt<TObject> ImportSecondPass(string file, TObject item,
protected virtual IEnumerable<uSyncAction> CleanFolder(string cleanFile, bool reportOnly, bool flat)
{
var folder = Path.GetDirectoryName(cleanFile);
if (!flat)
{
// when the folder isn't flat we don't want the parent folder, but rather the folder of the children
// of this item.
var cleanFileFolder = Path.GetFileNameWithoutExtension(cleanFile);
if (cleanFileFolder.IndexOf('_') >= 0)
{
cleanFileFolder = cleanFileFolder.Substring(0, cleanFileFolder.LastIndexOf('_'));
folder = Path.Combine(folder, cleanFileFolder);
}

logger.Debug(handlerType, "Non Flat Folder {folder}", folder);
}

if (!Directory.Exists(folder)) return Enumerable.Empty<uSyncAction>();


Expand All @@ -581,6 +567,8 @@ protected virtual IEnumerable<uSyncAction> CleanFolder(string cleanFile, bool re
var parent = GetCleanParent(cleanFile);
if (parent == null) return Enumerable.Empty<uSyncAction>();

logger.Debug(handlerType, "Got parent with {alias} from clean file {file}", GetItemAlias(parent), Path.GetFileName(cleanFile));

// keys should aways have at least one entry (the key from cleanFile)
// if it doesn't then something might have gone wrong.
// because we are being defensive when it comes to deletes,
Expand Down Expand Up @@ -609,10 +597,13 @@ private IList<Guid> GetFolderKeys(string folder, bool flat)

var cacheKey = $"{GetCacheKeyBase()}_{folderKey}";

logger.Debug(handlerType, "Getting Folder Keys : {cacheKey}", cacheKey);

return runtimeCache.GetCacheItem(cacheKey, () =>
{
// when it's not flat structure we also get the sub folders. (extra defensive get them all)
var keys = new List<Guid>();
var files = syncFileService.GetFiles(folder, "*.config").ToList();
var files = syncFileService.GetFiles(folder, "*.config", !flat).ToList();

foreach (var file in files)
{
Expand All @@ -624,6 +615,8 @@ private IList<Guid> GetFolderKeys(string folder, bool flat)
}
}

logger.Debug(handlerType, "Loaded {count} keys from {folder} [{cacheKey}]", keys.Count, folder, cacheKey);

return keys;

}, null);
Expand Down

0 comments on commit af186f6

Please sign in to comment.