コード例 #1
0
ファイル: FileUtil.java プロジェクト: luochen01/tongji_thesis
  /**
   * Given a Tar File as input it will untar the file in a the untar directory passed as the second
   * parameter
   *
   * <p>This utility will untar ".tar" files and ".tar.gz","tgz" files.
   *
   * @param inFile The tar file as input.
   * @param untarDir The untar directory where to untar the tar file.
   * @throws IOException
   */
  public static void unTar(File inFile, File untarDir) throws IOException {
    if (!untarDir.mkdirs()) {
      if (!untarDir.isDirectory()) {
        throw new IOException("Mkdirs failed to create " + untarDir);
      }
    }

    StringBuffer untarCommand = new StringBuffer();
    boolean gzipped = inFile.toString().endsWith("gz");
    if (gzipped) {
      untarCommand.append(" gzip -dc '");
      untarCommand.append(FileUtil.makeShellPath(inFile));
      untarCommand.append("' | (");
    }
    untarCommand.append("cd '");
    untarCommand.append(FileUtil.makeShellPath(untarDir));
    untarCommand.append("' ; ");
    untarCommand.append("tar -xf ");

    if (gzipped) {
      untarCommand.append(" -)");
    } else {
      untarCommand.append(FileUtil.makeShellPath(inFile));
    }
    String[] shellCmd = {"bash", "-c", untarCommand.toString()};
    ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
    shexec.execute();
    int exitcode = shexec.getExitCode();
    if (exitcode != 0) {
      throw new IOException(
          "Error untarring file " + inFile + ". Tar process exited with exit code " + exitcode);
    }
  }
コード例 #2
0
 /**
  * Wrap a command in a shell to capture debug script's stdout and stderr to debugout.
  *
  * @param cmd The command and the arguments that should be run
  * @param debugoutFilename The filename that stdout and stderr should be saved to.
  * @return the modified command that should be run
  * @throws IOException
  */
 public static List<String> captureDebugOut(List<String> cmd, File debugoutFilename)
     throws IOException {
   String debugout = FileUtil.makeShellPath(debugoutFilename);
   List<String> result = new ArrayList<String>(3);
   result.add(bashCommand);
   result.add("-c");
   StringBuffer mergedCmd = new StringBuffer();
   mergedCmd.append("exec ");
   boolean isExecutable = true;
   for (String s : cmd) {
     if (isExecutable) {
       // the executable name needs to be expressed as a shell path for
       // the
       // shell to find it.
       mergedCmd.append(FileUtil.makeShellPath(new File(s)));
       isExecutable = false;
     } else {
       mergedCmd.append(s);
     }
     mergedCmd.append(" ");
   }
   mergedCmd.append(" < /dev/null ");
   mergedCmd.append(" >");
   mergedCmd.append(debugout);
   mergedCmd.append(" 2>&1 ");
   result.add(mergedCmd.toString());
   return result;
 }
コード例 #3
0
  static String buildCommandLine(
      List<String> setup,
      List<String> cmd,
      File stdoutFilename,
      File stderrFilename,
      long tailLength,
      boolean useSetSid)
      throws IOException {

    String stdout = FileUtil.makeShellPath(stdoutFilename);
    String stderr = FileUtil.makeShellPath(stderrFilename);
    StringBuilder mergedCmd = new StringBuilder();

    if (!Shell.WINDOWS) {
      mergedCmd.append("export JVM_PID=`echo $$`\n");
    }

    if (setup != null) {
      for (String s : setup) {
        mergedCmd.append(s);
        mergedCmd.append("\n");
      }
    }
    if (tailLength > 0) {
      mergedCmd.append("(");
    } else if (ProcessTree.isSetsidAvailable && useSetSid && !Shell.WINDOWS) {
      mergedCmd.append("exec setsid ");
    } else {
      mergedCmd.append("exec ");
    }
    mergedCmd.append(addCommand(cmd, true));
    mergedCmd.append(" < /dev/null ");
    if (tailLength > 0) {
      mergedCmd.append(" | ");
      mergedCmd.append(tailCommand);
      mergedCmd.append(" -c ");
      mergedCmd.append(tailLength);
      mergedCmd.append(" >> ");
      mergedCmd.append(stdout);
      mergedCmd.append(" ; exit $PIPESTATUS ) 2>&1 | ");
      mergedCmd.append(tailCommand);
      mergedCmd.append(" -c ");
      mergedCmd.append(tailLength);
      mergedCmd.append(" >> ");
      mergedCmd.append(stderr);
      mergedCmd.append(" ; exit $PIPESTATUS");
    } else {
      mergedCmd.append(" 1>> ");
      mergedCmd.append(stdout);
      mergedCmd.append(" 2>> ");
      mergedCmd.append(stderr);
    }
    return mergedCmd.toString();
  }
コード例 #4
0
 private static String execCommand(File f, String... cmd) throws IOException {
   String[] args = new String[cmd.length + 1];
   System.arraycopy(cmd, 0, args, 0, cmd.length);
   args[cmd.length] = FileUtil.makeShellPath(f, true);
   String output = Shell.execCommand(args);
   return output;
 }
コード例 #5
0
 /** Use the command chmod to set permission. */
 @Override
 public void setPermission(Path p, FsPermission permission) throws IOException {
   if (NativeIO.isAvailable()) {
     NativeIO.POSIX.chmod(pathToFile(p).getCanonicalPath(), permission.toShort());
   } else {
     String perm = String.format("%04o", permission.toShort());
     Shell.execCommand(
         Shell.getSetPermissionCommand(perm, false, FileUtil.makeShellPath(pathToFile(p), true)));
   }
 }
コード例 #6
0
 /**
  * Add quotes to each of the command strings and return as a single string
  *
  * @param cmd The command to be quoted
  * @param isExecutable makes shell path if the first argument is executable
  * @return returns The quoted string.
  * @throws IOException
  */
 public static String addCommand(List<String> cmd, boolean isExecutable) throws IOException {
   StringBuffer command = new StringBuffer();
   for (String s : cmd) {
     command.append('\'');
     if (isExecutable) {
       // the executable name needs to be expressed as a shell path for the
       // shell to find it.
       command.append(FileUtil.makeShellPath(new File(s)));
       isExecutable = false;
     } else {
       command.append(s);
     }
     command.append('\'');
     command.append(" ");
   }
   return command.toString();
 }
コード例 #7
0
 /**
  * Get the real task-log file-path
  *
  * @param location Location of the log-file. This should point to an attempt-directory.
  * @param filter
  * @return
  * @throws IOException
  */
 static String getRealTaskLogFilePath(String location, LogName filter) throws IOException {
   return FileUtil.makeShellPath(new File(location, filter.toString()));
 }