Exemplo n.º 1
0
        /// <inheritdoc />
        public EnumerateDirectoryResult EnumerateDirectoryEntries(
            string directoryPath,
            bool enumerateDirectory,
            string pattern,
            uint directoriesToSkipRecursively,
            bool recursive,
            IDirectoryEntriesAccumulator accumulators)
        {
            try
            {
                var directoryEntries = Directory.GetFileSystemEntries(directoryPath);
                Array.Sort(directoryEntries, StringComparer.InvariantCulture);

                var enumerator  = directoryEntries.GetEnumerator();
                var accumulator = accumulators.Current;
                pattern = FileSystemName.TranslateWin32Expression(pattern);

                while (enumerator.MoveNext())
                {
                    var entry       = enumerator.Current as string;
                    var lastSegment = entry.Split(Path.DirectorySeparatorChar).Last();

                    FileAttributes attributes  = File.GetAttributes(entry);
                    var            isDirectory = (attributes & FileAttributes.Directory) == FileAttributes.Directory;

                    if (FileSystemName.MatchesWin32Expression(pattern, lastSegment))
                    {
                        if (!(enumerateDirectory ^ isDirectory) && directoriesToSkipRecursively == 0)
                        {
                            accumulator.AddFile(lastSegment);
                        }
                    }

                    accumulator.AddTrackFile(lastSegment, attributes);

                    if ((recursive || directoriesToSkipRecursively > 0) && isDirectory)
                    {
                        accumulators.AddNew(accumulator, lastSegment);

                        var recurs = EnumerateDirectoryEntries(
                            entry,
                            enumerateDirectory,
                            pattern,
                            directoriesToSkipRecursively == 0 ? 0 : directoriesToSkipRecursively - 1,
                            recursive,
                            accumulators);

                        if (!recurs.Succeeded)
                        {
                            return(recurs);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var result = EnumerateDirectoryResult.CreateFromException(directoryPath, ex);
                accumulators.Current.Succeeded = false;
                return(result);
            }

            return(new EnumerateDirectoryResult(
                       directoryPath,
                       EnumerateDirectoryStatus.Success,
                       (int)NativeIOConstants.ErrorNoMoreFiles));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public EnumerateDirectoryResult EnumerateDirectoryEntries(
            string directoryPath,
            bool enumerateDirectory,
            string pattern,
            uint directoriesToSkipRecursively,
            bool recursive,
            IDirectoryEntriesAccumulator accumulators,
            bool isEnumerationForDirectoryDeletion)
        {
            try
            {
                var directoryEntries = Directory.GetFileSystemEntries(directoryPath);
                Array.Sort(directoryEntries, StringComparer.InvariantCulture);

                var enumerator  = directoryEntries.GetEnumerator();
                var accumulator = accumulators.Current;

                var patternRegex = GetRegex(pattern);

                while (enumerator.MoveNext())
                {
                    var entry    = enumerator.Current as string;
                    var filename = entry.Split(Path.DirectorySeparatorChar).Last();

                    if (!isEnumerationForDirectoryDeletion && filename.Equals(DsStoreMetaFileName))
                    {
                        continue;
                    }

                    FileAttributes attributes  = File.GetAttributes(entry);
                    var            isDirectory = (attributes & FileAttributes.Directory) == FileAttributes.Directory;

                    if (patternRegex.Match(filename).Success)
                    {
                        if (!(enumerateDirectory ^ isDirectory) && directoriesToSkipRecursively == 0)
                        {
                            accumulator.AddFile(filename);
                        }
                    }

                    accumulator.AddTrackFile(filename, attributes);

                    if ((recursive || directoriesToSkipRecursively > 0) && isDirectory)
                    {
                        accumulators.AddNew(accumulator, filename);

                        var recurs = EnumerateDirectoryEntries(
                            entry,
                            enumerateDirectory,
                            pattern,
                            directoriesToSkipRecursively == 0 ? 0 : directoriesToSkipRecursively - 1,
                            recursive,
                            accumulators,
                            isEnumerationForDirectoryDeletion);

                        if (!recurs.Succeeded)
                        {
                            return(recurs);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var result = EnumerateDirectoryResult.CreateFromException(directoryPath, ex);
                accumulators.Current.Succeeded = false;
                return(result);
            }

            return(new EnumerateDirectoryResult(
                       directoryPath,
                       EnumerateDirectoryStatus.Success,
                       (int)NativeIOConstants.ErrorNoMoreFiles));
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public EnumerateDirectoryResult EnumerateDirectoryEntries(
            string directoryPath,
            bool enumerateDirectory,
            string pattern,
            uint directoriesToSkipRecursively,
            bool recursive,
            IDirectoryEntriesAccumulator accumulators)
        {
            var accumulator = accumulators.Current;

            if (!enumerateDirectory && directoriesToSkipRecursively == 0)
            {
                var files = FindMatches(m_pathToContent.Keys, AbsolutePath.Create(m_pathTable, directoryPath), pattern, false);
                foreach (var file in files)
                {
                    accumulator.AddFile(file.GetName(m_pathTable).ToString(m_pathTable.StringTable));
                    accumulator.AddTrackFile(file.GetName(m_pathTable).ToString(m_pathTable.StringTable), FileAttributes.Normal);
                }
            }

            if (recursive || enumerateDirectory || directoriesToSkipRecursively > 0)
            {
                var directories = FindMatches(
                    m_directories,
                    AbsolutePath.Create(m_pathTable, directoryPath),
                    enumerateDirectory ? pattern : "*",
                    false);

                foreach (var directory in directories)
                {
                    var dirName = directory.GetName(m_pathTable).ToString(m_pathTable.StringTable);
                    if (enumerateDirectory && directoriesToSkipRecursively == 0)
                    {
                        accumulator.AddFile(dirName);
                    }

                    accumulator.AddTrackFile(dirName, FileAttributes.Directory);

                    if ((recursive || directoriesToSkipRecursively > 0))
                    {
                        accumulators.AddNew(accumulator, dirName);
                        var recurs = EnumerateDirectoryEntries(
                            Path.Combine(directoryPath, dirName),
                            enumerateDirectory,
                            pattern,
                            directoriesToSkipRecursively == 0 ? 0 : directoriesToSkipRecursively - 1,
                            recursive,
                            accumulators);

                        if (!recurs.Succeeded)
                        {
                            return(recurs);
                        }
                    }
                }
            }

            return(new EnumerateDirectoryResult(
                       directoryPath,
                       EnumerateDirectoryStatus.Success,
                       (int)NativeIOConstants.ErrorNoMoreFiles));
        }