private void AddFiles(string directory, string filter, FileNameFilter hardFilter) { string[] fileArray = null; try { fileArray = Directory.GetFiles(directory, filter, SearchOption.TopDirectoryOnly); } catch (System.Exception e) { error = e.Message; return; } if (fileArray == null) { error = "Unknown error (files is null)"; return; } for (int i = 0; i < fileArray.Length; ++i) { string file = fileArray[i]; if (hardFilter != null) { string name = Path.GetFileName(file); if (hardFilter.Match(name)) { files.Add(file); } } else { files.Add(file); } } }
protected override void ProcessRecord() { source = new FileSource { Name = Name, Root = PathUtilities.GetRelativePath(Root.FullName), Converter = new FileToEntryConverter() }; if (!String.IsNullOrEmpty(InstallMode)) { var mode = (InstallMode)Enum.Parse(typeof(InstallMode), InstallMode); if (mode != Sitecore.Install.Utils.InstallMode.Undefined) { source.Converter.Transforms.Add( new InstallerConfigurationTransform( new BehaviourOptions(mode, MergeMode.Undefined))); } } if (string.IsNullOrEmpty(IncludeFilter)) { IncludeFilter = "*.*"; } source.Include.Add(new FileNameFilter(IncludeFilter)); source.Include.Add(new FileDateFilter(FileDateFilter.FileDateFilterType.CreatedFilter)); source.Include.Add(new FileDateFilter(FileDateFilter.FileDateFilterType.ModifiedFilter)); if (!string.IsNullOrEmpty(ExcludeFilter)) { var filter = new FileNameFilter(ExcludeFilter); source.Exclude.Add(filter); } WriteObject(source, false); }
public IntInfo(int value, string pattern) { this.pattern = pattern; this.value = value; if (this.pattern != null) { filter = new FileNameFilter(pattern); } }
public BoolInfo(bool value, string pattern) { this.pattern = pattern; this.value = value; if (this.pattern != null) { filter = new FileNameFilter(pattern); } }
private void Expand(Node node) { if (node.type == NodeType.Directory) { node.expanded = true; node.childs.Clear(); List <string> directories = null; List <string> files = null; try { string[] rawDirectories = Directory.GetDirectories(node.fullPath); string[] rawFiles = Directory.GetFiles(node.fullPath); directories = new List <string>(); files = new List <string>(); FileNameFilter filter = !string.IsNullOrEmpty(hideInFileTree) ? new FileNameFilter(hideInFileTree) : null; foreach (string directory in rawDirectories) { if (filter == null || !filter.Match(Path.GetFileName(directory))) { directories.Add(directory); } } foreach (string file in rawFiles) { if (filter == null || !filter.Match(Path.GetFileName(file))) { files.Add(file); } } } catch (Exception e) { node.childs.Add(new Node(NodeType.Error, CommonHelper.GetOneLine(e.Message), node.fullPath + "#error")); return; } foreach (string file in directories) { Node nodeI = new Node(NodeType.Directory, Path.GetFileName(file), Path.GetFullPath(file)); if (expanded.ContainsKey(nodeI.hash)) { Expand(nodeI); } node.childs.Add(nodeI); } foreach (string file in files) { Node nodeI = new Node(NodeType.File, Path.GetFileName(file), Path.GetFullPath(file)); if (expanded.ContainsKey(nodeI.hash)) { Expand(nodeI); } node.childs.Add(nodeI); } } }
public void ShouldExcludeFilesMatchingExcludeListButNotInIncludesList() { var globToRegex = new Mock<IGlobToRegexConverter>(); globToRegex.Setup(g => g.ConvertToRegex("*.xml")).Returns(new Regex(@".*\.xml$")); globToRegex.Setup(g => g.ConvertToRegex("include.xml")).Returns(new Regex(@"include\.xml$")); globToRegex.Setup(g => g.ConvertToRegex(@"_svn\")).Returns(new Regex(@"_svn\\.*$")); var excluder = new FileNameFilter(globToRegex.Object, new [] {"*.xml", @"_svn\"}, new [] {"include.xml"}); Assert.That(excluder.IncludeFile("test.xml"), Is.EqualTo(false)); Assert.That(excluder.IncludeFile("include.xml"), Is.EqualTo(true)); Assert.That(excluder.IncludeFile("test.txt"), Is.EqualTo(true)); Assert.That(excluder.IncludeFile(@"_svn\test.txt"), Is.EqualTo(false)); }
public void invalid_patterns_throw_an_ArgumentException() { var filter = new FileNameFilter(); Assert.Throws <ArgumentNullException>(() => filter.Filters.Add(null)); Assert.Throws <ArgumentException>(() => filter.Filters.Add(new PathFilter(true, ""))); Assert.Throws <ArgumentException>(() => filter.Filters.Add(new PathFilter(true, " "))); Assert.Throws <ArgumentException>(() => filter.Filters.Add(new PathFilter(true, @"d:"))); Assert.Throws <ArgumentException>(() => filter.Filters.Add(new PathFilter(true, @"C:\Pouf"))); Assert.Throws <ArgumentException>(() => filter.Filters.Add(new PathFilter(true, @"\\server\share"))); Assert.Throws <ArgumentException>(() => filter.Filters.Add(new PathFilter(true, @"\\?\C:\Pouf"))); Assert.Throws <ArgumentException>(() => filter.Filters.Add(new PathFilter(true, @"\\?\UNC\server\share"))); }
public void ShouldExcludeFilesMatchingExcludeListButNotInIncludesList() { var globToRegex = new Mock <IGlobToRegexConverter>(); globToRegex.Setup(g => g.ConvertToRegex("*.xml")).Returns(new Regex(@".*\.xml$")); globToRegex.Setup(g => g.ConvertToRegex("include.xml")).Returns(new Regex(@"include\.xml$")); globToRegex.Setup(g => g.ConvertToRegex(@"_svn\")).Returns(new Regex(@"_svn\\.*$")); var excluder = new FileNameFilter(globToRegex.Object, new [] { "*.xml", @"_svn\" }, new [] { "include.xml" }); Assert.That(excluder.IncludeFile("test.xml"), Is.EqualTo(false)); Assert.That(excluder.IncludeFile("include.xml"), Is.EqualTo(true)); Assert.That(excluder.IncludeFile("test.txt"), Is.EqualTo(true)); Assert.That(excluder.IncludeFile(@"_svn\test.txt"), Is.EqualTo(false)); }
public static void PackDirectoryToTGZ(string dir, string archive, FileNameFilter filter) { byte[] padding = new byte[1024]; byte[] tmp = new byte[1024 * 1024]; int filesDone = 0; long bytesDone = 0; using (var fs = File.Create(archive)) using (var gs = new GZipStream(fs, CompressionMode.Compress)) { ArchiveDirectoryToTARRecursively(gs, dir, "", ref filesDone, ref bytesDone, padding, tmp, filter); gs.Write(padding, 0, 1024); } }
/// <summary> /// Reflects directory structure of observed files and directories /// </summary> /// <param name="watcher">Raises events on file system changes</param> /// <param name="sequentialId">Used to generate identifiers to track file content</param> /// <param name="systemFileNameFilter">A deterministic function to limit the observed scope</param> public Mirror( Watcher watcher, SequentialId sequentialId, FileNameFilter systemFileNameFilter = null) { _watcher = watcher; _watcher.ChangeDetected += changeDetected; _watcher.Error += watcherError; _fileNameFilter = systemFileNameFilter ?? (fullFileName => true); _sequentialId = sequentialId; _root = new RootEntry <Metadata>(() => new Metadata(SequentialId.None)); _createdEntriesQueue.Enqueued += (sender, entry) => EnqueuedCreatedEntry?.Invoke(this, entry); _deletedEntriesQueue.Enqueued += (sender, entry) => EnqueuedDeletedEntry?.Invoke(this, entry); _movedEntriesQueue.Enqueued += (sender, entry) => EnqueuedMovedEntry?.Invoke(this, entry); }
public void when_no_exlude_nor_include_matches_result_is_None() { FileGroupTarget target = new FileGroupTarget(); FileNameFilter filter = new FileNameFilter(); filter.Filters.AddRangeArray(new PathFilter(true, "**/*.inc"), new PathFilter(false, "**/*.exc")); target.Filters.Add(filter); FileGroupTarget.Result r; { Assert.That(target.IncludedFile("toto.exc", out r), Is.EqualTo(FilterMatchResult.Excluded)); Assert.That(target.IncludedFile("a/b.toto.exc", out r), Is.EqualTo(FilterMatchResult.Excluded)); Assert.That(target.IncludedFile("toto.inc", out r), Is.EqualTo(FilterMatchResult.Included)); Assert.That(target.IncludedFile("a/b/b.toto.inc", out r), Is.EqualTo(FilterMatchResult.Included)); Assert.That(target.IncludedFile(String.Empty, out r), Is.EqualTo(FilterMatchResult.None)); Assert.That(target.IncludedFile("murfn", out r), Is.EqualTo(FilterMatchResult.None)); } }
/// <summary> /// Adds a filter /// </summary> /// <param name="filter"></param> public void AddFilter(IFileQueryFilter filter) { // The first simple file name filter can be used in DirectoryInfo.GetFiles() if (SimpleFileNameFilter == null && (filter is FileNameFilter && ((FileNameFilter)filter).IsSimpleFileNameFilter)) { SimpleFileNameFilter = (FileNameFilter)filter; } if (filter is FileContentsFilter) { // File contents filters are the most labor intensive so add them to the end of the list _filterList.AddLast(filter); } else { // Other filters go to the front of the list _filterList.AddFirst(filter); } }
protected override void ProcessRecord() { source = new FileSource {Name = Name, Root = Root, Converter = new FileToEntryConverter()}; if (string.IsNullOrEmpty(IncludeFilter)) { IncludeFilter = "*.*"; } source.Include.Add(new FileNameFilter(IncludeFilter)); source.Include.Add(new FileDateFilter(FileDateFilter.FileDateFilterType.CreatedFilter)); source.Include.Add(new FileDateFilter(FileDateFilter.FileDateFilterType.ModifiedFilter)); if (!string.IsNullOrEmpty(ExcludeFilter)) { var filter = new FileNameFilter(ExcludeFilter); source.Exclude.Add(filter); } WriteObject(source, false); }
public void selecting_files_from_this_solution() { using (var fs = new VF()) { FileGroupTarget fgt = new FileGroupTarget(); fgt.MatchBehavior = FileFilterMatchBehavior.NoneIsIncluded; FileNameFilter filter = new FileNameFilter(); filter.Filters.Add(new PathFilter(false, "/.git/")); filter.Filters.Add(new PathFilter(false, "/CK.Globbing/File*/")); filter.Filters.Add(new PathFilter(true, "/packages/**/Circles/*.jpg")); filter.Filters.Add(new PathFilter(false, "/packages/**/*.jpg")); filter.Filters.Add(new PathFilter(false, "/**/ob?/")); fgt.Filters.Add(filter); IList <string> result = fgt.IncludedFiles("E:\\Dev\\CK-FileFilter\\", fs).Select(r => r.FilePath).ToList(); Assert.That(result.Count, Is.GreaterThan(1)); Assert.That(result.Any(f => f.Contains("\\.git\\")), Is.False); Assert.That(result.Any(f => f.Contains("\\CK.Globbing\\File")), Is.False); Assert.That(result.Count(f => f.EndsWith(".jpg")), Is.GreaterThan(0)); Assert.That(result.Where(f => f.EndsWith(".jpg")).All(f => f.Contains("\\Circles\\"))); Assert.That(result.Any(f => f.Contains("\\obj\\")), Is.False); filter.Clear(); filter.Filters.Add(new PathFilter(false, "**/NUnit.Runners.2.6.4/tools/lib/**/*/")); filter.Filters.Add(new PathFilter(true, "**/NUnit.Runners.2.6.4/tools/lib/")); fgt.MatchBehavior = FileFilterMatchBehavior.Default; result = fgt.IncludedFiles("E:\\Dev\\CK-FileFilter\\", fs).Select(r => r.FilePath).ToList(); Assert.That(result.Count(), Is.EqualTo(8)); } }
protected override void ProcessRecord() { source = new FileSource { Name = Name, Root = PathUtilities.GetRelativePath(Root.FullName), Converter = new FileToEntryConverter()}; if (!String.IsNullOrEmpty(InstallMode)) { var mode = (InstallMode) Enum.Parse(typeof (InstallMode), InstallMode); if (mode != Sitecore.Install.Utils.InstallMode.Undefined) { source.Converter.Transforms.Add( new InstallerConfigurationTransform( new BehaviourOptions(mode, MergeMode.Undefined))); } } if (string.IsNullOrEmpty(IncludeFilter)) { IncludeFilter = "*.*"; } source.Include.Add(new FileNameFilter(IncludeFilter)); source.Include.Add(new FileDateFilter(FileDateFilter.FileDateFilterType.CreatedFilter)); source.Include.Add(new FileDateFilter(FileDateFilter.FileDateFilterType.ModifiedFilter)); if (!string.IsNullOrEmpty(ExcludeFilter)) { var filter = new FileNameFilter(ExcludeFilter); source.Exclude.Add(filter); } WriteObject(source, false); }
public void PackFromList() { var t1 = new FileGroupTarget() { Target = "/Target1" }; var fn1 = new FileNameFilter() { Root = "ForTarget1" }; var pf1 = new PathFilter(true, "/**.txt"); t1.Filters.Add(fn1); fn1.Filters.Add(pf1); var t2 = new FileGroupTarget() { Target = "/Target2" }; var fn2 = new FileNameFilter() { Root = "ForTarget2" }; var pf2 = new PathFilter(false, "/**.txt"); t2.Filters.Add(fn2); fn2.Filters.Add(pf2); var t3 = new FileGroupTarget() { Target = "/Target3" }; var fn3 = new FileNameFilter() { Root = "ForTarget3" }; var pf3e = new PathFilter(false, "/File6.txt"); var pf3i = new PathFilter(true, "/**.txt"); t3.Filters.Add(fn3); fn3.Filters.Add(pf3e); fn3.Filters.Add(pf3i); var t4 = new FileGroupTarget() { Target = "/" }; var fn4 = new FileNameFilter() { Root = "/" }; var pf4 = new PathFilter(true, "/RootFile.txt"); t4.Filters.Add(fn4); fn4.Filters.Add(pf4); var targets = new[] { t1, t2, t3, t4 }; var outputFile = System.IO.Path.Combine(_outputDirectory, "PackFromList.zip"); CKPackAliases.Pack(_context, targets, outputFile); Assert.IsTrue(File.Exists(outputFile), "Created zip - Output file not found"); CheckZipContent(outputFile, new[] { "RootFile.txt", @"Target1\File1.txt", @"Target1\File2.txt", @"Target3\File5.txt" }); }
public void Scan() { files.Clear(); error = null; string filter = this.filter; FileNameFilter hardFilter = null; if (string.IsNullOrEmpty(filter)) { filter = "*"; } else if (filter.Contains(";")) { hardFilter = new FileNameFilter(filter); filter = "*"; } List <string> ignoreDirsList = new List <string>(); string[] ignoreDirs = this.ignoreDirs.Replace('/', '\\').Split(';'); bool ignoreRoot = false; for (int i = 0; i < ignoreDirs.Length; ++i) { string dir = ignoreDirs[i]; dir = dir.Trim(); if (string.IsNullOrEmpty(dir)) { continue; } if (dir.Length > 0 && dir[0] == '\\') { dir = dir.Substring(1); } if (dir.Length > 0 && dir[dir.Length - 1] == '\\') { dir = dir.Substring(0, dir.Length - 1); } if (dir == "" || dir == ".") { ignoreRoot = true; continue; } if (dir.IndexOf('\\') != -1) { error = "Unexpected: \"" + dir + "\"\n(only single root dirs supported in ignore dirs list)"; dir = dir.Substring(0, dir.IndexOf('\\')); if (dir == "") { continue; } } ignoreDirsList.Add(dir.ToLowerInvariant()); } ignoreDirs = ignoreDirsList.ToArray(); List <string> directories = new List <string>(); if (directory.IndexOf(';') != -1) { foreach (string rawDirectoryI in directory.Split(';')) { string directoryI = rawDirectoryI.Trim(); if (string.IsNullOrEmpty(directoryI)) { continue; } directories.Add(directoryI); } } else { directories.Add(directory); } string currentDirectory = Directory.GetCurrentDirectory(); foreach (string rawDirectory in directories) { string directoryI = rawDirectory == "." ? currentDirectory : rawDirectory; if (ignoreDirs.Length == 0 && !ignoreRoot) { AddFilesRecursive(directoryI, filter, hardFilter); } else { if (!ignoreRoot) { AddFiles(directoryI, filter, hardFilter); } string[] dirs = null; try { dirs = Directory.GetDirectories(directoryI); } catch (System.Exception e) { error = e.Message + " (root directories)"; done = true; return; } if (dirs == null) { error = "Unknown error (root directories is null)"; done = true; return; } for (int i = 0; i < dirs.Length; ++i) { string dir = dirs[i]; string dirToMatch = dir; if (dirToMatch.IndexOf('/') != -1) { dirToMatch = dirToMatch.Replace('/', '\\'); } int index = dirToMatch.LastIndexOf('\\'); if (index != -1) { string dirName = dirToMatch.Substring(index + 1); if (!string.IsNullOrEmpty(dirName) && Array.IndexOf(ignoreDirs, dirName.ToLowerInvariant()) != -1) { continue; } } AddFilesRecursive(dir, filter, hardFilter); } } } done = true; }
static void ArchiveDirectoryToTARRecursively(Stream tarStream, string absoluteDirectory, string relativeDirectoryInUnixFormat, ref int filesDone, ref long bytesDone, byte[] paddingBuffer, byte[] tempBuffer, FileNameFilter filter) { WIN32_FIND_DATA findData; List <SubdirInfo> subdirs = new List <SubdirInfo>(); IntPtr hFind = FindFirstFile(absoluteDirectory + "\\*.*", out findData); int paddingSize; if (hFind == (IntPtr)(-1)) { return; } try { do { if ((findData.cFileName == ".") || (findData.cFileName == "..")) { continue; } if ((findData.dwFileAttributes & FileAttributes.Directory) != 0) { subdirs.Add(new SubdirInfo { Name = findData.cFileName, Date = DateTime.FromFileTime(MAKELONGLONG(findData.ftLastWriteTime.dwHighDateTime, findData.ftLastWriteTime.dwLowDateTime)) }); } else { string localFN = Path.Combine(absoluteDirectory, findData.cFileName); if (filter != null && !filter(localFN)) { continue; } using (var fsLocal = File.Open(localFN, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { string rel = relativeDirectoryInUnixFormat; if (rel != "") { rel += "/"; } rel += findData.cFileName; var timestamp = DateTime.FromFileTime(MAKELONGLONG(findData.ftLastWriteTime.dwHighDateTime, findData.ftLastWriteTime.dwLowDateTime)); long len = fsLocal.Length; var hdr = TarPacker.CreateHeader(rel, false, len, timestamp, out paddingSize); tarStream.Write(hdr, 0, hdr.Length); CopyStreamWithProgress(fsLocal, tarStream, ref filesDone, ref bytesDone, tempBuffer, len); tarStream.Write(paddingBuffer, 0, paddingSize); } } } while (FindNextFile(hFind, out findData)); } finally { FindClose(hFind); } foreach (var subdir in subdirs) { string rel = relativeDirectoryInUnixFormat; if (rel != "") { rel += "/"; } rel += subdir.Name; tarStream.Write(TarPacker.CreateHeader(rel, true, 0, subdir.Date, out paddingSize), 0, 512); ArchiveDirectoryToTARRecursively(tarStream, Path.Combine(absoluteDirectory, subdir.Name), rel, ref filesDone, ref bytesDone, paddingBuffer, tempBuffer, filter); } }
static void ArchiveDirectoryToTARRecursively(Stream tarStream, string absoluteDirectory, string relativeDirectoryInUnixFormat, ref int filesDone, ref long bytesDone, byte[] paddingBuffer, byte[] tempBuffer, FileNameFilter filter) { WIN32_FIND_DATA findData; List<SubdirInfo> subdirs = new List<SubdirInfo>(); IntPtr hFind = FindFirstFile(absoluteDirectory + "\\*.*", out findData); int paddingSize; if (hFind == (IntPtr)(-1)) return; try { do { if ((findData.cFileName == ".") || (findData.cFileName == "..")) continue; if ((findData.dwFileAttributes & FileAttributes.Directory) != 0) subdirs.Add(new SubdirInfo { Name = findData.cFileName, Date = DateTime.FromFileTime(MAKELONGLONG(findData.ftLastWriteTime.dwHighDateTime, findData.ftLastWriteTime.dwLowDateTime)) }); else { string localFN = Path.Combine(absoluteDirectory, findData.cFileName); if (filter != null && !filter(localFN)) continue; using (var fsLocal = File.Open(localFN, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { string rel = relativeDirectoryInUnixFormat; if (rel != "") rel += "/"; rel += findData.cFileName; var timestamp = DateTime.FromFileTime(MAKELONGLONG(findData.ftLastWriteTime.dwHighDateTime, findData.ftLastWriteTime.dwLowDateTime)); long len = fsLocal.Length; var hdr = TarPacker.CreateHeader(rel, false, len, timestamp, out paddingSize); tarStream.Write(hdr, 0, hdr.Length); CopyStreamWithProgress(fsLocal, tarStream, ref filesDone, ref bytesDone, tempBuffer, len); tarStream.Write(paddingBuffer, 0, paddingSize); } } } while (FindNextFile(hFind, out findData)); } finally { FindClose(hFind); } foreach (var subdir in subdirs) { string rel = relativeDirectoryInUnixFormat; if (rel != "") rel += "/"; rel += subdir.Name; tarStream.Write(TarPacker.CreateHeader(rel, true, 0, subdir.Date, out paddingSize), 0, 512); ArchiveDirectoryToTARRecursively(tarStream, Path.Combine(absoluteDirectory, subdir.Name), rel, ref filesDone, ref bytesDone, paddingBuffer, tempBuffer, filter); } }