private void TransferItem(USNJournalSyncLog syncLog, SyncMountpoint syncFrom) { try { syncLog.ActionStartDate = DateTime.Now; syncLog.ActionFinishDate = null; Singleton.Instance.Repository.Update(syncLog); bool successfull = false; if (syncLog.Action.GetType() == typeof (DeleteAction)) { var path = Path.Get(syncFrom.Path, syncLog.Action.RelativePath); logger.Info($"[{syncLog.Id}] [D] " + path); if (path.Exists) { if (syncLog.Action.IsDirectory) { path.Delete(true); } else { path.Delete(true); } } successfull = true; } else if (syncLog.Action.GetType() == typeof (RenameAction)) { var renameAction = syncLog.Action as RenameAction; logger.Info($"[{syncLog.Id}] [R] {renameAction.RenameFrom} to {renameAction.RelativePath}"); if (String.IsNullOrWhiteSpace(renameAction.RenameFrom)) { CopyFile(syncLog, syncFrom); } Path.Get(syncFrom.Path, renameAction.RenameFrom).Move(Path.Get(syncFrom.Path, renameAction.RelativePath).FullPath); successfull = true; } else { successfull = CopyFile(syncLog, syncFrom); } syncLog.ActionFinishDate = DateTime.Now; syncLog.Successfull = successfull; Singleton.Instance.Repository.Update(syncLog); foreach (var error in Singleton.Instance.Repository.Many<Error>(f=>f.SyncLog.Id == syncLog.Id)) { Singleton.Instance.Repository.Delete(error); } } catch (FileNotFoundException ex) { syncLog.RequiresManualIntervention = true; syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(ex, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = ex; error.ItemId = syncLog.Id; error.Message = ex.Message; Singleton.Instance.Repository.Add(error); } catch (Exception e) { syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(e, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = e; error.ItemId = syncLog.Id; error.Message = e.Message; Singleton.Instance.Repository.Add(error); } }
private static string GetRelativePath(string path, SyncMountpoint syncFrom) { var relativePath = Path.Get(path).MakeRelativeTo(syncFrom.Mountpoint.Reference.MountPoint.TrimEnd('\\')); var finalPath = Path.Get(syncFrom.Path, relativePath.ToString()); return finalPath.FullPath; }
private static bool CopyFile(USNJournalSyncLog syncLog, SyncMountpoint syncFrom) { logger.Info($"[{syncLog.Id}] [C] {syncLog.Action.RelativePath}"); var copyAction = syncLog.Action as UpdateAction; var publicPath = syncFrom.Mountpoint.Reference.PublicPath; var relativePath = copyAction.RelativePath; if (publicPath == null) { throw new NullReferenceException("publicPath"); } if (relativePath == null) { throw new NullReferenceException("relativePath"); } Path.Get(publicPath, relativePath).Copy(Path.Get(syncFrom.Path, relativePath), Overwrite.Always, true); return true; }