Esempio n. 1
0
        private static List <string> GetRecycleBinFiles(string dir)
        {
            var files = new List <string>();

            Privilege[] privs = { Privilege.EnableDelegation, Privilege.Impersonate, Privilege.Tcb };
            using (new PrivilegeEnabler(Privilege.Backup, privs))
            {
                var filters = new DirectoryEnumerationFilters
                {
                    // Used to abort the enumeration.
                    // CancellationToken = cancelSource.Token,

                    // Filter to decide whether to recurse into subdirectories.
                    RecursionFilter = entryInfo =>
                    {
                        if (!entryInfo.IsMountPoint && !entryInfo.IsSymbolicLink)
                        {
                            return(true);
                        }

                        return(false);
                    },

                    // Filter to process Exception handling.
                    ErrorFilter = delegate(int errorCode, string errorMessage, string pathProcessed)
                    {
                        _logger.Error($"Error accessing '{pathProcessed}'. Error: {errorMessage}");

                        // Return true to continue, false to throw the Exception.
                        return(true);
                    },

                    // Filter to in-/exclude file system entries during the enumeration.
                    InclusionFilter = entryInfo =>
                    {
                        if (entryInfo.FileName == "INFO2" || entryInfo.FileName.StartsWith("$I"))
                        {
                            _logger.Debug($"Found match: '{entryInfo.FullPath}'");
                            return(true);
                        }

                        return(false);
                    }
                };

                var dirEnumOptions =
                    DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive | DirectoryEnumerationOptions.ContinueOnException |
                    DirectoryEnumerationOptions.SkipReparsePoints;

                files.AddRange(Directory.EnumerateFileSystemEntryInfos <string>(dir, dirEnumOptions, filters).Where(File.Exists));
            }

            return(files);
        }
Esempio n. 2
0
    private static List <string> GetRecycleBinFiles(string dir)
    {
        var files = new List <string>();


#if NET6_0
        var enumerationOptions = new EnumerationOptions
        {
            IgnoreInaccessible    = true,
            MatchCasing           = MatchCasing.CaseSensitive,
            RecurseSubdirectories = true,
            AttributesToSkip      = 0
        };


        var files2 = Directory.EnumerateFileSystemEntries(dir, "$I*", enumerationOptions);


        files.AddRange(files2);


        files2 = Directory.EnumerateFileSystemEntries(dir, "INFO2", enumerationOptions);


        files.AddRange(files2);
#elif NET462
        Privilege[] privs = { Privilege.EnableDelegation, Privilege.Impersonate, Privilege.Tcb };
        using var enabler = new PrivilegeEnabler(Privilege.Backup, privs);
        var filters = new DirectoryEnumerationFilters
        {
            // Used to abort the enumeration.
            // CancellationToken = cancelSource.Token,

            // Filter to decide whether to recurse into subdirectories.
            RecursionFilter = entryInfo =>
            {
                if (!entryInfo.IsMountPoint && !entryInfo.IsSymbolicLink)
                {
                    return(true);
                }

                return(false);
            },

            // Filter to process Exception handling.
            ErrorFilter = delegate(int errorCode, string errorMessage, string pathProcessed)
            {
                Log.Error("Error accessing {PathProcessed}. Error: {ErrorMessage}", pathProcessed, errorCode);

                // Return true to continue, false to throw the Exception.
                return(true);
            },

            // Filter to in-/exclude file system entries during the enumeration.
            InclusionFilter = entryInfo =>
            {
                if (entryInfo.FileName == "INFO2" || entryInfo.FileName.StartsWith("$I"))
                {
                    Log.Debug("Found match: {FullPath}", entryInfo.FullPath);
                    return(true);
                }

                return(false);
            }
        };

        var dirEnumOptions =
            DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive | DirectoryEnumerationOptions.ContinueOnException |
            DirectoryEnumerationOptions.SkipReparsePoints;

        files.AddRange(Directory.EnumerateFileSystemEntryInfos <string>(dir, dirEnumOptions, filters).Where(File.Exists));
#endif

        return(files);
    }
Esempio n. 3
0
        // Better name ScanRootPath.
        // -- consider each breadth first scan, sort then add to the Store, [dont do one at time].
        public void RecurseTree()
        {
            if (Root == null)
            {
                throw new ArgumentException("Root is not set.");
            }
            var entryCount = 0;
            var dirs       = new Stack <Tuple <int, string> >();

            dirs.Push(Tuple.Create(Root.RootIndex, Root.Path));
            while (dirs.Count > 0)
            {
                var t           = dirs.Pop();
                var parentIndex = t.Item1;
                var directory   = t.Item2;

                Entry[] parentBlock;
                var     parentEntryIndex = EntryIndex(parentIndex, out parentBlock);
                int     siblingIndex     = 0; // entering a directory again

                // NEW
                //var fsEntries = new FindFileSystemEntryInfo
                //    {
                //        IsFullPath = true,
                //        InputPath = directory,
                //        AsLongPath = true,
                //        GetFsoType = null, // both files and folders.
                //        SearchOption = SearchOption.TopDirectoryOnly,
                //        SearchPattern = MatchAll,
                //        Transaction = null,
                //        ContinueOnAccessError = true // ignoring them all, cant collec them like use to.
                //    }.Enumerate();
                var fsEntries = Directory.EnumerateFileSystemEntryInfos <FileSystemEntryInfo>(directory, MatchAll, DirectoryEnumerationOptions.FilesAndFolders); // was SearchOption.TopDirectoryOnly is this a match?   Recursive is another flag that can be added, can we assume then top only


                // OLD
                //var fsEntries = Directory.GetFullFileSystemEntries
                //    (null, directory, MatchAll, SearchOption.TopDirectoryOnly, false, exceptionHandler, null);
                foreach (var fsEntry in fsEntries)
                {
                    var     newIndex = AddEntry();
                    Entry[] block;
                    var     entryIndex = EntryIndex(newIndex, out block);
                    block[entryIndex].Set(fsEntry);
                    block[entryIndex].Parent = parentIndex;
                    if (siblingIndex == 0)
                    {
                        parentBlock[parentEntryIndex].Child = newIndex; // first of siblings is child
                    }
                    else
                    {
                        Entry[] siblingBlock;
                        var     siblingEntryIndex = EntryIndex(siblingIndex, out siblingBlock);
                        siblingBlock[siblingEntryIndex].Sibling = newIndex;
                    }
                    siblingIndex = newIndex; // sibling chain for next entry
                    if (block[entryIndex].IsModifiedBad)
                    {
                        Console.WriteLine($"Bad date on \"{fsEntry.FullPath}\"");
                    }

                    if (fsEntry.IsDirectory)
                    {
                        // not required for saving - can we custom clear a field to not save?
                        // hmm dont save FullPath maybe ?
                        //block[entryIndex].FullPath = fsEntry.FullPath;
                        dirs.Push(Tuple.Create(newIndex, fsEntry.FullPath));
                    }
                    ++entryCount;
                    if (entryCount > EntryCountThreshold)
                    {
                        if (SimpleScanCountEvent != null)
                        {
                            SimpleScanCountEvent();
                        }
                        entryCount = 0;
                    }
                }
            }
            if (SimpleScanEndEvent != null)
            {
                SimpleScanEndEvent();
            }
        }