private static bool ValidateMoveInfo(Microsoft.Expression.Project.MoveInfo moveInfo, Microsoft.Expression.Project.MoveOptions moveOptions) { if (string.IsNullOrEmpty(moveInfo.Source) || string.IsNullOrEmpty(moveInfo.Destination)) { throw new ArgumentException("moveRequests"); } if (!Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(moveInfo.Source)) { return(false); } moveInfo.Source = Microsoft.Expression.Framework.Documents.PathHelper.ResolvePath(moveInfo.Source); moveInfo.Destination = Microsoft.Expression.Framework.Documents.PathHelper.ResolvePath(moveInfo.Destination); if (moveInfo.Source.Equals(moveInfo.Destination, StringComparison.OrdinalIgnoreCase)) { return(false); } if (Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(moveInfo.Destination)) { if ((moveOptions & Microsoft.Expression.Project.MoveOptions.OverwriteDestination) != Microsoft.Expression.Project.MoveOptions.OverwriteDestination) { throw new InvalidOperationException("Destination files exist"); } Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(moveInfo.Destination); } return(true); }
private static bool HasContentAlreadyBeenMoved(IEnumerable <Microsoft.Expression.Project.MoveInfo> movedDirectories, string fileName) { bool flag; DocumentReference documentReference = DocumentReference.Create(Microsoft.Expression.Framework.Documents.PathHelper.GetDirectoryNameOrRoot(fileName)); using (IEnumerator <Microsoft.Expression.Project.MoveInfo> enumerator = movedDirectories.GetEnumerator()) { while (enumerator.MoveNext()) { Microsoft.Expression.Project.MoveInfo current = enumerator.Current; DocumentReference documentReference1 = DocumentReference.Create(Microsoft.Expression.Framework.Documents.PathHelper.EnsurePathEndsInDirectorySeparator(current.Source)); if (!documentReference.Path.StartsWith(documentReference1.Path, StringComparison.OrdinalIgnoreCase)) { continue; } flag = true; return(flag); } return(false); } return(flag); }
private static IList <MoveResult> MoveSafeInternal(IEnumerable <Microsoft.Expression.Project.MoveInfo> moveRequests, Microsoft.Expression.Project.MoveOptions moveOptions, bool isMove, bool shouldThrowIOExceptions) { IList <MoveResult> moveResults; if (moveRequests == null) { throw new ArgumentNullException("moveRequests"); } IList <MoveResult> moveResults1 = new List <MoveResult>(); if (!moveRequests.Any <Microsoft.Expression.Project.MoveInfo>()) { return(moveResults1); } List <Microsoft.Expression.Project.MoveInfo> list = ( from moveInfo in moveRequests where ProjectPathHelper.ValidateMoveInfo(moveInfo, moveOptions) select moveInfo).ToList <Microsoft.Expression.Project.MoveInfo>(); List <Microsoft.Expression.Project.MoveInfo> moveInfos = new List <Microsoft.Expression.Project.MoveInfo>(); List <Microsoft.Expression.Project.MoveInfo> .Enumerator enumerator = list.GetEnumerator(); try { while (enumerator.MoveNext()) { Microsoft.Expression.Project.MoveInfo current = enumerator.Current; try { bool flag = (moveOptions & Microsoft.Expression.Project.MoveOptions.OverwriteDestination) == Microsoft.Expression.Project.MoveOptions.OverwriteDestination; bool flag1 = isMove; if (Microsoft.Expression.Framework.Documents.PathHelper.IsDirectory(current.Source)) { if (ProjectPathHelper.HasContentAlreadyBeenMoved(moveInfos, current.Source)) { MoveResult moveResult = new MoveResult() { Source = current.Source, Destination = current.Destination, MovedSuccessfully = true }; moveResults1.Add(moveResult); } else { using (IEnumerator <MoveResult> enumerator1 = ProjectPathHelper.MoveDirectorySafe(current, flag1, flag).GetEnumerator()) { while (enumerator1.MoveNext()) { moveResults1.Add(enumerator1.Current); } } } moveInfos.Add(current); } else if (!ProjectPathHelper.HasContentAlreadyBeenMoved(moveInfos, current.Source)) { Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(current.Source); string directoryNameOrRoot = Microsoft.Expression.Framework.Documents.PathHelper.GetDirectoryNameOrRoot(current.Destination); if (!Microsoft.Expression.Framework.Documents.PathHelper.DirectoryExists(directoryNameOrRoot)) { Directory.CreateDirectory(directoryNameOrRoot); } if (flag && Microsoft.Expression.Framework.Documents.PathHelper.FileExists(current.Destination)) { Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(current.Destination); } File.Copy(current.Source, current.Destination, flag); MoveResult moveResult1 = new MoveResult() { Source = current.Source, Destination = current.Destination, MovedSuccessfully = true }; moveResults1.Add(moveResult1); if (flag1) { File.Delete(current.Source); } } } catch (Exception exception) { if (!ProjectPathHelper.IsIOException(exception) || shouldThrowIOExceptions) { throw; } else { moveResults = null; return(moveResults); } } } return(moveResults1); } finally { ((IDisposable)enumerator).Dispose(); } return(moveResults); }
private static IList <MoveResult> MoveDirectorySafe(Microsoft.Expression.Project.MoveInfo moveRequest, bool deleteSource, bool shouldOverwrite) { if (!Microsoft.Expression.Framework.Documents.PathHelper.IsDirectory(moveRequest.Source) || !Microsoft.Expression.Framework.Documents.PathHelper.IsDirectory(moveRequest.Destination)) { throw new ArgumentException("Request must be a directory", "moveRequest"); } Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(moveRequest.Source); string source = moveRequest.Source; string destination = moveRequest.Destination; IList <MoveResult> moveResults = new List <MoveResult>(); int length = source.Length; string[] files = Directory.GetFiles(source, "*", SearchOption.AllDirectories); for (int i = 0; i < (int)files.Length; i++) { string str = files[i]; Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(str); string str1 = Microsoft.Expression.Framework.Documents.PathHelper.ResolveCombinedPath(destination, str.Substring(length)); string directoryName = Path.GetDirectoryName(str1); if (!Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(directoryName)) { Directory.CreateDirectory(directoryName); } bool flag = Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(str1); if (!flag || shouldOverwrite) { if (flag) { Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(str1); File.Delete(str1); } File.Move(str, str1); MoveResult moveResult = new MoveResult() { Source = str, Destination = str1, MovedSuccessfully = true }; moveResults.Add(moveResult); } } IEnumerable <string> strs = Directory.EnumerateDirectories(source, "*", SearchOption.AllDirectories).AppendItem <string>(source); foreach (string str2 in strs) { string str3 = Microsoft.Expression.Framework.Documents.PathHelper.ResolveCombinedPath(destination, str2.Substring(length)); if (Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(str3)) { continue; } Directory.CreateDirectory(str3); } try { if (deleteSource) { Directory.Delete(source, true); } } catch (Exception exception1) { Exception exception = exception1; if ((exception is IOException || exception is UnauthorizedAccessException) && (new DirectoryInfo(source)).Exists && ((int)Directory.GetFiles(source, "*", SearchOption.AllDirectories).Length != 0 || (int)Directory.GetDirectories(source, "*", SearchOption.AllDirectories).Length != 0)) { throw; } } MoveResult moveResult1 = new MoveResult() { Destination = moveRequest.Destination, Source = moveRequest.Source, MovedSuccessfully = true }; moveResults.Add(moveResult1); return(moveResults); }