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

The following examples show how to use org.apache.hadoop.fs.FileSystem#getXAttrs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3MapReduceCpTestUtils.java    From circus-train with Apache License 2.0 6 votes vote down vote up
/**
 * 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
Source File: TestINodeAttributeProvider.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@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
Source File: DistCpTestUtils.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
  * 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
Source File: TestINodeAttributeProvider.java    From big-c with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: DistCpTestUtils.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
  * 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 6
Source File: FSOperations.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: FSXAttrBaseTest.java    From hadoop with Apache License 2.0 5 votes vote down vote up
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 8
Source File: DistCpUtils.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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 9
Source File: DistCpUtils.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source File: FSOperations.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source File: FSXAttrBaseTest.java    From big-c with Apache License 2.0 5 votes vote down vote up
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 12
Source File: DistCpUtils.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * 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 13
Source File: DistCpUtils.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * 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 14
Source File: DistCpUtils.java    From hadoop with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: DistCpUtils.java    From big-c with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}