private void Init() { try { int cur = 0; string firstDestFileFullPath = String.Empty; foreach (DestinationType d in this.destinationInfo) { // need to keep track on the instance count cur++; // now populate the copy operation VRASCopyOperationHelper helper = new VRASCopyOperationHelper(); // figure out the source if (this.copyFromFirstDestinationFolder & cur > 1) { // was made the first run helper.SourceFullString = firstDestFileFullPath; } else { helper.SourceFullString = this.sourceFile.FullName; } // figure out the destination helper.DestInfo = this.DestinationFileGenerator(d.DestinationLocation, this.sourceFile.DirectoryName, this.sourceFile.FullName, this.sourceRootFolder, this.sourceFile.Name, d.ArchiveSettings, this.batchSetName, this.defaultRegExString); // assign this incase this is to be the new source for the copy. For the next run through the destination lists if (cur == 1) { firstDestFileFullPath = helper.DestInfo.DestFullString; } // need to hash compare? helper.HashCompare = d.HashCompareFlag; this.filesToCopy.Add(helper); // Delete all in process file in destination folder foreach (string file in Directory.GetFiles(d.DestinationLocation, "*" + this.tempFileExt)) { try { File.Delete(file); } catch { // Swallow exceptions if we cannot delete it. } } } } catch (Exception ex) { VRASLogEvent.LogMesage( VRASLogEvent.EventLogName, "Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, Convert.ToInt32(VRAS.VRASLogEvent.EventIDs.FileCopyError), VRASLogEvent.EventSourceDefault); } }
private bool CopyFile(VRASCopyOperationHelper helper) { bool success = false; int retries = 0; string tempFile = helper.DestInfo.DestFullString + this.tempFileExt; // Try to copy the file N times acording to the config try { // make sure the file is still there if (!File.Exists(helper.SourceFullString)) { this.copyErrorOccured = true; throw new Exception(String.Format("Discovered File: {0} is missing before copy operations", helper.SourceFullString)); } FileInfo source = new FileInfo(helper.SourceFullString); // attempt to copy the file N times while (retries <= this.copyRetries && !success) { try { // make sure the dir is there if (!Directory.Exists(helper.DestInfo.DestPathOnly)) { Directory.CreateDirectory(helper.DestInfo.DestPathOnly); } if (File.Exists(helper.DestInfo.DestFullString)) { if (CryptoHashCompare(helper.SourceFullString, helper.DestInfo.DestFullString)) { // Same file success = true; break; } else { // Overwrite the file so delete it first File.Delete(helper.DestInfo.DestFullString); } } // copy the file if the file is still there if (File.Exists(helper.SourceFullString)) { source.CopyTo(tempFile, true); } // Move the temp file to final destination if (File.Exists(tempFile)) { File.Move(tempFile, helper.DestInfo.DestFullString); } else { throw new Exception("Move file Failed for " + tempFile + " and " + helper.DestInfo.DestFullString + " in CopySet: " + this.batchSetName); } // does a hash compare need to be done? if (helper.HashCompare) { if (!CryptoHashCompare(helper.SourceFullString, helper.DestInfo.DestFullString)) { throw new Exception("Backup HASH Validation Failed for " + helper.SourceFullString + " and " + helper.DestInfo.DestFullString + " in CopySet: " + this.batchSetName); } } success = true; } catch (Exception ex) { if (retries >= this.copyRetries) { this.copyErrorOccured = true; throw new Exception(String.Format("Error with copy operation: {0}. Source: {1}. Destination: {2}.", ex.Message, source.FullName, helper.DestInfo.DestFullString)); } else { // sleep before retry retries++; Thread.Sleep(this.copyRetrySleepMs); } } } } catch { try { // attempt to clean the file off if we failed in the copy or has validation. if (File.Exists(helper.DestInfo.DestFullString)) { File.Delete(helper.DestInfo.DestFullString); } this.copyErrorOccured = true; this.errorString = String.Format("In Error condition. Needed to remove the destintion file {0}", helper.DestInfo.DestFullString); } catch { } // still want to throw the exception throw; } finally { try { // Cleanup temp file on best efforts if (File.Exists(tempFile)) { File.Delete(tempFile); } } catch { } } return(success); }