Esempio n. 1
0
 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;
 }
Esempio n. 2
0
  private List<Path> createTestData() throws Exception {
    List<Path> list = new ArrayList<Path>();
    Configuration conf = new Configuration();
    conf.set("fs.default.name", "hdfs://localhost:8020");
    FileSystem fs = FileSystem.get(conf);
    fs.mkdirs(new Path("/user/guest"));
    fs.setOwner(new Path("/user/guest"), REMOTE_USER, "users");

    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = new Date(System.currentTimeMillis() + 3 * 3600000);
    Path path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    list.add(path);
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    list.add(path);
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    list.add(path);
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    list.add(path);
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    list.add(path);
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    list.add(path);
    fs.create(path).close();
    date = new Date(date.getTime() - 3600000);
    path = new Path("/examples/input-data/rawLogs/" + formatter.format(date) + "/file");
    list.add(path);
    fs.create(path).close();
    new FsShell(conf)
        .run(new String[] {"-chown", "-R", "guest:users", "/examples/input-data/rawLogs"});
    return list;
  }
Esempio n. 3
0
  /**
   * Test for {@link TFS#setOwner(Path, String, String)}. It will test both owner and group are
   * null.
   */
  @Test
  public void checkNullOwnerAndGroupTest() throws Exception {
    Path fileD = new Path("/chownfileD");

    create(sTFS, fileD);

    FileStatus fs = sTFS.getFileStatus(fileD);
    String defaultOwner = fs.getOwner();
    String defaultGroup = fs.getGroup();

    sTFS.setOwner(fileD, null, null);

    fs = sTFS.getFileStatus(fileD);
    Assert.assertEquals(defaultOwner, fs.getOwner());
    Assert.assertEquals(defaultGroup, fs.getGroup());
  }
Esempio n. 4
0
 private static void updatePermissions(
     FileStatus src, FileStatus dst, EnumSet<FileAttribute> preseved, FileSystem destFileSys)
     throws IOException {
   String owner = null;
   String group = null;
   if (preseved.contains(FileAttribute.USER) && !src.getOwner().equals(dst.getOwner())) {
     owner = src.getOwner();
   }
   if (preseved.contains(FileAttribute.GROUP) && !src.getGroup().equals(dst.getGroup())) {
     group = src.getGroup();
   }
   if (owner != null || group != null) {
     destFileSys.setOwner(dst.getPath(), owner, group);
   }
   if (preseved.contains(FileAttribute.PERMISSION)
       && !src.getPermission().equals(dst.getPermission())) {
     destFileSys.setPermission(dst.getPath(), src.getPermission());
   }
 }
Esempio n. 5
0
  /**
   * Test for {@link TFS#setOwner(Path, String, String)}. It will test changing both owner and group
   * of file using TFS.
   */
  @Test
  public void changeOwnerAndGroupTest() throws Exception {
    Path fileC = new Path("/chownfileC");
    final String newOwner = "test-user1";
    final String newGroup = "test-group1";

    create(sTFS, fileC);

    FileStatus fs = sTFS.getFileStatus(fileC);
    String defaultOwner = fs.getOwner();
    String defaultGroup = fs.getGroup();

    Assert.assertNotEquals(defaultOwner, newOwner);
    Assert.assertNotEquals(defaultGroup, newGroup);

    sTFS.setOwner(fileC, newOwner, newGroup);

    fs = sTFS.getFileStatus(fileC);
    Assert.assertEquals(newOwner, fs.getOwner());
    Assert.assertEquals(newGroup, fs.getGroup());
  }
  public void testFilePermision() throws Exception {
    final Configuration conf = new HdfsConfiguration();
    conf.setBoolean(DFSConfigKeys.DFS_PERMISSIONS_ENABLED_KEY, true);
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(3).build();
    cluster.waitActive();

    try {
      FileSystem nnfs = FileSystem.get(conf);
      // test permissions on files that do not exist
      assertFalse(nnfs.exists(CHILD_FILE1));
      try {
        nnfs.setOwner(CHILD_FILE1, "foo", "bar");
        assertTrue(false);
      } catch (java.io.FileNotFoundException e) {
        LOG.info("GOOD: got " + e);
      }
      try {
        nnfs.setPermission(CHILD_FILE1, new FsPermission((short) 0777));
        assertTrue(false);
      } catch (java.io.FileNotFoundException e) {
        LOG.info("GOOD: got " + e);
      }
      // following dir/file creations are legal
      nnfs.mkdirs(CHILD_DIR1);
      FSDataOutputStream out = nnfs.create(CHILD_FILE1);
      byte data[] = new byte[FILE_LEN];
      RAN.nextBytes(data);
      out.write(data);
      out.close();
      nnfs.setPermission(CHILD_FILE1, new FsPermission("700"));

      // following read is legal
      byte dataIn[] = new byte[FILE_LEN];
      FSDataInputStream fin = nnfs.open(CHILD_FILE1);
      int bytesRead = fin.read(dataIn);
      assertTrue(bytesRead == FILE_LEN);
      for (int i = 0; i < FILE_LEN; i++) {
        assertEquals(data[i], dataIn[i]);
      }

      ////////////////////////////////////////////////////////////////
      // test illegal file/dir creation
      UserGroupInformation userGroupInfo =
          UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES);

      FileSystem userfs = DFSTestUtil.getFileSystemAs(userGroupInfo, conf);

      // make sure mkdir of a existing directory that is not owned by
      // this user does not throw an exception.
      userfs.mkdirs(CHILD_DIR1);

      // illegal mkdir
      assertTrue(!canMkdirs(userfs, CHILD_DIR2));

      // illegal file creation
      assertTrue(!canCreate(userfs, CHILD_FILE2));

      // illegal file open
      assertTrue(!canOpen(userfs, CHILD_FILE1));

      nnfs.setPermission(ROOT_PATH, new FsPermission((short) 0755));
      nnfs.setPermission(CHILD_DIR1, new FsPermission("777"));
      nnfs.setPermission(new Path("/"), new FsPermission((short) 0777));
      final Path RENAME_PATH = new Path("/foo/bar");
      userfs.mkdirs(RENAME_PATH);
      assertTrue(canRename(userfs, RENAME_PATH, CHILD_DIR1));
    } finally {
      cluster.shutdown();
    }
  }
Esempio n. 7
0
  public void testPreserveOption() throws Exception {
    Configuration conf = new Configuration();
    MiniDFSCluster cluster = null;
    try {
      cluster = new MiniDFSCluster(conf, 2, true, null);
      String nnUri = FileSystem.getDefaultUri(conf).toString();
      FileSystem fs = FileSystem.get(URI.create(nnUri), conf);

      { // test preserving user
        MyFile[] files = createFiles(URI.create(nnUri), "/srcdat");
        FileStatus[] srcstat = getFileStatus(fs, "/srcdat", files);
        for (int i = 0; i < srcstat.length; i++) {
          fs.setOwner(srcstat[i].getPath(), "u" + i, null);
        }
        ToolRunner.run(
            new DistCpV1(conf), new String[] {"-pu", nnUri + "/srcdat", nnUri + "/destdat"});
        assertTrue(
            "Source and destination directories do not match.", checkFiles(fs, "/destdat", files));

        FileStatus[] dststat = getFileStatus(fs, "/destdat", files);
        for (int i = 0; i < dststat.length; i++) {
          assertEquals("i=" + i, "u" + i, dststat[i].getOwner());
        }
        deldir(fs, "/destdat");
        deldir(fs, "/srcdat");
      }

      { // test preserving group
        MyFile[] files = createFiles(URI.create(nnUri), "/srcdat");
        FileStatus[] srcstat = getFileStatus(fs, "/srcdat", files);
        for (int i = 0; i < srcstat.length; i++) {
          fs.setOwner(srcstat[i].getPath(), null, "g" + i);
        }
        ToolRunner.run(
            new DistCpV1(conf), new String[] {"-pg", nnUri + "/srcdat", nnUri + "/destdat"});
        assertTrue(
            "Source and destination directories do not match.", checkFiles(fs, "/destdat", files));

        FileStatus[] dststat = getFileStatus(fs, "/destdat", files);
        for (int i = 0; i < dststat.length; i++) {
          assertEquals("i=" + i, "g" + i, dststat[i].getGroup());
        }
        deldir(fs, "/destdat");
        deldir(fs, "/srcdat");
      }

      { // test preserving mode
        MyFile[] files = createFiles(URI.create(nnUri), "/srcdat");
        FileStatus[] srcstat = getFileStatus(fs, "/srcdat", files);
        FsPermission[] permissions = new FsPermission[srcstat.length];
        for (int i = 0; i < srcstat.length; i++) {
          permissions[i] = new FsPermission((short) (i & 0666));
          fs.setPermission(srcstat[i].getPath(), permissions[i]);
        }

        ToolRunner.run(
            new DistCpV1(conf), new String[] {"-pp", nnUri + "/srcdat", nnUri + "/destdat"});
        assertTrue(
            "Source and destination directories do not match.", checkFiles(fs, "/destdat", files));

        FileStatus[] dststat = getFileStatus(fs, "/destdat", files);
        for (int i = 0; i < dststat.length; i++) {
          assertEquals("i=" + i, permissions[i], dststat[i].getPermission());
        }
        deldir(fs, "/destdat");
        deldir(fs, "/srcdat");
      }

      { // test preserving times
        MyFile[] files = createFiles(URI.create(nnUri), "/srcdat");
        fs.mkdirs(new Path("/srcdat/tmpf1"));
        fs.mkdirs(new Path("/srcdat/tmpf2"));
        FileStatus[] srcstat = getFileStatus(fs, "/srcdat", files);
        FsPermission[] permissions = new FsPermission[srcstat.length];
        for (int i = 0; i < srcstat.length; i++) {
          fs.setTimes(srcstat[i].getPath(), 40, 50);
        }

        ToolRunner.run(
            new DistCpV1(conf), new String[] {"-pt", nnUri + "/srcdat", nnUri + "/destdat"});

        FileStatus[] dststat = getFileStatus(fs, "/destdat", files);
        for (int i = 0; i < dststat.length; i++) {
          assertEquals("Modif. Time i=" + i, 40, dststat[i].getModificationTime());
          assertEquals(
              "Access Time i=" + i + srcstat[i].getPath() + "-" + dststat[i].getPath(),
              50,
              dststat[i].getAccessTime());
        }

        assertTrue(
            "Source and destination directories do not match.", checkFiles(fs, "/destdat", files));

        deldir(fs, "/destdat");
        deldir(fs, "/srcdat");
      }
    } finally {
      if (cluster != null) {
        cluster.shutdown();
      }
    }
  }