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

The following examples show how to use org.apache.hadoop.fs.FileSystem#setOwner() . 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: TestDFSPermission.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAccessGroupMember() throws IOException, InterruptedException {
  FileSystem rootFs = FileSystem.get(conf);
  Path p2 = new Path("/p2");
  rootFs.mkdirs(p2);
  rootFs.setOwner(p2, UserGroupInformation.getCurrentUser().getShortUserName(), GROUP1_NAME);
  rootFs.setPermission(p2, new FsPermission((short) 0740));
  fs = USER1.doAs(new PrivilegedExceptionAction<FileSystem>() {
    @Override
    public FileSystem run() throws Exception {
      return FileSystem.get(conf);
    }
  });
  fs.access(p2, FsAction.READ);
  try {
    fs.access(p2, FsAction.EXECUTE);
    fail("The access call should have failed.");
  } catch (AccessControlException e) {
    assertTrue("Permission denied messages must carry the username",
            e.getMessage().contains(USER1_NAME));
    assertTrue("Permission denied messages must carry the path parent",
            e.getMessage().contains(
                p2.getParent().toUri().getPath()));
  }
}
 
Example 2
Source Project: incubator-gobblin   File: HadoopUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Try to set owner and permissions for the path. Will not throw exception.
 */
public static void setPermissions(Path location, Optional<String> owner, Optional<String> group, FileSystem fs,
    FsPermission permission) {
  try {
    if (!owner.isPresent()) {
      return;
    }
    if (!group.isPresent()) {
      return;
    }
    fs.setOwner(location, owner.get(), group.get());
    fs.setPermission(location, permission);
    if (!fs.isDirectory(location)) {
      return;
    }
    for (FileStatus fileStatus : fs.listStatus(location)) {
      setPermissions(fileStatus.getPath(), owner, group, fs, permission);
    }
  } catch (IOException e) {
    log.warn("Exception occurred while trying to change permissions : " + e.getMessage());
  }
}
 
Example 3
Source Project: localization_nifi   File: PutHDFS.java    License: Apache License 2.0 5 votes vote down vote up
protected void changeOwner(final ProcessContext context, final FileSystem hdfs, final Path name) {
    try {
        // Change owner and group of file if configured to do so
        String owner = context.getProperty(REMOTE_OWNER).getValue();
        String group = context.getProperty(REMOTE_GROUP).getValue();
        if (owner != null || group != null) {
            hdfs.setOwner(name, owner, group);
        }
    } catch (Exception e) {
        getLogger().warn("Could not change owner or group of {} on HDFS due to {}", new Object[]{name, e});
    }
}
 
Example 4
static void mkdir(FileSystem fs, String dir,
                  String user, String group, short mode) throws IOException {
  Path p = new Path(dir);
  fs.mkdirs(p);
  fs.setPermission(p, new FsPermission(mode));
  fs.setOwner(p, user, group);
}
 
Example 5
static void mkdir(FileSystem fs, String dir,
                  String user, String group, short mode) throws IOException {
  Path p = new Path(dir);
  fs.mkdirs(p);
  fs.setPermission(p, new FsPermission(mode));
  fs.setOwner(p, user, group);
}
 
Example 6
Source Project: big-c   File: TestCopyMapper.java    License: Apache License 2.0 5 votes vote down vote up
private static void changeUserGroup(String user, String group)
        throws IOException {
  FileSystem fs = cluster.getFileSystem();
  FsPermission changedPermission = new FsPermission(
          FsAction.ALL, FsAction.ALL, FsAction.ALL
  );
  for (Path path : pathList)
    if (fs.isFile(path)) {
      fs.setOwner(path, user, group);
      fs.setPermission(path, changedPermission);
    }
}
 
Example 7
Source Project: hadoop   File: DistCh.java    License: Apache License 2.0 5 votes vote down vote up
void run(Configuration conf) throws IOException {
  FileSystem fs = src.getFileSystem(conf);
  if (permission != null) {
    fs.setPermission(src, permission);
  }
  if (owner != null || group != null) {
    fs.setOwner(src, owner, group);
  }
}
 
Example 8
Source Project: big-c   File: TestDistCpUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPreserveGroupOnFile() throws IOException {
  FileSystem fs = FileSystem.get(config);
  EnumSet<FileAttribute> attributes = EnumSet.of(FileAttribute.GROUP);

  Path dst = new Path("/tmp/dest2");
  Path src = new Path("/tmp/src2");

  createFile(fs, src);
  createFile(fs, dst);

  fs.setPermission(src, fullPerm);
  fs.setOwner(src, "somebody", "somebody-group");
  fs.setTimes(src, 0, 0);
  fs.setReplication(src, (short) 1);

  fs.setPermission(dst, noPerm);
  fs.setOwner(dst, "nobody", "nobody-group");
  fs.setTimes(dst, 100, 100);
  fs.setReplication(dst, (short) 2);

  CopyListingFileStatus srcStatus = new CopyListingFileStatus(fs.getFileStatus(src));

  DistCpUtils.preserve(fs, dst, srcStatus, attributes, false);

  CopyListingFileStatus dstStatus = new CopyListingFileStatus(fs.getFileStatus(dst));

  // FileStatus.equals only compares path field, must explicitly compare all fields
  Assert.assertFalse(srcStatus.getPermission().equals(dstStatus.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(dstStatus.getOwner()));
  Assert.assertTrue(srcStatus.getGroup().equals(dstStatus.getGroup()));
  Assert.assertFalse(srcStatus.getAccessTime() == dstStatus.getAccessTime());
  Assert.assertFalse(srcStatus.getModificationTime() == dstStatus.getModificationTime());
  Assert.assertFalse(srcStatus.getReplication() == dstStatus.getReplication());
}
 
Example 9
Source Project: RDFS   File: DistCh.java    License: Apache License 2.0 5 votes vote down vote up
void run(Configuration conf) throws IOException {
  FileSystem fs = src.getFileSystem(conf);
  if (permission != null) {
    fs.setPermission(src, permission);
  }
  if (owner != null || group != null) {
    fs.setOwner(src, owner, group);
  }
}
 
Example 10
Source Project: hadoop   File: TestDistCpUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPreservePermissionOnDirectory() throws IOException {
  FileSystem fs = FileSystem.get(config);
  EnumSet<FileAttribute> attributes = EnumSet.of(FileAttribute.PERMISSION);

  Path dst = new Path("/tmp/abc");
  Path src = new Path("/tmp/src");

  createDirectory(fs, src);
  createDirectory(fs, dst);

  fs.setPermission(src, fullPerm);
  fs.setOwner(src, "somebody", "somebody-group");

  fs.setPermission(dst, noPerm);
  fs.setOwner(dst, "nobody", "nobody-group");

  CopyListingFileStatus srcStatus = new CopyListingFileStatus(fs.getFileStatus(src));

  DistCpUtils.preserve(fs, dst, srcStatus, attributes, false);

  CopyListingFileStatus dstStatus = new CopyListingFileStatus(fs.getFileStatus(dst));

  // FileStatus.equals only compares path field, must explicitly compare all fields
  Assert.assertTrue(srcStatus.getPermission().equals(dstStatus.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(dstStatus.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(dstStatus.getGroup()));
}
 
Example 11
Source Project: big-c   File: TestCopyFiles.java    License: Apache License 2.0 5 votes vote down vote up
static Path createHomeDirectory(FileSystem fs, UserGroupInformation ugi
    ) throws IOException {
  final Path home = new Path("/user/" + ugi.getUserName());
  fs.mkdirs(home);
  fs.setOwner(home, ugi.getUserName(), ugi.getGroupNames()[0]);
  fs.setPermission(home, new FsPermission((short)0700));
  return home;
}
 
Example 12
Source Project: hadoop   File: TestDistCpUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPreserveUserOnDirectory() throws IOException {
  FileSystem fs = FileSystem.get(config);
  EnumSet<FileAttribute> attributes = EnumSet.of(FileAttribute.USER);

  Path dst = new Path("/tmp/abc");
  Path src = new Path("/tmp/src");

  createDirectory(fs, src);
  createDirectory(fs, dst);

  fs.setPermission(src, fullPerm);
  fs.setOwner(src, "somebody", "somebody-group");

  fs.setPermission(dst, noPerm);
  fs.setOwner(dst, "nobody", "nobody-group");

  CopyListingFileStatus srcStatus = new CopyListingFileStatus(fs.getFileStatus(src));

  DistCpUtils.preserve(fs, dst, srcStatus, attributes, false);

  CopyListingFileStatus dstStatus = new CopyListingFileStatus(fs.getFileStatus(dst));

  // FileStatus.equals only compares path field, must explicitly compare all fields
  Assert.assertFalse(srcStatus.getPermission().equals(dstStatus.getPermission()));
  Assert.assertTrue(srcStatus.getOwner().equals(dstStatus.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(dstStatus.getGroup()));
}
 
Example 13
Source Project: big-c   File: TestDistCpUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPreserveUserOnDirectory() throws IOException {
  FileSystem fs = FileSystem.get(config);
  EnumSet<FileAttribute> attributes = EnumSet.of(FileAttribute.USER);

  Path dst = new Path("/tmp/abc");
  Path src = new Path("/tmp/src");

  createDirectory(fs, src);
  createDirectory(fs, dst);

  fs.setPermission(src, fullPerm);
  fs.setOwner(src, "somebody", "somebody-group");

  fs.setPermission(dst, noPerm);
  fs.setOwner(dst, "nobody", "nobody-group");

  CopyListingFileStatus srcStatus = new CopyListingFileStatus(fs.getFileStatus(src));

  DistCpUtils.preserve(fs, dst, srcStatus, attributes, false);

  CopyListingFileStatus dstStatus = new CopyListingFileStatus(fs.getFileStatus(dst));

  // FileStatus.equals only compares path field, must explicitly compare all fields
  Assert.assertFalse(srcStatus.getPermission().equals(dstStatus.getPermission()));
  Assert.assertTrue(srcStatus.getOwner().equals(dstStatus.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(dstStatus.getGroup()));
}
 
Example 14
Source Project: big-c   File: GridmixTestUtils.java    License: Apache License 2.0 4 votes vote down vote up
static void changePermission(String user, Path homeDirectory, FileSystem fs)
    throws IOException {
  fs.setOwner(homeDirectory, user, "");
}
 
Example 15
@Override
public void setOwner(UserPrincipal owner) throws IOException {
  FileSystem fs = path.getFileSystem().getHDFS();
  fs.setOwner(path.getRawResolvedPath(), owner.getName(), null);
}
 
Example 16
Source Project: hadoop   File: GridmixTestUtils.java    License: Apache License 2.0 4 votes vote down vote up
static void changePermission(String user, Path homeDirectory, FileSystem fs)
    throws IOException {
  fs.setOwner(homeDirectory, user, "");
}
 
Example 17
Source Project: hadoop   File: TestDistCpUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPreserveOnFileUpwardRecursion() throws IOException {
  FileSystem fs = FileSystem.get(config);
  EnumSet<FileAttribute> attributes = EnumSet.allOf(FileAttribute.class);
  // Remove ACL because tests run with dfs.namenode.acls.enabled false
  attributes.remove(FileAttribute.ACL);
  
  Path src = new Path("/tmp/src2");
  Path f0 = new Path("/f0");
  Path f1 = new Path("/d1/f1");
  Path f2 = new Path("/d1/d2/f2");
  Path d1 = new Path("/d1/");
  Path d2 = new Path("/d1/d2/");

  createFile(fs, src);
  createFile(fs, f0);
  createFile(fs, f1);
  createFile(fs, f2);

  fs.setPermission(src, almostFullPerm);
  fs.setOwner(src, "somebody", "somebody-group");
  fs.setTimes(src, 0, 0);
  fs.setReplication(src, (short) 1);

  fs.setPermission(d1, fullPerm);
  fs.setOwner(d1, "anybody", "anybody-group");
  fs.setTimes(d1, 400, 400);
  fs.setReplication(d1, (short) 3);

  fs.setPermission(d2, fullPerm);
  fs.setOwner(d2, "anybody", "anybody-group");
  fs.setTimes(d2, 300, 300);
  fs.setReplication(d2, (short) 3);

  fs.setPermission(f0, fullPerm);
  fs.setOwner(f0, "anybody", "anybody-group");
  fs.setTimes(f0, 200, 200);
  fs.setReplication(f0, (short) 3);

  fs.setPermission(f1, fullPerm);
  fs.setOwner(f1, "anybody", "anybody-group");
  fs.setTimes(f1, 200, 200);
  fs.setReplication(f1, (short) 3);

  fs.setPermission(f2, fullPerm);
  fs.setOwner(f2, "anybody", "anybody-group");
  fs.setTimes(f2, 200, 200);
  fs.setReplication(f2, (short) 3);

  CopyListingFileStatus srcStatus = new CopyListingFileStatus(fs.getFileStatus(src));

  DistCpUtils.preserve(fs, f2, srcStatus, attributes, false);

  cluster.triggerHeartbeats();

  // FileStatus.equals only compares path field, must explicitly compare all fields
  // attributes of src -> f2 ? should be yes
  CopyListingFileStatus f2Status = new CopyListingFileStatus(fs.getFileStatus(f2));
  Assert.assertTrue(srcStatus.getPermission().equals(f2Status.getPermission()));
  Assert.assertTrue(srcStatus.getOwner().equals(f2Status.getOwner()));
  Assert.assertTrue(srcStatus.getGroup().equals(f2Status.getGroup()));
  Assert.assertTrue(srcStatus.getAccessTime() == f2Status.getAccessTime());
  Assert.assertTrue(srcStatus.getModificationTime() == f2Status.getModificationTime());
  Assert.assertTrue(srcStatus.getReplication() == f2Status.getReplication());

  // attributes of src -> f1 ? should be no
  CopyListingFileStatus f1Status = new CopyListingFileStatus(fs.getFileStatus(f1));
  Assert.assertFalse(srcStatus.getPermission().equals(f1Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(f1Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(f1Status.getGroup()));
  Assert.assertFalse(srcStatus.getAccessTime() == f1Status.getAccessTime());
  Assert.assertFalse(srcStatus.getModificationTime() == f1Status.getModificationTime());
  Assert.assertFalse(srcStatus.getReplication() == f1Status.getReplication());

  // attributes of src -> f0 ? should be no
  CopyListingFileStatus f0Status = new CopyListingFileStatus(fs.getFileStatus(f0));
  Assert.assertFalse(srcStatus.getPermission().equals(f0Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(f0Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(f0Status.getGroup()));
  Assert.assertFalse(srcStatus.getAccessTime() == f0Status.getAccessTime());
  Assert.assertFalse(srcStatus.getModificationTime() == f0Status.getModificationTime());
  Assert.assertFalse(srcStatus.getReplication() == f0Status.getReplication());

  // attributes of src -> d2 ? should be no
  CopyListingFileStatus d2Status = new CopyListingFileStatus(fs.getFileStatus(d2));
  Assert.assertFalse(srcStatus.getPermission().equals(d2Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(d2Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(d2Status.getGroup()));
  Assert.assertTrue(d2Status.getAccessTime() == 300);
  Assert.assertTrue(d2Status.getModificationTime() == 300);
  Assert.assertFalse(srcStatus.getReplication() == d2Status.getReplication());

  // attributes of src -> d1 ? should be no
  CopyListingFileStatus d1Status = new CopyListingFileStatus(fs.getFileStatus(d1));
  Assert.assertFalse(srcStatus.getPermission().equals(d1Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(d1Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(d1Status.getGroup()));
  Assert.assertTrue(d1Status.getAccessTime() == 400);
  Assert.assertTrue(d1Status.getModificationTime() == 400);
  Assert.assertFalse(srcStatus.getReplication() == d1Status.getReplication());
}
 
Example 18
@Override
public void setGroup(GroupPrincipal group) throws IOException {
  FileSystem fs = path.getFileSystem().getHDFS();
  fs.setOwner(path.getRawResolvedPath(), null, group.getName());
}
 
Example 19
Source Project: big-c   File: TestDistCpUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPreserveOnFileDownwardRecursion() throws IOException {
  FileSystem fs = FileSystem.get(config);
  EnumSet<FileAttribute> attributes = EnumSet.allOf(FileAttribute.class);
  // Remove ACL because tests run with dfs.namenode.acls.enabled false
  attributes.remove(FileAttribute.ACL);

  Path src = new Path("/tmp/src2");
  Path f0 = new Path("/f0");
  Path f1 = new Path("/d1/f1");
  Path f2 = new Path("/d1/d2/f2");
  Path d1 = new Path("/d1/");
  Path d2 = new Path("/d1/d2/");

  createFile(fs, src);
  createFile(fs, f0);
  createFile(fs, f1);
  createFile(fs, f2);

  fs.setPermission(src, almostFullPerm);
  fs.setOwner(src, "somebody", "somebody-group");
  fs.setTimes(src, 0, 0);
  fs.setReplication(src, (short) 1);

  fs.setPermission(d1, fullPerm);
  fs.setOwner(d1, "anybody", "anybody-group");
  fs.setTimes(d1, 400, 400);
  fs.setReplication(d1, (short) 3);

  fs.setPermission(d2, fullPerm);
  fs.setOwner(d2, "anybody", "anybody-group");
  fs.setTimes(d2, 300, 300);
  fs.setReplication(d2, (short) 3);

  fs.setPermission(f0, fullPerm);
  fs.setOwner(f0, "anybody", "anybody-group");
  fs.setTimes(f0, 200, 200);
  fs.setReplication(f0, (short) 3);

  fs.setPermission(f1, fullPerm);
  fs.setOwner(f1, "anybody", "anybody-group");
  fs.setTimes(f1, 200, 200);
  fs.setReplication(f1, (short) 3);

  fs.setPermission(f2, fullPerm);
  fs.setOwner(f2, "anybody", "anybody-group");
  fs.setTimes(f2, 200, 200);
  fs.setReplication(f2, (short) 3);

  CopyListingFileStatus srcStatus = new CopyListingFileStatus(fs.getFileStatus(src));

  DistCpUtils.preserve(fs, f0, srcStatus, attributes, false);

  cluster.triggerHeartbeats();

  // FileStatus.equals only compares path field, must explicitly compare all fields
  // attributes of src -> f0 ? should be yes
  CopyListingFileStatus f0Status = new CopyListingFileStatus(fs.getFileStatus(f0));
  Assert.assertTrue(srcStatus.getPermission().equals(f0Status.getPermission()));
  Assert.assertTrue(srcStatus.getOwner().equals(f0Status.getOwner()));
  Assert.assertTrue(srcStatus.getGroup().equals(f0Status.getGroup()));
  Assert.assertTrue(srcStatus.getAccessTime() == f0Status.getAccessTime());
  Assert.assertTrue(srcStatus.getModificationTime() == f0Status.getModificationTime());
  Assert.assertTrue(srcStatus.getReplication() == f0Status.getReplication());

  // attributes of src -> f1 ? should be no
  CopyListingFileStatus f1Status = new CopyListingFileStatus(fs.getFileStatus(f1));
  Assert.assertFalse(srcStatus.getPermission().equals(f1Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(f1Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(f1Status.getGroup()));
  Assert.assertFalse(srcStatus.getAccessTime() == f1Status.getAccessTime());
  Assert.assertFalse(srcStatus.getModificationTime() == f1Status.getModificationTime());
  Assert.assertFalse(srcStatus.getReplication() == f1Status.getReplication());

  // attributes of src -> f2 ? should be no
  CopyListingFileStatus f2Status = new CopyListingFileStatus(fs.getFileStatus(f2));
  Assert.assertFalse(srcStatus.getPermission().equals(f2Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(f2Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(f2Status.getGroup()));
  Assert.assertFalse(srcStatus.getAccessTime() == f2Status.getAccessTime());
  Assert.assertFalse(srcStatus.getModificationTime() == f2Status.getModificationTime());
  Assert.assertFalse(srcStatus.getReplication() == f2Status.getReplication());

  // attributes of src -> d1 ? should be no
  CopyListingFileStatus d1Status = new CopyListingFileStatus(fs.getFileStatus(d1));
  Assert.assertFalse(srcStatus.getPermission().equals(d1Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(d1Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(d1Status.getGroup()));
  Assert.assertTrue(d1Status.getAccessTime() == 400);
  Assert.assertTrue(d1Status.getModificationTime() == 400);
  Assert.assertFalse(srcStatus.getReplication() == d1Status.getReplication());

  // attributes of src -> d2 ? should be no
  CopyListingFileStatus d2Status = new CopyListingFileStatus(fs.getFileStatus(d2));
  Assert.assertFalse(srcStatus.getPermission().equals(d2Status.getPermission()));
  Assert.assertFalse(srcStatus.getOwner().equals(d2Status.getOwner()));
  Assert.assertFalse(srcStatus.getGroup().equals(d2Status.getGroup()));
  Assert.assertTrue(d2Status.getAccessTime() == 300);
  Assert.assertTrue(d2Status.getModificationTime() == 300);
  Assert.assertFalse(srcStatus.getReplication() == d2Status.getReplication());
}
 
Example 20
Source Project: incubator-gobblin   File: HadoopUtils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Set the group associated with a given path.
 *
 * @param fs the {@link FileSystem} instance used to perform the file operation
 * @param path the given path
 * @param group the group associated with the path
 * @throws IOException
 */
public static void setGroup(FileSystem fs, Path path, String group) throws IOException {
  fs.setOwner(path, fs.getFileStatus(path).getOwner(), group);
}