Skip to content

Commit

Permalink
fixed logging
Browse files Browse the repository at this point in the history
  • Loading branch information
asdfjkl committed Jan 3, 2020
1 parent ee3b052 commit a4e086b
Showing 1 changed file with 30 additions and 140 deletions.
170 changes: 30 additions & 140 deletions yahb/CopyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,23 @@ public List<String> createDirectoryList() {
{
if(cfg.verboseMode)
{
cfg.addToLog("ERR:" + currentDir + ": skipping");
cfg.addToLog(currentDir + ": skipping");
}
}
}
catch (UnauthorizedAccessException e)
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + currentDir + ":" + e.Message);
}
cfg.addToLog("ERR:" + currentDir + ":" + e.Message);
continue;
}
catch (DirectoryNotFoundException e)
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + currentDir + ":" + e.Message);
}
cfg.addToLog("ERR:" + currentDir + ":" + e.Message);
continue;
}
catch (PathTooLongException e)
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + currentDir + ":" + e.Message);
}
cfg.addToLog("ERR:" + currentDir + ":" + e.Message);
continue;
}
foreach (string str in subdirs)
Expand Down Expand Up @@ -142,7 +133,7 @@ public List<String> createDirectoryList() {
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + dir_i + ": skipping");
cfg.addToLog(dir_i + ": skipping");
}
}
}
Expand All @@ -152,7 +143,7 @@ public List<String> createDirectoryList() {

public void createFileList(List<String> inputDirList) {

cfg.addToLog("create list of files ... ");
cfg.addToLog("creating list of files ... ");

foreach (string dir_i in inputDirList)
{
Expand All @@ -163,10 +154,7 @@ public void createFileList(List<String> inputDirList) {
}
catch (System.IO.PathTooLongException e)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + dir_i + ": " + e.Message);
}
this.cfg.addToLog("ERR:" + dir_i + ": " + e.Message);
continue;
}

Expand Down Expand Up @@ -226,16 +214,13 @@ public void createFileList(List<String> inputDirList) {
this.sourceFileList.Add(file_i);
} catch (System.IO.PathTooLongException e)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + file_i + ": " + e.Message);
}
this.cfg.addToLog("ERR:" + file_i + ": " + e.Message);
}
} else
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + file_i + ": skipping");
cfg.addToLog(file_i + ": skipping");
}
}
}
Expand All @@ -253,7 +238,7 @@ public void createFileList(List<String> inputDirList) {
{
Console.WriteLine(df.driveTimeFilename);
}*/
cfg.addToLog("create list of files ... DONE");
cfg.addToLog("creating list of files ... DONE");
}

public string getLastDir(string destDirectory)
Expand Down Expand Up @@ -289,26 +274,17 @@ public string getLastDir(string destDirectory)
}
catch (UnauthorizedAccessException e)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDirectory + ": " + e.Message);
}
this.cfg.addToLog("ERR:" + destDirectory + ": " + e.Message);
throw new ArgumentException();
}
catch (DirectoryNotFoundException e)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDirectory + ": " + e.Message);
}
this.cfg.addToLog("ERR:" + destDirectory + ": " + e.Message);
throw new ArgumentException();
}
catch (PathTooLongException e)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDirectory + ": " + e.Message);
}
this.cfg.addToLog("ERR:" + destDirectory + ": " + e.Message);
throw new ArgumentException();
}
}
Expand Down Expand Up @@ -348,50 +324,32 @@ public void doCopy()
}
catch (DirectoryNotFoundException ex)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
}
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
continue;
}
catch (NotSupportedException ex)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
}
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
continue;
}
catch (PathTooLongException ex)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
}
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
continue;
}
catch (UnauthorizedAccessException ex)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
}
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
continue;
}
catch (ArgumentException ex)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
}
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
continue;
}
catch (IOException ex)
{
if (cfg.verboseMode)
{
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
}
this.cfg.addToLog("ERR:" + destDir + ": " + ex.Message);
continue;
}
}
Expand Down Expand Up @@ -462,11 +420,17 @@ public void doCopy()
fi_dest.CreationTime = fi_source.CreationTime;
fi_dest.LastWriteTime = fi_source.LastWriteTime;
fi_dest.LastAccessTime = fi_source.LastAccessTime;
//cfg.addToLog(x.sourceFile + ": file copied");
if (cfg.verboseMode)
{
cfg.addToLog(x.sourceFile + ": file copied");
}
}
} else
{
//cfg.addToLog(x.sourceFile + ": file copied");
if (cfg.verboseMode)
{
cfg.addToLog(x.sourceFile + ": file copied");
}
}
}
catch (IOException copyError)
Expand All @@ -476,26 +440,17 @@ public void doCopy()
int errorCode = Marshal.GetHRForException(copyError) & ((1 << 16) - 1);
if (errorCode == ERROR_SHARING_VIOLATION || errorCode == ERROR_LOCK_VIOLATION)
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + x.sourceFile + ": sharing or lock violation, trying later w/ shadow copy");
}
cfg.addToLog("ERR:" + x.sourceFile + ": sharing or lock violation, trying later w/ shadow copy");
Tuple<String, DestinationFile> tpl = new Tuple<String, DestinationFile>(x.sourceFile, x.destFile);
tryWithVSS.Add(tpl);
} else
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + x.sourceFile + ": " + copyError.Message);
}
cfg.addToLog("ERR:" + x.sourceFile + ": " + copyError.Message);
}

} else
{
if (cfg.verboseMode)
{
cfg.addToLog("ERR:" + x.sourceFile + ": " + copyError.Message);
}
cfg.addToLog("ERR:" + x.sourceFile + ": " + copyError.Message);
}
}
counter += 1;
Expand Down Expand Up @@ -557,28 +512,6 @@ public void doCopy()
}
}
}

/*
// Initialize the shadow copy subsystem.
using (VssBackup vss = new VssBackup())
{
vss.Setup(Path.GetPathRoot(x.sourceFile));
string snap_path = vss.GetSnapshotPath(x.sourceFile);
cfg.addToLog(snap_path);
//string destpath = "F:\\201912232238\\c__\\Users\\user\\MyFiles\\workspace\\filelock\\foo.txt";
string a = "F:\\" + x.destFile.timestamp + "\\" + x.destFile.fileName;
cfg.addToLog(a);
Alphaleonis.Win32.Filesystem.File.Copy(snap_path, a);
//File.Copy(snap_path, x.destFile.driveTimeFilename);
cfg.addToLog(x.sourceFile + ": copied snapshot via VSS");
}*/

this.cfg.addToLog("-----------------------------------------------------------------");
}

Expand Down Expand Up @@ -640,48 +573,5 @@ static extern bool CreateHardLink(
IntPtr lpSecurityAttributes
);


/*
* better: use File.Copy, check for IOException and then for ERROR_SHARING_VIOLATION
*
private bool IsFileLocked(FileInfo file)
{
try
{
using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
{
stream.Close();
}
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
//file is not locked
return false;
} */

/*
*
*
*
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode )]
static extern bool CreateHardLink(
string lpFileName,
string lpExistingFileName,
IntPtr lpSecurityAttributes
);
Usage:
CreateHardLink(newLinkPath,sourcePath, IntPtr.Zero);
*/

}
}

0 comments on commit a4e086b

Please sign in to comment.