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; } }
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"); }