private List <FileCopyInfo> GetFilesToCopy(string sourceFolder, string destinationFolder, string[] subfolders, ref int fileCountToCopy, ref long bytesToCopy) { List <FileCopyInfo> filesToCopy = new List <FileCopyInfo>(); foreach (var subfolderName in subfolders) { List <string> allFiles = DirSearch(Path.Combine(sourceFolder, subfolderName), false); foreach (string oldFilePath in allFiles.Where(x => !IsIgnored(x))) { string newFilePath = oldFilePath.Replace(sourceFolder, destinationFolder); var newFileInfo = new Delimon.Win32.IO.FileInfo(newFilePath); var oldFileInfo = new Delimon.Win32.IO.FileInfo(oldFilePath); if (!newFileInfo.Exists || oldFileInfo.LastWriteTime > newFileInfo.LastWriteTime) { fileCountToCopy++; bytesToCopy += oldFileInfo.Length; filesToCopy.Add(new FileCopyInfo { SourcePath = oldFilePath, DestinationPath = newFilePath, FileSizeBytes = oldFileInfo.Length }); } } } return(filesToCopy); }
/// <summary> /// Checks whether file is locked by the operating system or another process. Also if you have permission to the file. /// </summary> /// <param name="file"></param> /// <returns></returns> public static bool IsFileLocked(Delimon.Win32.IO.FileInfo file) { FileStream stream = null; try { file.IsReadOnly = false; stream = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); } catch (Exception) { //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); } finally { try { if (stream != null) { stream.Close(); stream.Dispose(); } } catch (Exception) { } } //file is not locked return(false); }
/// <summary> /// Cores the content of the process file. /// </summary> /// <param name="filePath">The file path.</param> /// <returns></returns> private bool ContainsProhibitedFileContent( Delimon.Win32.IO.FileInfo filePath, HeaderSignature[] sigs, bool ignoreExtension) { WriteError(string.Format( @"Audit Folder ContentDetectorEngine: Processing content of file '{0}' ({1:0,0} bytes).", filePath.FullName, filePath.Length), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true); SingleFileContentProcessor processor = new SingleFileContentProcessor(filePath); bool result = processor.ContainsProhibitedContent(ignoreExtension, sigs); if (result) { WriteError( string.Format( @"Audit Folder ContentDetectorEngine: Detected PROHIBITED content in file '{0}' ({1:0,0} bytes).", filePath.FullName, filePath.Length), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true); } else { WriteError( string.Format( @"Audit Folder ContentDetectorEngine: Detected NO prohibited content in file '{0}' ({1:0,0} bytes).", filePath.FullName, filePath.Length), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true); } return(result); }
/// <summary> /// Refresh FilInfo file attributes. Windows 7 doesn't update attributes actively. /// </summary> /// <param name="file"></param> /// <returns></returns> public static Delimon.Win32.IO.FileInfo RefreshFileInfo(Delimon.Win32.IO.FileInfo file) { FileStream fs = null; try { fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); fs.ReadByte(); fs.Close(); fs.Dispose(); fs = null; file.Refresh(); } catch (Exception) { } finally { try { if (fs != null) { fs.Close(); fs.Dispose(); } } catch (Exception) { } } return(file); }
private List <string> GetFilesToDelete(string sourceFolder, string destinationFolder, string[] subfolders, ref int fileCountToDelete, ref long bytesToDelete) { List <string> filesToDelete = new List <string>(); foreach (var subfolderName in subfolders) { List <string> allFiles = DirSearch(Path.Combine(destinationFolder, subfolderName), true); foreach (string destFilePath in allFiles) { string srcFilePath = destFilePath.Replace(destinationFolder, sourceFolder); var destFileInfo = new Delimon.Win32.IO.FileInfo(destFilePath); if (IsIgnored(srcFilePath) || !File.Exists(srcFilePath)) { fileCountToDelete++; bytesToDelete += destFileInfo.Length; filesToDelete.Add(destFilePath); } } } return(filesToDelete); }
/// <summary> /// apply change to a file in new task (deleting or copying) /// </summary> /// <param name="sfei">sync execution information about file</param> /// <param name="interruptChecker">is called when the cancellation or pause request should be checked in order to handel them</param> public static bool ApplyFileChange(SyncFileExecutionInfo sfei, Func <bool> interruptChecker) { if (interruptChecker()) { return(true); } string sfp = sfei.AbsoluteSourcePath; string dfp = sfei.AbsoluteDestPath; Delimon.Win32.IO.FileInfo sfi = new Delimon.Win32.IO.FileInfo(sfp); sfei.StartedNow(); try { if (sfei.Remove) { File.SetAttributes(dfp, FileAttributes.Normal); //change attributes to avoid UnauthorizedAccessException File.Delete(dfp); } else if (sfi.Exists) { //Copy Methods: //FileMove2(sfp, dfp, interruptChecker); File.Copy(sfp, dfp, true); } sfei.SyncFileInfo.SyncStatus = SyncElementStatus.ChangeApplied; } catch (IOException ioe) { string[] parts = ioe.Message.Split('\"'); if (parts.Length > 1) { string path = parts[1]; int conflictPath = path.Contains(sfei.SyncFileInfo.SyncInfo.Link.Path1) ? 1 : 2; sfei.SyncFileInfo.Conflicted(new FileConflictInfo(sfei.SyncFileInfo, ConflictType.IO, conflictPath, "RunApplyFileChangeTask", ioe.Message, ioe)); } } catch (UnauthorizedAccessException uae) { string[] parts = uae.Message.Split('\"'); if (parts.Length > 1) { string path = parts[1]; int conflictPath = path.Contains(sfei.SyncFileInfo.SyncInfo.Link.Path1) ? 1 : 2; sfei.SyncFileInfo.Conflicted(new FileConflictInfo(sfei.SyncFileInfo, ConflictType.UA, conflictPath, "RunApplyFileChangeTask", uae.Message, uae)); } } catch (Exception e) { sfei.SyncFileInfo.Conflicted(new FileConflictInfo(sfei.SyncFileInfo, ConflictType.Unknown, 0, "RunApplyFileChangeTask", e.Message, e)); } sfei.EndedNow(); return(false); }
/// <summary> /// apply change to a file in new task (deleting or copying) /// </summary> /// <param name="sfei">sync execution information about file</param> /// <param name="interruptChecker">is called when the cancellation or pause request should be checked in order to handel them</param> public static bool ApplyFileChange(SyncFileExecutionInfo sfei, Func<bool> interruptChecker) { if(interruptChecker()) return true; string sfp = sfei.AbsoluteSourcePath; string dfp = sfei.AbsoluteDestPath; Delimon.Win32.IO.FileInfo sfi = new Delimon.Win32.IO.FileInfo(sfp); sfei.StartedNow(); try { if (sfei.Remove) { File.SetAttributes(dfp, FileAttributes.Normal); //change attributes to avoid UnauthorizedAccessException File.Delete(dfp); } else if (sfi.Exists) { //Copy Methods: //FileMove2(sfp, dfp, interruptChecker); File.Copy(sfp, dfp, true); } sfei.SyncFileInfo.SyncStatus = SyncElementStatus.ChangeApplied; } catch (IOException ioe) { string[] parts = ioe.Message.Split('\"'); if (parts.Length > 1) { string path = parts[1]; int conflictPath = path.Contains(sfei.SyncFileInfo.SyncInfo.Link.Path1) ? 1 : 2; sfei.SyncFileInfo.Conflicted(new FileConflictInfo(sfei.SyncFileInfo, ConflictType.IO, conflictPath, "RunApplyFileChangeTask", ioe.Message, ioe)); } } catch (UnauthorizedAccessException uae) { string[] parts = uae.Message.Split('\"'); if (parts.Length > 1) { string path = parts[1]; int conflictPath = path.Contains(sfei.SyncFileInfo.SyncInfo.Link.Path1) ? 1 : 2; sfei.SyncFileInfo.Conflicted(new FileConflictInfo(sfei.SyncFileInfo, ConflictType.UA, conflictPath, "RunApplyFileChangeTask", uae.Message, uae)); } } catch (Exception e) { sfei.SyncFileInfo.Conflicted(new FileConflictInfo(sfei.SyncFileInfo, ConflictType.Unknown, 0, "RunApplyFileChangeTask", e.Message, e)); } sfei.EndedNow(); return false; }
/// <summary> /// Check if 2 Files are updated for one way synchronisation /// </summary> /// <param name="sfi">source file</param> /// <param name="dfi">destination file</param> /// <param name="interruptChecker">is called when the cancellation or pause request should be checked in order to handel them</param> /// <returns>true if the files are not updated</returns> public static bool CompareFiles_OneWay(Delimon.Win32.IO.FileInfo sfi, Delimon.Win32.IO.FileInfo dfi, Func <bool> interruptChecker) { bool d = !dfi.Exists || sfi.LastWriteTime > dfi.LastWriteTime || (sfi.LastWriteTime < dfi.LastWriteTime && !Helper.FilesAreEqual(sfi, dfi, interruptChecker)); return(!dfi.Exists || sfi.LastWriteTime > dfi.LastWriteTime || (sfi.LastWriteTime < dfi.LastWriteTime && !Helper.FilesAreEqual(sfi, dfi, interruptChecker))); }
private void FileResultContructor(Delimon.Win32.IO.FileInfo dfile, string strComment, string strFileFilterSearched) { Name = dfile.Name; FullPath = dfile.FullName; Extension = dfile.Extension; CreationTime = dfile.CreationTime; LastWriteTime = dfile.LastWriteTime; Owner = Common.GetFileOwner(FullPath); Length = dfile.Length; ParentDirectoryPath = dfile.Directory.FullName; ObjectType = Common.FileFilterObjectType.File; Comment = strComment; FileFilterSearched = strFileFilterSearched; Deleted = false; }
// ------------------------------------------------------------------ #endregion #region Private methods. // ------------------------------------------------------------------ /// <summary> /// Does the content of the check contains file prohibited. /// </summary> /// <param name="filePath">The file path.</param> /// <param name="indentLevel">The nesting depth.</param> /// <returns></returns> private bool DoVerifyFileHeader( Delimon.Win32.IO.FileInfo filePath, HeaderSignature[] sigs, bool ignoreExtension) { if (filePath == null || !filePath.Exists || filePath.Length <= 0) { return(false); } else { WriteError( string.Format( @"Audit Folder ContentDetectorEngine: Verifying file '{0}' ({1:0,0} bytes).", filePath.FullName, filePath.Length), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true); return(IsVerifiedContent(filePath, sigs, ignoreExtension)); } }
public static bool IsZipRelatedFile(Delimon.Win32.IO.FileInfo file1) { bool blZipExtension = false; byte[] Zipbytes = { 0x50, 0x4B }; //504B using (FileStream fs = file1.Open(Delimon.Win32.IO.FileMode.Open, Delimon.Win32.IO.FileAccess.Read, Delimon.Win32.IO.FileShare.ReadWrite)) { if (fs == null) { return(false); } else { int read = 2; byte[] buffer = new byte[read]; int readCount = fs.Read(buffer, 0, read); using (MemoryStream ms = new MemoryStream(buffer)) { ms.Seek(0, SeekOrigin.Begin); byte[] realBuffer = new byte[readCount]; ms.Read(realBuffer, 0, readCount); blZipExtension = realBuffer == Zipbytes; } } try { fs.Close(); } catch (Exception) { } } return(blZipExtension); }
private long ComputeFolderSpace(string[] files) { pbFileProgress.Maximum = files.Length; pbFileProgress.Step = 1; long lngSpace = 0; Delimon.Win32.IO.FileInfo oFileInfo; int i = 1; foreach (var file in files) { this.lblProgress.Text = "Calculation file size: " + file; this.lblProgress.Update(); oFileInfo = new Delimon.Win32.IO.FileInfo(file); lngSpace += oFileInfo.Length; pbFileProgress.Value = i++; pbFileProgress.Update(); } return(lngSpace); }
public static string ResolveSingleFilePath(this string sourcePath) { var results = string.Empty; if (!string.IsNullOrEmpty(sourcePath)) { if (sourcePath.Length < GlobalDefinitions.MAX_PATH) { if (System.IO.File.Exists(sourcePath)) { var path = System.IO.Path.GetDirectoryName(sourcePath); if (sourcePath.StartsWith(@".\")) { path = string.Empty; sourcePath = sourcePath.Replace(@".\", string.Empty); } path = string.IsNullOrEmpty(path) ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, sourcePath) : sourcePath; results = path; } } if (string.IsNullOrEmpty(results)) { if (sourcePath.StartsWith(@".\")) { sourcePath = sourcePath.Replace(@".\", AppDomain.CurrentDomain.BaseDirectory); } var longformat = string.Format(@"\\?\{0}", sourcePath); var fileInfo = new Delimon.Win32.IO.FileInfo(longformat); var dirInfo = new Delimon.Win32.IO.DirectoryInfo(sourcePath); if (Delimon.Win32.IO.File.Exists(longformat) && fileInfo.Length > 0) { results = longformat; } } } return(results); }
/// <summary> /// Compare Files Byte for Byte /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <param name="interruptChecker">is called when the cancellation or pause request should be checked in order to handel them</param> /// <returns></returns> public static bool FilesAreEqual(Delimon.Win32.IO.FileInfo first, Delimon.Win32.IO.FileInfo second, Func <bool> interruptChecker) { if (first.Length != second.Length) { return(false); } using (FileStream fs1 = first.OpenRead()) using (FileStream fs2 = second.OpenRead()) { for (int i = 0; i < first.Length; i++) { if (fs1.ReadByte() != fs2.ReadByte()) { return(false); } interruptChecker(); } } return(true); }
// [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] //[return: MarshalAs(UnmanagedType.Bool)] //public static extern SafeFileHandle createFile(string filename); // [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] // internal static extern SafeFileHandle CreateFile( //string lpFileName, //EFileAccess dwDesiredAccess, //EFileShare dwShareMode, //IntPtr lpSecurityAttributes, //ECreationDisposition dwCreationDisposition, //EFileAttributes dwFlagsAndAttributes, // IntPtr hTemplateFile); // { //try //{ // string path = @"C:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\" + // @"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\" + // @"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\" + // @" DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\" + // @"EEEEEEEEEEE\" + // @"HELLLLLLLLLLLLLLLLOOOOOOOOOOOOOOOOOOO1.txt"; // path = @"\\?\" + path; // FileInfo extractFileInfo = new FileInfo(path); //} //catch (Exception ex) { MessageBox.Show(ex.Message); } // } private void button7_Click(object sender, EventArgs e) { try { // StreamReader sr = new StreamReader(@" C:\AutomationBuilder\"+ // @"CommonInstaller\Build\config.txt"); // Delimon.Win32.IO.dll string path = @"C:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\" + @"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\" + @"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\" + @" DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\" + @"EEEEEEEEEEE\" + @"HELLLLLLLLLLLLLLLLOOOOOOOOOOOOOOOOOOO.txt"; path = @"\\?\" + path; // SafeFileHandle fileHandle = CreateFile(path, //EFileAccess.GenericWrite, EFileShare.None, IntPtr.Zero, //ECreationDisposition.CreateAlways, IntPtr.Zero); //createFile(path); // Delimon.Win32.IO.Path // FileInfo extractFileInfo = new FileInfo(path); Delimon.Win32.IO.FileInfo extractFileInfo = new Delimon.Win32.IO.FileInfo(path); //Delimon.Win32.IO.Directory dir = new Delimon.Win32.IO.Directory(); //Delimon.Win32.IO.File fil = new Delimon.Win32.IO.File(); FileStream fileStream = extractFileInfo.Create(); // fileStream = new FileStream(path, FileMode.Create); // Delimon.Win32.IO.Helpers hl = new Delimon.Win32.IO.Helpers(); // PackagePart packagePart = null; // packagePart.GetStream().CopyTo(extractFileInfo.Create()); // bool retValu = extractFileInfo.Exists == true; // StreamReader sr = new StreamReader(path); // Delimon.Win32.IO.FileInfo // MessageBox.Show(retValu.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// compare 2 files for one way synchronisation in new task /// </summary> /// <param name="sourcePath">absolute source folder path (homepath as defined in link)</param> /// <param name="destPath">absolute destination folder path (homepath as defined in link)</param> /// <param name="fileName">file name</param> /// <param name="relativePath">file path relative to the homepath without filename</param> /// <param name="interruptChecker">is called when the cancellation or pause request should be checked in order to handel them</param> /// <returns>true if the operation was canceled</returns> public static bool DoFileComparison_OneWay(string sourcePath, string destPath, SyncFileInfo file, Func <bool> interruptChecker) { if (interruptChecker()) { return(true); } file.SyncStatus = SyncElementStatus.ChangeDetectingStarted; string sf = sourcePath + file.FileInfo.FullPath; string df = destPath + file.FileInfo.FullPath; Delimon.Win32.IO.FileInfo srcFileInfo = new Delimon.Win32.IO.FileInfo(sf); Delimon.Win32.IO.FileInfo destFileInfo = new Delimon.Win32.IO.FileInfo(df); try { if (Helper.CompareFiles_OneWay(srcFileInfo, destFileInfo, interruptChecker)) { file.FileInfo.Size = srcFileInfo.Length; new SyncFileExecutionInfo(file, file.SyncInfo.Link.Direction, false); } } catch (Exception e) { if (file.SyncInfo != null) { file.Conflicted(new FileConflictInfo(file, ConflictType.Unknown, 0, "RunOneWayFileCompareTask", e.Message, e)); } else { file.SyncInfo.Log(new LogMessage(LogType.ERROR, e.Message, e)); } } return(false); }
/// <summary> /// Matches the file. /// </summary> /// <param name="filePath">The file path.</param> /// <param name="ignoreExtension">if set to <c>true</c> /// [ignore extension].</param> /// <returns></returns> public bool MatchesFile( Delimon.Win32.IO.FileInfo filePath, bool ignoreExtension) { if (filePath == null || !filePath.Exists || filePath.Length <= 0) { return(false); } else { if (ignoreExtension || MatchesFileExtension(filePath.Extension)) { using (FileStream fs = filePath.Open(Delimon.Win32.IO.FileMode.Open, Delimon.Win32.IO.FileAccess.Read, Delimon.Win32.IO.FileShare.ReadWrite)) { return(MatchesStream(fs)); } } else { return(false); } } }
/// <summary> /// Matches the file. /// </summary> /// <param name="filePath">The file path.</param> /// <returns></returns> public bool MatchesFile( Delimon.Win32.IO.FileInfo filePath) { // By default, don't care about the extension. return(MatchesFile(filePath, true)); }
/// <summary> /// Calculates Folder Size by adding up all file.Length /// this works for UNC and Non UNC paths. This can also handle long file names. /// </summary> /// <param name="folder"></param> /// <returns></returns> public static float CalculateFolderSize(string folder) { float folderSize = 0.0f; try { //Checks if the path is valid or not if (!DirectoryExists(folder)) return folderSize; else { try { foreach (string file in Delimon.Win32.IO.Directory.GetFiles(folder)) { if (Delimon.Win32.IO.File.Exists(file)) { Delimon.Win32.IO.FileInfo finfo = new Delimon.Win32.IO.FileInfo(file); folderSize += (float) finfo.Length; } } foreach (string dir in Delimon.Win32.IO.Directory.GetDirectories(folder)) { folderSize += CalculateFolderSize(dir); } } catch (NotSupportedException) { //Console.WriteLine("Unable to calculate folder size: {0}", e.Message); } } } catch (UnauthorizedAccessException) { //Console.WriteLine("Unable to calculate folder size: {0}", e.Message); } return folderSize; }
/* * Iterates through all files & folders starting from the given path and sums the file lengths. * * path: The directory to start iterating from. * returns: the total size of all files that we are targeting(determined by ValidateExtension) in Kilobytes. */ public static int DetermineSize(string path) { long totalSize = 0; if (CheckIfExcluded(path) == false) {//for the possibility that the path passed in is excluded and my code didn't catch it somehow. Stack<string> directories = new Stack<string>(100); if (!Directory.Exists(path)) throw new ArgumentException(); directories.Push(path); while (directories.Count > 0) { string currentDirectory = directories.Pop(); if (!currentDirectory.Equals(windowsFolderPath) && !currentDirectory.Equals(programFilesFolderPath)) { string[] subDirectories; try { subDirectories = Directory.GetDirectories(currentDirectory); } catch (Exception) { continue; } //check for excluded directories List<string> includedSubDirectories = new List<string>(); foreach (string directory in subDirectories) { if (CheckIfExcluded(directory) == false) { includedSubDirectories.Add(directory); } } string[] files = null; try { files = Directory.GetFiles(currentDirectory); } catch (Exception) { continue; } foreach (string file in files) { try { Delimon.Win32.IO.FileInfo fInfo = null; bool extValid = ValidateExtension(Path.GetExtension(file)); if (!extValid) continue; else fInfo = new Delimon.Win32.IO.FileInfo(file); //Add the length of the current file to the total: The whole purpose of this method... totalSize += fInfo.Length; } catch (FileNotFoundException) { continue; } } foreach (string directoryName in includedSubDirectories) { directories.Push(directoryName); } } } } //finally, calculate the amount //change from bytes to kilobytes totalSize /= 1024; return Convert.ToInt32(totalSize); }
public DelimonFileInfoWrapper(Delimon.Win32.IO.FileInfo instance) { _instance = instance; }
/// <summary> /// Check if 2 Files are updated for two way synchronisation. /// The order of the files does not matter. /// </summary> /// <param name="fi1">file 1</param> /// <param name="fi2">file 2</param> /// <param name="remove">if remove is enabled</param> /// <param name="parentDir1">parent directory of file 1</param> /// <param name="parentDir2">parent directory of file 2</param> /// <returns>compare result</returns> public static TwoWayCompareResult CompareFiles_TwoWay(SyncFileInfo file) { Delimon.Win32.IO.FileInfo fi1 = new Delimon.Win32.IO.FileInfo(file.AbsolutePath1); Delimon.Win32.IO.FileInfo fi2 = new Delimon.Win32.IO.FileInfo(file.AbsolutePath2); Delimon.Win32.IO.DirectoryInfo parent1 = new Delimon.Win32.IO.DirectoryInfo(file.FileInfo.Parent.SyncDirInfo.AbsolutePath1); Delimon.Win32.IO.DirectoryInfo parent2 = new Delimon.Win32.IO.DirectoryInfo(file.FileInfo.Parent.SyncDirInfo.AbsolutePath2); bool remove = file.SyncInfo.Link.Remove; if (!fi1.Exists) { // if the parent directory of file 1 is older than file 2 -> create file in parent directory 1 // otherwise remove file 2 if remove ist enabled if (file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo == null) { if (parent1.LastWriteTime <= parent2.LastWriteTime) { return(new TwoWayCompareResult(SyncDirection.To1, false)); } else if (remove) { return(new TwoWayCompareResult(SyncDirection.To2, true)); } } else if (file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo.Direction == SyncDirection.To1) { return(new TwoWayCompareResult(SyncDirection.To1, false)); } else if (remove) { return(new TwoWayCompareResult(SyncDirection.To2, true)); } return(null); } if (!fi2.Exists) { // if the parent directory of file 2 is older than file 1 -> create file in parent directory 2 // otherwise remove file 1 if remove ist enabled if (file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo == null) { if (parent2.LastWriteTime <= parent1.LastWriteTime) { return(new TwoWayCompareResult(SyncDirection.To2, false)); } else if (remove) { return(new TwoWayCompareResult(SyncDirection.To1, true)); } } else if (file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo.Direction == SyncDirection.To2) { return(new TwoWayCompareResult(SyncDirection.To2, false)); } else if (remove) { return(new TwoWayCompareResult(SyncDirection.To1, true)); } return(null); } // update file 1 if file 2 is newer if (fi1.LastWriteTime < fi2.LastWriteTime) { return(new TwoWayCompareResult(SyncDirection.To1, false)); } // update file 2 if file 1 is newer if (fi1.LastWriteTime > fi2.LastWriteTime) { return(new TwoWayCompareResult(SyncDirection.To2, false)); } return(null); }
private static async Task <bool> Process(System.Net.Http.HttpClient client, string path, Uri uri, bool update, System.Threading.CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { throw new OperationCanceledException(cancellationToken); } Console.Write("Processing {0}", uri); var fileName = SanitizePath(Delimon.Win32.IO.Path.Combine(path, uri.Host) + uri.LocalPath.Replace(System.IO.Path.AltDirectorySeparatorChar, System.IO.Path.DirectorySeparatorChar), '_'); if (!update && System.IO.File.Exists(fileName)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" - Exists"); Console.ForegroundColor = DefaultConsoleColor; return(true); } // get the head using (var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Head, uri)) { System.Net.Http.HttpResponseMessage response; try { response = await client.SendAsync(request, cancellationToken); } catch (System.Net.Http.HttpRequestException ex) { WriteError(ex); return(false); } using (response) { Console.ForegroundColor = response.StatusCode == System.Net.HttpStatusCode.OK ? ConsoleColor.DarkGreen : ConsoleColor.DarkRed; Console.Write(" - {0}", response.StatusCode); Console.ForegroundColor = DefaultConsoleColor; if (response.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine(); return(true); } if (uri != request.RequestUri) { WriteInformation(" - redirect detected"); return(true); } if (response.Content == null) { WriteInformation(" - no content detected"); return(true); } Console.WriteLine(); if (response.Content.Headers.ContentType != null && response.Content.Headers.ContentType.MediaType == "text/html") { // process this as a HTML page await ProcessHtml(client, path, uri, update, cancellationToken); } else { // create the file name var contentLength = response.Content.Headers.ContentLength; // check to see if this exists if (Delimon.Win32.IO.File.Exists(fileName)) { var fileInfo = new Delimon.Win32.IO.FileInfo(fileName); var fileLength = fileInfo.Length; if (fileLength == contentLength) { return(true); } else { // file is is not correct, continue with download } } int count = 0; while (!await ProcessLink(client, fileName, uri, contentLength, cancellationToken) && count < RetryCount) { count++; System.Threading.Thread.Sleep(5000); } if (Delimon.Win32.IO.File.Exists(fileName)) { var fileInfo = new Delimon.Win32.IO.FileInfo(fileName); var fileLength = fileInfo.Length; if (fileLength != contentLength) { WriteWarning("Invalid length, expected {0} - got {1}", contentLength, fileLength); return(false); } } } } } return(true); }
/// <summary> /// Find Files and Directories in the immediate folder - handles long path names, expects local or UNC path /// </summary> /// <param name="dirName">Directory to search in UNC path or local path format</param> /// <param name="dtFilters">Data table with "Enabled" and "FileFilter" columns required</param> /// <param name="checkSubFolders">Recursively check all sub folders</param> /// <returns></returns> public static List <ContentDetectorLib.FileResult> FindImmediateFilesAndDirs(string dirName, DataTable dtFilters, bool checkSubFolders) { string strDirConverted = LongPathPrepend(dirName); List <ContentDetectorLib.FileResult> results = new List <ContentDetectorLib.FileResult>(); IntPtr findHandle = INVALID_HANDLE_VALUE; bool blIgnoreFile = false; WIN32_FIND_DATA findData; if (dtFilters != null) { //For Each File filter find them in the current folder foreach (DataRow row in dtFilters.Rows) { try { FindFileFilter filter = new FindFileFilter(row); if (filter.Enabled && filter.FileFilter != "") { //Valid File Filter? if (VerifyPattern(filter.FileFilter) && Delimon.Win32.IO.Directory.Exists(dirName)) { try { //Search for the file filter in the current directory if (filter.ObjectType == ContentDetectorLib.Common.FileFilterObjectType.Folder) { findHandle = FindFirstFileEx(strDirConverted + @"\" + filter.FileFilter, FINDEX_INFO_LEVELS.FindExInfoBasic, out findData, FINDEX_SEARCH_OPS.FindExSearchLimitToDirectories, IntPtr.Zero, FIND_FIRST_EX_LARGE_FETCH); } else { findHandle = FindFirstFileEx(strDirConverted + @"\" + filter.FileFilter, FINDEX_INFO_LEVELS.FindExInfoBasic, out findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, FIND_FIRST_EX_LARGE_FETCH); } if (findHandle != INVALID_HANDLE_VALUE) { bool found; do { string currentFileName = findData.cFileName; blIgnoreFile = false; char[] delimiters = new char[] { ';' }; if (filter.ExcludeFiles != "") { string[] strArr_excludedfiles = filter.ExcludeFiles.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); if (!(strArr_excludedfiles == null || strArr_excludedfiles.Length == 0)) { //loop through excluded folders foreach (string strExclude in strArr_excludedfiles) { if (currentFileName.ToLower() == strExclude.ToLower()) { blIgnoreFile = true; } } } } if (!blIgnoreFile) { // if this is a directory, add directory found to the results. if (((int)findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { if (filter.ObjectType == ContentDetectorLib.Common.FileFilterObjectType.Folder || filter.ObjectType == ContentDetectorLib.Common.FileFilterObjectType.Both) { if (currentFileName != "." && currentFileName != "..") { string strFilePath = RemovePrependGetPath(Path.Combine(dirName, currentFileName)); try { Delimon.Win32.IO.DirectoryInfo ddir = new Delimon.Win32.IO.DirectoryInfo(strFilePath); ContentDetectorLib.FileResult fr1 = new ContentDetectorLib.FileResult(ddir); fr1.FileFilterSearched = filter.FileFilter; results.Add(fr1); //results.Add("\"" + strFilePath + "\"" + ",FolderCreated: " + ddir.CreationTime.ToString("G") + ",Owner: " + strOwner); } catch (Exception) { ContentDetectorLib.FileResult fr1 = new ContentDetectorLib.FileResult(); fr1.FileFilterSearched = filter.FileFilter; fr1.ObjectType = ContentDetectorLib.Common.FileFilterObjectType.Folder; fr1.FullPath = strFilePath; results.Add(fr1); } } } } // it’s a file; add it to the results else { if (filter.ObjectType == ContentDetectorLib.Common.FileFilterObjectType.File || filter.ObjectType == ContentDetectorLib.Common.FileFilterObjectType.Both) { string strFilePath = RemovePrependGetPath(Path.Combine(dirName, currentFileName)); Delimon.Win32.IO.FileInfo dfile; try { dfile = new Delimon.Win32.IO.FileInfo(strFilePath); if (filter.DeleteFilesFound) { ContentDetectorLib.FileResult fr1 = new ContentDetectorLib.FileResult(dfile); fr1.FileFilterSearched = filter.FileFilter; fr1.Deleted = true; results.Add(fr1); //Delete the ransomware related file //results.Add("File Deleted: " + "\"" + strFilePath + "\"" + ",FileCreated: " + dfile.CreationTime.ToString("G") + ",Owner: " + strOwner); dfile.Delete(); } else { ContentDetectorLib.FileResult fr1 = new ContentDetectorLib.FileResult(dfile); fr1.FileFilterSearched = filter.FileFilter; results.Add(fr1); //Document the file found //results.Add("\"" + strFilePath + "\"" + ",FileCreated: " + dfile.CreationTime.ToString("G") + ",Owner: " + strOwner); } } catch (Exception) { ContentDetectorLib.FileResult fr1 = new ContentDetectorLib.FileResult(); fr1.FileFilterSearched = filter.FileFilter; fr1.FullPath = strFilePath; fr1.ObjectType = ContentDetectorLib.Common.FileFilterObjectType.None; fr1.Comment = "\"" + strFilePath + "\""; results.Add(fr1); //results.Add("\"" + strFilePath + "\""); } } } } // find next if any found = FindNextFile(findHandle, out findData); }while (found); } } finally { // close the find handle FindClose(findHandle); } } else { //invalid search filter if (results.Count == 0) { ContentDetectorLib.FileResult fr1 = new ContentDetectorLib.FileResult(); fr1.FileFilterSearched = filter.FileFilter; fr1.ObjectType = ContentDetectorLib.Common.FileFilterObjectType.None; fr1.Comment = "Invalid Search Filter or Directory Problem: " + filter.FileFilter + " " + dirName; results.Add(fr1); //results.Add("Invalid Search Filter or Directory Problem: " + filter.FileFilter + " " + dirName); } } } } catch { } } } return(results); }
public static void FileCopy(string src, string dst, bool overwrite, bool gentle = false) { if (isLinux) { bool was_ro = false; System.IO.FileInfo fi3 = null; if (!gentle) { fi3 = new System.IO.FileInfo (dst); if ((fi3.Attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly) { was_ro = true; fi3.Attributes &= ~System.IO.FileAttributes.ReadOnly; } } System.IO.File.Copy (src, dst, overwrite); if (was_ro) { fi3.Attributes = fi3.Attributes | System.IO.FileAttributes.ReadOnly; } } else { bool was_ro = false; Delimon.Win32.IO.FileInfo fi3 = null; if (!gentle) { fi3 = new Delimon.Win32.IO.FileInfo (dst); if ((fi3.Attributes & Delimon.Win32.IO.FileAttributes.ReadOnly) == Delimon.Win32.IO.FileAttributes.ReadOnly) { was_ro = true; fi3.Attributes &= ~Delimon.Win32.IO.FileAttributes.ReadOnly; } } Delimon.Win32.IO.File.Copy (src, dst, overwrite); if (was_ro) { fi3.Attributes = fi3.Attributes | Delimon.Win32.IO.FileAttributes.ReadOnly; } } }
public static byte[] GenHashOnFileExcludingMetadata(string filename, out long fileLen) { bool md5CreatedOk = false; byte[] md5Val = new byte[0]; byte[] pdfImageTagBytes = Encoding.ASCII.GetBytes("<</Subtype/Image/Length"); fileLen = 0; using (var md5 = MD5.Create()) { try { Delimon.Win32.IO.FileInfo finfo = new Delimon.Win32.IO.FileInfo(filename); fileLen = finfo.Length; if (finfo.Length < 100000000) { byte[] fileData = Delimon.Win32.IO.File.ReadAllBytes(filename); bool completeMatch = false; int matchPos = 0; // Find the tell-tale PDF scanned file string of bytes for (int testPos = 0; testPos < fileData.Length - pdfImageTagBytes.Length; testPos++) { completeMatch = true; for (int i = 0; i < pdfImageTagBytes.Length; i++) if (fileData[testPos + i] != pdfImageTagBytes[i]) { completeMatch = false; break; } if (completeMatch) { matchPos = testPos; string extractedText = Encoding.UTF8.GetString(fileData, matchPos + pdfImageTagBytes.Length, 20); Match match = Regex.Match(extractedText, @"\s*?(\d+)", RegexOptions.IgnoreCase); if (match.Success) { // Finally, we get the Group value and display it. int imgLen = 0; bool rslt = Int32.TryParse(match.Groups[1].Value, out imgLen); if (rslt && (imgLen < fileData.Length - matchPos - 100)) { md5Val = md5.ComputeHash(fileData, matchPos, imgLen); md5CreatedOk = true; } } break; } } } if (!md5CreatedOk) { using (var stream = Delimon.Win32.IO.File.OpenRead(filename)) { md5Val = md5.ComputeHash(stream); md5CreatedOk = true; } } } catch(Exception excp) { logger.Error("Exception in GenHashOnFileExcludingMetadata " + excp.Message); } } if (md5CreatedOk) return md5Val; return new byte[0]; }
/// <summary> /// Check if 2 Files are updated for two way synchronisation. /// The order of the files does not matter. /// </summary> /// <param name="fi1">file 1</param> /// <param name="fi2">file 2</param> /// <param name="remove">if remove is enabled</param> /// <param name="parentDir1">parent directory of file 1</param> /// <param name="parentDir2">parent directory of file 2</param> /// <returns>compare result</returns> public static TwoWayCompareResult CompareFiles_TwoWay(SyncFileInfo file) { Delimon.Win32.IO.FileInfo fi1 = new Delimon.Win32.IO.FileInfo(file.AbsolutePath1); Delimon.Win32.IO.FileInfo fi2 = new Delimon.Win32.IO.FileInfo(file.AbsolutePath2); Delimon.Win32.IO.DirectoryInfo parent1 = new Delimon.Win32.IO.DirectoryInfo(file.FileInfo.Parent.SyncDirInfo.AbsolutePath1); Delimon.Win32.IO.DirectoryInfo parent2 = new Delimon.Win32.IO.DirectoryInfo(file.FileInfo.Parent.SyncDirInfo.AbsolutePath2); bool remove = file.SyncInfo.Link.Remove; if (!fi1.Exists) { // if the parent directory of file 1 is older than file 2 -> create file in parent directory 1 // otherwise remove file 2 if remove ist enabled if(file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo == null) { if (parent1.LastWriteTime <= parent2.LastWriteTime) return new TwoWayCompareResult(SyncDirection.To1, false); else if (remove) return new TwoWayCompareResult(SyncDirection.To2, true); } else if (file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo.Direction == SyncDirection.To1) return new TwoWayCompareResult(SyncDirection.To1, false); else if (remove) return new TwoWayCompareResult(SyncDirection.To2, true); return null; } if (!fi2.Exists) { // if the parent directory of file 2 is older than file 1 -> create file in parent directory 2 // otherwise remove file 1 if remove ist enabled if (file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo == null) { if (parent2.LastWriteTime <= parent1.LastWriteTime) return new TwoWayCompareResult(SyncDirection.To2, false); else if (remove) return new TwoWayCompareResult(SyncDirection.To1, true); } else if (file.FileInfo.Parent.SyncDirInfo.SyncDirExecutionInfo.Direction == SyncDirection.To2) return new TwoWayCompareResult(SyncDirection.To2, false); else if (remove) return new TwoWayCompareResult(SyncDirection.To1, true); return null; } // update file 1 if file 2 is newer if (fi1.LastWriteTime < fi2.LastWriteTime) return new TwoWayCompareResult(SyncDirection.To1, false); // update file 2 if file 1 is newer if (fi1.LastWriteTime > fi2.LastWriteTime) return new TwoWayCompareResult(SyncDirection.To2, false); return null; }
public DelimonFileInfoWrapper(string fullName) { _instance = new Delimon.Win32.IO.FileInfo(fullName); }
public FileInfo(string path) { if (isLinux) { sys_fi = new System.IO.FileInfo (path); } else { delim_fi = new Delimon.Win32.IO.FileInfo (path); } }
public static void FileSetLastWriteTimeUtc(string path, DateTime time, bool gentle = false) { if (isLinux) { bool was_ro = false; System.IO.FileInfo fi3 = null; if (!gentle) { fi3 = new System.IO.FileInfo (path); if ((fi3.Attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly) { was_ro = true; fi3.Attributes &= ~System.IO.FileAttributes.ReadOnly; } } System.IO.File.SetLastWriteTimeUtc (path, time); if (was_ro) { fi3.Attributes = fi3.Attributes | System.IO.FileAttributes.ReadOnly; } } else { bool was_ro = false; Delimon.Win32.IO.FileInfo fi3 = null; if (!gentle) { fi3 = new Delimon.Win32.IO.FileInfo (path); if ((fi3.Attributes & Delimon.Win32.IO.FileAttributes.ReadOnly) == Delimon.Win32.IO.FileAttributes.ReadOnly) { was_ro = true; fi3.Attributes &= ~Delimon.Win32.IO.FileAttributes.ReadOnly; } } Delimon.Win32.IO.File.SetLastWriteTimeUtc (path, time); if (was_ro) { fi3.Attributes = fi3.Attributes | Delimon.Win32.IO.FileAttributes.ReadOnly; } } }
public FileResult(Delimon.Win32.IO.FileInfo dfile, string strComment, string strFileFilterSearched) { FileResultContructor(dfile, strComment, strFileFilterSearched); }
/// <summary> /// Determines whether File Extension Matches Header of the file /// </summary> /// <param name="filePath">The file path.</param> /// <param name="indentLevel">The nesting depth.</param> /// <returns> /// <c>true</c> if [is prohibited content] [the specified file path]; otherwise, <c>false</c>. /// </returns> private bool IsVerifiedContent(Delimon.Win32.IO.FileInfo filePath, HeaderSignature[] sigs, bool ignoreExtension) { return(CoreVerifyFileContent(filePath, sigs, ignoreExtension)); }
/* * Iterates through all files and folders starting from the incoming root folder and calls ProcessFile for every file encountered. * * root: The path of the folder to start the iteration at. * returns: void. * */ public static void ScanDirectories(string root) { //For now, I will test the passed in root to see if it is excluded from scanning if (CheckIfExcluded(root) == false) //root is not excluded {//if not excluded, do the scanning // Testing code for Michael to scan one directory //root = "Y:\\Documents\\College\\11-12\\Spring\\CSCD488\\Testbed"; int currentSize = 0; Stack<string> directories = new Stack<string>(100); if (!Directory.Exists(root)) throw new ArgumentException(); directories.Push(root); while (directories.Count > 0) { string currentDirectory = directories.Pop(); if (!currentDirectory.Equals(windowsFolderPath) && !currentDirectory.Equals(programFilesFolderPath)) { string[] subDirectories; //get the subDirectories of the current directory try { subDirectories = Directory.GetDirectories(currentDirectory); } catch (UnauthorizedAccessException u) { Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, u.ToString()); continue; } catch (System.IO.DirectoryNotFoundException d) { Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, d.ToString()); continue; } //I will use a list to store the subDirectories at this point. The string[] is there because Directory.GetDirectories returns a string[], but //I'll need to remove excluded paths. To do that, i'll just copy the included scannable paths to a list. List<string> includedSubDirectories = new List<string>(); //populate the includedSubDirectories with the actual paths to be scanned foreach (string directory in subDirectories) { if(CheckIfExcluded(directory) == false) //meaning, the directory IS to be scanned, it is not excluded { includedSubDirectories.Add(directory); } } //at the end of this foreach loop, only the directories to be scanned should be in the includedSubDirectories list string[] files = null; try { files = Directory.GetFiles(currentDirectory); } catch (UnauthorizedAccessException u) { Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, u.ToString()); continue; } catch (System.IO.DirectoryNotFoundException d) { Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, d.ToString()); continue; } try { mainUIForm.lblCurDir.BeginInvoke(new MainForm.InvokeDelegateFilename(mainUIForm.UpdateLblCurFolder), new object[] { currentDirectory }); } catch (InvalidOperationException) { continue; } //iterate through all the files from the current directory foreach (string file in files) { while (!scanning) { } try { Delimon.Win32.IO.FileInfo fInfo = null; //====Extension Validation: Only create the FileInfo object if it is a targeted file type. =================================================== bool extValid = ValidateExtension(Path.GetExtension(file)); if (!extValid) continue; else fInfo = new Delimon.Win32.IO.FileInfo(file); //============================================================================================================================================ ProcessFile(fInfo); //====Update form fields ===================================================================================================================== if (currentSize < mainUIForm.theProgressBar.Maximum) currentSize += (Convert.ToInt32(fInfo.Length) / 1024); int percentage = (int)((double)currentSize / totalSize * 100); numScanned++; try { mainUIForm.theProgressBar.BeginInvoke(new MainForm.InvokeDelegateProgressBar(mainUIForm.UpdateProgressBar), new object[] { currentSize }); mainUIForm.lblPercentage.BeginInvoke(new MainForm.InvokeDelegatePercentage(mainUIForm.UpdateLblPercentage), new object[] { percentage }); mainUIForm.lblItemsScanned.BeginInvoke(new MainForm.InvokeDelegateScanned(mainUIForm.UpdateLblItemsScanned), new object[] { numScanned }); } catch (InvalidOperationException) { continue; } //============================================================================================================================================ } catch (FileNotFoundException f) { Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, f.ToString()); continue; } catch (UnauthorizedAccessException u) { Database.AddToTableUnScannable(currentDirectory, currentDirectory, Environment.UserName, u.ToString()); continue; } } foreach (string directoryName in includedSubDirectories) { directories.Push(directoryName); } } } } }
public FileResult(Delimon.Win32.IO.FileInfo dfile) { FileResultContructor(dfile, "", ""); }
public FileInfoBase FromFileName(string fileName) { var realFileInfo = new Delimon.Win32.IO.FileInfo(fileName); return new DelimonFileInfoWrapper(realFileInfo); }
public FileResult(Delimon.Win32.IO.FileInfo dfile, string strComment) { FileResultContructor(dfile, strComment, ""); }
// ------------------------------------------------------------------ /// <summary> /// Initializes a new instance of the /// <see cref="SingleFileContentProcessor"/> class. /// </summary> /// <param name="filePath">The file path.</param> public SingleFileContentProcessor( Delimon.Win32.IO.FileInfo filePath) { _filePath = filePath; }
void ThreadedStuff() { failed = false; trace = ""; errors = new List <PackageError>(); var pendingPackages = new List <PackageToWrite>(); var currentPack = new PackageToWrite(); long currentPackSize = 0; uint currentFileAmount = 0; pendingPackages.Add(currentPack); //var dirInfo = new DirectoryInfo(downloadsDir); var packages = new List <DBPFFile>(); var entryAmount = 0; var gotCompression = false; fileAmount = 0; currentFile = 0; downloadsDir = downloadsDir.Replace("/", "\\"); startedWriting = false; var packageCount = 0; //For logging purposes foreach (var element in Delimon.Win32.IO.Directory.GetFiles(downloadsDir, "*.package", Delimon.Win32.IO.SearchOption.AllDirectories)) { try { var pack = new DBPFFile(element); currentPackSize += new Delimon.Win32.IO.FileInfo(element).Length; currentFileAmount += pack.NumEntries; if ((currentPackSize >= packageMaxSize && packageMaxSize != 0) || (currentFileAmount >= packageMaxFileAmount && packageMaxFileAmount != 0)) { currentPack = new PackageToWrite(); currentPackSize = 0; currentFileAmount = 0; pendingPackages.Add(currentPack); } fileAmount += pack.NumEntries; if (pack.hasCompression) { gotCompression = true; currentPack.compress = true; fileAmount -= 1; } entryAmount += (int)pack.NumEntries; //packages.Add(pack); currentPack.packages.Add(pack); packageCount += 1; } catch (Exception e) { var err = new PackageError() { file = element, trace = e.ToString() }; errors.Add(err); //MessageBox.Show("There was an error reading package " + element + "." + Environment.NewLine + e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } if (fileAmount == 0) { MessageBox.Show("There are no packages to merge in here, or they're all empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); isFinished = true; return; } fileAmount *= 2; startedWriting = true; byte[] dirfil = null; try { for (var i = 0; i < pendingPackages.Count; i++) { var packToWrite = pendingPackages[i]; var dirStream = new MemoryStream(); var dirWriter = new BinaryWriter(dirStream); foreach (var packa in packToWrite.packages) { foreach (var element in packa.m_EntryByID) { if (element.Value.TypeID != 0xE86B1EEF && element.Value.uncompressedSize != 0) { //fileAmount += 1; //TypeID dirWriter.Write(element.Value.TypeID); //GroupID dirWriter.Write(element.Value.GroupID); //InstanceID dirWriter.Write(element.Value.InstanceID); //ResourceID dirWriter.Write(element.Value.InstanceID2); //UncompressedSize dirWriter.Write(element.Value.uncompressedSize); } } } dirfil = dirStream.ToArray(); dirWriter.Dispose(); dirStream.Dispose(); var fname = Path.Combine(Path.GetDirectoryName(target), Path.GetFileNameWithoutExtension(target) + i.ToString() + ".package"); packToWrite.name = fname; var mStream = new FileStream(fname, FileMode.Create); var mWriter = new BinaryWriter(mStream); //HeeeADER mWriter.Write(new char[] { 'D', 'B', 'P', 'F' }); //major version mWriter.Write((int)1); //minor version mWriter.Write((int)2); mWriter.Write(new byte[12]); //Date stuff mWriter.Write((int)0); mWriter.Write((int)0); //Index major mWriter.Write((int)7); //Num entries var entryAmountOffset = mStream.Position; mWriter.Write((int)0); //Index offset var indexOff = mStream.Position; //Placeholder mWriter.Write((int)0); //Index size var indexSize = mStream.Position; //Placeholder mWriter.Write((int)0); //Trash Entry Stuff mWriter.Write((int)0); mWriter.Write((int)0); mWriter.Write((int)0); //Index Minor Ver mWriter.Write((int)2); //Padding mWriter.Write(new byte[32]); var lastPos = mStream.Position; mStream.Position = indexOff; mWriter.Write((int)lastPos); mStream.Position = lastPos; var entryAmount2 = 0; var indeOf = mStream.Position; long dirFilOffset = 0; if (packToWrite.compress) { currentFile += 1; //TypeID mWriter.Write(0xE86B1EEF); //GroupID mWriter.Write(0xE86B1EEF); //InstanceID mWriter.Write(0x286B1F03); //ResourceID mWriter.Write(0x00000000); //File Offset dirFilOffset = mStream.Position; mWriter.Write((int)0); //File Size mWriter.Write(dirfil.Length); entryAmount2 += 1; } foreach (var packa in packToWrite.packages) { foreach (var element in packa.m_EntryByID) { if (element.Value.TypeID != 0xE86B1EEF) { currentFile += 1; //TypeID mWriter.Write(element.Value.TypeID); //GroupID mWriter.Write(element.Value.GroupID); //InstanceID mWriter.Write(element.Value.InstanceID); //ResourceID mWriter.Write(element.Value.InstanceID2); //File Offset element.Value.writeOff = mStream.Position; mWriter.Write((int)0); //File Size mWriter.Write(element.Value.FileSize); entryAmount2 += 1; } } } lastPos = mStream.Position; mStream.Position = entryAmountOffset; mWriter.Write(entryAmount2); var siz = lastPos - indeOf; mStream.Position = indexOff; mWriter.Write(indeOf); mStream.Position = indexSize; mWriter.Write(siz); mStream.Position = lastPos; if (packToWrite.compress) { lastPos = mStream.Position; mStream.Position = dirFilOffset; mWriter.Write((int)lastPos); mStream.Position = lastPos; mWriter.Write(dirfil); } foreach (var packa in packToWrite.packages) { foreach (var element in packa.m_EntryByID) { System.GC.Collect(); // Just in case? if (element.Value.TypeID != 0xE86B1EEF) { currentFile += 1; lastPos = mStream.Position; mStream.Position = element.Value.writeOff; mWriter.Write((int)lastPos); mStream.Position = lastPos; var ent = packa.GetEntry(element.Value); mWriter.Write(ent); } } } mWriter.Dispose(); mStream.Dispose(); } } catch (Exception e) { MessageBox.Show("There was an error writing the merged packages:" + Environment.NewLine + e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); failed = true; trace = e.ToString(); } var logName = Path.Combine(Path.GetDirectoryName(target), "CCMerger.log"); using (StreamWriter logStream = new StreamWriter(logName)) { logStream.WriteLine("CCMerger log - version " + Program.version); logStream.WriteLine(); logStream.WriteLine("[Settings Used]"); logStream.WriteLine("Max files per package: " + packFiles.Value); logStream.WriteLine("Max package size: " + packSize.Value + " mb"); logStream.WriteLine(); logStream.WriteLine("[Results]"); logStream.WriteLine("Merged " + packageCount + " packages containing " + fileAmount + " files into " + pendingPackages.Count + " packages."); logStream.WriteLine(); logStream.WriteLine("[Errors]"); logStream.WriteLine(errors.Count + " packages failed to merge."); foreach (var element in errors) { logStream.WriteLine("Package name: " + element.file); logStream.WriteLine(element.trace); } if (failed) { logStream.WriteLine("Failed to merge packages :("); logStream.WriteLine(trace); } else { logStream.WriteLine("Packages were merged succesfully :)"); } } if (logBox.Checked) { foreach (var element in pendingPackages) { var simpleName = Path.GetFileName(element.name); var fname = Path.Combine(Path.GetDirectoryName(element.name), Path.GetFileNameWithoutExtension(element.name) + ".txt"); using (StreamWriter logStream = new StreamWriter(fname)) { logStream.WriteLine("Contents of " + simpleName + ":"); logStream.WriteLine(); foreach (var packs in element.packages) { logStream.WriteLine(Path.GetFileName(packs.fname)); } logStream.WriteLine(); } } } isFinished = true; }
/// <summary> /// compare 2 files for one way synchronisation in new task /// </summary> /// <param name="sourcePath">absolute source folder path (homepath as defined in link)</param> /// <param name="destPath">absolute destination folder path (homepath as defined in link)</param> /// <param name="fileName">file name</param> /// <param name="relativePath">file path relative to the homepath without filename</param> /// <param name="interruptChecker">is called when the cancellation or pause request should be checked in order to handel them</param> /// <returns>true if the operation was canceled</returns> public static bool DoFileComparison_OneWay(string sourcePath, string destPath, SyncFileInfo file, Func<bool> interruptChecker) { if(interruptChecker()) return true; file.SyncStatus = SyncElementStatus.ChangeDetectingStarted; string sf = sourcePath + file.FileInfo.FullPath; string df = destPath + file.FileInfo.FullPath; Delimon.Win32.IO.FileInfo srcFileInfo = new Delimon.Win32.IO.FileInfo(sf); Delimon.Win32.IO.FileInfo destFileInfo = new Delimon.Win32.IO.FileInfo(df); try { if (Helper.CompareFiles_OneWay(srcFileInfo, destFileInfo, interruptChecker)) { file.FileInfo.Size = srcFileInfo.Length; new SyncFileExecutionInfo(file, file.SyncInfo.Link.Direction, false); } } catch (Exception e) { if (file.SyncInfo != null) file.Conflicted(new FileConflictInfo(file, ConflictType.Unknown, 0, "RunOneWayFileCompareTask", e.Message, e)); else file.SyncInfo.Log(new LogMessage(LogType.ERROR, e.Message, e)); } return false; }