public FilePrevs(VFile f)
        {
            InitializeComponent();
            if (f == null)
                throw new ArgumentException("Il file passato non può essere null.");

            _f = f;
            Load();
        }
 internal void ShowRecoverWindow(VFile s)
 {
     FilePrevs _recover = new FilePrevs(s);
     _recover.ShowDialog();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Data una versione del VFS del client (clientVersion) ed una versione di destinazione richiesta, la funzione ritorna la lista
        /// di tutti i file modificati. Se targetVFSVersion è null, si ritornano le modifiche fino all'ultima versione del VFS.
        /// </summary>
        /// <param name="clientVersion"></param>
        /// <returns></returns>
        public IEnumerable<VFile> GetVFSUpdates(long clientVFSVersion, ref long? targetVFSVersion)
        {
            // La versione del client può essere negativa, se è la prima volta che si sincronizza.
            lock (this)
            {
                List<VFile> res = new List<VFile>();
                try
                {
                    SQLiteConnection conn;
                    string dir;
                    string db = UserManager.Instance.GetUserDbPath(this.Id, out dir);
                    using (conn = new SQLiteConnection(String.Format("Data Source={0};Version=3;", db)))
                    {
                        conn.Open();
                        using (SQLiteCommand cmd = conn.CreateCommand())
                        {
                            // Se non viene specificato alcun targetVFSVersion, recupera l'ultima versione del vfs.
                            if (targetVFSVersion == null)
                            {
                                // Recupero l'ultima versione del vfs da aggiornare
                                cmd.CommandText = "SELECT ifnull(max(vfsversion),-1) FROM files WHERE iduser=$iduser";
                                cmd.Parameters.AddWithValue("iduser", this.Id);
                                targetVFSVersion = (long)cmd.ExecuteScalar();
                            }

                            if (clientVFSVersion > targetVFSVersion)
                                throw new UserPullListException("La versione del VFS client specificata ("+clientVFSVersion+") è maggiore di quella target "+targetVFSVersion);

                            cmd.Parameters.Clear();
                            cmd.CommandText = "SELECT id,version,vfsversion,nchunk,name,path,lastupdate,deleted,hash,iduser,dimension "
                                                + "FROM files "
                                                + "WHERE vfsversion > $m AND vfsversion <= $n AND iduser=$iduser "
                                                + "GROUP BY id ";

                            cmd.Parameters.AddWithValue("m", clientVFSVersion);
                            cmd.Parameters.AddWithValue("n", targetVFSVersion);
                            cmd.Parameters.AddWithValue("iduser", this.Id);

                            using (var r = cmd.ExecuteReader())
                            {
                                while (r.Read())
                                {
                                    long id = r.GetInt64(0);
                                    long version = r.GetInt64(1);
                                    long vfsversion = r.GetInt64(2);
                                    long nchunk = r.GetInt64(3);
                                    string name = r.GetString(4);
                                    string path = r.GetString(5);
                                    DateTime lastupdate = DateTime.Parse(r.GetString(6));
                                    bool deleted = r.GetInt64(7) == 1 ? true : false;
                                    string hash = r.GetString(8);
                                    long iduser = r.GetInt64(9);
                                    long dimension = r.GetInt64(10);

                                    VFile v = new VFile(id, version, vfsversion, nchunk, name, path, lastupdate, deleted, hash, iduser, dimension);
                                    res.Add(v);
                                }
                            }
                            return res;
                        }

                    }
                }
                catch (Exception e)
                {
                    throw new UserPullListException(e.Message);
                }
            }
        }
Exemplo n.º 4
0
        public IEnumerable<VFile> GetArchivedFile(string path, string name)
        {
            lock (this)
            {
                List<VFile> res = new List<VFile>();
                try
                {
                    SQLiteConnection conn;
                    string dir;
                    string db = UserManager.Instance.GetUserDbPath(this.Id, out dir);
                    using (conn = new SQLiteConnection(String.Format("Data Source={0};Version=3;", db)))
                    {
                        conn.Open();
                        using (SQLiteCommand cmd = conn.CreateCommand())
                        {
                            // vado a predere l'id del file che mi interessa.
                            cmd.CommandText = "SELECT * FROM files WHERE name = $name AND path = $path";
                            cmd.Parameters.AddWithValue("name", name);
                            cmd.Parameters.AddWithValue("path", path);
                            using (SQLiteDataReader r = cmd.ExecuteReader())
                            {
                                bool found = false;
                                while (r.Read())
                                {
                                    found = true;
                                    long id = r.GetInt64(0);
                                    long version = r.GetInt64(1);
                                    long vfsversion = r.GetInt64(2);
                                    long nchunk = r.GetInt64(3);
                                    string nameF = r.GetString(4);
                                    string pathF = r.GetString(5);
                                    long dim = r.GetInt64(6);
                                    DateTime lastUpdate = DateTime.Parse(r.GetString(7));
                                    bool deleted = r.GetBoolean(8);
                                    string hash = r.GetString(9);
                                    long idUser = r.GetInt64(10);

                                    VFile vf = new VFile(id, version, vfsversion, nchunk, nameF, pathF, lastUpdate, deleted, hash, idUser, dim);
                                    res.Add(vf);

                                }

                                if (!found)
                                {
                                    throw new Exception("Si sta tentando di ottenere lo storico di un file che non esiste.");
                                }
                                return res;
                            }
                        }
                    }

                }
                catch (Exception e)
                {
                    throw new ArchivedFileException(e.Message);
                }
            }
        }
Exemplo n.º 5
0
        public void DeleteDirectory(long clientVfsVersion, string fullPath, ConnectionHandler connupdate)
        {
            if (String.IsNullOrEmpty(fullPath))
                throw new JsonRequestException("Il nome della directory non è valido.");

            // Per definizione la directory deve avere un full path terminante con slash
            if (!fullPath.EndsWith("" + Path.DirectorySeparatorChar))
                throw new JsonRequestException("Il path completo della directory deve terminare con uno slash.");

            try
            {
                lock (this)
                {
                    // Controlla che lo user sia sincronizzato.
                    SQLiteConnection conn;
                    string dir;
                    string db = UserManager.Instance.GetUserDbPath(this.Id, out dir);
                    using (conn = new SQLiteConnection(String.Format("Data Source={0};Version=3;", db)))
                    {
                        conn.Open();
                        using (SQLiteTransaction tr = conn.BeginTransaction())
                        {
                            try
                            {
                                using (SQLiteCommand cmd = conn.CreateCommand())
                                {
                                    // Controlla che lo user sia allineato in termini di versione del vfs
                                    cmd.CommandText = "SELECT ifnull(max(vfsversion),-1) FROM files WHERE iduser = $iduser";
                                    cmd.Parameters.AddWithValue("iduser", this.Id);
                                    long currver = (long)cmd.ExecuteScalar();

                                    //se non è allineato, sollevare un'eccezzione di tipo needed sync
                                    if (currver > clientVfsVersion)
                                        throw new NotSynchedException("La versione del client " + clientVfsVersion + " non corrisponde all'ultima del server " + currver + ", perciò il push di qualsiasi file è disabilitato.");

                                    cmd.Parameters.Clear();

                                    // Elimina tutte le sottodirecties.
                                    cmd.CommandText = "DELETE FROM directories WHERE path LIKE $path AND iduser = $iduser";
                                    cmd.Parameters.AddWithValue("path", fullPath + "%");
                                    cmd.Parameters.AddWithValue("iduser", this.Id);
                                    int rows = cmd.ExecuteNonQuery();

                                    // Selezionare tutti i file contenuti in tutte le sotto directory di quella che vogliamo eliminare
                                    // Per ogni file trovato inserisco un nuovo record con deleted pari a 1 e vfsversion = curver +1.

                                    cmd.Parameters.Clear();
                                    cmd.CommandText = "SELECT id, version, name, path, hash "
                                                        + "FROM files "
                                                        + "WHERE path LIKE $path AND iduser=$iduser AND deleted=0 "
                                                        + "AND version=(SELECT max(F1.version) FROM files F1 WHERE F1.id = id)";

                                    cmd.Parameters.AddWithValue("path", fullPath+"%");
                                    cmd.Parameters.AddWithValue("iduser", this.Id);
                                    List<VFile> lista = new List<VFile>();
                                    using (SQLiteDataReader r = cmd.ExecuteReader())
                                    {

                                        while (r.Read())
                                        {
                                            long id = r.GetInt64(0);
                                            long version = r.GetInt64(1);
                                            string name = r.GetString(2);
                                            string path = r.GetString(3);
                                            string hash = r.GetString(4);

                                            VFile v = new VFile(id, version+1, currver + 1, 0, name, path, DateTime.Now, true, hash, this.Id, 0);
                                            lista.Add(v);
                                        }
                                    }
                                    cmd.Parameters.Clear();
                                    cmd.CommandText = "INSERT INTO files(id,version,vfsversion,nchunk,name,path,dimension,lastupdate,deleted,hash,iduser) "
                                                        + "VALUES($id,$version,$vfsversion,$nchunk,$name,$path,$dimension,$lastupdate,$deleted,$hash,$iduser)";

                                    foreach(VFile vf in lista)
                                    {
                                        cmd.Parameters.AddWithValue("id",vf.Id);
                                        cmd.Parameters.AddWithValue("version", vf.Version);
                                        cmd.Parameters.AddWithValue("vfsversion", vf.Vfsversion);
                                        cmd.Parameters.AddWithValue("nchunk", vf.Nchunck);
                                        cmd.Parameters.AddWithValue("name", vf.Name);
                                        cmd.Parameters.AddWithValue("path", vf.Path);
                                        cmd.Parameters.AddWithValue("dimension", vf.Dim);
                                        cmd.Parameters.AddWithValue("lastupdate", vf.LastUpdate);
                                        cmd.Parameters.AddWithValue("deleted", vf.Deleted?1:0);
                                        cmd.Parameters.AddWithValue("hash", vf.Hash);
                                        cmd.Parameters.AddWithValue("iduser", vf.IdUser);

                                        if (cmd.ExecuteNonQuery() != 1) {
                                            throw new DeleteDirectoryException("Si è verificato un problema durante l'aggiornamento dei record del db per i file contenuti nelle sottodirectories.");
                                        }
                                    }

                                    tr.Commit();
                                    _nCounter++;
                                    Monitor.PulseAll(this);
                                }
                            }
                            catch (Exception e)
                            {
                                tr.Rollback();
                                throw e;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // Qualsiasi cosa accada a questo livello, incapsula in una eccezione definita e facilmente identificabile.
                throw new DeleteDirectoryException(e.Message);
            }
        }