Skip to content

Commit

Permalink
Merge pull request #271 from MarkSummerville/master
Browse files Browse the repository at this point in the history
Fix copy vs move bug #267
  • Loading branch information
SirSparkles authored Jan 8, 2018
2 parents 49e3467 + d195c88 commit ea0fa3b
Showing 1 changed file with 29 additions and 53 deletions.
82 changes: 29 additions & 53 deletions TVRename#/ItemsAndActions/ActionCopyMoveRename.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
using Alphaleonis.Win32.Filesystem;
using System;
using System.CodeDom;
using Alphaleonis.Win32.Filesystem;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Security.AccessControl;
using System.Threading;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace TVRename
{
using Alphaleonis.Win32.Filesystem;
using System;
using System.IO;
using System.Windows.Forms;
using DirectoryInfo = Alphaleonis.Win32.Filesystem.DirectoryInfo;
using File = Alphaleonis.Win32.Filesystem.File;
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
using FileSystemInfo = Alphaleonis.Win32.Filesystem.FileSystemInfo;


public class ActionCopyMoveRename : ActionFileOperation
{
Expand Down Expand Up @@ -76,41 +63,31 @@ public override bool Go(ref bool pause, TVRenameStats stats)

try
{
if (FileHelper.Same(this.From, this.To))
{
// XP won't actually do a rename if its only a case difference
string tempName = TempFor(this.To);

//From.MoveTo(tempName);
//File.Move(tempName, To.FullName);

if (IsMoveRename())
{
// This step could be slow, so report progress
CopyMoveResult moveResult = Alphaleonis.Win32.Filesystem.File.Move(this.From.FullName, tempName, MoveOptions.CopyAllowed | MoveOptions.ReplaceExisting, CopyProgressCallback, null);
if (moveResult.ErrorCode != 0) throw new Exception(moveResult.ErrorMessage);
}
else
{
//we are copying
// This step could be slow, so report progress
CopyMoveResult moveResult = Alphaleonis.Win32.Filesystem.File.Copy(this.From.FullName, tempName, CopyOptions.None, true, CopyProgressCallback, null);
if (moveResult.ErrorCode != 0) throw new Exception(moveResult.ErrorMessage);
}


// This step very quick, so no progress reporting
Alphaleonis.Win32.Filesystem.File.Move(tempName, this.To.FullName, MoveOptions.ReplaceExisting);

//we use a temp name just in case we are interruted or some other problem occurs
string tempName = TempFor(To);

}
else {
//From.MoveTo(To.FullName);
CopyMoveResult moveResult = Alphaleonis.Win32.Filesystem.File.Move(this.From.FullName, this.To.FullName, MoveOptions.CopyAllowed | MoveOptions.ReplaceExisting, CopyProgressCallback, null);
// If both full filenames are the same then we want to move it away and back
//This deals with an issue on some systems (XP?) that case insensitive moves did not occur
if (IsMoveRename() || FileHelper.Same(From, To))
{
// This step could be slow, so report progress
CopyMoveResult moveResult = File.Move(From.FullName, tempName, MoveOptions.CopyAllowed | MoveOptions.ReplaceExisting, CopyProgressCallback, null);
if (moveResult.ErrorCode != 0) throw new Exception(moveResult.ErrorMessage);
}
else
{
//we are copying
Debug.Assert(Operation == Op.Copy);

// This step could be slow, so report progress
CopyMoveResult copyResult = File.Copy(From.FullName, tempName, CopyOptions.None, true, CopyProgressCallback, null);
if (copyResult.ErrorCode != 0) throw new Exception(copyResult.ErrorMessage);
}

// Copying the temp file into the correct name is very quick, so no progress reporting
File.Move(tempName, To.FullName, MoveOptions.ReplaceExisting);

this.Done = true;
Done = true;

switch (Operation)
{
Expand All @@ -128,18 +105,17 @@ public override bool Go(ref bool pause, TVRenameStats stats)
}

}
catch (System.Exception e)
catch (Exception e)
{
this.Done = true;
this.Error = true;
this.ErrorText = e.Message;
Done = true;
Error = true;
ErrorText = e.Message;
}

// set NTFS permissions
try
{
if (security != null)
To.SetAccessControl(security);
if (security != null) To.SetAccessControl(security);
}
catch
{
Expand Down Expand Up @@ -276,7 +252,7 @@ private static void KeepTimestamps(FileSystemInfo from, FileSystemInfo to)
private CopyMoveProgressResult CopyProgressCallback(long TotalFileSize, long TotalBytesTransferred, long StreamSize, long StreamBytesTransferred, int StreamNumber, CopyMoveProgressCallbackReason CallbackReason, Object UserData)
{
double pct = TotalBytesTransferred * 100.0 / TotalFileSize;
this.PercentDone = pct > 100.0 ? 100.0 : pct;
PercentDone = pct > 100.0 ? 100.0 : pct;
return CopyMoveProgressResult.Continue;
}

Expand All @@ -303,4 +279,4 @@ private long SourceFileSize()
}
}
}
}
}

0 comments on commit ea0fa3b

Please sign in to comment.