Beispiel #1
0
        /// <summary>
        /// delete folder
        /// </summary>
        /// <param name="sdei">sync execution information about directory</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 DeleteFolder(SyncDirExecutionInfo sdei, Func <bool> interruptChecker)
        {
            if (interruptChecker())
            {
                return(true);
            }

            string ddp = sdei.AbsoluteDestPath;

            Delimon.Win32.IO.DirectoryInfo ddi = new Delimon.Win32.IO.DirectoryInfo(ddp);

            if (!ddi.Exists)
            {
                return(false);
            }

            //do not remove if directory is not empty
            if (ddi.GetFiles().Length > 0 || ddi.GetDirectories().Length > 0)
            {
                sdei.SyncDirInfo.Conflicted(new DirConflictInfo(sdei.SyncDirInfo, ConflictType.DirNotEmpty,
                                                                sdei.Direction == SyncDirection.To1 ? 1 : 2, "RunFolderDeletionTask",
                                                                $"The directory to be deleted was not empty. Path: {sdei.SyncDirInfo.DirInfo.FullPath}", null));
                return(false);
            }

            interruptChecker();

            sdei.StartedNow();
            try
            {
                ddi.Delete();
                sdei.SyncDirInfo.SyncStatus = SyncElementStatus.ChangeApplied;
            }
            catch (Exception e)
            {
                sdei.SyncDirInfo.Conflicted(new DirConflictInfo(sdei.SyncDirInfo, ConflictType.Unknown,
                                                                sdei.Direction == SyncDirection.To2 ? 2 : 1, "RunFolderDeletionTask", e.Message, e));
            }
            sdei.EndedNow();

            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// delete folder
        /// </summary>
        /// <param name="sdei">sync execution information about directory</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 DeleteFolder(SyncDirExecutionInfo sdei, Func<bool> interruptChecker)
        {
            if (interruptChecker()) return true;

            string ddp = sdei.AbsoluteDestPath;
            Delimon.Win32.IO.DirectoryInfo ddi = new Delimon.Win32.IO.DirectoryInfo(ddp);

            if (!ddi.Exists)
                return false;

            //do not remove if directory is not empty
            if (ddi.GetFiles().Length > 0 || ddi.GetDirectories().Length > 0)
            {
                sdei.SyncDirInfo.Conflicted(new DirConflictInfo(sdei.SyncDirInfo, ConflictType.DirNotEmpty,
                    sdei.Direction == SyncDirection.To1 ? 1 : 2, "RunFolderDeletionTask",
                    $"The directory to be deleted was not empty. Path: {sdei.SyncDirInfo.DirInfo.FullPath}", null));
                return false;
            }

            interruptChecker();

            sdei.StartedNow();
            try
            {
                ddi.Delete();
                sdei.SyncDirInfo.SyncStatus = SyncElementStatus.ChangeApplied;
            }
            catch (Exception e)
            {
                sdei.SyncDirInfo.Conflicted(new DirConflictInfo(sdei.SyncDirInfo, ConflictType.Unknown,
                    sdei.Direction == SyncDirection.To2 ? 2 : 1, "RunFolderDeletionTask", e.Message, e));
            }
            sdei.EndedNow();

            return false;
        }
        /// <summary>
        /// Determines whether the specified folder contains files with
        /// file extensions that do not match the content.
        /// </summary>
        /// <param name="folderPath">The folder path.</param>
        /// <param name="recursive">if set to <c>true</c> [recursive].</param>
        /// <returns>
        ///     <c>true</c> if [contains folder prohibited content]
        /// [the specified folder path]; otherwise, <c>false</c>.
        /// </returns>
        private void containsFolderVerifyContent(
            Delimon.Win32.IO.DirectoryInfo folderPath,
            bool recursive,
            ref List <FileResult> verifiedFiles,
            ref List <FileResult> unVerifiedFiles,
            ref List <FileResult> unknownFiles,
            ref List <FileResult> ProhibitedFiles,
            ref bool blShuttingDown,
            string excludeFolders,
            HeaderSignature[] sigs,
            bool blValidateZipFiles,
            bool blHeaderVerificationIgnoreFileExtensions,
            bool blProhibitedFilesIgnoreFileExtensions
            )
        {
            WriteError(
                string.Format(
                    @"Audit Folder ContentDetectorEngine: Checking folder '{0}'.",
                    folderPath.FullName), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true);



            if (sigs == null)
            {
                sigs = HeaderSignature.StockSignatures;
            }



            Delimon.Win32.IO.FileInfo[] filePaths = folderPath.GetFiles();

            int index = 0;

            //Load either default signatures or custom for determining ExtensionSupported?



            foreach (Delimon.Win32.IO.FileInfo filePath in filePaths)
            {
                if (blShuttingDown)
                {
                    WriteError(
                        string.Format(
                            @"Audit Folder ContentDetectorEngine: Shutting Down: was about to check file '{0}'.",
                            filePath.FullName), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true);
                    break;
                }
                try
                {
                    WriteError(
                        string.Format(
                            @"Audit Folder ContentDetectorEngine: [{0}/{1}] Checking file '{2}' ({3:0,0} bytes).",
                            index + 1, filePaths.Length,
                            filePath.FullName,
                            filePath.Length), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true);

                    bool blVerifiedContent = false;

                    blVerifiedContent = DoVerifyFileHeader(filePath, sigs, blHeaderVerificationIgnoreFileExtensions);

                    //Verify File Headers
                    if (!HeaderSignature.ExtensionSupported(filePath.Extension, sigs))
                    {
                        unknownFiles.Add(new FileResult(filePath));
                    }
                    else if (!blVerifiedContent)
                    {
                        if (blValidateZipFiles && HeaderSignature.ZipRelatedExtension(filePath.Extension))
                        {
                            using (Stream filestream1 = filePath.Open(Delimon.Win32.IO.FileMode.Open, Delimon.Win32.IO.FileAccess.Read, Delimon.Win32.IO.FileShare.ReadWrite))
                            {
                                try
                                {
                                    if (ZipFile.IsZipFile(filestream1, true) == false)
                                    {
                                        unVerifiedFiles.Add(new FileResult(filePath, "Error: Zip File is Corrupted or Encrypted!"));
                                    }
                                    try
                                    {
                                        filestream1.Close();
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                                catch (Exception ex)
                                {
                                    WriteError(
                                        string.Format(
                                            @"Audit Folder ContentDetectorEngine: [{0}/{1}] Error Zip Checking file '{2}' ({3:0,0} bytes). " + ex.Message,
                                            index + 1, filePaths.Length,
                                            filePath.FullName,
                                            filePath.Length), System.Diagnostics.EventLogEntryType.Error, 6000, 60, false);
                                    unknownFiles.Add(new FileResult(filePath, "Error checking the file"));
                                }
                            }
                        }
                        else
                        {
                            unVerifiedFiles.Add(new FileResult(filePath, "Extension and file header for the file does not match any expected file signature."));
                        }
                    }
                    else
                    {
                        if (blValidateZipFiles && blVerifiedContent && HeaderSignature.IsZipRelatedFile(filePath))
                        {
                            if (ZipFile.IsZipFile(filePath.OpenRead(), true) == false)
                            {
                                unVerifiedFiles.Add(new FileResult(filePath, "Error: Zip File is Corrupted or Encrypted!"));
                            }
                            else
                            {
                                verifiedFiles.Add(new FileResult(filePath, "Zip File Verified"));
                            }
                        }
                        else
                        {
                            verifiedFiles.Add(new FileResult(filePath));
                        }
                    }

                    //check for prohibited files
                    if (ContainsProhibitedFileContent(filePath, sigs, blProhibitedFilesIgnoreFileExtensions))
                    {
                        ProhibitedFiles.Add(new FileResult(filePath));
                    }
                }
                catch (Exception)
                {
                    WriteError(
                        string.Format(
                            @"Audit Folder ContentDetectorEngine: [{0}/{1}] Error Checking file '{2}' ({3:0,0} bytes).",
                            index + 1, filePaths.Length,
                            filePath.FullName,
                            filePath.Length), System.Diagnostics.EventLogEntryType.Error, 6000, 60, false);
                    unknownFiles.Add(new FileResult(filePath, "Error checking the file"));
                }


                index++;
            }



            // --

            if (recursive)
            {
                Delimon.Win32.IO.DirectoryInfo[] folderPaths = folderPath.GetDirectories();

                foreach (Delimon.Win32.IO.DirectoryInfo childFolderPath in folderPaths)
                {
                    bool blIgnoreDirectory = false;
                    if (blShuttingDown)
                    {
                        WriteError(
                            string.Format(
                                @"Audit Folder ContentDetectorEngine: Shutting Down: was about to check folder '{0}'.",
                                childFolderPath.FullName), System.Diagnostics.EventLogEntryType.Information, 6000, 60, true);
                        break;
                    }

                    try
                    {
                        char[]   delimiters             = new char[] { ';' };
                        string[] strArr_excludedfolders = excludeFolders.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                        if (!(strArr_excludedfolders == null || strArr_excludedfolders.Length == 0))
                        {
                            //loop through excluded folders
                            foreach (string strExclude in strArr_excludedfolders)
                            {
                                if (childFolderPath.Name.ToLower() == strExclude.ToLower())
                                {
                                    blIgnoreDirectory = true;
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }

                    if (!blIgnoreDirectory)
                    {
                        containsFolderVerifyContent(childFolderPath, recursive, ref verifiedFiles, ref unVerifiedFiles, ref unknownFiles, ref ProhibitedFiles, ref blShuttingDown, excludeFolders, sigs, blValidateZipFiles, blHeaderVerificationIgnoreFileExtensions, blProhibitedFilesIgnoreFileExtensions);
                    }
                }
            }
        }