def FileFinderOSFromClient( args: rdf_file_finder.FileFinderArgs ) -> Iterator[rdf_client_fs.StatEntry]: """This function expands paths from the args and returns related stat entries. Args: args: A proto message with arguments for the file finder action. Yields: Stat entries corresponding to the found files. """ stat_cache = filesystem.StatCache() opts = args.action.stat for path in GetExpandedPaths(args): try: content_conditions = conditions.ContentCondition.Parse( args.conditions) for content_condition in content_conditions: with io.open(path, "rb") as fd: result = list(content_condition.Search(fd)) if not result: raise _SkipFileException() stat = stat_cache.Get(path, follow_symlink=opts.resolve_links) stat_entry = client_utils.StatEntryFromStatPathSpec( stat, ext_attrs=opts.collect_ext_attrs) yield stat_entry except _SkipFileException: pass
def FileFinderOSFromClient(args): """This function expands paths from the args and returns related stat entries. Args: args: An `rdf_file_finder.FileFinderArgs` object. Yields: `rdf_paths.PathSpec` instances. """ stat_cache = filesystem.StatCache() opts = args.action.stat for path in GetExpandedPaths(args): try: content_conditions = conditions.ContentCondition.Parse( args.conditions) for content_condition in content_conditions: with io.open(path, "rb") as fd: result = list(content_condition.Search(fd)) if not result: raise _SkipFileException() stat = stat_cache.Get(path, follow_symlink=opts.resolve_links) stat_entry = client_utils.StatEntryFromStatPathSpec( stat, ext_attrs=opts.collect_ext_attrs) yield stat_entry except _SkipFileException: pass
def testSmartSymlinkCache(self): with open(self.Path("foo"), "wb") as fd: fd.write(b"12345") stat_cache = filesystem.StatCache() with MockStat() as stat_mock: foo_stat = stat_cache.Get(self.Path("foo"), follow_symlink=False) self.assertEqual(foo_stat.GetSize(), 5) self.assertTrue(stat_mock.FromPath.called) with MockStat() as stat_mock: other_foo_stat = stat_cache.Get(self.Path("foo"), follow_symlink=True) self.assertEqual(other_foo_stat.GetSize(), 5) self.assertFalse(stat_mock.FromPath.called)
def testFollowSymlink(self): with io.open(self.Path("foo"), "wb") as fd: fd.write(b"123456") os.symlink(self.Path("foo"), self.Path("bar")) stat_cache = filesystem.StatCache() with MockStat() as stat_mock: bar_stat = stat_cache.Get(self.Path("bar"), follow_symlink=False) self.assertTrue(bar_stat.IsSymlink()) self.assertTrue(stat_mock.FromPath.called) with MockStat() as stat_mock: foo_stat = stat_cache.Get(self.Path("bar"), follow_symlink=True) self.assertFalse(foo_stat.IsSymlink()) self.assertEqual(foo_stat.GetSize(), 6) self.assertTrue(stat_mock.FromPath.called)
def Run(self, args): if args.pathtype != rdf_paths.PathSpec.PathType.OS: raise ValueError( "FileFinderOS can only be used with OS paths, got {}".format( args.pathspec)) self.stat_cache = filesystem.StatCache() action = self._ParseAction(args) for path in GetExpandedPaths(args): self.Progress() try: matches = self._Validate(args, path) result = rdf_file_finder.FileFinderResult() result.matches = matches action.Execute(path, result) self.SendReply(result) except _SkipFileException: pass
def testBasicUsage(self): with io.open(self.Path("foo"), "wb") as fd: fd.write(b"123") with io.open(self.Path("bar"), "wb") as fd: fd.write(b"123456") with io.open(self.Path("baz"), "wb") as fd: fd.write(b"123456789") stat_cache = filesystem.StatCache() with MockStat() as stat_mock: foo_stat = stat_cache.Get(self.Path("foo")) self.assertEqual(foo_stat.GetSize(), 3) self.assertTrue(stat_mock.FromPath.called) with MockStat() as stat_mock: bar_stat = stat_cache.Get(self.Path("bar")) self.assertEqual(bar_stat.GetSize(), 6) self.assertTrue(stat_mock.FromPath.called) with MockStat() as stat_mock: other_foo_stat = stat_cache.Get(self.Path("foo")) self.assertEqual(other_foo_stat.GetSize(), 3) self.assertFalse(stat_mock.FromPath.called) with MockStat() as stat_mock: other_bar_stat = stat_cache.Get(self.Path("bar")) self.assertEqual(other_bar_stat.GetSize(), 6) self.assertFalse(stat_mock.FromPath.called) with MockStat() as stat_mock: baz_stat = stat_cache.Get(self.Path("baz")) self.assertEqual(baz_stat.GetSize(), 9) self.assertTrue(stat_mock.FromPath.called) with MockStat() as stat_mock: other_baz_stat = stat_cache.Get(self.Path("baz")) self.assertEqual(other_baz_stat.GetSize(), 9) self.assertFalse(stat_mock.FromPath.called)