diff --git a/wbackup/ArchiveTarFile.cs b/wbackup/ArchiveTarFile.cs index 51bbdc1..1492b38 100644 --- a/wbackup/ArchiveTarFile.cs +++ b/wbackup/ArchiveTarFile.cs @@ -8,6 +8,8 @@ using ICSharpCode.SharpZipLib.Tar; using ICSharpCode.SharpZipLib.BZip2; +using log4net; + namespace wbackup { class ArchiveTarFile : BackupStreamBase @@ -52,7 +54,7 @@ protected override void DoWork(string sourceFileName) { - Console.WriteLine("Archive {0}, {1}", Interlocked.Increment(ref archiveCount), sourceFileName); + if (log.IsInfoEnabled) log.InfoFormat("Archive {0}, {1}", Interlocked.Increment(ref archiveCount), sourceFileName); using (var fis = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read)) { diff --git a/wbackup/BackupStreamBase.cs b/wbackup/BackupStreamBase.cs index 1e2c0e6..6cfd4f3 100644 --- a/wbackup/BackupStreamBase.cs +++ b/wbackup/BackupStreamBase.cs @@ -4,10 +4,14 @@ using System.Threading; using System.Collections.Concurrent; +using log4net; + namespace wbackup { class BackupStreamBase : IDisposable { + protected ILog log; + protected bool working; protected BackupStreamBase nextStream = null; protected ConcurrentQueue sourceFileQueue = null; @@ -17,6 +21,8 @@ public BackupStreamBase(BackupStreamBase stream = null) { + log = LogManager.GetLogger(this.GetType().FullName); + nextStream = stream; sourceFileQueue = new ConcurrentQueue(); threadInstance = new Thread(this.DoWork); @@ -95,12 +101,25 @@ } } + public void Wait() + { + while (threadInstance.IsAlive) + { + Thread.Sleep(1000); + } + + if (null != nextStream) + nextStream.Wait(); + } + /// /// ソースからデータを取得して処理する /// /// public void DoWork(object data) { + if (log.IsDebugEnabled) log.Error("begin thread"); + working = true; while (true) { @@ -116,13 +135,23 @@ if (sourceFileName == "") { AddNextSource(""); + if (log.IsDebugEnabled) log.Error("detect end of queue"); break; } // ファイルの処理を行う - DoWork(sourceFileName); + try + { + DoWork(sourceFileName); + } + catch (Exception exp) + { + if (log.IsErrorEnabled) log.Error("Exception at DoWork", exp); + } } + working = false; + if (log.IsDebugEnabled) log.Error("finish thread"); } /// diff --git a/wbackup/ClearArchiveFlag.cs b/wbackup/ClearArchiveFlag.cs index 6575a67..e11b7d3 100644 --- a/wbackup/ClearArchiveFlag.cs +++ b/wbackup/ClearArchiveFlag.cs @@ -18,7 +18,7 @@ protected override void DoWork(string sourceFileName) { - Console.WriteLine("Clear {0}, {1}", Interlocked.Increment(ref archiveCount), sourceFileName); + if (log.IsDebugEnabled) log.DebugFormat("Clear {0}, {1}", Interlocked.Increment(ref archiveCount), sourceFileName); var attributes = File.GetAttributes(sourceFileName); if (attributes.HasFlag(FileAttributes.Archive)) diff --git a/wbackup/ListingAllSourceFile.cs b/wbackup/ListingAllSourceFile.cs index 16cacb2..86cd356 100644 --- a/wbackup/ListingAllSourceFile.cs +++ b/wbackup/ListingAllSourceFile.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using System.Text.RegularExpressions; +using log4net; + namespace wbackup { class ListingAllSourceFile : BackupStreamBase @@ -15,7 +17,6 @@ public ListingAllSourceFile(BackupStreamBase stream = null):base(stream) { - } /// @@ -43,11 +44,22 @@ if (false == isIgnoreFile(fileName)) { var fileAttrib = File.GetAttributes(fileName); + if (false == fileAttrib.HasFlag(FileAttributes.Hidden)) { + if (log.IsDebugEnabled) log.DebugFormat("Add File, {0}, {1:X}", fileName, fileAttrib); + File.SetAttributes(fileName, fileAttrib | FileAttributes.Archive); AddNextSource(fileName); } + else + { + if (log.IsDebugEnabled) log.DebugFormat("Not Archive File, {0}, {1:X}", fileName, fileAttrib); + } + } + else + { + if (log.IsDebugEnabled) log.DebugFormat("Ignore File, {0}", fileName); } } @@ -56,6 +68,7 @@ var dirAttrib = File.GetAttributes(directory); if (false == dirAttrib.HasFlag(FileAttributes.Hidden)) { + if (log.IsDebugEnabled) log.DebugFormat("Open Directory, {0}", directory); ListingSourceFiles(directory); } } diff --git a/wbackup/ListingArchiveSourceFile.cs b/wbackup/ListingArchiveSourceFile.cs index 6eb0775..723014f 100644 --- a/wbackup/ListingArchiveSourceFile.cs +++ b/wbackup/ListingArchiveSourceFile.cs @@ -45,9 +45,19 @@ var fileAttrib = File.GetAttributes(fileName); if (false == fileAttrib.HasFlag(FileAttributes.Hidden) && true == fileAttrib.HasFlag(FileAttributes.Archive)) { + if (log.IsDebugEnabled) log.DebugFormat("Add File, {0}, {1:X}", fileName, fileAttrib); + File.SetAttributes(fileName, fileAttrib | FileAttributes.Archive); AddNextSource(fileName); } + else + { + if (log.IsDebugEnabled) log.DebugFormat("Not Archive File, {0}, {1:X}", fileName, fileAttrib); + } + } + else + { + if (log.IsDebugEnabled) log.DebugFormat("Ignore File, {0}", fileName); } } @@ -56,6 +66,7 @@ var dirAttrib = File.GetAttributes(directory); if (false == dirAttrib.HasFlag(FileAttributes.Hidden)) { + if (log.IsDebugEnabled) log.DebugFormat("Open Directory, {0}", directory); ListingSourceFiles(directory); } } diff --git a/wbackup/Program.cs b/wbackup/Program.cs index e44832a..4976f73 100644 --- a/wbackup/Program.cs +++ b/wbackup/Program.cs @@ -9,10 +9,12 @@ using System.Security.Cryptography.X509Certificates; using System.Numerics; using System.Text; +using System.Threading.Tasks; + using ICSharpCode.SharpZipLib.GZip; using ICSharpCode.SharpZipLib.Tar; -using System.Threading.Tasks; using ICSharpCode.SharpZipLib.BZip2; +using log4net; namespace wbackup { @@ -20,29 +22,34 @@ { static void Main(string[] args) { + log4net.Config.XmlConfigurator.Configure(new FileInfo(@"config\log4net.config")); + + ILog log = LogManager.GetLogger(typeof(Program)); + if (log.IsInfoEnabled) log.Info("Application [wbackup] Start"); + CommandParser param = new CommandParser(args); + BackupStreamBase backupEngine = null; + if (param.backupMode == BackupMopde.Full) { - ListingAllSourceFile backupEngine = new ListingAllSourceFile(new ArchiveTarFile(param.destinationDir, new ClearArchiveFlag())); - backupEngine.AddSource(param.sourceDir); - backupEngine.AddSource(""); - backupEngine.Start(); + backupEngine = new ListingAllSourceFile(new ArchiveTarFile(param.destinationDir, new ClearArchiveFlag())); } if (param.backupMode == BackupMopde.Incremental) { - ListingArchiveSourceFile backupEngine = new ListingArchiveSourceFile(new ArchiveTarFile(param.destinationDir)); - backupEngine.AddSource(param.sourceDir); - backupEngine.AddSource(""); - backupEngine.Start(); + backupEngine = new ListingArchiveSourceFile(new ArchiveTarFile(param.destinationDir)); } if (param.backupMode == BackupMopde.Differencial) { - ListingArchiveSourceFile backupEngine = new ListingArchiveSourceFile(new ArchiveTarFile(param.destinationDir, new ClearArchiveFlag())); - backupEngine.AddSource(param.sourceDir); - backupEngine.AddSource(""); - backupEngine.Start(); + backupEngine = new ListingArchiveSourceFile(new ArchiveTarFile(param.destinationDir, new ClearArchiveFlag())); } + + backupEngine.AddSource(param.sourceDir); + backupEngine.AddSource(""); + backupEngine.Start(); + backupEngine.Wait(); + + if (log.IsInfoEnabled) log.Info("Application [wbackup] Stop"); } } } diff --git a/wbackup/Properties/AssemblyInfo.cs b/wbackup/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..12c7905 --- /dev/null +++ b/wbackup/Properties/AssemblyInfo.cs @@ -0,0 +1,63 @@ +#region Apache License +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to you under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +using System.Reflection; +using System.Runtime.CompilerServices; + +// +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +// + +//[assembly: AssemblyTitle("log4net - ConsoleApp")] +//[assembly: AssemblyDescription("log4net ConsoleApp")] +//[assembly: AssemblyConfiguration("")] +//[assembly: AssemblyProduct("log4net - TestApp")] +//[assembly: AssemblyCulture("")] + +// +// In order to sign your assembly you must specify a key to use. Refer to the +// Microsoft .NET Framework documentation for more information on assembly signing. +// +// Use the attributes below to control which key is used for signing. +// +// Notes: +// (*) If no key is specified, the assembly is not signed. +// (*) KeyName refers to a key that has been installed in the Crypto Service +// Provider (CSP) on your machine. KeyFile refers to a file which contains +// a key. +// (*) If the KeyFile and the KeyName values are both specified, the +// following processing occurs: +// (1) If the KeyName can be found in the CSP, that key is used. +// (2) If the KeyName does not exist and the KeyFile does exist, the key +// in the KeyFile is installed into the CSP and used. +// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. +// When specifying the KeyFile, the location of the KeyFile should be +// relative to the project output directory which is +// %Project Directory%\obj\. For example, if your KeyFile is +// located in the project directory, you would specify the AssemblyKeyFile +// attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")] +// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework +// documentation for more information on this. +// + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] +//[assembly: AssemblyKeyName("")] diff --git a/wbackup/config/log4net.config b/wbackup/config/log4net.config new file mode 100644 index 0000000..dbbb39b --- /dev/null +++ b/wbackup/config/log4net.config @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + +
+