Exemplo n.º 1
0
 private int execCommand(String cmd) throws Exception {
   int exitCode;
   try {
     String output = Shell.execCommand("bash", "-c", cmd);
     LOG.info("Output from '" + cmd + "': " + output);
     exitCode = 0;
   } catch (Shell.ExitCodeException e) {
     exitCode = e.getExitCode();
     LOG.info("Error executing '" + cmd + "', exitCode = " + exitCode, e);
   }
   return exitCode;
 }
    /// loads permissions, owner, and group from `ls -ld`
    private void loadPermissionInfo() {
      IOException e = null;
      try {
        String output =
            FileUtil.execCommand(new File(getPath().toUri()), Shell.getGetPermissionCommand());
        StringTokenizer t = new StringTokenizer(output, Shell.TOKEN_SEPARATOR_REGEX);
        // expected format
        // -rw-------    1 username groupname ...
        String permission = t.nextToken();
        if (permission.length() > FsPermission.MAX_PERMISSION_LENGTH) {
          // files with ACLs might have a '+'
          permission = permission.substring(0, FsPermission.MAX_PERMISSION_LENGTH);
        }
        setPermission(FsPermission.valueOf(permission));
        t.nextToken();

        String owner = t.nextToken();
        // If on windows domain, token format is DOMAIN\\user and we want to
        // extract only the user name
        if (Shell.WINDOWS) {
          int i = owner.indexOf('\\');
          if (i != -1) owner = owner.substring(i + 1);
        }
        setOwner(owner);

        setGroup(t.nextToken());
      } catch (Shell.ExitCodeException ioe) {
        if (ioe.getExitCode() != 1) {
          e = ioe;
        } else {
          setPermission(null);
          setOwner(null);
          setGroup(null);
        }
      } catch (IOException ioe) {
        e = ioe;
      } finally {
        if (e != null) {
          throw new RuntimeException(
              "Error while running command to get "
                  + "file permissions : "
                  + StringUtils.stringifyException(e));
        }
      }
    }