Esempio n. 1
0
        /// <summary>
        /// Funzione Statica per la deserializzazione di un oggetto FileAttr da uno Stream
        /// </summary>
        /// <param name="stream">Stream da cui deserializzare</param>
        /// <returns></returns>
        public static FileAttr deserializeFrom(Stream stream)
        {
            XmlSerializer ser = new XmlSerializer(typeof(FileAttr));
            FileAttr      f   = null;

            try
            {
                f = (FileAttr)ser.Deserialize(stream);
            }
            catch (Exception)
            {
                return(null);
            }

            return(f);
        }
Esempio n. 2
0
        /// <summary>
        /// Metodo che Si occupa di ricevere effettivamente il file dallo stream indicato
        /// </summary>
        /// <param name="f">Attributi del file da ricevere</param>
        /// <returns>True se Ricevuto Correttamente</returns>
        private bool recvFileByte(FileAttr f)
        {
            if (f == null)
            {
                return(false);
            }

            if (stateWrite != null)
            {
                String state = "Ripristino: " + f.path;
                if (state.Length > 124)
                {
                    state  = state.Substring(0, 120);
                    state += "...";
                }
                stateWrite(state);
            }

            DirectoryInfo d = Directory.CreateDirectory(f.directory);

            if (!d.Exists)
            {
                return(false);
            }

            Stream file = File.Open(f.path, FileMode.Create);
            int    tot = 0, rec = 0;
            long   diff;

            Byte[] bu = new Byte[1024];

            try
            {
                int i = 0;
                while (tot != f.size && !_shouldStop)
                {
                    try
                    {
                        if ((diff = f.size - tot) < 1024)
                        {
                            rec = netStream.Read(bu, 0, (int)diff);
                        }
                        else
                        {
                            rec = netStream.Read(bu, 0, 1024);
                        }
                        if (rec <= 0)
                        {
                            return(false);
                        }
                        tot += rec;
                        file.Write(bu, 0, rec);
                    }
                    catch
                    {
                        if (!sock.Connected)
                        {
                            return(false);
                        }
                        i++;
                        if (i >= 50)
                        {
                            i++;
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }
            finally
            {
                file.Close();
            }

            if (_shouldStop && f.size != tot)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Metodo eseguito dai Thread che scansionano i file e creano le liste corrette
        /// </summary>
        private void scanningThread()
        {
            //Fino a quando non devo fermarmi
            while (!_shouldStop)
            {
                String path;
                Stream stream = null;

                //Se la Coda è bloccata e vuota ritorno
                if (!_files.tasks.dequeue(out path))
                {
                    break;
                }

                try
                {
                    //Prelevo le info del file
                    FileInfo f = new FileInfo(path);

                    //Calcolo il Checksum
                    stream = f.Open(FileMode.Open);
                    MD5      checksum = MD5.Create();
                    Byte[]   ck       = checksum.ComputeHash(stream);
                    FileAttr attr     = null;

                    //Controllo se il file esiste già
                    if (_files.serverFiles.ContainsKey(path))
                    {
                        //Se è nei file del server, e se il checksum e diverso lo aggiungo ai file di cui fare update
                        if (_files.serverFiles.TryRemove(path, out attr))
                        {
                            if (!attr.checksum.Equals(Convert.ToBase64String(ck)))
                            {
                                _files.updFiles.add(new FileAttr(path, f.DirectoryName, Convert.ToBase64String(ck), f.Length));
                            }
                        }
                    }
                    else
                    {
                        //File non presente sul Server, lo aggiungo ai nuovi
                        _files.newFiles.add(new FileAttr(path, f.DirectoryName, Convert.ToBase64String(ck), f.Length));
                    }
                }
                catch (SecurityException)
                {
                    _files.sendErr("Non hai i permessi necessari per accedere al file " + path);
                    FileAttr rem;
                    _files.serverFiles.TryRemove(path, out rem);
                }
                catch (UnauthorizedAccessException)
                {
                    _files.sendErr("Accesso al file " + path + " Negato!");
                    FileAttr rem;
                    _files.serverFiles.TryRemove(path, out rem);
                }
                catch (IOException)
                {
                    //_files.sendErr("Impossibile Sincronizzare il file " + path + " perchè in uso in questo momento!");
                    FileAttr rem;
                    _files.serverFiles.TryRemove(path, out rem);
                }
                catch (Exception e)
                {
                    _files.sendErrFatal("A causa di un errore imprevisto è impossibile effettuare la scansione dei file in questo momento." + e.ToString());
                    _shouldStop = true;
                    return;
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
            }
        }