Beispiel #1
0
  protected void mkdir(String... mkdirOptions) throws RuntimeIOException {
    CmdLine commandLine = CmdLine.build("mkdir");
    for (String opt : mkdirOptions) {
      commandLine.addArgument(opt);
    }
    commandLine.addArgument(getPath());

    CapturingOverthereProcessOutputHandler capturedOutput = capturingHandler();
    int errno = executeCommand(multiHandler(loggingHandler(logger), capturedOutput), commandLine);
    if (errno != 0) {
      throw new RuntimeIOException(
          "Cannot create directory or -ies "
              + this
              + ": "
              + capturedOutput.getError()
              + " (errno="
              + errno
              + ")");
    }

    if (logger.isDebugEnabled()) {
      logger.debug(
          "Created directory " + this + " (with options:" + Joiner.on(' ').join(mkdirOptions));
    }
  }
Beispiel #2
0
  @Override
  public List<OverthereFile> listFiles() {
    logger.debug("Listing directory {}", this);

    CapturingOverthereProcessOutputHandler capturedOutput = capturingHandler();
    // Yes, this *is* meant to be 'el es minus one'! Each file should go one a separate line, even
    // if we create a pseudo-tty. Long format is NOT what we
    // want here.
    int errno =
        executeCommand(
            multiHandler(loggingHandler(logger), capturedOutput), build("ls", "-1", getPath()));
    if (errno != 0) {
      throw new RuntimeIOException(
          "Cannot list directory "
              + this
              + ": "
              + capturedOutput.getError()
              + " (errno="
              + errno
              + ")");
    }

    List<OverthereFile> files = newArrayList();
    for (String lsLine : capturedOutput.getOutputLines()) {
      files.add(connection.getFile(this, lsLine));
    }

    return files;
  }
Beispiel #3
0
  @Override
  public void deleteRecursively() throws RuntimeIOException {
    logger.debug("Recursively deleting file or directory {}", this);

    CapturingOverthereProcessOutputHandler capturedOutput = capturingHandler();
    int errno =
        executeCommand(
            multiHandler(loggingHandler(logger), capturedOutput),
            CmdLine.build("rm", "-rf", getPath()));
    if (errno != 0) {
      throw new RuntimeIOException(
          "Cannot recursively delete file or directory "
              + this
              + ": "
              + capturedOutput.getError()
              + " (errno="
              + errno
              + ")");
    }
  }
Beispiel #4
0
  @Override
  protected void deleteFile() {
    logger.debug("Deleting file {}", this);

    CapturingOverthereProcessOutputHandler capturedOutput = capturingHandler();
    int errno =
        executeCommand(
            multiHandler(loggingHandler(logger), capturedOutput),
            CmdLine.build("rm", "-f", getPath()));
    if (errno != 0) {
      throw new RuntimeIOException(
          "Cannot delete file "
              + this
              + ": "
              + capturedOutput.getError()
              + " (errno="
              + errno
              + ")");
    }
  }
Beispiel #5
0
  @Override
  public void setExecutable(boolean executable) {
    logger.debug("Setting execute permission on {} to {}", this, executable);

    CapturingOverthereProcessOutputHandler capturedOutput = capturingHandler();
    int errno =
        executeCommand(
            multiHandler(loggingHandler(logger), capturedOutput),
            CmdLine.build("chmod", executable ? "+x" : "-x", getPath()));
    if (errno != 0) {
      throw new RuntimeIOException(
          "Cannot set execute permission on file "
              + this
              + " to "
              + executable
              + ": "
              + capturedOutput.getError()
              + " (errno="
              + errno
              + ")");
    }
  }
Beispiel #6
0
  @Override
  public void renameTo(OverthereFile dest) {
    logger.debug("Renaming {} to {}", this, dest);

    if (dest instanceof SshScpFile) {
      SshScpFile sshScpDestFile = (SshScpFile) dest;
      if (sshScpDestFile.getConnection() == getConnection()) {
        CapturingOverthereProcessOutputHandler capturedOutput = capturingHandler();
        int errno =
            executeCommand(
                multiHandler(loggingHandler(logger), capturedOutput),
                CmdLine.build("mv", getPath(), sshScpDestFile.getPath()));
        if (errno != 0) {
          throw new RuntimeIOException(
              "Cannot rename file/directory "
                  + this
                  + ": "
                  + capturedOutput.getError()
                  + " (errno="
                  + errno
                  + ")");
        }
      } else {
        throw new RuntimeIOException(
            "Cannot rename ssh_scp file/directory "
                + this
                + " to ssh_scp file/directory "
                + dest
                + " because it is in a different connection");
      }
    } else {
      throw new RuntimeIOException(
          "Cannot rename ssh_scp file/directory "
              + this
              + " to non-ssh_scp file/directory "
              + dest);
    }
  }
Beispiel #7
0
  /**
   * Gets information about the file by executing "ls -ld" on it.
   *
   * @param file the file
   * @return the information about the file, never <code>null</code>.
   * @throws RuntimeIOException if an I/O exception occurs
   */
  public LsResults getFileInfo() throws RuntimeIOException {
    LsResults results = new LsResults();
    CapturingOverthereProcessOutputHandler capturedOutput = capturingHandler();
    int errno = executeCommand(capturedOutput, CmdLine.build("ls", "-ld", getPath()));
    if (errno == 0) {
      results.exists = true;
      if (capturedOutput.getOutputLines().size() > 0) {
        // parse ls results
        String outputLine = capturedOutput.getOutputLines().get(0);
        if (logger.isDebugEnabled()) logger.debug("ls output = " + outputLine);
        StringTokenizer outputTokens = new StringTokenizer(outputLine);
        if (outputTokens.countTokens() < 5) {
          throw new RuntimeIOException(
              "ls -ld "
                  + getPath()
                  + " returned output that contains less than the expected 5 tokens");
        }
        String permissions = outputTokens.nextToken();
        @SuppressWarnings("unused")
        String inodelinkes = outputTokens.nextToken();
        @SuppressWarnings("unused")
        String owner = outputTokens.nextToken();
        @SuppressWarnings("unused")
        String group = outputTokens.nextToken();
        String size = outputTokens.nextToken();

        results.isFile = permissions.length() >= 1 && permissions.charAt(0) == '-';
        results.isDirectory = permissions.length() >= 1 && permissions.charAt(0) == 'd';
        results.canRead = permissions.length() >= 2 && permissions.charAt(1) == 'r';
        results.canWrite = permissions.length() >= 3 && permissions.charAt(2) == 'w';
        results.canExecute = permissions.length() >= 4 && permissions.charAt(3) == 'x';
        try {
          results.length = Integer.parseInt(size);
        } catch (NumberFormatException exc) {
          logger.warn(
              "Cannot parse length of "
                  + this.getPath()
                  + " from ls output: "
                  + outputLine
                  + ". Length will be reported as -1.",
              exc);
        }
      }
    } else {
      results.exists = false;
    }

    if (logger.isDebugEnabled())
      logger.debug(
          "Listed file "
              + this
              + ": exists="
              + results.exists
              + ", isDirectory="
              + results.isDirectory
              + ", length="
              + results.length
              + ", canRead="
              + results.canRead
              + ", canWrite="
              + results.canWrite
              + ", canExecute="
              + results.canExecute);
    return results;
  }