Java Code Examples for org.apache.hadoop.fs.permission.FsPermission#valueOf()

The following examples show how to use org.apache.hadoop.fs.permission.FsPermission#valueOf() . 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: TestAccessController.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testBulkLoadWithoutWritePermission() throws Exception {
  // Use the USER_CREATE to initialize the source directory.
  Path testDataDir0 = TEST_UTIL.getDataTestDirOnTestFS("testBulkLoadWithoutWritePermission0");
  Path testDataDir1 = TEST_UTIL.getDataTestDirOnTestFS("testBulkLoadWithoutWritePermission1");
  AccessTestAction bulkLoadAction1 =
      new BulkLoadAccessTestAction(FsPermission.valueOf("-r-xr-xr-x"), testDataDir0);
  AccessTestAction bulkLoadAction2 =
      new BulkLoadAccessTestAction(FS_PERMISSION_ALL, testDataDir1);
  // Test the incorrect case.
  BulkLoadHelper.setPermission(TEST_UTIL.getTestFileSystem(),
    TEST_UTIL.getTestFileSystem().getWorkingDirectory(), FS_PERMISSION_ALL);
  try {
    USER_CREATE.runAs(bulkLoadAction1);
    fail("Should fail because the hbase user has no write permission on hfiles.");
  } catch (IOException e) {
  }
  // Ensure the correct case.
  USER_CREATE.runAs(bulkLoadAction2);
}
 
Example 2
Source File: HdfsUtils.java    From datacollector with Apache License 2.0 6 votes vote down vote up
/**
 * Parse String representation of permissions into HDFS FsPermission class.
 *
 * This method accepts the following formats:
 *  * Octal like '777' or '770'
 *  * HDFS style changes like 'a-rwx'
 *  * Unix style write up with 9 characters like 'rwxrwx---'
 *
 * @param permissions String representing the permissions
 * @return Parsed FsPermission object
 */
public static FsPermission parseFsPermission(String permissions) throws IllegalArgumentException {
  try {
    // Octal or symbolic representation
    return new FsPermission(permissions);
  } catch (IllegalArgumentException e) {
    // FsPermission.valueOf will work with unix style permissions which is 10 characters
    // where the first character says the type of file
    if (permissions.length() == 9) {
      // This means it is a posix standard without the first character for file type
      // We will simply set it to '-' suggesting regular file
      permissions = "-" + permissions;
    }

    // Try to parse unix style format.
    return FsPermission.valueOf(permissions);
  }
}
 
Example 3
Source File: FileSystemStorage.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void writeFile(final String content, final Path file, boolean writeTempFileFirst, Set<PosixFilePermission> permissions)
    throws IOException {
  FsPermission fsPermission;
  if (permissions == null || permissions.isEmpty()) {
    fsPermission = FsPermission.getFileDefault();
  } else {
    // FsPermission expects a 10-character string because of the leading
    // directory indicator, i.e. "drwx------". The JDK toString method returns
    // a 9-character string, so prepend a leading character.
    fsPermission = FsPermission.valueOf("-" + PosixFilePermissions.toString(permissions));
  }
  callHdfsOperation(new HdfsOperation<Void>() {
    @Override
    public Void call() throws IOException {
      InputStream in = new ByteArrayInputStream(content.getBytes(
          zConf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_ENCODING)));
      Path tmpFile = new Path(file.toString() + ".tmp");
      IOUtils.copyBytes(in, fs.create(tmpFile), hadoopConf);
      fs.setPermission(tmpFile, fsPermission);
      fs.delete(file, true);
      fs.rename(tmpFile, file);
      return null;
    }
  });
}
 
Example 4
Source File: HftpFileSystem.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void startElement(String ns, String localname, String qname,
            Attributes attrs) throws SAXException {
  if ("listing".equals(qname)) return;
  if (!"file".equals(qname) && !"directory".equals(qname)) {
    if (RemoteException.class.getSimpleName().equals(qname)) {
      throw new SAXException(RemoteException.valueOf(attrs));
    }
    throw new SAXException("Unrecognized entry: " + qname);
  }
  long modif;
  long atime = 0;
  try {
    final SimpleDateFormat ldf = df.get();
    modif = ldf.parse(attrs.getValue("modified")).getTime();
    String astr = attrs.getValue("accesstime");
    if (astr != null) {
      atime = ldf.parse(astr).getTime();
    }
  } catch (ParseException e) { throw new SAXException(e); }
  FileStatus fs = "file".equals(qname)
    ? new FileStatus(
          Long.parseLong(attrs.getValue("size")), false,
          Short.valueOf(attrs.getValue("replication")).shortValue(),
          Long.parseLong(attrs.getValue("blocksize")),
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          HftpFileSystem.this.makeQualified(
              new Path(getUri().toString(), attrs.getValue("path"))))
    : new FileStatus(0L, true, 0, 0L,
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          HftpFileSystem.this.makeQualified(
              new Path(getUri().toString(), attrs.getValue("path"))));
  fslist.add(fs);
}
 
Example 5
Source File: HftpFileSystem.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
public void startElement(String ns, String localname, String qname,
            Attributes attrs) throws SAXException {
  if ("listing".equals(qname)) return;
  if (!"file".equals(qname) && !"directory".equals(qname)) {
    if (RemoteException.class.getSimpleName().equals(qname)) {
      throw new SAXException(RemoteException.valueOf(attrs));
    }
    throw new SAXException("Unrecognized entry: " + qname);
  }
  long modif;
  long atime = 0;
  try {
    final SimpleDateFormat ldf = df.get();
    modif = ldf.parse(attrs.getValue("modified")).getTime();
    String astr = attrs.getValue("accesstime");
    if (astr != null) {
      atime = ldf.parse(astr).getTime();
    }
  } catch (ParseException e) { throw new SAXException(e); }
  FileStatus fs = "file".equals(qname)
    ? new FileStatus(
          Long.valueOf(attrs.getValue("size")).longValue(), false,
          Short.valueOf(attrs.getValue("replication")).shortValue(),
          Long.valueOf(attrs.getValue("blocksize")).longValue(),
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          new Path(getUri().toString(), attrs.getValue("path"))
            .makeQualified(HftpFileSystem.this))
    : new FileStatus(0L, true, 0, 0L,
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          new Path(getUri().toString(), attrs.getValue("path"))
            .makeQualified(HftpFileSystem.this));
  fslist.add(fs);
}
 
Example 6
Source File: HftpFileSystem.java    From RDFS with Apache License 2.0 5 votes vote down vote up
public void startElement(String ns, String localname, String qname,
            Attributes attrs) throws SAXException {
  if ("listing".equals(qname)) return;
  if (!"file".equals(qname) && !"directory".equals(qname)) {
    if (RemoteException.class.getSimpleName().equals(qname)) {
      throw new SAXException(RemoteException.valueOf(attrs));
    }
    throw new SAXException("Unrecognized entry: " + qname);
  }
  long modif;
  long atime = 0;
  try {
    final SimpleDateFormat ldf = df.get();
    modif = ldf.parse(attrs.getValue("modified")).getTime();
    String astr = attrs.getValue("accesstime");
    if (astr != null) {
      atime = ldf.parse(astr).getTime();
    }
  } catch (ParseException e) { throw new SAXException(e); }
  FileStatus fs = "file".equals(qname)
    ? new FileStatus(
          Long.valueOf(attrs.getValue("size")).longValue(), false,
          Short.valueOf(attrs.getValue("replication")).shortValue(),
          Long.valueOf(attrs.getValue("blocksize")).longValue(),
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          new Path(getUri().toString(), attrs.getValue("path"))
            .makeQualified(HftpFileSystem.this))
    : new FileStatus(0L, true, 0, 0L,
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          new Path(getUri().toString(), attrs.getValue("path"))
            .makeQualified(HftpFileSystem.this));
  fslist.add(fs);
}
 
Example 7
Source File: TestFileStatus.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Check that FileStatus are not equal if their paths are not equal.
 */
@Test
public void testNotEquals() {
  Path path1 = new Path("path1");
  Path path2 = new Path("path2");
  FileStatus fileStatus1 = new FileStatus(1, true, 1, 1, 1, 1,
      FsPermission.valueOf("-rw-rw-rw-"), "one", "one", null, path1);
  FileStatus fileStatus2 = new FileStatus(1, true, 1, 1, 1, 1,
      FsPermission.valueOf("-rw-rw-rw-"), "one", "one", null, path2);
  assertFalse(fileStatus1.equals(fileStatus2));
  assertFalse(fileStatus2.equals(fileStatus1));
}
 
Example 8
Source File: TestFileStatus.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Check that FileStatus are equal if their paths are equal.
 */
@Test
public void testEquals() {
  Path path = new Path("path");
  FileStatus fileStatus1 = new FileStatus(1, true, 1, 1, 1, 1,
      FsPermission.valueOf("-rw-rw-rw-"), "one", "one", null, path);
  FileStatus fileStatus2 = new FileStatus(2, true, 2, 2, 2, 2,
      FsPermission.valueOf("---x--x--x"), "two", "two", null, path);
  assertEquals(fileStatus1, fileStatus2);
}
 
Example 9
Source File: LocalJavaKeyStoreProvider.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void flush() throws IOException {
  super.flush();
  if (!Shell.WINDOWS) {
    Files.setPosixFilePermissions(Paths.get(file.getCanonicalPath()),
        permissions);
  } else {
    // FsPermission expects a 10-character string because of the leading
    // directory indicator, i.e. "drwx------". The JDK toString method returns
    // a 9-character string, so prepend a leading character.
    FsPermission fsPermission = FsPermission.valueOf(
        "-" + PosixFilePermissions.toString(permissions));
    FileUtil.setPermission(file, fsPermission);
  }
}
 
Example 10
Source File: AzureNativeFileSystemStore.java    From big-c with Apache License 2.0 5 votes vote down vote up
private static PermissionStatus fromJSONMap(
    @SuppressWarnings("rawtypes") Map object) {
  return new PermissionStatus((String) object.get(OWNER_TAG),
      (String) object.get(GROUP_TAG),
      // The initial - below is the Unix file type,
      // which FsPermission needs there but ignores.
      FsPermission.valueOf("-" + (String) object.get(PERMISSIONS_TAG)));
}
 
Example 11
Source File: HftpFileSystem.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void startElement(String ns, String localname, String qname,
            Attributes attrs) throws SAXException {
  if ("listing".equals(qname)) return;
  if (!"file".equals(qname) && !"directory".equals(qname)) {
    if (RemoteException.class.getSimpleName().equals(qname)) {
      throw new SAXException(RemoteException.valueOf(attrs));
    }
    throw new SAXException("Unrecognized entry: " + qname);
  }
  long modif;
  long atime = 0;
  try {
    final SimpleDateFormat ldf = df.get();
    modif = ldf.parse(attrs.getValue("modified")).getTime();
    String astr = attrs.getValue("accesstime");
    if (astr != null) {
      atime = ldf.parse(astr).getTime();
    }
  } catch (ParseException e) { throw new SAXException(e); }
  FileStatus fs = "file".equals(qname)
    ? new FileStatus(
          Long.parseLong(attrs.getValue("size")), false,
          Short.valueOf(attrs.getValue("replication")).shortValue(),
          Long.parseLong(attrs.getValue("blocksize")),
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          HftpFileSystem.this.makeQualified(
              new Path(getUri().toString(), attrs.getValue("path"))))
    : new FileStatus(0L, true, 0, 0L,
          modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
          attrs.getValue("owner"), attrs.getValue("group"),
          HftpFileSystem.this.makeQualified(
              new Path(getUri().toString(), attrs.getValue("path"))));
  fslist.add(fs);
}
 
Example 12
Source File: FileContextLocation.java    From twill with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the given permission to {@link FsPermission}.
 *
 * @param permission the permission as passed to the {@link #createNew(String)} or {@link #getOutputStream(String)}
 *                   methods.
 * @return a new {@link FsPermission}.
 */
private FsPermission parsePermissions(String permission) {
  if (permission.length() == 3) {
    return new FsPermission(permission);
  } else if (permission.length() == 9) {
    // The FsPermission expect a 10 characters string, which it will ignore the first character
    return FsPermission.valueOf("-" + permission);
  } else {
    throw new IllegalArgumentException("Invalid permission " + permission +
                                         ". Permission should either be a three digit or nine character string.");
  }
}
 
Example 13
Source File: TestFileStatus.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Check that FileStatus are not equal if their paths are not equal.
 */
@Test
public void testNotEquals() {
  Path path1 = new Path("path1");
  Path path2 = new Path("path2");
  FileStatus fileStatus1 = new FileStatus(1, true, 1, 1, 1, 1,
      FsPermission.valueOf("-rw-rw-rw-"), "one", "one", null, path1);
  FileStatus fileStatus2 = new FileStatus(1, true, 1, 1, 1, 1,
      FsPermission.valueOf("-rw-rw-rw-"), "one", "one", null, path2);
  assertFalse(fileStatus1.equals(fileStatus2));
  assertFalse(fileStatus2.equals(fileStatus1));
}
 
Example 14
Source File: TestFileStatus.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Check that FileStatus are equal if their paths are equal.
 */
@Test
public void testEquals() {
  Path path = new Path("path");
  FileStatus fileStatus1 = new FileStatus(1, true, 1, 1, 1, 1,
      FsPermission.valueOf("-rw-rw-rw-"), "one", "one", null, path);
  FileStatus fileStatus2 = new FileStatus(2, true, 2, 2, 2, 2,
      FsPermission.valueOf("---x--x--x"), "two", "two", null, path);
  assertEquals(fileStatus1, fileStatus2);
}
 
Example 15
Source File: LocalJavaKeyStoreProvider.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void flush() throws IOException {
  super.flush();
  if (!Shell.WINDOWS) {
    Files.setPosixFilePermissions(Paths.get(file.getCanonicalPath()),
        permissions);
  } else {
    // FsPermission expects a 10-character string because of the leading
    // directory indicator, i.e. "drwx------". The JDK toString method returns
    // a 9-character string, so prepend a leading character.
    FsPermission fsPermission = FsPermission.valueOf(
        "-" + PosixFilePermissions.toString(permissions));
    FileUtil.setPermission(file, fsPermission);
  }
}
 
Example 16
Source File: AzureNativeFileSystemStore.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private static PermissionStatus fromJSONMap(
    @SuppressWarnings("rawtypes") Map object) {
  return new PermissionStatus((String) object.get(OWNER_TAG),
      (String) object.get(GROUP_TAG),
      // The initial - below is the Unix file type,
      // which FsPermission needs there but ignores.
      FsPermission.valueOf("-" + (String) object.get(PERMISSIONS_TAG)));
}
 
Example 17
Source File: DremioHadoopFileSystemWrapper.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static FsPermission toFsPermission(Set<PosixFilePermission> permissions) {
  return FsPermission.valueOf("-" + PosixFilePermissions.toString(permissions));
}
 
Example 18
Source File: DremioHadoopFileSystemWrapper.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static FsPermission toFsPermission(Set<PosixFilePermission> permissions) {
  return FsPermission.valueOf("-" + PosixFilePermissions.toString(permissions));
}
 
Example 19
Source File: HadoopFileSystem.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static FsPermission toFsPermission(Set<PosixFilePermission> permissions) {
  return FsPermission.valueOf("-" + PosixFilePermissions.toString(permissions));
}