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)); } }
@Override public int execute( final OverthereExecutionOutputHandler stdoutHandler, final OverthereExecutionOutputHandler stderrHandler, final CmdLine commandLine) { final OverthereProcess process = startProcess(commandLine); Thread stdoutReaderThread = null; Thread stderrReaderThread = null; final CountDownLatch latch = new CountDownLatch(2); try { stdoutReaderThread = getThread("stdout", commandLine.toString(), stdoutHandler, process.getStdout(), latch); stdoutReaderThread.start(); stderrReaderThread = getThread("stderr", commandLine.toString(), stderrHandler, process.getStderr(), latch); stderrReaderThread.start(); try { latch.await(); return process.waitFor(); } catch (InterruptedException exc) { Thread.currentThread().interrupt(); logger.info("Execution interrupted, destroying the process."); process.destroy(); throw new RuntimeIOException("Execution interrupted", exc); } } finally { quietlyJoinThread(stdoutReaderThread); quietlyJoinThread(stderrReaderThread); } }
public static void main(String[] args) throws IOException { ConnectionOptions options = new ConnectionOptions(); options.set(ADDRESS, "unix-box"); options.set(USERNAME, "demo"); options.set(PASSWORD, "secret"); options.set(OPERATING_SYSTEM, UNIX); options.set(CONNECTION_TYPE, SFTP); OverthereConnection connection = Overthere.getConnection("ssh", options); try { connection.execute(CmdLine.build("cp", "-r", "/var/log/apt", "/tmp/logs1")); OverthereFile logs1 = connection.getFile("/tmp/logs1"); OverthereFile logs2 = connection.getFile("/tmp/logs2"); logs2.delete(); System.err.println("Exists #1: " + logs1.exists()); System.err.println("Exists #2: " + logs2.exists()); logs1.renameTo(logs2); System.err.println("Exists #1: " + logs1.exists()); System.err.println("Exists #2: " + logs2.exists()); logs2.copyTo(logs1); System.err.println("Exists #1: " + logs1.exists()); System.err.println("Exists #2: " + logs2.exists()); logs1.deleteRecursively(); logs2.deleteRecursively(); System.err.println("Exists #1: " + logs1.exists()); System.err.println("Exists #2: " + logs2.exists()); } finally { connection.close(); } }
@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 + ")"); } }
@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 + ")"); } }
@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 + ")"); } }
@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); } }
/** * 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; }