Exemplo n.º 1
0
  def testFileFinderStatExtAttrs(self):
    with test_lib.AutoTempFilePath() as temp_filepath:
      client_test_lib.SetExtAttr(temp_filepath, name="user.bar", value="baz")
      client_test_lib.SetExtAttr(temp_filepath, name="user.quux", value="norf")

      action = rdf_file_finder.FileFinderAction.Stat()
      results = self.RunFlow(action=action, paths=[temp_filepath])
      self.assertEqual(len(results), 1)

      stat_entry = results[0][1].stat_entry
      self.assertItemsEqual(stat_entry.ext_attrs, [
          rdf_client.ExtAttr(name="user.bar", value="baz"),
          rdf_client.ExtAttr(name="user.quux", value="norf"),
      ])
Exemplo n.º 2
0
def GetExtAttrs(filepath):
  """Fetches extended file attributes.

  Args:
    filepath: A path to the file.

  Yields:
    `ExtAttr` pairs.
  """
  path = CanonicalPathToLocalPath(filepath)

  try:
    attr_names = xattr.listxattr(path)
  except (IOError, OSError) as error:
    msg = "Failed to retrieve extended attributes for '%s': %s"
    logging.error(msg, path, error)
    return

  for attr_name in attr_names:
    try:
      attr_value = xattr.getxattr(path, attr_name)
    except (IOError, OSError) as error:
      msg = "Failed to retrieve attribute '%s' for '%s': %s"
      logging.error(msg, attr_name, path, error)
      continue

    yield rdf_client.ExtAttr(name=attr_name, value=attr_value)
Exemplo n.º 3
0
    def testFileFinderStatExtAttrs(self):
        with test_lib.AutoTempFilePath() as temp_filepath:
            if subprocess.call(["which", "setfattr"]) != 0:
                raise unittest.SkipTest("`setfattr` command is not available")
            subprocess.check_call(
                ["setfattr", temp_filepath, "-n", "user.bar", "-v", "baz"])
            subprocess.check_call(
                ["setfattr", temp_filepath, "-n", "user.quux", "-v", "norf"])

            action = rdf_file_finder.FileFinderAction.Stat()
            results = self.RunFlow(action=action, paths=[temp_filepath])
            self.assertEqual(len(results), 1)

            stat_entry = results[0][1].stat_entry
            self.assertItemsEqual(stat_entry.ext_attrs, [
                rdf_client.ExtAttr(name="user.bar", value="baz"),
                rdf_client.ExtAttr(name="user.quux", value="norf"),
            ])
Exemplo n.º 4
0
def AddStatEntryExtAttrs(stat_entry):
    """Fills `ext_attrs` field of the `StatEntry` object.

  Args:
    stat_entry: A `StatEntry` object to fill-in.
  """
    path = CanonicalPathToLocalPath(stat_entry.pathspec.path)

    for attr_name in xattr.listxattr(path):
        attr_value = xattr.getxattr(path, attr_name)

        attr = rdf_client.ExtAttr(name=attr_name, value=attr_value)
        stat_entry.ext_attrs.append(attr)