#region Disclaimer / License // Copyright (C) 2011, 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; using System.IO; namespace Duplicati.Library.SharpRSync { /// /// This class wraps the usage of SharpRSync into an easy to use static class /// public class Interface { /// /// Generates a signature, and writes it to the stream /// /// The stream to create the signature for /// The stream to write the signature into public static void GenerateSignature(Stream input, Stream output) { SharpRSync.ChecksumFileWriter cs = new Duplicati.Library.SharpRSync.ChecksumFileWriter(output); cs.AddStream(input); } /// /// 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); using (FileStream fs1 = File.OpenRead(filename)) using (FileStream fs2 = File.Create(outputfile)) GenerateSignature(fs1, fs2); } /// /// Generates a delta stream /// /// The signature for the stream /// The (possibly) altered stream to create the delta for /// The delta output public static void GenerateDelta(Stream signature, Stream input, Stream output) { SharpRSync.ChecksumFileReader cs = new Duplicati.Library.SharpRSync.ChecksumFileReader(signature); SharpRSync.DeltaFile df = new Duplicati.Library.SharpRSync.DeltaFile(cs); df.GenerateDeltaFile(input, output); } /// /// 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); using (FileStream fs1 = File.OpenRead(signaturefile)) using (FileStream fs2 = File.OpenRead(filename)) using (FileStream fs3 = File.Create(deltafile)) GenerateDelta(fs1, fs2, fs3); } /// /// 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); using (FileStream fs1 = File.OpenRead(basefile)) using (FileStream fs2 = File.OpenRead(deltafile)) using (FileStream fs3 = File.Create(outputfile)) PatchFile(fs1, fs2, fs3); } /// /// Constructs a stream from a basestream and a delta stream /// /// The most recent full copy of the file /// The delta file /// The restored file public static void PatchFile(Stream basestream, Stream delta, Stream output) { SharpRSync.DeltaFile df = new Duplicati.Library.SharpRSync.DeltaFile(delta); df.PatchFile(basestream, output); } } }