コード例 #1
0
        internal void AskForSelectedBackupVersion(MainWindow.RecoveringQuery_st recQuery)
        {
            int version = recQuery.versionToRecover;

            MyLogger.print("Ripristino della versione " + recQuery.versionToRecover + "... ");
            sendToServer(commRecoverBackup);
            waitForAck(commCmdAckFromServer);
            //seleziono versione
            sendToServer(version.ToString());

            int fileCount = recQuery.recInfos.getVersionSpecificCount(version);

            try
            {
                for (int i = 0; i < fileCount; i++)
                {
                    //legge nome del file
                    string fileName = socketReadline();
                    string newPathAndName;
                    //definisce percorso dove salvare il file
                    if (recQuery.recoveringFolderPath != "")
                    {
                        //path da usare: quello specificato da utente
                        System.Diagnostics.Debug.Assert(fileName.Contains(mainWindow.settings.getRootFolder()));
                        //elimina la rootFolder. lascia // iniziale
                        string localPath = fileName.Substring(mainWindow.settings.getRootFolder().Length);
                        newPathAndName = recQuery.recoveringFolderPath.TrimEnd(Path.AltDirectorySeparatorChar) + localPath;
                    }
                    else
                    {
                        //path da usare: quello originale del file
                        newPathAndName = fileName;
                    }

                    //apro il file
                    FileStream fout;
                    Directory.CreateDirectory(System.IO.Path.GetDirectoryName(newPathAndName));
                    try
                    {
                        fout = new FileStream(newPathAndName, FileMode.Create);
                    }
                    catch (Exception e) when(e is IOException || e is UnauthorizedAccessException)
                    {
                        //se file è protetto ne crea una copia a fianco
                        newPathAndName += "-restoredCopy";
                        fout            = new FileStream(newPathAndName, FileMode.Create);
                        MyLogger.print("Impossibile ripristinare il file " + newPathAndName + ", salvo con suffisso \"restoredCopy\"\n");
                    }

                    System.DateTime LastModifyDate;
                    try
                    {
                        LastModifyDate = RecFileContent(newPathAndName, fout);
                        fout.Close();
                        FileInfo fi = new FileInfo(newPathAndName);
                        fi.LastWriteTime = LastModifyDate;
                    }
                    catch (CancelFileRequestException)
                    {
                        MyLogger.print("Operazione Annullata\n");
                        fout.Close();
                        deleteFile(newPathAndName);
                        return;
                    }
                    catch (IOException)
                    {
                        MyLogger.popup("Impossibile accedere al file " + newPathAndName + " Operazione interrotta.", MessageBoxImage.Error);
                        fout.Close();
                        deleteFile(newPathAndName);
                        return;
                    }
                }

                //mainWindow.Dispatcher.Invoke(mainWindow.DelShowOkMsg, "Ripristino versione completato!", MessageBoxImage.Information);
                //MyLogger.print("Ripristino versione " + recQuery.versionToRecover.ToString() + " riuscito");
                MyLogger.popup("Ripristino versione " + recQuery.versionToRecover.ToString() + " riuscito", MessageBoxImage.Information);
            }
            catch (Exception e)
            {
                MyLogger.print("ripristino fallito");
                mainWindow.Dispatcher.Invoke(mainWindow.DelShowOkMsg, "Ripristino versione fallita", MessageBoxImage.Error);
                MyLogger.debug(e.ToString());
                return;
            }
        }
コード例 #2
0
        /// <summary>
        /// return true se ricezione corretta, false altrimenti
        /// </summary>
        /// <param name="fout">already opened output fileStream</param>
        /// <returns>last modify date of the file</returns>
        private System.DateTime RecFileContent(string FileNameAndPath, FileStream fout)
        {
            //ricezione hash
            byte[] hashReceived = new byte[32];
            var    count        = serverStream.Read(hashReceived, 0, 32);
            string strHash      = System.Text.Encoding.UTF8.GetString(hashReceived);

            //remove \r\n
            socketReadline();

            //legge dimensione del file
            long sizeFile = Convert.ToInt64(socketReadline(), 16);

            //legge data_ultima_modifica file
            var lmfile = MyConverter.UnixTimestampToDateTime(Convert.ToInt64(socketReadline(), 16));

            //data_rec
            sendToServer(commDataRec);

            int attempt = 0;

            do
            {
                //ricezione e salvataggio su disco.
                var  buffer = new byte[1024];
                int  bytesRead;
                long totalBytesRead = 0;
                fout.Seek(0, SeekOrigin.Begin);

                //step della progress bar
                long stepSize = (long)Math.Ceiling((double)sizeFile / 10);
                long nextStep = 0;

                do
                {
                    bytesRead = serverStream.Read(buffer, 0, buffer.Length);
                    fout.Write(buffer, 0, bytesRead);
                    totalBytesRead += bytesRead;

                    //aggiorna progress bar
                    if (totalBytesRead > nextStep)
                    {
                        mainWindow.recoverW.Dispatcher.BeginInvoke(mainWindow.recoverW.DelSetRecProgressValues, sizeFile, 0, totalBytesRead);
                        nextStep += stepSize;
                    }
                }while (totalBytesRead < sizeFile);

                mainWindow.recoverW.Dispatcher.BeginInvoke(mainWindow.recoverW.DelSetRecProgressValues, sizeFile, 0, sizeFile);

                if (totalBytesRead != sizeFile)
                {
                    sendToServer(commSndAgain);
                    attempt++;
                    if (attempt < 5)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                //calcolo e confronto hash
                var computedHash = RecordFile.calcHash(FileNameAndPath, fout);
                if (computedHash == strHash)
                {
                    sendToServer(commDataAck);
                    return(lmfile);
                }
                else
                {
                    sendToServer(commSndAgain);
                    attempt++;
                }
            }while (attempt < 5);

            MyLogger.print("errore nel download del file. impossibile effettuare il ripristino.\n");
            //todo: catchare questa eccezione nelle funzioni chiamanti
            throw new CancelFileRequestException();
        }
コード例 #3
0
        /// <summary>
        /// restitrusce true se login ha avuto successo. se no false.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public void login(string user, string password)
        {
            if (mainWindow.logged)
            {
                logout();
            }

            if (!clientSocket.Connected)
            {
                newConnection();
            }

            //string -> utf8
            this.user = utf8.GetBytes(user);

            SHA256 mySha256 = SHA256Managed.Create();

            byte[] utf8psw = utf8.GetBytes(password);
            //hash(utf8(psw))
            byte[] hashPswByte = mySha256.ComputeHash(utf8psw, 0, utf8psw.Length);
            string hex         = BitConverter.ToString(hashPswByte).Replace("-", string.Empty); //rappresentazione in esadecimale -> 32 caratteri.

            this.hashPassword = utf8.GetBytes(hex);

            sendToServer(commLogin_str);

            //invio "[username]\r\n[sha-256_password]\r\n"
            byte[] userPassword = ConcatByte(this.user, separator_r_n);
            userPassword = ConcatByte(userPassword, this.hashPassword);
            userPassword = ConcatByte(userPassword, this.separator_r_n);
            sendToServer(userPassword);
            switch (/*commloggedok)*/ strRecCommFromServer())
            {
            case commloggedok:
                mainWindow.logged = true;
                break;

            case commloginerr:
                //create_ac?
                MyLogger.print("errore nel login\n");
                bool wantNewAcc = (bool)mainWindow.Dispatcher.Invoke(mainWindow.DelAskNewAccount);
                if (wantNewAcc)
                {
                    //newConnection();
                    createAccount(user, password);     //login automatico
                    mainWindow.logged = true;
                    return;
                }
                else
                {
                    mainWindow.logged = false;
                    throw new LoginFailedException();
                }
                break;

            case commAlreadyLogged:
                MyLogger.print("utente già connesso");
                mainWindow.logged = false;
                throw new LoginFailedException();
                break;

            default:
                mainWindow.logged = false;
                throw new LoginFailedException();
                break;
            }
        }
コード例 #4
0
        public void askForSingleFile(RecoverRecord rr)
        {
            FileStream fout;
            string     localFileName;

            MyLogger.print("Ripristino file in corso...");
            sendToServer(commRecoverFile);
            waitForAck(commCmdAckFromServer);
            try
            {
                if (File.Exists(rr.rf.nameAndPath))
                {
                    //chiede se sovrascrivere
                    string message = "file già esistente. si desidera sovrascriverlo?";
                    string caption = "Attenzione!";

                    bool wantOverwrite = (bool)mainWindow.recoverW.Dispatcher.Invoke(mainWindow.recoverW.DelYesNoQuestion, message, caption);
                    if (!wantOverwrite)
                    {
                        //salvare con nome
                        throw new IOException("need to save with name");
                    }
                }
                else
                {
                    Directory.CreateDirectory(System.IO.Path.GetDirectoryName(rr.rf.nameAndPath));
                }
                localFileName = rr.rf.nameAndPath;
                fout          = File.Open(localFileName, FileMode.OpenOrCreate);
            }
            catch (Exception e) when(e is IOException || e is UnauthorizedAccessException)
            {
                //file omonimo esiste già o altri errori nell'aprire il file. apro una dialog di salvataggio
                Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
                sfd.InitialDirectory = mainWindow.settings.getRootFolder();
                sfd.FileName         = rr.rf.getJustName();
                Nullable <bool> result = sfd.ShowDialog();

                localFileName = sfd.FileName;
                if (result == true)
                {
                    try
                    {
                        fout = File.Open(localFileName, FileMode.Create);
                    }
                    catch (Exception)
                    {
                        //mainWindow.Dispatcher.Invoke(mainWindow.DelShowOkMsg, "Impossibile aprire il file", MessageBoxImage.Error);
                        MyLogger.popup("Impossibile aprire il file. Operazione Annullata\n", MessageBoxImage.Error);
                        MyLogger.print("Impossibile aprire il file. Operazione Annullata\n");
                        //annullo richiesta recupero di questo file
                        return;
                    }
                }
                else
                {
                    MyLogger.print("Operazione Annullata\n");
                    //annullo richiesta recupero di questo file
                    return;
                }
            }

            //invio nome singolo file
            sendToServer(rr.rf.nameAndPath + "\r\n" + rr.backupVersion.ToString() + "\r\n");


            System.DateTime LastModifyDate;
            try
            {
                //ricevi contenuto file
                LastModifyDate = RecFileContent(localFileName, fout);
            }
            catch (CancelFileRequestException)
            {
                MyLogger.print("Operazione Annullata\n");
                fout.Close();
                deleteFile(localFileName);
                return;
            }
            catch (IOException)
            {
                MyLogger.popup("Impossibile accedere al file. Operazione annullata.", MessageBoxImage.Error);
                fout.Close();
                deleteFile(localFileName);
                return;
            }

            fout.Close();

            FileInfo fi = new FileInfo(localFileName);

            fi.LastWriteTime = LastModifyDate;
            MyLogger.print("completato.\n");
        }
コード例 #5
0
        /*-------------------------------------------------------------------------------------------------------------*/
        /*---logic Tread methods---------------------------------------------------------------------------------------*/
        /*-------------------------------------------------------------------------------------------------------------*/

        //siamo nel secondo thread, quello che non gestisce la interfaccia grafica.
        private void logicThreadStart()
        {
            MyLogger.debug("LogicThread starting");

            //setto interfaccia a busy
            this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.busy);

            try //catch errori non recuperabili per il thread
            {
                //reset richieste eventualmente vecchie per il logic thread
                resetLogicThreadNeeds();

                //avvio tmer per verifica abort signals periodicamente
                AbortTimer.Start();

                //inizializzo oggetto per connessione con server
                sm = new SessionManager(settings.getIP(), settings.getPorta(), this);
                //bool connected = false;

                //gestione del login
                sm.login(settings.getUser(), settings.getPassw());
                logged = true;

                //selezione cartella
                sm.setRootFolder(settings.getRootFolder());

                //attiva modalità logged nella UI
                this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.logged);

                //voglio iniziare con una sync
                needToSync = true;
                //needToSync = false;

                //è la prima sync per questa connessione
                firstConnSync = true;
                //firstConnSync = false;

                //ciclo finchè la connessione è attiva. si esce solo con eccezione o con chiusura thread logico (anch'essa un'eccezione).
                while (true)
                {
                    //verifica se deve sincronizzare
                    if (needToSync)
                    {
                        this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.busy);

                        MyLogger.print("AutoSync in corso\n");

                        // se è la prima sincronizzazione di questa connessione al server, crea DirMonitor
                        if (firstConnSync)
                        {
                            firstConnSync = false;
                            try
                            {
                                //init del dirMonitor
                                d = new DirMonitor(settings.getRootFolder(), sm);
                            }
                            catch (EmptyDirException)
                            {
                                //se arrivo qui è perchè c'è stata la prima connessione, l'initial backup di una cartella vuota e il download delle recoverInfo sempre vuote.
                                firstConnSync = true;
                            }
                        }
                        else //non è la prima connessione
                        {
                            //scandisco root folder
                            d.scanDir();

                            //sincronizzo tutte le modifiche
                            SyncAll();
                        }
                        needToSync = false;
                        this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.logged);
                        MyLogger.print("Completata\n");
                    }

                    //verifica se deve richiedere l'intero ultimo backup
                    if (needToRecoverWholeBackup)
                    {
                        this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.busy);

                        sm.AskForSelectedBackupVersion(RecoveringQuery);
                        needToRecoverWholeBackup = false;

                        this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.logged);
                    }

                    //verifica se deve richiedere dati per ripristino di file vecchi
                    if (needToAskRecoverInfo)
                    {
                        this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.busy);

                        var recInfos = sm.askForRecoverInfo();
                        needToAskRecoverInfo = false;

                        System.Diagnostics.Debug.Assert(recoverW != null);

                        recoverW.Dispatcher.Invoke(DelSetRecoverInfos, recInfos);

                        //qui l'interfaccia sarà resettata dalla recoverwindow che si chiude
                        //this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.logged);
                    }

                    //recupera recoverRecord
                    if (needToAskForFileToRecover)
                    {
                        this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.busy);

                        //recupera file
                        sm.askForSingleFile(fileToRecover);

                        needToAskForFileToRecover = false;

                        this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.logged);
                    }

                    WaitForSyncTime();
                }
            } //fine try esterno
            catch (SocketException)
            {
                MyLogger.print("impossibile connettersi. Server non ragiungibile");
            }
            catch (LoginFailedException)
            {
                MyLogger.print("errore nel login. Correggere dati di accesso o creare nuovo utente.");
            }
            catch (RootSetErrorException)
            {
                MyLogger.print("errore nella selezione della cartella. Correggere il path");
            }
            catch (AbortLogicThreadException)
            {
                MyLogger.print("Connessione Interrotta\n");
                MyLogger.debug("LogicThread closing per abort logic thread exception");
            }
            catch (DirectoryNotFoundException)
            {
                MyLogger.print("impossibile trovare directory specificata. Selezionarne un'altra");
            }
            catch (Exception e) //eccezione sconosciuta.
            {
                MyLogger.line();
                MyLogger.debug(e.Message);
                MyLogger.line();
                MyLogger.debug(e.ToString());
                MyLogger.debug("LogicThread closing");
                MyLogger.print("Errore sconosciuto. Chiusura connessione.\n");
            }


            this.Dispatcher.BeginInvoke(DelSetInterfaceLoggedMode, interfaceMode_t.notLogged);
            //if (!TerminateLogicThread)
            //{
            //    //setta la UI in modalità unlocked a meno che non sia TerminateLogicThread settata,
            //    //se no mainThread va in join e va in deadlock (non esegue invoke())
            //    this.Dispatcher.BeginInvoke(DelSetInterfaceLoggedMode, interfaceMode_t.notLogged);
            //}

            sm.closeConnection();
            //disattivo il timer che sblocca periodicamente il logicThread affinchè controlli se deve abortire
            AbortTimer.Stop();
            //this.Dispatcher.Invoke(DelSetInterfaceLoggedMode, interfaceMode_t.notLogged);
            logged = false;
            //TODO: ma quando chiudo recoverW diventa null?
            if (recoverW != null)
            {
                recoverW.Dispatcher.BeginInvoke(recoverW.DelCloseWindow);
            }
            return; //logic thread termina qui
        }