private void CrawlRemoteFolder(IFolder remoteSubFolder, string remotePath, string localFolder, IList<string> remoteFolders)
{
SleepWhileSuspended();
try
{
if (Utils.WorthSyncing(localFolder, remoteSubFolder.Name, repoInfo))
{
Logger.Info("CrawlRemote localFolder:\"" + localFolder + "\" remoteSubFolder.Path:\"" + remoteSubFolder.Path + "\" remoteSubFolder.Name:\"" + remoteSubFolder.Name + "\"");
remoteFolders.Add(remoteSubFolder.Name);
var subFolderItem = database.GetFolderSyncItemFromRemotePath(remoteSubFolder.Path);
if (null == subFolderItem)
{
subFolderItem = SyncItemFactory.CreateFromRemoteFolder(remoteSubFolder.Path, repoInfo, database);
}
// Check whether local folder exists.
if (Directory.Exists(subFolderItem.LocalPath))
{
try
{
activityListener.ActivityStarted();
// Recurse into folder.
CrawlSync(remoteSubFolder, remotePath, subFolderItem.LocalPath);
}
finally
{
activityListener.ActivityStopped();
}
}
else
{
// Maybe the whole synchronized folder has disappeared?
// While rare for normal filesystems, that happens rather often with mounted folders (for instance encrypted folders)
// In such a case, we should abort this synchronization rather than delete the remote subfolder.
if (!Directory.Exists(repoInfo.TargetDirectory))
{
throw new Exception("Local folder has disappeared: " + repoInfo.TargetDirectory + " , aborting synchronization");
}
// If there was previously a file with this name, delete it.
// TODO warn if local changes in the file.
if (File.Exists(subFolderItem.LocalPath))
{
try
{
activityListener.ActivityStarted();
Utils.DeleteEvenIfReadOnly(subFolderItem.LocalPath);
}
finally
{
activityListener.ActivityStopped();
}
}
if (database.ContainsFolder(subFolderItem))
{
try
{
activityListener.ActivityStarted();
// If there was previously a folder with this name, it means that
// the user has deleted it voluntarily, so delete it from server too.
DeleteRemoteFolder(remoteSubFolder, subFolderItem, remotePath);
}
finally
{
activityListener.ActivityStopped();
}
}
else
{
try
{
// The folder has been recently created on server, so download it.
activityListener.ActivityStarted();
Directory.CreateDirectory(subFolderItem.LocalPath);
// Create database entry for this folder.
// TODO - Yannick - Add metadata
database.AddFolder(subFolderItem, remoteSubFolder.Id, remoteSubFolder.LastModificationDate);
Logger.Info("Added folder to database: " + subFolderItem.LocalPath);
// Recursive copy of the whole folder.
RecursiveFolderCopy(remoteSubFolder, remotePath, subFolderItem.LocalPath);
}
finally
{
activityListener.ActivityStopped();
}
}
}
}
}
catch (Exception e)
{
activityListener.ActivityStopped();
ProcessRecoverableException("Could not crawl sync remote folder: " + remoteSubFolder.Path, e);
}
}