Skip to content

Commit

Permalink
(chocolateyGH-1047) Add "replace" filesystem operation
Browse files Browse the repository at this point in the history
Allows for the more correct replacement and backup of files on the filesystem.
  • Loading branch information
RichiCoder1 committed Dec 20, 2016
1 parent 5b68b01 commit d9d354d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,25 @@ public bool copy_file_unsafe(string sourceFilePath, string destinationFilePath,
return success != 0;
}


public void replace_file(string sourceFilePath, string destinationFilePath, string backupFilePath)
{
this.Log().Debug(ChocolateyLoggers.Verbose, () => "Attempting to replace \"{0}\"{1} with \"{2}\". Backup placed at \"{3}\".".format_with(destinationFilePath, Environment.NewLine, sourceFilePath, backupFilePath));

allow_retries(
() =>
{
try
{
File.Replace(sourceFilePath, destinationFilePath, backupFilePath);
}
catch (IOException)
{
Alphaleonis.Win32.Filesystem.File.Replace(sourceFilePath, destinationFilePath, backupFilePath);
}
});
}

// ReSharper disable InconsistentNaming

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851.aspx
Expand Down
8 changes: 8 additions & 0 deletions src/chocolatey/infrastructure/filesystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public interface IFileSystem
/// <returns>true if copy was successful, otherwise false</returns>
bool copy_file_unsafe(string sourceFilePath, string destinationFilePath, bool overwriteExisting);

/// <summary>
/// Replace an existing file.
/// </summary>
/// <param name="sourceFilePath">Where is the file now?</param>
/// <param name="destinationFilePath">Where would you like it to go?</param>
/// <param name="backupFilePath">Where should the existing file be placed? Null if nowhere.</param>
void replace_file(string sourceFilePath, string destinationFilePath, string backupFilePath);

/// <summary>
/// Deletes the specified file.
/// </summary>
Expand Down

0 comments on commit d9d354d

Please sign in to comment.