Exemplo n.º 1
0
        /// <summary>
        /// Metodo privato per l'Invio dei file appartenenti ad un tipo di lista
        /// </summary>
        /// <param name="l">Lista da cui prelevare i FileAttr</param>
        /// <param name="type">Tipo di Comando: NEW, UPD, DEL</param>
        /// <returns>True se file inviato correttamente</returns>
        private bool sendFiles(List <FileAttr> l, Command.type type)
        {
            foreach (FileAttr fa in l)
            {
                if (_shouldStop)
                {
                    return(false);
                }

                String state = "Sincronizzo: " + fa.path;
                if (state.Length > 124)
                {
                    state  = state.Substring(0, 120);
                    state += "...";
                }
                _files.sendMsg(state);

                Command c = new Command(type);
                c.file = fa;
                if (!c.serializeTo(netStream))
                {
                    _files.sendErrFatal("Errore: Impossibile Comunicare correttamente con il Server");
                    _shouldStop = true;
                    return(false);
                }

                if (type != Command.type.DEL)
                {
                    if (!sendFile(fa.path))
                    {
                        _files.sendErrFatal("Errore: Impossibile trasferire i file sul server");
                        _shouldStop = true;
                        return(false);
                    }
                }

                //Aspetto comando risposta SUCC
                Command res = Command.deserializeFrom(sock, netStream, ref _shouldStop);
                if (_shouldStop)
                {
                    return(false);
                }
                else if (res == null || res.cmd != Command.type.SUCC)
                {
                    _files.sendErrFatal("Errore: il Server indicato non risponde correttamente");
                    _shouldStop = true;
                    return(false);
                }

                prog += diffForFile;
                _files.updBar(prog);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Metodo che controlla se l'utente esiste e se la pwd è corretta, se non esiste lo crea,
        /// si occupa anche di creare Directory e DB per l'utente
        /// </summary>
        /// <param name="s">Settings Relative alla richiesta ricevuta</param>
        /// <param name="cmdtype">Tipo di Comando con cui è stata richiesta l'autenticazione</param>
        /// <returns>True se loggato con successo</returns>
        private bool authUser(Settings s, Command.type cmdtype)
        {
            /*Controllo se esiste user nel DB*/
            Settings u;
            int      thID = Thread.CurrentThread.ManagedThreadId;

            /*Controllo se utente valido*/
            if (!isUserValid(s.user, Path.GetInvalidFileNameChars()))
            {
                return(false);
            }

            foreach (Syncro sync in executingSync)
            {
                if (sync.syncSettings.user == s.user)
                {
                    Console.WriteLine("(" + thID + ") ->Utente {0} già autenticato su questo server", s.user);
                    return(false);
                }
            }

            foreach (Restore rest in executingRestore)
            {
                if (rest.syncSettings.user == s.user)
                {
                    Console.WriteLine("(" + thID + ") ->Utente {0} già autenticato su questo server", s.user);
                    return(false);
                }
            }


            try
            {
                users.Open();
                if (!Settings.getSettingsDB(users, s.folder, s.user, s.pwd, out u))
                {
                    return(false);
                }

                /*Elmino Dir se utente nuovo ma dir già esistente*/
                if (!u.active)
                {
                    try
                    {
                        DirectoryInfo d = new DirectoryInfo(s.user);
                        if (d.Exists)
                        {
                            d.Delete(true);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("(" + thID + ")_ERRORE: impossibile Eliminare directory relativa a vecchio utente {0} ({1})", s.user, e.Message);
                        return(false);
                    }
                }

                /*Eliminazione vecchia folder*/
                if (s.folder != u.folder)
                {
                    Console.WriteLine("(" + thID + ") ->Directory differente: {0} per utente {1} ", s.folder, s.user);
                    try
                    {
                        if (cmdtype != Command.type.SYNC_REQ)
                        {
                            return(false);
                        }

                        if (!Settings.delSettingsDB(users, u))
                        {
                            return(false);
                        }

                        DirectoryInfo d = new DirectoryInfo(s.user);
                        if (d.Exists)
                        {
                            d.Delete(true);
                        }

                        if (!Settings.getSettingsDB(users, s.folder, s.user, s.pwd, out u))
                        {
                            return(false);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("(" + thID + ")_ERRORE: impossibile cambiare directory per utente {0} ({1})", s.user, e.Message);
                        return(false);
                    }
                }
            }
            catch
            {
                return(false);
            }
            finally
            {
                if (users.State == System.Data.ConnectionState.Open)
                {
                    users.Close();
                }
            }

            /*Creazione Directory per Utente se è il Caso*/
            try
            {
                DirectoryInfo dir = new DirectoryInfo(s.user);
                if (!dir.Exists)
                {
                    dir.Create();
                }
            }
            catch (Exception)
            {
                Console.WriteLine("(" + thID + ")_ERRORE: impossibile creare Directory per l'utente: " + s.user);
                return(false);
            }

            /*Creazione DB se è il caso*/
            String   nomeDb = s.user + @"\" + s.user + ".sqlite";
            FileInfo db     = new FileInfo(nomeDb);

            if (db.Exists)
            {
                return(true);
            }

            SQLiteConnection c = null;

            try
            {
                SQLiteConnection.CreateFile(nomeDb);
                c = new SQLiteConnection("Data Source=" + nomeDb + ";Version=3;");

                /*Creo Tabella Necessaria*/
                String        sql = "CREATE TABLE files (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(248), directory VARCHAR(248), size INTEGER, checksum TEXT, syncData DATETIME, version INTEGER, deleted BOOLEAN)";
                SQLiteCommand cmd = new SQLiteCommand(sql, c);
                c.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                Console.WriteLine("(" + thID + ")_ERRORE: impossibile creare il DB per l'utente: " + s.user);
                return(false);
            }
            finally
            {
                if (c != null && c.State == System.Data.ConnectionState.Open)
                {
                    c.Close();
                }
            }
            return(true);
        }