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

The following examples show how to use org.apache.hadoop.fs.FileSystem#setAcl() . These examples are extracted from open source projects. 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 Project: hadoop   File: BaseTestHttpFSWith.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Simple acl tests on a directory: set a default acl, remove default acls.
 * @throws Exception
 */
private void testDirAcls() throws Exception {
  if ( isLocalFS() ) {
    return;
  }

  final String defUser1 = "default:user:glarch:r-x";

  FileSystem proxyFs = FileSystem.get(getProxiedFSConf());
  FileSystem httpfs = getHttpFSFileSystem();

  Path dir = getProxiedFSTestDir();

  /* ACL Status on a directory */
  AclStatus proxyAclStat = proxyFs.getAclStatus(dir);
  AclStatus httpfsAclStat = httpfs.getAclStatus(dir);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  /* Set a default ACL on the directory */
  httpfs.setAcl(dir, (AclEntry.parseAclSpec(defUser1,true)));
  proxyAclStat = proxyFs.getAclStatus(dir);
  httpfsAclStat = httpfs.getAclStatus(dir);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  /* Remove the default ACL */
  httpfs.removeDefaultAcl(dir);
  proxyAclStat = proxyFs.getAclStatus(dir);
  httpfsAclStat = httpfs.getAclStatus(dir);
  assertSameAcls(httpfsAclStat, proxyAclStat);
}
 
Example 2
Source Project: big-c   File: BaseTestHttpFSWith.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Simple acl tests on a directory: set a default acl, remove default acls.
 * @throws Exception
 */
private void testDirAcls() throws Exception {
  if ( isLocalFS() ) {
    return;
  }

  final String defUser1 = "default:user:glarch:r-x";

  FileSystem proxyFs = FileSystem.get(getProxiedFSConf());
  FileSystem httpfs = getHttpFSFileSystem();

  Path dir = getProxiedFSTestDir();

  /* ACL Status on a directory */
  AclStatus proxyAclStat = proxyFs.getAclStatus(dir);
  AclStatus httpfsAclStat = httpfs.getAclStatus(dir);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  /* Set a default ACL on the directory */
  httpfs.setAcl(dir, (AclEntry.parseAclSpec(defUser1,true)));
  proxyAclStat = proxyFs.getAclStatus(dir);
  httpfsAclStat = httpfs.getAclStatus(dir);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  /* Remove the default ACL */
  httpfs.removeDefaultAcl(dir);
  proxyAclStat = proxyFs.getAclStatus(dir);
  httpfsAclStat = httpfs.getAclStatus(dir);
  assertSameAcls(httpfsAclStat, proxyAclStat);
}
 
Example 3
Source Project: hadoop   File: BaseTestHttpFSWith.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Simple ACL tests on a file:  Set an acl, add an acl, remove one acl,
 * and remove all acls.
 * @throws Exception
 */
private void testFileAcls() throws Exception {
  if ( isLocalFS() ) {
    return;
  }

  final String aclUser1 = "user:foo:rw-";
  final String aclUser2 = "user:bar:r--";
  final String aclGroup1 = "group::r--";
  final String aclSet = "user::rwx," + aclUser1 + ","
          + aclGroup1 + ",other::---";

  FileSystem proxyFs = FileSystem.get(getProxiedFSConf());
  FileSystem httpfs = getHttpFSFileSystem();

  Path path = new Path(getProxiedFSTestDir(), "testAclStatus.txt");
  OutputStream os = proxyFs.create(path);
  os.write(1);
  os.close();

  AclStatus proxyAclStat = proxyFs.getAclStatus(path);
  AclStatus httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.setAcl(path, AclEntry.parseAclSpec(aclSet,true));
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.modifyAclEntries(path, AclEntry.parseAclSpec(aclUser2, true));
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.removeAclEntries(path, AclEntry.parseAclSpec(aclUser1, true));
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.removeAcl(path);
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);
}
 
Example 4
Source Project: hadoop   File: DistCpUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Preserve attribute on file matching that of the file status being sent
 * as argument. Barring the block size, all the other attributes are preserved
 * by this function
 *
 * @param targetFS - File system
 * @param path - Path that needs to preserve original file status
 * @param srcFileStatus - Original file status
 * @param attributes - Attribute set that needs to be preserved
 * @param preserveRawXattrs if true, raw.* xattrs should be preserved
 * @throws IOException - Exception if any (particularly relating to group/owner
 *                       change or any transient error)
 */
public static void preserve(FileSystem targetFS, Path path,
                            CopyListingFileStatus srcFileStatus,
                            EnumSet<FileAttribute> attributes,
                            boolean preserveRawXattrs) throws IOException {

  FileStatus targetFileStatus = targetFS.getFileStatus(path);
  String group = targetFileStatus.getGroup();
  String user = targetFileStatus.getOwner();
  boolean chown = false;

  if (attributes.contains(FileAttribute.ACL)) {
    List<AclEntry> srcAcl = srcFileStatus.getAclEntries();
    List<AclEntry> targetAcl = getAcl(targetFS, targetFileStatus);
    if (!srcAcl.equals(targetAcl)) {
      targetFS.setAcl(path, srcAcl);
    }
    // setAcl doesn't preserve sticky bit, so also call setPermission if needed.
    if (srcFileStatus.getPermission().getStickyBit() !=
        targetFileStatus.getPermission().getStickyBit()) {
      targetFS.setPermission(path, srcFileStatus.getPermission());
    }
  } else if (attributes.contains(FileAttribute.PERMISSION) &&
    !srcFileStatus.getPermission().equals(targetFileStatus.getPermission())) {
    targetFS.setPermission(path, srcFileStatus.getPermission());
  }

  final boolean preserveXAttrs = attributes.contains(FileAttribute.XATTR);
  if (preserveXAttrs || preserveRawXattrs) {
    final String rawNS =
        StringUtils.toLowerCase(XAttr.NameSpace.RAW.name());
    Map<String, byte[]> srcXAttrs = srcFileStatus.getXAttrs();
    Map<String, byte[]> targetXAttrs = getXAttrs(targetFS, path);
    if (srcXAttrs != null && !srcXAttrs.equals(targetXAttrs)) {
      for (Entry<String, byte[]> entry : srcXAttrs.entrySet()) {
        String xattrName = entry.getKey();
        if (xattrName.startsWith(rawNS) || preserveXAttrs) {
          targetFS.setXAttr(path, xattrName, entry.getValue());
        }
      }
    }
  }

  if (attributes.contains(FileAttribute.REPLICATION) && !targetFileStatus.isDirectory() &&
      (srcFileStatus.getReplication() != targetFileStatus.getReplication())) {
    targetFS.setReplication(path, srcFileStatus.getReplication());
  }

  if (attributes.contains(FileAttribute.GROUP) &&
      !group.equals(srcFileStatus.getGroup())) {
    group = srcFileStatus.getGroup();
    chown = true;
  }

  if (attributes.contains(FileAttribute.USER) &&
      !user.equals(srcFileStatus.getOwner())) {
    user = srcFileStatus.getOwner();
    chown = true;
  }

  if (chown) {
    targetFS.setOwner(path, user, group);
  }
  
  if (attributes.contains(FileAttribute.TIMES)) {
    targetFS.setTimes(path, 
        srcFileStatus.getModificationTime(), 
        srcFileStatus.getAccessTime());
  }
}
 
Example 5
/**
 * Tests that ViewFileSystem dispatches calls for every ACL method through the
 * mount table to the correct underlying FileSystem with all Path arguments
 * translated as required.
 */
@Test
public void testAclMethods() throws Exception {
  Configuration conf = ViewFileSystemTestSetup.createConfig();
  FileSystem mockFs1 = setupMockFileSystem(conf, new URI("mockfs1:/"));
  FileSystem mockFs2 = setupMockFileSystem(conf, new URI("mockfs2:/"));
  FileSystem viewFs = FileSystem.get(FsConstants.VIEWFS_URI, conf);

  Path viewFsPath1 = new Path("/mounts/mockfs1/a/b/c");
  Path mockFsPath1 = new Path("/a/b/c");
  Path viewFsPath2 = new Path("/mounts/mockfs2/d/e/f");
  Path mockFsPath2 = new Path("/d/e/f");
  List<AclEntry> entries = Collections.emptyList();

  viewFs.modifyAclEntries(viewFsPath1, entries);
  verify(mockFs1).modifyAclEntries(mockFsPath1, entries);
  viewFs.modifyAclEntries(viewFsPath2, entries);
  verify(mockFs2).modifyAclEntries(mockFsPath2, entries);

  viewFs.removeAclEntries(viewFsPath1, entries);
  verify(mockFs1).removeAclEntries(mockFsPath1, entries);
  viewFs.removeAclEntries(viewFsPath2, entries);
  verify(mockFs2).removeAclEntries(mockFsPath2, entries);

  viewFs.removeDefaultAcl(viewFsPath1);
  verify(mockFs1).removeDefaultAcl(mockFsPath1);
  viewFs.removeDefaultAcl(viewFsPath2);
  verify(mockFs2).removeDefaultAcl(mockFsPath2);

  viewFs.removeAcl(viewFsPath1);
  verify(mockFs1).removeAcl(mockFsPath1);
  viewFs.removeAcl(viewFsPath2);
  verify(mockFs2).removeAcl(mockFsPath2);

  viewFs.setAcl(viewFsPath1, entries);
  verify(mockFs1).setAcl(mockFsPath1, entries);
  viewFs.setAcl(viewFsPath2, entries);
  verify(mockFs2).setAcl(mockFsPath2, entries);

  viewFs.getAclStatus(viewFsPath1);
  verify(mockFs1).getAclStatus(mockFsPath1);
  viewFs.getAclStatus(viewFsPath2);
  verify(mockFs2).getAclStatus(mockFsPath2);
}
 
Example 6
Source Project: big-c   File: BaseTestHttpFSWith.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Simple ACL tests on a file:  Set an acl, add an acl, remove one acl,
 * and remove all acls.
 * @throws Exception
 */
private void testFileAcls() throws Exception {
  if ( isLocalFS() ) {
    return;
  }

  final String aclUser1 = "user:foo:rw-";
  final String aclUser2 = "user:bar:r--";
  final String aclGroup1 = "group::r--";
  final String aclSet = "user::rwx," + aclUser1 + ","
          + aclGroup1 + ",other::---";

  FileSystem proxyFs = FileSystem.get(getProxiedFSConf());
  FileSystem httpfs = getHttpFSFileSystem();

  Path path = new Path(getProxiedFSTestDir(), "testAclStatus.txt");
  OutputStream os = proxyFs.create(path);
  os.write(1);
  os.close();

  AclStatus proxyAclStat = proxyFs.getAclStatus(path);
  AclStatus httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.setAcl(path, AclEntry.parseAclSpec(aclSet,true));
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.modifyAclEntries(path, AclEntry.parseAclSpec(aclUser2, true));
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.removeAclEntries(path, AclEntry.parseAclSpec(aclUser1, true));
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);

  httpfs.removeAcl(path);
  proxyAclStat = proxyFs.getAclStatus(path);
  httpfsAclStat = httpfs.getAclStatus(path);
  assertSameAcls(httpfsAclStat, proxyAclStat);
}
 
Example 7
Source Project: big-c   File: DistCpUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Preserve attribute on file matching that of the file status being sent
 * as argument. Barring the block size, all the other attributes are preserved
 * by this function
 *
 * @param targetFS - File system
 * @param path - Path that needs to preserve original file status
 * @param srcFileStatus - Original file status
 * @param attributes - Attribute set that needs to be preserved
 * @param preserveRawXattrs if true, raw.* xattrs should be preserved
 * @throws IOException - Exception if any (particularly relating to group/owner
 *                       change or any transient error)
 */
public static void preserve(FileSystem targetFS, Path path,
                            CopyListingFileStatus srcFileStatus,
                            EnumSet<FileAttribute> attributes,
                            boolean preserveRawXattrs) throws IOException {

  FileStatus targetFileStatus = targetFS.getFileStatus(path);
  String group = targetFileStatus.getGroup();
  String user = targetFileStatus.getOwner();
  boolean chown = false;

  if (attributes.contains(FileAttribute.ACL)) {
    List<AclEntry> srcAcl = srcFileStatus.getAclEntries();
    List<AclEntry> targetAcl = getAcl(targetFS, targetFileStatus);
    if (!srcAcl.equals(targetAcl)) {
      targetFS.setAcl(path, srcAcl);
    }
    // setAcl doesn't preserve sticky bit, so also call setPermission if needed.
    if (srcFileStatus.getPermission().getStickyBit() !=
        targetFileStatus.getPermission().getStickyBit()) {
      targetFS.setPermission(path, srcFileStatus.getPermission());
    }
  } else if (attributes.contains(FileAttribute.PERMISSION) &&
    !srcFileStatus.getPermission().equals(targetFileStatus.getPermission())) {
    targetFS.setPermission(path, srcFileStatus.getPermission());
  }

  final boolean preserveXAttrs = attributes.contains(FileAttribute.XATTR);
  if (preserveXAttrs || preserveRawXattrs) {
    final String rawNS =
        StringUtils.toLowerCase(XAttr.NameSpace.RAW.name());
    Map<String, byte[]> srcXAttrs = srcFileStatus.getXAttrs();
    Map<String, byte[]> targetXAttrs = getXAttrs(targetFS, path);
    if (srcXAttrs != null && !srcXAttrs.equals(targetXAttrs)) {
      for (Entry<String, byte[]> entry : srcXAttrs.entrySet()) {
        String xattrName = entry.getKey();
        if (xattrName.startsWith(rawNS) || preserveXAttrs) {
          targetFS.setXAttr(path, xattrName, entry.getValue());
        }
      }
    }
  }

  if (attributes.contains(FileAttribute.REPLICATION) && !targetFileStatus.isDirectory() &&
      (srcFileStatus.getReplication() != targetFileStatus.getReplication())) {
    targetFS.setReplication(path, srcFileStatus.getReplication());
  }

  if (attributes.contains(FileAttribute.GROUP) &&
      !group.equals(srcFileStatus.getGroup())) {
    group = srcFileStatus.getGroup();
    chown = true;
  }

  if (attributes.contains(FileAttribute.USER) &&
      !user.equals(srcFileStatus.getOwner())) {
    user = srcFileStatus.getOwner();
    chown = true;
  }

  if (chown) {
    targetFS.setOwner(path, user, group);
  }
  
  if (attributes.contains(FileAttribute.TIMES)) {
    targetFS.setTimes(path, 
        srcFileStatus.getModificationTime(), 
        srcFileStatus.getAccessTime());
  }
}
 
Example 8
/**
 * Tests that ViewFileSystem dispatches calls for every ACL method through the
 * mount table to the correct underlying FileSystem with all Path arguments
 * translated as required.
 */
@Test
public void testAclMethods() throws Exception {
  Configuration conf = ViewFileSystemTestSetup.createConfig();
  FileSystem mockFs1 = setupMockFileSystem(conf, new URI("mockfs1:/"));
  FileSystem mockFs2 = setupMockFileSystem(conf, new URI("mockfs2:/"));
  FileSystem viewFs = FileSystem.get(FsConstants.VIEWFS_URI, conf);

  Path viewFsPath1 = new Path("/mounts/mockfs1/a/b/c");
  Path mockFsPath1 = new Path("/a/b/c");
  Path viewFsPath2 = new Path("/mounts/mockfs2/d/e/f");
  Path mockFsPath2 = new Path("/d/e/f");
  List<AclEntry> entries = Collections.emptyList();

  viewFs.modifyAclEntries(viewFsPath1, entries);
  verify(mockFs1).modifyAclEntries(mockFsPath1, entries);
  viewFs.modifyAclEntries(viewFsPath2, entries);
  verify(mockFs2).modifyAclEntries(mockFsPath2, entries);

  viewFs.removeAclEntries(viewFsPath1, entries);
  verify(mockFs1).removeAclEntries(mockFsPath1, entries);
  viewFs.removeAclEntries(viewFsPath2, entries);
  verify(mockFs2).removeAclEntries(mockFsPath2, entries);

  viewFs.removeDefaultAcl(viewFsPath1);
  verify(mockFs1).removeDefaultAcl(mockFsPath1);
  viewFs.removeDefaultAcl(viewFsPath2);
  verify(mockFs2).removeDefaultAcl(mockFsPath2);

  viewFs.removeAcl(viewFsPath1);
  verify(mockFs1).removeAcl(mockFsPath1);
  viewFs.removeAcl(viewFsPath2);
  verify(mockFs2).removeAcl(mockFsPath2);

  viewFs.setAcl(viewFsPath1, entries);
  verify(mockFs1).setAcl(mockFsPath1, entries);
  viewFs.setAcl(viewFsPath2, entries);
  verify(mockFs2).setAcl(mockFsPath2, entries);

  viewFs.getAclStatus(viewFsPath1);
  verify(mockFs1).getAclStatus(mockFsPath1);
  viewFs.getAclStatus(viewFsPath2);
  verify(mockFs2).getAclStatus(mockFsPath2);
}
 
Example 9
Source Project: hadoop   File: FSOperations.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Executes the filesystem operation.
 *
 * @param fs filesystem instance to use.
 *
 * @return void.
 *
 * @throws IOException thrown if an IO error occurred.
 */
@Override
public Void execute(FileSystem fs) throws IOException {
  fs.setAcl(path, aclEntries);
  return null;
}
 
Example 10
Source Project: big-c   File: FSOperations.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Executes the filesystem operation.
 *
 * @param fs filesystem instance to use.
 *
 * @return void.
 *
 * @throws IOException thrown if an IO error occurred.
 */
@Override
public Void execute(FileSystem fs) throws IOException {
  fs.setAcl(path, aclEntries);
  return null;
}