Java Code Examples for org.apache.hadoop.fs.FileSystem.getXAttrs()

The following are Jave code examples for showing how to use getXAttrs() of the org.apache.hadoop.fs.FileSystem class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: circus-train   File: S3MapReduceCpTestUtils.java   Source Code and License Vote up 6 votes
/**
 * Asserts the XAttrs returned by getXAttrs for a specific path match an expected set of XAttrs.
 *
 * @param path String path to check
 * @param fs FileSystem to use for the path
 * @param expectedXAttrs XAttr[] expected xAttrs
 * @throws Exception if there is any error
 */
public static void assertXAttrs(Path path, FileSystem fs, Map<String, byte[]> expectedXAttrs) throws Exception {
  Map<String, byte[]> xAttrs = fs.getXAttrs(path);
  assertEquals(path.toString(), expectedXAttrs.size(), xAttrs.size());
  Iterator<Entry<String, byte[]>> i = expectedXAttrs.entrySet().iterator();
  while (i.hasNext()) {
    Entry<String, byte[]> e = i.next();
    String name = e.getKey();
    byte[] value = e.getValue();
    if (value == null) {
      assertTrue(xAttrs.containsKey(name) && xAttrs.get(name) == null);
    } else {
      assertArrayEquals(value, xAttrs.get(name));
    }
  }
}
 
Example 2
Project: hadoop   File: TestINodeAttributeProvider.java   Source Code and License Vote up 6 votes
@Test
public void testCustomProvider() throws Exception {
  FileSystem fs = FileSystem.get(miniDFS.getConfiguration(0));
  fs.mkdirs(new Path("/user/xxx"));
  FileStatus status = fs.getFileStatus(new Path("/user/xxx"));
  Assert.assertEquals(System.getProperty("user.name"), status.getOwner());
  Assert.assertEquals("supergroup", status.getGroup());
  Assert.assertEquals(new FsPermission((short) 0755), status.getPermission());
  fs.mkdirs(new Path("/user/authz"));
  Path p = new Path("/user/authz");
  status = fs.getFileStatus(p);
  Assert.assertEquals("foo", status.getOwner());
  Assert.assertEquals("bar", status.getGroup());
  Assert.assertEquals(new FsPermission((short) 0770), status.getPermission());
  AclStatus aclStatus = fs.getAclStatus(p);
  Assert.assertEquals(1, aclStatus.getEntries().size());
  Assert.assertEquals(AclEntryType.GROUP, aclStatus.getEntries().get(0)
          .getType());
  Assert.assertEquals("xxx", aclStatus.getEntries().get(0)
          .getName());
  Assert.assertEquals(FsAction.ALL, aclStatus.getEntries().get(0)
          .getPermission());
  Map<String, byte[]> xAttrs = fs.getXAttrs(p);
  Assert.assertTrue(xAttrs.containsKey("user.test"));
  Assert.assertEquals(2, xAttrs.get("user.test").length);
}
 
Example 3
Project: hadoop   File: DistCpTestUtils.java   Source Code and License Vote up 6 votes
/**
  * Asserts the XAttrs returned by getXAttrs for a specific path match an
  * expected set of XAttrs.
  *
  * @param path String path to check
  * @param fs FileSystem to use for the path
  * @param expectedXAttrs XAttr[] expected xAttrs
  * @throws Exception if there is any error
  */
public static void assertXAttrs(Path path, FileSystem fs,
    Map<String, byte[]> expectedXAttrs)
    throws Exception {
  Map<String, byte[]> xAttrs = fs.getXAttrs(path);
  assertEquals(path.toString(), expectedXAttrs.size(), xAttrs.size());
  Iterator<Entry<String, byte[]>> i = expectedXAttrs.entrySet().iterator();
  while (i.hasNext()) {
    Entry<String, byte[]> e = i.next();
    String name = e.getKey();
    byte[] value = e.getValue();
    if (value == null) {
      assertTrue(xAttrs.containsKey(name) && xAttrs.get(name) == null);
    } else {
      assertArrayEquals(value, xAttrs.get(name));
    }
  }
}
 
Example 4
Project: hadoop   File: FSOperations.java   Source Code and License Vote up 5 votes
/**
 * Executes the filesystem operation.
 *
 * @param fs filesystem instance to use.
 *
 * @return Map a map object (JSON friendly) with the xattrs.
 *
 * @throws IOException thrown if an IO error occured.
 */
@Override
public Map execute(FileSystem fs) throws IOException {
  Map<String, byte[]> xattrs = null;
  if (names != null && !names.isEmpty()) {
    xattrs = fs.getXAttrs(path, names);
  } else {
    xattrs = fs.getXAttrs(path);
  }
  return xAttrsToJSON(xattrs, encoding);
}
 
Example 5
Project: hadoop   File: FSXAttrBaseTest.java   Source Code and License Vote up 5 votes
private void verifySecurityXAttrExists(FileSystem userFs) throws Exception {
  try {
    final Map<String, byte[]> xattrs = userFs.getXAttrs(filePath);
    Assert.assertEquals(1, xattrs.size());
    Assert.assertNotNull(xattrs.get(security1));
    Assert.assertArrayEquals("expected empty byte[] from getXAttr",
        new byte[0], userFs.getXAttr(filePath, security1));

  } catch (AccessControlException e) {
    fail("getXAttrs failed but expected it to succeed");
  }
}
 
Example 6
Project: hadoop   File: DistCpUtils.java   Source Code and License Vote up 5 votes
/**
 * Converts a FileStatus to a CopyListingFileStatus.  If preserving ACLs,
 * populates the CopyListingFileStatus with the ACLs. If preserving XAttrs,
 * populates the CopyListingFileStatus with the XAttrs.
 *
 * @param fileSystem FileSystem containing the file
 * @param fileStatus FileStatus of file
 * @param preserveAcls boolean true if preserving ACLs
 * @param preserveXAttrs boolean true if preserving XAttrs
 * @param preserveRawXAttrs boolean true if preserving raw.* XAttrs
 * @throws IOException if there is an I/O error
 */
public static CopyListingFileStatus toCopyListingFileStatus(
    FileSystem fileSystem, FileStatus fileStatus, boolean preserveAcls, 
    boolean preserveXAttrs, boolean preserveRawXAttrs) throws IOException {
  CopyListingFileStatus copyListingFileStatus =
    new CopyListingFileStatus(fileStatus);
  if (preserveAcls) {
    FsPermission perm = fileStatus.getPermission();
    if (perm.getAclBit()) {
      List<AclEntry> aclEntries = fileSystem.getAclStatus(
        fileStatus.getPath()).getEntries();
      copyListingFileStatus.setAclEntries(aclEntries);
    }
  }
  if (preserveXAttrs || preserveRawXAttrs) {
    Map<String, byte[]> srcXAttrs = fileSystem.getXAttrs(fileStatus.getPath());
    if (preserveXAttrs && preserveRawXAttrs) {
       copyListingFileStatus.setXAttrs(srcXAttrs);
    } else {
      Map<String, byte[]> trgXAttrs = Maps.newHashMap();
      final String rawNS =
          StringUtils.toLowerCase(XAttr.NameSpace.RAW.name());
      for (Map.Entry<String, byte[]> ent : srcXAttrs.entrySet()) {
        final String xattrName = ent.getKey();
        if (xattrName.startsWith(rawNS)) {
          if (preserveRawXAttrs) {
            trgXAttrs.put(xattrName, ent.getValue());
          }
        } else if (preserveXAttrs) {
          trgXAttrs.put(xattrName, ent.getValue());
        }
      }
      copyListingFileStatus.setXAttrs(trgXAttrs);
    }
  }
  return copyListingFileStatus;
}
 
Example 7
Project: hadoop   File: DistCpUtils.java   Source Code and License Vote up 5 votes
/**
 * Determines if a file system supports XAttrs by running a getXAttrs request
 * on the file system root. This method is used before distcp job submission
 * to fail fast if the user requested preserving XAttrs, but the file system
 * cannot support XAttrs.
 * 
 * @param fs FileSystem to check
 * @throws XAttrsNotSupportedException if fs does not support XAttrs
 */
public static void checkFileSystemXAttrSupport(FileSystem fs)
    throws XAttrsNotSupportedException {
  try {
    fs.getXAttrs(new Path(Path.SEPARATOR));
  } catch (Exception e) {
    throw new XAttrsNotSupportedException("XAttrs not supported for file system: "
      + fs.getUri());
  }
}
 
Example 8
Project: hadoop   File: DistCpUtils.java   Source Code and License Vote up 2 votes
/**
 * Returns a file's all xAttrs.
 * 
 * @param fileSystem FileSystem containing the file
 * @param path file path
 * @return Map containing all xAttrs
 * @throws IOException if there is an I/O error
 */
public static Map<String, byte[]> getXAttrs(FileSystem fileSystem,
    Path path) throws IOException {
  return fileSystem.getXAttrs(path);
}