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);
        }
Esempio n. 2
0
        public static FBFileClient generate(FBFile file)
        {
            Config conf = Config.Instance();
            FileInfo f = whoami(file, new DirectoryInfo(conf.targetPath.get()));

            return new FBFileClient(f);
        }
Esempio n. 3
0
        public override FBAbstractElement generate()
        {
            FileInfo finf = new FileInfo(this.path);
            FBFile file = new FBFile(finf.Name);
            file.dimension = finf.Length;

            FileStream fileStream = finf.Open(FileMode.Open);
            fileStream.Position = 0;

            SHA512 hasher = new SHA512Managed();
            file.hash = System.Text.Encoding.Default.GetString(hasher.ComputeHash(fileStream));
            fileStream.Close();

            return file;
        }
Esempio n. 4
0
        private FBFileClient(FileInfo finf)
            : base("")
        {
            FullName = finf.FullName;
            FBFile file = new FBFile(finf.Name);
            this.Name = finf.Name;
            this.dimension = finf.Length;
            if (finf.Exists) {
                Console.WriteLine("ciao");
            }
            FileStream fileStream = finf.Open(FileMode.Open);
            fileStream.Position = 0;

            SHA512 hasher = new SHA512Managed();
            file.hash = System.Text.Encoding.Default.GetString(hasher.ComputeHash(fileStream));
            fileStream.Close();
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        public static FileInfo whoami(FBFile file, DirectoryInfo dinfo)
        {
            String path =  dinfo.FullName + "\\" + file.Name;

            FileInfo fi = new FileInfo(path);
            if (fi.Exists)
            {
                return fi;
            }

            foreach (DirectoryInfo dir in dinfo.GetDirectories())
            {
                var ret = whoami(file, dir);
                if (ret != null)
                    return ret;
            }

            return null;
        }
Esempio n. 7
0
 public PhysicFile(FBFile abs, string path)
 {
     this.abstractFile = abs;
     this.hardFile = new FileInfo(path);
 }
Esempio n. 8
0
 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;
 }
Esempio n. 9
0
 private PhysicFile findPhysicFile(FBFile f)
 {
     foreach(PhysicFile ph in realFiles.list)
     {
         if (ph.getFBFile().Equals(f))
             return ph;
     }
     return null;
 }
Esempio n. 10
0
        public void ManageCompleteUpload(FBFile f, PhysicFile pf, string token)
        {
            this.uploadedFiles.add(pf);

            this.necessaryFiles.Remove(f);
            SecureChannel chan;
            this.channels.TryRemove(token, out chan);
        }
Esempio n. 11
0
        private void sync()
        {
            String dirPath = conf.targetPath.get();

            if (dirPath == null || !Directory.Exists(dirPath))
            {
                throw new DirectoryNotFoundException("Directory in configuration is not valid");
            }
            try
            {
                cv = (FBVersion)vb.generate();
            }catch (Exception e)
            {
                MessageBox.Show(e.Message + "\nThe application will be closed", "Unexpected error");
                Environment.Exit(0);
            }
            SerializedVersion serV = new SerializedVersion();

            serV.encodedVersion = cv.serialize();
            threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Start syncing");
            FileStream fs = null;

            try {
                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;

                    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();
                    }
                    this.status = "Idle";
                }
                else
                {
                    threadCallback.Invoke(TypeThread.SYNC, StatusCode.IDLE, "Nothing to be done");
                }
            }
            catch (System.ServiceModel.CommunicationException e)
            {
                MessageBox.Show("There is a problem with connection, please retry to login!", "Error in connection");
                threadCallback.Invoke(TypeThread.SYNC, StatusCode.ABORTED, "Connection error");
            }
            catch
            {
                server.rollback();
            }
        }