#region Disclaimer / License // Copyright (C) 2008, Kenneth Skovhede // http://www.hexad.dk, opensource@hexad.dk // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion using System; using System.Collections.Generic; using System.Text; namespace Duplicati.Library.Main.RSync { /// /// This class wraps the usage of RDiff.exe so the use of RDiff looks like native calls /// public class RDiffWrapper { /// /// The name of the RDiff program to call /// public static string RDIFF_PROGRAM = "rdiff"; /// /// Generates a signature file /// /// The file to create the signature for /// The file write the signature to public static void GenerateSignature(string filename, string outputfile) { if (System.IO.File.Exists(outputfile)) System.IO.File.Delete(outputfile); //System.IO.File.Create(outputfile); ReadProgramOutput("signature \"" + filename + "\" \"" + outputfile + "\"", null); } /// /// Generates a delta file /// /// The signature for the file /// The file to create the delta for /// The delta output file public static void GenerateDelta(string signaturefile, string filename, string deltafile) { if (System.IO.File.Exists(deltafile)) System.IO.File.Delete(deltafile); ReadProgramOutput("delta \"" + signaturefile + "\" \"" + filename + "\" \"" + deltafile + "\"", null); } /// /// Patches a file /// /// The most recent full copy of the file /// The delta file /// The restored file public static void PatchFile(string basefile, string deltafile, string outputfile) { if (System.IO.File.Exists(outputfile)) System.IO.File.Delete(outputfile); ReadProgramOutput("patch \"" + basefile + "\" \"" + deltafile + "\" \"" + outputfile + "\"", null); } /// /// Internal helper function that wraps the usage of rdiff /// /// The command line arguments /// An optional stream to write output to /// The data from stdout private static byte[] ReadProgramOutput(string args, System.IO.Stream output) { System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo(RDIFF_PROGRAM); pi.Arguments = args; pi.CreateNoWindow = true; pi.RedirectStandardOutput = true; pi.RedirectStandardError = true; pi.UseShellExecute = false; pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; System.Diagnostics.Process p = System.Diagnostics.Process.Start(pi); System.IO.Stream outstream = output; if (outstream == null) outstream = new System.IO.MemoryStream(); byte[] buf = new byte[1024]; int a; while ((a = p.StandardOutput.BaseStream.Read(buf, 0, buf.Length)) != 0) outstream.Write(buf, 0, a); p.WaitForExit(); if (p.ExitCode != 0) throw new Exception(p.StandardError.ReadToEnd()); else if (output == null) return (outstream as System.IO.MemoryStream).ToArray(); else return null; } } }