Skip to content

Commit

Permalink
Added checks to re-enable --use-move-for-put.
Browse files Browse the repository at this point in the history
This fixes duplicati#4745
  • Loading branch information
kenkendk committed Jun 14, 2022
1 parent 1128de1 commit b4eeb45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Duplicati/Library/Backend/File/FileBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ public class File : IBackend, IStreamingBackend, IQuotaEnabledBackend, IRenameEn
private const string OPTION_ALTERNATE_PATHS = "alternate-target-paths";
private const string OPTION_MOVE_FILE = "use-move-for-put";
private const string OPTION_FORCE_REAUTH = "force-smb-authentication";
private const string OPTION_DISABLE_LENGTH_VERIFICATION = "disable-length-verification";

private readonly string m_path;
private string m_username;
private string m_password;
private readonly bool m_moveFile;
private bool m_hasAutenticated;
private readonly bool m_forceReauth;
private readonly bool m_verifyDestinationLength;

private readonly byte[] m_copybuffer = new byte[Utility.Utility.DEFAULT_BUFFER_SIZE];

Expand Down Expand Up @@ -127,6 +129,7 @@ public File(string url, Dictionary<string, string> options)

m_moveFile = Utility.Utility.ParseBoolOption(options, OPTION_MOVE_FILE);
m_forceReauth = Utility.Utility.ParseBoolOption(options, OPTION_FORCE_REAUTH);
m_verifyDestinationLength = Utility.Utility.ParseBoolOption(options, OPTION_DISABLE_LENGTH_VERIFICATION);
m_hasAutenticated = false;
}

Expand Down Expand Up @@ -222,15 +225,20 @@ public Task PutAsync(string targetFilename, string sourceFilePath, CancellationT
if (systemIO.FileExists(targetFilePath))
systemIO.FileDelete(targetFilePath);

var sourceFileInfo = new FileInfo(sourceFilePath);
var sourceFileLength = sourceFileInfo.Exists ? (long?)sourceFileInfo.Length : null;

systemIO.FileMove(sourceFilePath, targetFilePath);
if (m_verifyDestinationLength)
VerifyMatchingSize(targetFilePath, null, sourceFileLength);
}
else
{
systemIO.FileCopy(sourceFilePath, targetFilePath, true);
systemIO.FileCopy(sourceFilePath, targetFilePath, true);
if (m_verifyDestinationLength)
VerifyMatchingSize(targetFilePath, sourceFilePath);
}

VerifyMatchingSize(targetFilePath, sourceFilePath);

return Task.FromResult(true);
}

Expand All @@ -255,6 +263,8 @@ public IList<ICommandLineArgument> SupportedCommands
new CommandLineArgument(OPTION_ALTERNATE_PATHS, CommandLineArgument.ArgumentType.Path, Strings.FileBackend.AlternateTargetPathsShort, Strings.FileBackend.AlternateTargetPathsLong(OPTION_DESTINATION_MARKER, System.IO.Path.PathSeparator)),
new CommandLineArgument(OPTION_MOVE_FILE, CommandLineArgument.ArgumentType.Boolean, Strings.FileBackend.UseMoveForPutShort, Strings.FileBackend.UseMoveForPutLong),
new CommandLineArgument(OPTION_FORCE_REAUTH, CommandLineArgument.ArgumentType.Boolean, Strings.FileBackend.ForceReauthShort, Strings.FileBackend.ForceReauthLong),
new CommandLineArgument(OPTION_DISABLE_LENGTH_VERIFICATION, CommandLineArgument.ArgumentType.Boolean, Strings.FileBackend.DisableLengthVerificationShort, Strings.FileBackend.DisableLengthVerificationShort),

});

}
Expand Down Expand Up @@ -426,7 +436,8 @@ private static void VerifyMatchingSize(string targetFilePath, Stream sourceStrea
if (!targetFileInfo.Exists)
throw new FileMissingException($"Target file does not exist. Target: {targetFilePath}");

long? sourceStreamLength = Utility.Utility.GetStreamLength(sourceStream, out bool isStreamPostion);
bool isStreamPostion = false;
long? sourceStreamLength = sourceStream == null ? null : Utility.Utility.GetStreamLength(sourceStream, out isStreamPostion);

if (sourceStreamLength.HasValue && targetFileInfo.Length != sourceStreamLength.Value)
throw new FileMissingException($"Target file size ({targetFileInfo.Length:n0}) is different from the source length ({sourceStreamLength.Value:n0}){(isStreamPostion ? " - ending stream position)" : "")}. Target: {targetFilePath}");
Expand Down
2 changes: 2 additions & 0 deletions Duplicati/Library/Backend/File/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ internal static class FileBackend {
public static string UseMoveForPutShort { get { return LC.L(@"Move the file instead of copying it"); } }
public static string ForceReauthShort { get { return LC.L(@"Force authentication against remote share"); } }
public static string ForceReauthLong { get { return LC.L(@"If this option is set, any existing authentication against the remote share is dropped before attempting to authenticate"); } }
public static string DisableLengthVerificationShort { get { return LC.L(@"Disable length verification"); } }
public static string DisableLengthVerificationLong { get { return LC.L(@"As an extra precaution the uploaded file length will be checked against the local source length."); } }
}
}

0 comments on commit b4eeb45

Please sign in to comment.