public void NecessaryFilesToUpgradeTest() { string[] lines1 = { "First line", "Second line", "Third line" }; string[] lines2 = { "First line", "Second line", "NO" }; System.IO.Directory.CreateDirectory("asd"); System.IO.Directory.CreateDirectory(@"asd\ciao"); DirectoryInfo rinfo = new DirectoryInfo("asd"); System.IO.File.WriteAllLines(@"asd\uno.txt", lines1); System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines1); System.IO.File.WriteAllLines(@"asd\ciao\tre.txt", lines1); System.IO.File.WriteAllLines(@"asd\ciao\quattro.txt", lines1); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion vold = (FolderBackup.Shared.FBVersion)vb.generate(); System.IO.File.WriteAllLines(@"asd\ciao\cinque.txt", lines2); System.IO.File.WriteAllLines(@"asd\sei.txt", lines2); System.IO.File.WriteAllLines(@"asd\sette.txt", lines1); FolderBackup.Shared.FBVersion vnew = (FolderBackup.Shared.FBVersion)vb.generate(); FBVersion diff = vnew - vold; List <FBFile> fl = FBVersion.getNecessaryFilesToUpgrade(vnew, vold.fileList); Assert.AreEqual(fl.Count, 1); FBFileBuilder fb = new FBFileBuilder(@"asd\ciao\cinque.txt"); FBFile necessary = (FBFile)fb.generate(); Assert.AreEqual(fl[0], necessary); System.IO.Directory.Delete("asd", true); }
public void PersistAndDeserialize() { string[] lines = { "First line", "Second line", "Third line" }; System.IO.Directory.CreateDirectory("asd"); System.IO.Directory.CreateDirectory(@"asd\ciao"); DirectoryInfo rinfo = new DirectoryInfo("asd"); System.IO.File.WriteAllLines(@"asd\uno.txt", lines); System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines); FileInfo finfo = new FileInfo(@"asd\ciao\due.txt"); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); Stream TestFileStream = File.Create("version.bin"); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(TestFileStream, v); TestFileStream.Close(); TestFileStream = File.OpenRead("version.bin"); BinaryFormatter deserializer = new BinaryFormatter(); FBVersion v2 = (FBVersion)deserializer.Deserialize(TestFileStream); TestFileStream.Close(); File.Delete("version.bin"); System.IO.Directory.Delete("asd", true); Assert.AreEqual(v2, v); }
public void RevertWithFileDeletion() { this.TestInitialize(); string[] lines = { "First line", "Second line", "Third line" }; System.IO.File.WriteAllLines(@"asd\ciao\tre.txt", lines); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); server.newTransaction(serV); server.commit(); UploadData ud = server.resetToPreviousVersion(1); UsefullMethods.ReceiveFile(ud.ip, ud.port, ud.token, @"asd\asd.zip"); String ignore; List <Instruction> instructionList = UsefullMethods.ExtractInstructions(@"asd\asd.zip", out ignore); Assert.IsTrue(instructionList.Count == 1); Assert.IsTrue(instructionList[0].cmd == InstructionType.DELETE); Assert.IsTrue(instructionList[0].src == @"ciao\tre.txt"); }
public override FBAbstractElement Clone() { FBVersion cloned = new FBVersion(this.Name); cloned.timestamp = new DateTime(timestamp.Ticks); cloned.root = (FBDirectory)this.root.Clone(); return(cloned); }
public override FBAbstractElement generate() { FBVersion vers = new FBVersion(); FBDirectoryBuilder db = new FBDirectoryBuilder(this.path); vers.root = (FBDirectory) db.generate(); return vers; }
public static FBVersion operator -(FBVersion first, FBVersion second) { FBVersion ret = new FBVersion(); ret.Name = "Diff"; ret.timestamp = new DateTime(); ret.root = first.root - second.root; return ret; }
public static byte[] serialize(FBVersion version) { MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, version); return ms.ToArray(); }
override public FBAbstractElement generate() { FBVersion vers = new FBVersion(); FBDirectoryBuilder db = new FBDirectoryBuilder(this.path); vers.root = (FBDirectory)db.generate(); return(vers); }
static public byte[] serialize(FBVersion version) { MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, version); return(ms.ToArray()); }
static public FBVersion operator -(FBVersion first, FBVersion second) { FBVersion ret = new FBVersion(); ret.Name = "Diff"; ret.timestamp = new DateTime(); ret.root = first.root - second.root; return(ret); }
public void GetFileFailTest() { doTransaction(); server.commit(); FBVersion vers = new FBVersion(); FBFile file = (FBFile)(new FBFileBuilder(@"asd\due.txt")).generate(); file.hash = "asdsdaasd"; vers.addElement(file); SerializedVersion serV = new SerializedVersion(vers.serialize()); var uploadData = server.getFile(serV); Assert.IsNull(uploadData); }
public void DetectEqualVersionTest() { doTransaction(); server.commit(); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); Assert.IsFalse(server.newTransaction(serV)); }
public static List<FBFile> getNecessaryFilesToUpgrade(FBVersion newV, List<FBFile> old) { List<FBFile> necessaryFiles = new List<FBFile>(); List<FBFile> newVersionFiles = newV.fileList; foreach (FBFile newF in newVersionFiles) { if (necessaryFiles.Contains(newF)) continue; if (old.Contains(newF)) continue; necessaryFiles.Add(newF); } return necessaryFiles; }
public void RollbackTest() { FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); Assert.IsTrue(server.newTransaction(serV)); byte[][] bfiles = server.getFilesToUpload(); foreach (byte[] bf in bfiles) { FBFile f = FBFile.deserialize(bf); Assert.IsTrue(v.fileList.Contains(f)); } FBFile file = (FBFile) new FBFileBuilder(@"asd\uno.txt").generate(); string[] lines = { "First line", "Second line", "Third line" }; System.IO.File.WriteAllLines(@"asd\uno.txt", lines); FileStream fstream = new FileStream(@"asd\uno.txt", FileMode.Open, FileAccess.Read); //Assert.AreEqual(server.uploadFile(fstream), file.hash); //fstream.Close(); UploadData credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); file = (FBFile) new FBFileBuilder(@"asd\due.txt").generate(); string[] lines1 = { "First line", "Second line", "Third lines" }; System.IO.File.WriteAllLines(@"asd\due.txt", lines1); fstream = new FileStream(@"asd\due.txt", FileMode.Open, FileAccess.Read); //Assert.AreEqual(server.uploadFile(fstream), file.hash); //fstream.Close(); credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); Assert.IsTrue(server.rollback()); Assert.IsTrue(server.newTransaction(serV)); bfiles = server.getFilesToUpload(); foreach (byte[] bf in bfiles) { FBFile f = FBFile.deserialize(bf); Assert.IsTrue(v.fileList.Contains(f)); } }
public void GetFileTest() { doTransaction(); server.commit(); FBVersion vers = new FBVersion(); FBFile file = (FBFile) (new FBFileBuilder(@"asd\due.txt")).generate(); vers.addElement(file); SerializedVersion serV = new SerializedVersion(vers.serialize()); var uploadData = server.getFile(serV); UsefullMethods.ReceiveFile(uploadData.ip, uploadData.port, uploadData.token, @"asd\test.txt"); String content = File.ReadAllText(@"asd\test.txt"); Assert.IsTrue(content.Contains("Third lines")); }
private void TestInitialize() { server = new Server.Server(); AuthenticationData ad = server.authStep1("test1"); token = server.authStep2(ad.token, "test1", AuthenticationPrimitives.hashPassword("test1", ad.salt, ad.token)); CleanUp(); server = new Server.Server(); ad = server.authStep1("test1"); token = server.authStep2(ad.token, "test1", AuthenticationPrimitives.hashPassword("test1", ad.salt, ad.token)); string[] lines = { "First line", "Second line", "Third line" }; string[] lines1 = { "First line", "Second line", "Third lines" }; System.IO.Directory.CreateDirectory("asd"); System.IO.Directory.CreateDirectory(@"asd\ciao"); rinfo = new DirectoryInfo("asd"); System.IO.File.WriteAllLines(@"asd\uno.txt", lines); System.IO.File.WriteAllLines(@"asd\due.txt", lines1); System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); server.newTransaction(serV); byte[][] bfiles = server.getFilesToUpload(); FBFile file = (FBFile) new FBFileBuilder(@"asd\uno.txt").generate(); FileStream fstream = new FileStream(@"asd\uno.txt", FileMode.Open, FileAccess.Read); UploadData credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); fstream.Close(); file = (FBFile) new FBFileBuilder(@"asd\due.txt").generate(); fstream = new FileStream(@"asd\due.txt", FileMode.Open, FileAccess.Read); credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); Assert.IsTrue(server.commit()); }
public void PersistenceTest() { doTransaction(); server.commit(); server = null; server = new Server.Server(); AuthenticationData ad = server.authStep1("test1"); token = server.authStep2(ad.token, "test1", AuthenticationPrimitives.hashPassword("test1", ad.salt, ad.token)); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); Assert.IsFalse(server.newTransaction(serV)); }
public void TwoVersionsAreEquals() { string[] lines = { "First line", "Second line", "Third line" }; System.IO.Directory.CreateDirectory("asd"); System.IO.Directory.CreateDirectory(@"asd\ciao"); DirectoryInfo rinfo = new DirectoryInfo("asd"); System.IO.File.WriteAllLines(@"asd\uno.txt", lines); System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines); FileInfo finfo = new FileInfo(@"asd\ciao\due.txt"); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v1 = (FolderBackup.Shared.FBVersion)vb.generate(); FolderBackup.Shared.FBVersion v2 = (FolderBackup.Shared.FBVersion)vb.generate(); Assert.AreEqual(v1, v2); System.IO.Directory.Delete("asd", true); }
static public List <FBFile> getNecessaryFilesToUpgrade(FBVersion newV, List <FBFile> old) { List <FBFile> necessaryFiles = new List <FBFile>(); List <FBFile> newVersionFiles = newV.fileList; foreach (FBFile newF in newVersionFiles) { if (necessaryFiles.Contains(newF)) { continue; } if (old.Contains(newF)) { continue; } necessaryFiles.Add(newF); } return(necessaryFiles); }
public void RevertWithNewFile() { this.TestInitialize(); string[] lines = { "First line", "Second line", "Third line", "Fourth line" }; System.IO.File.WriteAllLines(@"asd\ciao\tre.txt", lines); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); server.newTransaction(serV); FileStream fstream = new FileStream(@"asd\ciao\tre.txt", FileMode.Open, FileAccess.Read); server.getFilesToUpload(); UploadData credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); server.commit(); System.IO.File.Delete(@"asd\ciao\tre.txt"); vb = new FBVersionBuilder(rinfo.FullName); v = (FolderBackup.Shared.FBVersion)vb.generate(); serV = new SerializedVersion(v.serialize()); server.newTransaction(serV); server.commit(); UploadData ud = server.resetToPreviousVersion(1); UsefullMethods.ReceiveFile(ud.ip, ud.port, ud.token, @"asd\asd.zip"); String ignore; List <Instruction> instructionList = UsefullMethods.ExtractInstructions(@"asd\asd.zip", out ignore); Assert.IsTrue(instructionList.Count == 1); Assert.IsTrue(instructionList[0].cmd == InstructionType.NEW); Assert.IsTrue(instructionList[0].dst == @"ciao\tre.txt"); }
public void VersionBuilderGenerate() { string[] lines = { "First line", "Second line", "Third line" }; System.IO.Directory.CreateDirectory("asd"); System.IO.Directory.CreateDirectory(@"asd\ciao"); DirectoryInfo rinfo = new DirectoryInfo("asd"); System.IO.File.WriteAllLines(@"asd\uno.txt", lines); System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines); FileInfo finfo = new FileInfo(@"asd\ciao\due.txt"); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); Assert.AreEqual(v.root.content.Count, (int)2); Assert.IsInstanceOfType(v.root.content["ciao"], v.root.GetType()); Assert.AreEqual(((FolderBackup.Shared.FBDirectory)v.root.content["ciao"]).content.Count, 1); Assert.AreEqual(((FolderBackup.Shared.FBFile)((FolderBackup.Shared.FBDirectory)v.root.content["ciao"]).content["due.txt"]).dimension, finfo.Length); Assert.AreEqual(((FolderBackup.Shared.FBDirectory)v.root.content["ciao"]).content["due.txt"].isEqualTo(v.root.content["uno.txt"]), true); System.IO.Directory.Delete("asd", true); }
public string getFile(FBFile f) { SerializedVersion serV = new SerializedVersion(); FBVersion tmpVer = new FBVersion(); tmpVer.addElement(f); serV.encodedVersion = tmpVer.serialize(); var uploadData = server.getFile(serV); string tmpPath = Path.GetTempPath()+f.Name; if (File.Exists(tmpPath)) { File.Delete(tmpPath); } UsefullMethods.ReceiveFile(uploadData.ip, uploadData.port, uploadData.token, tmpPath); return tmpPath; }
private UploadData resetInternals(FBVersion newVersion, FBVersion current) { FBVersion diff = newVersion - current; newVersion.setAbsoluteNameToFile(); if (diff.root != null) diff.setAbsoluteNameToFile(); current.setAbsoluteNameToFile(); List<Instruction> instrucionList = new List<Instruction>(); try { File.Delete(user.rootDirectory.FullName + @"\tmp.zip"); } catch { } ZipArchive zip = ZipFile.Open(user.rootDirectory.FullName + @"\tmp.zip", ZipArchiveMode.Update); if (diff.fileList != null) foreach (FBAbstractElement to in diff.fileList) { bool found = false; foreach (FBAbstractElement from in current.fileList) { if (from.Equals(to)) { found = true; instrucionList.Add(new Instruction(InstructionType.COPY, from.Name, to.Name)); break; } } if (!found) { foreach (PhysicFile ph in realFiles.list) { if (ph.getFBFile().Equals(to)) { if (zip.GetEntry(ph.getRealFileInfo().Name) == null) { zip.CreateEntryFromFile(ph.getRealFileInfo().FullName, ph.getRealFileInfo().Name, CompressionLevel.Optimal); } instrucionList.Add(new Instruction(InstructionType.NEW, ph.getRealFileInfo().Name, to.Name)); } } } } diff = current - newVersion; if (diff.fileList != null) { //diff.setAbsoluteNameToFile(); foreach (FBAbstractElement toDelete in diff.fileList) { instrucionList.Add(new Instruction(InstructionType.DELETE, toDelete.Name, "")); } } Stream FilesStream = File.OpenWrite(this.user.rootDirectory + @"\instructions.bin"); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(FilesStream, instrucionList); FilesStream.Close(); zip.CreateEntryFromFile(this.user.rootDirectory + @"\instructions.bin", "instructions.bin", CompressionLevel.Optimal); File.Delete(this.user.rootDirectory + @"\instructions.bin"); zip.Dispose(); FilesStream = new FileStream(user.rootDirectory.FullName + @"\tmp.zip", FileMode.Open, FileAccess.Read); string token = Server.GetUniqueKey(20); SecureDownloader sr = new SecureDownloader(this, token, this.ManageCompleteUpload, this.ManageFailedUpload, FilesStream); UInt16 port = sr.port; return new UploadData(UsefullMethods.GetLocalIPAddress(), port, token); }
public byte[] serialize() { return(FBVersion.serialize(this)); }
private void versionClick(object sender, RoutedEventArgs e) { String name = ((System.Windows.Controls.Button)sender).Name; //search version FBVersion v = null; foreach (System.Windows.Controls.Button x in versionBox.Items) { if (sender == x) { Color c = (Color)ColorConverter.ConvertFromString("#FF9C1A04"); x.Background = new SolidColorBrush(c); c = (Color)ColorConverter.ConvertFromString("#FFFFFFFF"); x.Foreground = new SolidColorBrush(c); } else { Color c = (Color)ColorConverter.ConvertFromString("#FFFFFFFF"); x.Background = new SolidColorBrush(c); c = (Color)ColorConverter.ConvertFromString("#FF000000"); x.Foreground = new SolidColorBrush(c); } } foreach (FBVersion x in this.versions) { if (x.timestamp.ToString(TIMESTAMP_FORMAT) == name) { v = x; break; } } versionView.Items.Clear(); this.selectedVersion = v; versionView.Items.Add(CreateDirectoryNode(this.selectedVersion.root, this.selectedVersion.root.Name)); if (selectedVersion == versions[versions.Length-1]) { this.revertVersion.IsEnabled = false; }else { this.revertVersion.IsEnabled = true; } }
private void buildGraphic() { SerializedVersion[] sversions = server.getOldVersions(); this.versions = new FBVersion[sversions.Length]; int i = 0; se.watcher.EnableRaisingEvents = false; versionBox.Items.Clear(); foreach (SerializedVersion v in sversions) { versions[i] = FBVersion.deserialize(v.encodedVersion); System.Windows.Controls.Button button = new System.Windows.Controls.Button(); button.Name = versions[i].timestamp.ToString(TIMESTAMP_FORMAT); button.Content = versions[i].timestamp.ToString("MMM, dd yyyy HH:MM"); button.Click += versionClick; button.MinWidth = 200; button.MinHeight = 22; if (i == sversions.Length - 1) { Color c = (Color)ColorConverter.ConvertFromString("#FF9C1A04"); button.Background = new SolidColorBrush(c); c = (Color)ColorConverter.ConvertFromString("#FFFFFFFF"); button.Foreground = new SolidColorBrush(c); } else { Color c = (Color)ColorConverter.ConvertFromString("#FFFFFFFF"); button.Background = new SolidColorBrush(c); c = (Color)ColorConverter.ConvertFromString("#FF000000"); button.Foreground = new SolidColorBrush(c); } versionBox.Items.Add(button); versionButtons.Add(button); i++; } versionView.Items.Clear(); if (versions.Length > 0) { this.selectedVersion = versions[versions.Length - 1]; versionView.Items.Add(CreateDirectoryNode(this.selectedVersion.root, this.selectedVersion.root.Name)); this.revertVersion.IsEnabled = false; } }
private void sync() { cv = (FBVersion)vb.generate(); String dirPath = conf.targetPath.get(); if (dirPath == null || !Directory.Exists(dirPath)) { throw new DirectoryNotFoundException("Directory in configuration is not valid"); } SerializedVersion serV = new SerializedVersion(); serV.encodedVersion = cv.serialize(); threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Start syncing"); if (server.newTransaction(serV)) { this.status = "Syncing"; byte[][] bfiles = server.getFilesToUpload(); List<FBFile> fileToSync = new List<FBFile>(); foreach (byte[] bf in bfiles) { fileToSync.Add(FBFile.deserialize(bf)); } int i = 0; try { foreach (FBFile f in fileToSync) { if (!this.stopSync) { threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Syncing file "+i+" of " + fileToSync.Count); i++; FBFileClient cf = FBFileClient.generate(f); UploadData cedential = server.uploadFile(); UsefullMethods.SendFile(cedential.ip,cedential.port,cedential.token, new FileStream(cf.FullName, FileMode.Open)); }else { break; } } if (!this.stopSync) { server.commit(); threadCallback.Invoke(TypeThread.SYNC, StatusCode.SUCCESS, "Sync completed"); } else { threadCallback.Invoke(TypeThread.SYNC, StatusCode.ABORTED, "Syncing Stopped"); server.rollback(); } } catch { server.rollback(); } this.status = "Idle"; }else { threadCallback.Invoke(TypeThread.SYNC, StatusCode.IDLE, "Nothing to be done"); } }
private void resetVersion(UploadData ud,FBVersion v) { string filename = Path.GetTempFileName(); UsefullMethods.ReceiveFile(ud.ip, ud.port, ud.token, filename); String pathFiles; List<Instruction> instructionList = UsefullMethods.ExtractInstructions(filename, out pathFiles); foreach (Instruction i in instructionList) { ExecuteInstruction(i, pathFiles); } CleanUpDir(v.root,""); StartSync(); }
public void resetToVersion(FBVersion v) { SerializedVersion serV = new SerializedVersion(); serV.encodedVersion = v.serialize(); UploadData ud = server.resetToCraftedVersion(serV); resetVersion(ud, v); }
public void resetPrevoiusVersion(int vIndex, FBVersion v) { UploadData ud = server.resetToPreviousVersion(vIndex); resetVersion(ud,v); }
public override FBAbstractElement Clone() { FBVersion cloned = new FBVersion(this.Name); cloned.timestamp = new DateTime(timestamp.Ticks); cloned.root = (FBDirectory) this.root.Clone(); return cloned; }