def _ProcessFilePath(self, path, args): try: stat = utils.Stat(path, follow_symlink=False) except OSError: return if (not (stat.IsRegular() or stat.IsDirectory()) and not self.process_non_regular_files): return for metadata_condition in MetadataCondition.Parse(args.conditions): if not metadata_condition.Check(stat): return matches = [] for content_condition in ContentCondition.Parse(args.conditions): result = list(content_condition.Search(path)) if not result: return matches.extend(result) result = self._ProcessFile(path, stat, args) if result: result.matches = matches self.SendReply(result)
def testGetSize(self): with test_lib.AutoTempFilePath() as temp_filepath: with open(temp_filepath, "wb") as fd: fd.write("foobarbaz") stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertEqual(stat.GetSize(), 9)
def testRegular(self): with test_lib.AutoTempFilePath() as temp_filepath: stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertFalse(stat.IsDirectory()) self.assertTrue(stat.IsRegular()) self.assertFalse(stat.IsSocket()) self.assertFalse(stat.IsSymlink())
def testGetLinuxFlags(self): with test_lib.AutoTempFilePath() as temp_filepath: client_test_lib.Chattr(temp_filepath, attrs=["+c", "+d"]) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertTrue(stat.IsRegular()) self.assertTrue(stat.GetLinuxFlags() & self.FS_COMPR_FL) self.assertTrue(stat.GetLinuxFlags() & self.FS_NODUMP_FL) self.assertFalse(stat.GetLinuxFlags() & self.FS_IMMUTABLE_FL) self.assertEqual(stat.GetOsxFlags(), 0)
def testGetOsxFlags(self): with test_lib.AutoTempFilePath() as temp_filepath: client_test_lib.Chflags(temp_filepath, flags=["nodump", "hidden"]) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertTrue(stat.IsRegular()) self.assertTrue(stat.GetOsxFlags() & self.UF_NODUMP) self.assertTrue(stat.GetOsxFlags() & self.UF_HIDDEN) self.assertFalse(stat.GetOsxFlags() & self.UF_IMMUTABLE) self.assertEqual(stat.GetLinuxFlags(), 0)
def testGetFlagsSymlink(self): with test_lib.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\ test_lib.AutoTempFilePath() as temp_filepath: temp_linkpath = os.path.join(temp_dirpath, "foo") os.symlink(temp_filepath, temp_linkpath) stat = utils.Stat(temp_linkpath, follow_symlink=False) self.assertTrue(stat.IsSymlink()) self.assertEqual(stat.GetLinuxFlags(), 0) self.assertEqual(stat.GetOsxFlags(), 0)
def testGetOsxFlags(self): with test_lib.AutoTempFilePath() as temp_filepath: subprocess.check_call(["chflags", "nodump hidden", temp_filepath]) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertTrue(stat.IsRegular()) self.assertTrue(stat.GetOsxFlags() & self.UF_NODUMP) self.assertTrue(stat.GetOsxFlags() & self.UF_HIDDEN) self.assertFalse(stat.GetOsxFlags() & self.UF_IMMUTABLE) self.assertEqual(stat.GetLinuxFlags(), 0)
def testGetTime(self): adate = datetime.datetime(2017, 10, 2, 8, 45) mdate = datetime.datetime(2001, 5, 3, 10, 30) with test_lib.AutoTempFilePath() as temp_filepath: self._Touch(temp_filepath, "-a", adate) self._Touch(temp_filepath, "-m", mdate) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertEqual(stat.GetAccessTime(), self._EpochMillis(adate)) self.assertEqual(stat.GetModificationTime(), self._EpochMillis(mdate))
def testSymlink(self): with test_lib.AutoTempDirPath(remove_non_empty=True) as temp_dirpath,\ test_lib.AutoTempFilePath() as temp_filepath: with open(temp_filepath, "wb") as fd: fd.write("foobar") temp_linkpath = os.path.join(temp_dirpath, "foo") os.symlink(temp_filepath, temp_linkpath) stat = utils.Stat(temp_linkpath, follow_symlink=False) self.assertFalse(stat.IsDirectory()) self.assertFalse(stat.IsRegular()) self.assertFalse(stat.IsSocket()) self.assertTrue(stat.IsSymlink()) stat = utils.Stat(temp_linkpath, follow_symlink=True) self.assertFalse(stat.IsDirectory()) self.assertTrue(stat.IsRegular()) self.assertFalse(stat.IsSocket()) self.assertFalse(stat.IsSymlink()) self.assertEqual(stat.GetSize(), 6)
def testGetLinuxFlags(self): with test_lib.AutoTempFilePath() as temp_filepath: if subprocess.call(["which", "chattr"]) != 0: raise unittest.SkipTest("`chattr` command is not available") if subprocess.call(["chattr", "+c", "+d", temp_filepath]): reason = "extended attributes not supported by filesystem" raise unittest.SkipTest(reason) stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertTrue(stat.IsRegular()) self.assertTrue(stat.GetLinuxFlags() & self.FS_COMPR_FL) self.assertTrue(stat.GetLinuxFlags() & self.FS_NODUMP_FL) self.assertFalse(stat.GetLinuxFlags() & self.FS_IMMUTABLE_FL) self.assertEqual(stat.GetOsxFlags(), 0)
def Stat(self, fname, stat, opts): if opts.resolve_links and stat.IsSymlink(): try: stat = utils.Stat(fname, follow_symlink=True) except OSError: return None pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=client_utils.LocalPathToCanonicalPath(fname), path_options=rdf_paths.PathSpec.Options.CASE_LITERAL) return files.MakeStatResponse(stat, pathspec=pathspec, ext_attrs=opts.ext_attrs)
def testGetFlagsSocket(self): with test_lib.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: temp_socketpath = os.path.join(temp_dirpath, "foo") sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(temp_socketpath) stat = utils.Stat(temp_socketpath, follow_symlink=False) self.assertTrue(stat.IsSocket()) self.assertEqual(stat.GetLinuxFlags(), 0) self.assertEqual(stat.GetOsxFlags(), 0) finally: sock.close()
def testSocket(self): with test_lib.AutoTempDirPath(remove_non_empty=True) as temp_dirpath: temp_socketpath = os.path.join(temp_dirpath, "foo") sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(temp_socketpath) stat = utils.Stat(temp_socketpath, follow_symlink=False) self.assertFalse(stat.IsDirectory()) self.assertFalse(stat.IsRegular()) self.assertTrue(stat.IsSocket()) self.assertFalse(stat.IsSymlink()) finally: sock.close()
def _Generate(self, base_path, relative_components): """Generates the relative filenames.""" new_base = os.path.join(base_path, *relative_components) if not relative_components: yield new_base try: filenames = os.listdir(new_base) except OSError as e: if e.errno == errno.EACCES: # permission denied. logging.info(e) return for f in filenames: new_components = relative_components + [f] relative_name = os.path.join(*new_components) yield relative_name if len(new_components) >= self.depth: continue filename = os.path.join(base_path, relative_name) try: stat = utils.Stat(filename) if not stat.IsDirectory(): continue if filename in self.mountpoints_blacklist: continue if (not self.follow_links and utils.Stat( filename, follow_symlink=False).IsSymlink()): continue for res in self._Generate(base_path, new_components): yield res except OSError as e: if e.errno not in [errno.ENOENT, errno.ENOTDIR, errno.EINVAL]: logging.info(e)
def StatEntryFromPath(path, pathspec, ext_attrs=False): """Builds a stat entry object from a given path. Args: path: A path (string value) to stat. pathspec: A `PathSpec` corresponding to the `path`. ext_attrs: Whether to include extended file attributes in the result. Returns: `StatEntry` object. """ try: stat = utils.Stat(path) except (IOError, OSError) as error: logging.error("Failed to obtain stat for '%s': %s", pathspec, error) return rdf_client.StatEntry(pathspec=pathspec) return StatEntryFromStat(stat, pathspec, ext_attrs=ext_attrs)
def _ProcessFile(self, fname, stat, args): if args.action.action_type == args.action.Action.STAT: return self._ExecuteStat(fname, stat, args) # For directories, only Stat makes sense. if stat.IsDirectory(): return None # We never want to hash/download the link, always the target. if stat.IsSymlink(): try: stat = utils.Stat(fname, follow_symlink=True) except OSError: return None if args.action.action_type == args.action.Action.DOWNLOAD: return self._ExecuteDownload(fname, stat, args) if args.action.action_type == args.action.Action.HASH: return self._ExecuteHash(fname, stat, args) raise ValueError("incorrect action type: %s" % args.action.action_type)
def _GetStat(self, local_path): try: return utils.Stat(local_path) except IOError as e: logging.info("Failed to Stat %s. Err: %s", local_path, e) return None
def ListFiles(self): for f in os.listdir(self._AbsPath()): ps = self.pathspec.Copy() ps.last.path = os.path.join(ps.last.path, f) stat = utils.Stat(self._AbsPath(f)) yield files.MakeStatResponse(stat, ps)
def Stat(self): """Get Stat for self.path.""" test_data_path = self._AbsPath() stat = utils.Stat(test_data_path) return files.MakeStatResponse(stat, self.pathspec)
def Stat(self): return utils.Stat(self.temp_filepath, follow_symlink=False)
def _GetDevice(self, path): try: return utils.Stat(path).GetDevice() except (IOError, OSError) as error: logging.error("Failed to obtain device for '%s' (%s)", path, error) return None
def testGetPath(self): with test_lib.AutoTempFilePath() as temp_filepath: stat = utils.Stat(temp_filepath, follow_symlink=False) self.assertEqual(stat.GetPath(), temp_filepath)