//--- constructor ----------------------- public Archive( ActionDelegate action, Ctrl ctrl, bool debug ) { this.action = action; this.ctrl = ctrl; this.debug = debug; this.start = DateTime.Now; this.log = new StreamWriter( PathCombine( this.ctrl.Path, DateFolderSecond( start ) + ".log" ), false, Encoding.ASCII ); LogInfo( "---------------" ); LogInfo( Status( "Archive", this.ctrl.Path ) ); }
//--- constructor ----------------------- public FormEdit( Ctrl ctrl ) { this.ctrl = ctrl; InitializeComponent(); }
//--- method ---------------------------- public static Ctrl Import( string filename ) { try { filename = System.IO.Path.GetFullPath( filename ); if( ! File.Exists( filename ) ) { throw new FileNotFoundException( "Import control file", filename ); } using( XmlReader r = XmlReader.Create( filename ) ) { Ctrl c; switch( r.IsStartElement() ? r.NamespaceURI : string.Empty ) { case Ctrl.XmlNamespace: { c = (Ctrl)(new XmlSerializer( typeof(Ctrl) )).Deserialize( r ); c.Filename = filename; c.version = Revision.current; break; } case v1.Ctrl.XmlNamespace: { v1.Ctrl c1 = (v1.Ctrl)(new XmlSerializer( typeof(v1.Ctrl) )).Deserialize( r ); c = c1.Upgrade( filename ); c.version = Revision.v1; break; } default: { c = new Ctrl(); c.Filename = filename; c.version = Revision.current; break; } } return c; } } catch( Exception ex ) { ex.Data[ExFilename] = filename; throw; } }
private void MenuNew_Click( object sender, EventArgs e ) { try { string ext = System.IO.Path.GetExtension( Ctrl.ArchiveFilename ); SaveFileDialog f = new SaveFileDialog(); f.AddExtension = true; f.CheckPathExists = true; f.CreatePrompt = false; f.DefaultExt = ext; f.FileName = Ctrl.ArchiveFilename; f.Filter = "control files (*" + ext + ")|*" + ext; f.FilterIndex = 1; f.InitialDirectory = System.Environment.GetFolderPath( System.Environment.SpecialFolder.MyDocuments ); f.OverwritePrompt = true; f.RestoreDirectory = true; f.Title = "Where should the new control file be saved?"; if( f.ShowDialog() == DialogResult.OK ) { Ctrl ctrl = new Ctrl(); ctrl.Filename = Filename = f.FileName; ctrl.Export(); } } catch( Exception ex ) { Msg( ex ); } }
//--- backup ---------------------------- public void Process( Operation operation, Ctrl ctrl, bool debug ) { try { this.ctrl = ctrl; this.debug = debug; this.logging = new BlockingCollection<string>( Maximum_Items_In_Queue ); this.status = new BackupStatus(); this.cancel = new CancellationTokenSource(); pause.Set(); this.start = DateTime.Now; this.queue = new BlockingCollection<UpdateRequest>( Maximum_Items_In_Queue ); this.current = null; this.history = null; this.log = new Thread( new ThreadStart( LogHandler ) ); this.log.Name = "Log"; this.log.Priority = ThreadPriority.BelowNormal; this.log.Start(); switch( operation ) { case Operation.Merge: { this.merge = new Thread( new ThreadStart( MergeHandler ) ); this.merge.Name = "Update"; this.merge.Priority = ThreadPriority.BelowNormal; this.merge.Start(); break; } case Operation.Backup: { this.scan = new Thread( new ThreadStart( ScanHandler ) ); this.scan .Name = "Scan"; this.scan .Priority = ThreadPriority.BelowNormal; this.scan .Start(); this.update = new Thread( new ThreadStart( UpdateHandler ) ); this.update.Name = "Update"; this.update.Priority = ThreadPriority.BelowNormal; this.update.Start(); break; } } } catch( Exception ex ) { Msg( "Process: {0}", Except.ToString( ex, debug ) ); } }
string MergeMinHistoryFolder( string last, Ctrl.DateFolderLevel level ) { CtrlArchive archive = ctrl.Archive; if( (archive != null) && (last.Length >= (int)level) ) { DateTime dt = Ctrl.ParseDateFolder( last, level ); switch( level ) { case Ctrl.DateFolderLevel.Month : if( archive.Month > 0 ) { return Ctrl.DateFolder( dt.AddMonths ( 1 - archive.Month ), level ); } break; case Ctrl.DateFolderLevel.Day : if( archive.Day > 0 ) { return Ctrl.DateFolder( dt.AddDays ( 1 - archive.Day ), level ); } break; case Ctrl.DateFolderLevel.Hour : if( archive.Hour > 0 ) { return Ctrl.DateFolder( dt.AddHours ( 1 - archive.Hour ), level ); } break; case Ctrl.DateFolderLevel.Minute : if( archive.Minute > 0 ) { return Ctrl.DateFolder( dt.AddMinutes ( 1 - archive.Minute ), level ); } break; } } return null; }
void Merge( Ctrl.DateFolderLevel level ) { if( IsCancelRequested ) { throw new Exception( "Merge cancelled." ); } Msg( "Level: {0}", level ); string[] a = ctrl.HistoryFolders(); if( a.Length > 0 ) { string s = MergeMinHistoryFolder( a[a.Length - 1], level ); if( ! string.IsNullOrEmpty( s ) ) { //..merge directories int i = a.Length; //..skip all folders being kept do { --i; } while( (i > 0) && (string.Compare( a[i], s, true ) >= 0 ) ); //..scan remaining folders for potential merges and renames int z; switch( level ) { case Ctrl.DateFolderLevel.Month : z = (int)Ctrl.DateFolderLevel.Year ; break; case Ctrl.DateFolderLevel.Day : z = (int)Ctrl.DateFolderLevel.Month ; break; case Ctrl.DateFolderLevel.Hour : z = (int)Ctrl.DateFolderLevel.Day ; break; case Ctrl.DateFolderLevel.Minute: z = (int)Ctrl.DateFolderLevel.Hour ; break; default: z = (int)Ctrl.DateFolderLevel.Second; break; } while( i >= 0 ) { int j = i; bool b = true; while( (--i >= 0) && (string.Compare( a[i], 0, a[j], 0, z, true ) == 0) ) { if( b ) { b = false; Log( " {0} - {1}", level, a[j] ); } Log( " ..remove/merge {0}", a[i] ); Merge( a[i], a[j] ); } //rename any folder where the name is longer than it needs for uniquness if( a[j].Length > z ) { if( b ) { b = false; Log( " {0} - {1}", level, a[j] ); } string t = a[j].Substring( 0, z ); Log( " ..rename to {0}", t ); DirectoryMove( ctrl.HistoryFullPath( a[j] ), ctrl.HistoryFullPath( t ) ); } } } } }