private void lDeletedFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e) { DependencyObject obj = (DependencyObject)e.OriginalSource; while (obj != null && obj != lDeletedFiles) { if (obj.GetType() == typeof(System.Windows.Controls.ListViewItem)) { string deletedFileName = ((VersionListViewItem)lDeletedFiles.SelectedItem).sFilename; string deletedFilePath = ((VersionListViewItem)lDeletedFiles.SelectedItem).sPath; FileUtenteList fileUtenteList = FileUtenteList.getInstance(); FileUtente[] deletedFileList = fileUtenteList.Deleted; foreach (FileUtente fu in deletedFileList) { if (fu.Nome == deletedFileName && fu.Path == deletedFilePath) { deletedFileUtente = fu; break; } } lDeletedFileVersions.Items.Clear(); foreach (DateTime fv in deletedFileUtente.Items) { lDeletedFileVersions.Items.Add(new FileVersionListViewItem(fv)); } break; } obj = VisualTreeHelper.GetParent(obj); } }
static public FileUtenteList getInstance() { if (_instance == null) { _instance = new FileUtenteList(); } _instance.__file_list = new FileUtente[_instance.__list_ids_files.Count]; _instance.__deleted_list = new FileUtente[_instance.__list_deleted_ids.Count]; return(_instance); }
private void GetDelFiles() { try { lDeletedFiles.Items.Clear(); lDeletedFileVersions.Items.Clear(); FileUtenteList fileUtenteList = FileUtenteList.getInstance(); foreach (FileUtente fu in fileUtenteList.Deleted) { lDeletedFiles.Items.Add(new VersionListViewItem(fu.Nome, fu.Path, fu.TempoModifica)); } lDeletedFiles.SelectedIndex = 0; } catch (Exception ex) { updateStatus(ex.Message); } }
private void GetFiles() { try { lDetails.Items.Clear(); lFileVersions.Items.Clear(); FileUtenteList list = FileUtenteList.getInstance(); for (int i = 0; i < list.Length; i++) { lDetails.Items.Add(new VersionListViewItem(list[i].Nome, list[i].Path, list[i].TempoModifica)); } lDetails.SelectedIndex = 0; } catch (Exception ex) { updateStatus(ex.Message); } }
public static void Check() { TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time"); if (!Directory.Exists(base_path)) { return; } if (Monitor.TryEnter(syncLock)) { try { List <string[]> files = FileUtenteList.exploreFileSystem(base_path); List <FileUtente> toBeDeleted = new List <FileUtente>(); FileUtenteList list = FileUtenteList.getInstance(); string[] entry = new string[2]; string path_completo; Command c; CultureInfo it = new CultureInfo("it-IT"); Thread.CurrentThread.CurrentCulture = it; if (!init) { throw new ClientException("La classe per il controllo delle modifiche non è inizializzata correttamente.", ClientErrorCode.ControlloNonInizializzato); } if (!Command.Logged) { ComandoLogin login = new ComandoLogin(user, pwd); login.esegui(); } FileInfo finfo; foreach (FileUtente fu in list) { //Check if still exists, and if its modified entry[0] = fu.Nome; entry[1] = fu.Path; int index; if ((index = files.FindIndex(fTest => (fTest[0] == entry[0] && fTest[1] == entry[1]))) >= 0) { files.RemoveAt(index); //Il file selezionato esiste ancora... path_completo = base_path + entry[1] + Path.DirectorySeparatorChar + entry[0]; finfo = new FileInfo(path_completo); DateTime dt = TimeZoneInfo.ConvertTime(finfo.LastWriteTime, TimeZoneInfo.Local, tz); DateTime tMod = DateTime.SpecifyKind(dt, DateTimeKind.Utc); tMod = tMod.AddTicks(-(tMod.Ticks % TimeSpan.TicksPerSecond)); if (DateTime.Compare(tMod, fu.TempoModifica) != 0) { FileStream fs = File.Open(path_completo, FileMode.Open); string new_sha = FileUtente.CalcolaSHA256(fs); if (new_sha != fu.SHA256Contenuto) { fu.aggiornaDati((int)finfo.Length, finfo.LastWriteTime); c = new ComandoAggiornaContenutoFile(entry[0], entry[1], (int)finfo.Length, finfo.LastWriteTime, fu.SHA256Contenuto); c.esegui(); } } } else { toBeDeleted.Add(fu); } } //Cancelliamo foreach (FileUtente _fu in toBeDeleted) { list.Delete(_fu.Id); c = new ComandoEliminaFile(_fu.Nome, _fu.Path); c.esegui(); } //file nuovi FileUtente fu2; foreach (string[] n_file in files) { string file_path_completo = base_path + n_file[1] + Path.DirectorySeparatorChar + n_file[0]; finfo = new FileInfo(file_path_completo); DateTime dt = TimeZoneInfo.ConvertTime(finfo.LastWriteTime, TimeZoneInfo.Local, tz); DateTime tMod = DateTime.SpecifyKind(dt, DateTimeKind.Utc); tMod = tMod.AddTicks(-(tMod.Ticks % TimeSpan.TicksPerSecond)); dt = TimeZoneInfo.ConvertTime(finfo.CreationTime, TimeZoneInfo.Local, tz); DateTime tCre = DateTime.SpecifyKind(dt, DateTimeKind.Utc); tCre = tCre.AddTicks(-(tCre.Ticks % TimeSpan.TicksPerSecond)); FileStream fs = File.Open(file_path_completo, FileMode.Open); string new_sha = FileUtente.CalcolaSHA256(fs); fu2 = list.CreaNuovo(n_file[0], n_file[1], tCre, tMod, (int)finfo.Length, new_sha); c = new ComandoNuovoFile(n_file[0], n_file[1], (int)finfo.Length, tCre, tMod, new_sha); c.esegui(); } } finally { if (Monitor.IsEntered(syncLock)) { Monitor.Exit(syncLock); } } } }
/// <summary> /// In ordine questa funzione: /// - Cancella il contenuto della cartella base_path /// - Cancella il database e lo ricrea con le tabelle vuote /// - Scarica la lista dei path relativi /// - Per ogni path, ricrea le cartelle e scarica i file con le loro versioni /// </summary> public static void RestoreAsLastStatusOnServer() { bool tmp = checker.Enabled; checker.Enabled = false; Log l = Log.getLog(); lock (syncLock) { //Puliamo la cartella... if (Directory.Exists(base_path)) { Directory.Delete(base_path, recursive: true); } try { DB_Table.RebuildDB(); Directory.CreateDirectory(base_path); ComandoListFolders c = new ComandoListFolders(); c.esegui(); FileUtenteList flist = FileUtenteList.getInstance(); foreach (string path_rel in c.Paths) { string[] directories = path_rel.Split('\\'); string tmp_path = base_path; foreach (string dir in directories) { tmp_path += dir + "\\"; if (!Directory.Exists(tmp_path)) { Directory.CreateDirectory(tmp_path); } } ComandoListDir c2 = new ComandoListDir(path_rel); c2.esegui(); foreach (string nome_file in c2.FileNames) { ComandoListVersions c_vers = new ComandoListVersions(nome_file, path_rel); c_vers.esegui(); DateTime[] versions = c_vers.Versions; DateTime last_vers = versions.Max(); ComandoScaricaFile c_scarica = new ComandoScaricaFile(nome_file, path_rel, last_vers); c_scarica.esegui(); FileUtente fu = flist.CreaNuovo(nome_file, path_rel, last_vers, last_vers, c_scarica.Dim, c_scarica.SHAContenuto); foreach (DateTime dt in versions) { if (dt != last_vers) { fu.AggiungiVersione(dt); } } } } } catch (ServerException e) { l.log(e.Message); throw; } checker.Enabled = tmp; } }
/// <summary> /// Carica la versione richiesta del file all'utente che lo richiede. /// </summary> /// <param name="s"></param> /// <param name="dati"> /// [0]: nome_file /// [1]: path relativo /// [2]: timestamp versione (in Ticks) /// </param> /// <returns> /// OK intermedio /// token /// dimensione file /// sha contenuto /// </returns> public override bool esegui() { StringBuilder sb = new StringBuilder(); sb.Append(nome_comando).Append(Environment.NewLine). Append(nome_file).Append(Environment.NewLine). Append(path).Append(Environment.NewLine). Append(t_creazione.Ticks).Append(Environment.NewLine). Append(Environment.NewLine); sendData(sb.ToString()); //control_stream_writer.Write(sb.ToString()); //control_stream_writer.Flush(); string response = null; var respEnumerator = getResponses().GetEnumerator(); respEnumerator.MoveNext(); response = respEnumerator.Current; if (response == null) { Monitor.Exit(sharedLock); return(false); } try { string token = response; if (!respEnumerator.MoveNext() || respEnumerator.Current == null) { Monitor.Exit(sharedLock); return(false); } this.dim = Int32.Parse(respEnumerator.Current); if (!respEnumerator.MoveNext() || respEnumerator.Current == null) { Monitor.Exit(sharedLock); return(false); } this.sha_contenuto = respEnumerator.Current; data_stream = CollegamentoDati.getCollegamentoDati(token); respEnumerator.MoveNext(); } catch { Monitor.Exit(sharedLock); error_message = Properties.Messaggi.collegamentoDati; error_code = ServerErrorCode.CollegamentoDatiNonDisponibile; Connected = false; return(false); } byte[] buffer = new byte[1024]; int size = 1024; int tot_read = 0; try { while ((size = data_stream.Read(buffer, 0, size)) != 0) { //Scrivo su un file temporaneo. Se tutto va bene sostituisco quello presente //nella cartella dell'utente tot_read += size; tmp_file.Write(buffer, 0, size); tmp_file.Flush(); } } catch { Monitor.Exit(sharedLock); error_message = Properties.Messaggi.erroreConnessioneServer; error_code = ServerErrorCode.ConnessioneInterrotta; Connected = false; return(false); } finally { tmp_file.Close(); data_stream.Close(); } if (tot_read != this.dim) { error_message = Properties.Messaggi.datiInconsistenti; error_code = ServerErrorCode.DatiInconsistenti; Monitor.Exit(sharedLock); return(false); } SHA256 sha_obj = SHA256Managed.Create(); byte[] hash_val; tmp_file = File.Open(tmp_path, FileMode.Open); hash_val = sha_obj.ComputeHash(tmp_file); tmp_file.Close(); StringBuilder hex = new StringBuilder(hash_val.Length * 2); foreach (byte b in hash_val) { hex.AppendFormat("{0:x2}", b); } string sha_reale = hex.ToString(); if (sha_reale != sha_contenuto.Trim()) { error_message = Properties.Messaggi.datiInconsistenti; error_code = ServerErrorCode.DatiInconsistenti; Monitor.Exit(sharedLock); return(false); } if (!respEnumerator.MoveNext() || respEnumerator.Current == null) { Monitor.Exit(sharedLock); return(false); } while (respEnumerator.MoveNext()) { ; } Monitor.Exit(sharedLock); FileUtenteList list = FileUtenteList.getInstance(); //vado a vedere il flag di validità sul db //se è TRUE -> è una retrive di un file esistente //se è FALSE -> è una retrive di un file cancellato if (list.getValidity(this.nome_file, this.path) == true) { list[this.nome_file, this.path].aggiornaDatiPrec(this.dim, this.t_creazione, this.SHAContenuto); } else { FileUtente[] deleted = list.Deleted; foreach (FileUtente fu in deleted) { if (fu.Nome == this.nome_file && fu.Path == this.path) { list.Ripristina(fu.Id); fu.aggiornaDatiPrec(this.dim, this.t_creazione, this.SHAContenuto); break; } } } try { // Ensure that the target does not exist. if (File.Exists(path_completo)) { File.Delete(path_completo); } // Move the file. File.Move(tmp_path, path_completo); } catch (Exception e) { error_message = "Errore nel sostituire la versione precedente. " + e.Message; error_code = ServerErrorCode.Unknown; return(false); } return(true); }