FtpChannel.java

  1. /*
  2.  * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.transport;

  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.util.Collection;
  16. import java.util.concurrent.TimeUnit;

  17. /**
  18.  * An interface providing FTP operations over a {@link RemoteSession}. All
  19.  * operations are supposed to throw {@link FtpException} for remote file system
  20.  * errors and other IOExceptions on connection errors.
  21.  *
  22.  * @since 5.2
  23.  */
  24. public interface FtpChannel {

  25.     /**
  26.      * An {@link Exception} for reporting SFTP errors.
  27.      */
  28.     static class FtpException extends IOException {

  29.         private static final long serialVersionUID = 7176525179280330876L;

  30.         public static final int OK = 0;

  31.         public static final int EOF = 1;

  32.         public static final int NO_SUCH_FILE = 2;

  33.         public static final int NO_PERMISSION = 3;

  34.         public static final int UNSPECIFIED_FAILURE = 4;

  35.         public static final int PROTOCOL_ERROR = 5;

  36.         public static final int UNSUPPORTED = 8;

  37.         private final int status;

  38.         public FtpException(String message, int status) {
  39.             super(message);
  40.             this.status = status;
  41.         }

  42.         public FtpException(String message, int status, Throwable cause) {
  43.             super(message, cause);
  44.             this.status = status;
  45.         }

  46.         public int getStatus() {
  47.             return status;
  48.         }
  49.     }

  50.     /**
  51.      * Connects the {@link FtpChannel} to the remote end.
  52.      *
  53.      * @param timeout
  54.      *            for establishing the FTP connection
  55.      * @param unit
  56.      *            of the {@code timeout}
  57.      * @throws IOException
  58.      */
  59.     void connect(int timeout, TimeUnit unit) throws IOException;

  60.     /**
  61.      * Disconnects and {@link FtpChannel}.
  62.      */
  63.     void disconnect();

  64.     /**
  65.      * @return whether the {@link FtpChannel} is connected
  66.      */
  67.     boolean isConnected();

  68.     /**
  69.      * Changes the current remote directory.
  70.      *
  71.      * @param path
  72.      *            target directory
  73.      * @throws IOException
  74.      *             if the operation could not be performed remotely
  75.      */
  76.     void cd(String path) throws IOException;

  77.     /**
  78.      * @return the current remote directory path
  79.      * @throws IOException
  80.      */
  81.     String pwd() throws IOException;

  82.     /**
  83.      * Simplified remote directory entry.
  84.      */
  85.     interface DirEntry {
  86.         String getFilename();

  87.         long getModifiedTime();

  88.         boolean isDirectory();
  89.     }

  90.     /**
  91.      * Lists contents of a remote directory
  92.      *
  93.      * @param path
  94.      *            of the directory to list
  95.      * @return the directory entries
  96.      * @throws IOException
  97.      */
  98.     Collection<DirEntry> ls(String path) throws IOException;

  99.     /**
  100.      * Deletes a directory on the remote file system. The directory must be
  101.      * empty.
  102.      *
  103.      * @param path
  104.      *            to delete
  105.      * @throws IOException
  106.      */
  107.     void rmdir(String path) throws IOException;

  108.     /**
  109.      * Creates a directory on the remote file system.
  110.      *
  111.      * @param path
  112.      *            to create
  113.      * @throws IOException
  114.      */
  115.     void mkdir(String path) throws IOException;

  116.     /**
  117.      * Obtain an {@link InputStream} to read the contents of a remote file.
  118.      *
  119.      * @param path
  120.      *            of the file to read
  121.      *
  122.      * @return the stream to read from
  123.      * @throws IOException
  124.      */
  125.     InputStream get(String path) throws IOException;

  126.     /**
  127.      * Obtain an {@link OutputStream} to write to a remote file. If the file
  128.      * exists already, it will be overwritten.
  129.      *
  130.      * @param path
  131.      *            of the file to read
  132.      *
  133.      * @return the stream to read from
  134.      * @throws IOException
  135.      */
  136.     OutputStream put(String path) throws IOException;

  137.     /**
  138.      * Deletes a file on the remote file system.
  139.      *
  140.      * @param path
  141.      *            to delete
  142.      * @throws IOException
  143.      *             if the file does not exist or could otherwise not be deleted
  144.      */
  145.     void rm(String path) throws IOException;

  146.     /**
  147.      * Deletes a file on the remote file system. If the file does not exist, no
  148.      * exception is thrown.
  149.      *
  150.      * @param path
  151.      *            to delete
  152.      * @throws IOException
  153.      *             if the file exist but could not be deleted
  154.      */
  155.     default void delete(String path) throws IOException {
  156.         try {
  157.             rm(path);
  158.         } catch (FileNotFoundException e) {
  159.             // Ignore; it's OK if the file doesn't exist
  160.         } catch (FtpException f) {
  161.             if (f.getStatus() == FtpException.NO_SUCH_FILE) {
  162.                 return;
  163.             }
  164.             throw f;
  165.         }
  166.     }

  167.     /**
  168.      * Renames a file on the remote file system. If {@code to} exists, it is
  169.      * replaced by {@code from}. (POSIX rename() semantics)
  170.      *
  171.      * @param from
  172.      *            original name of the file
  173.      * @param to
  174.      *            new name of the file
  175.      * @throws IOException
  176.      * @see <a href=
  177.      *      "http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html">stdio.h:
  178.      *      rename()</a>
  179.      */
  180.     void rename(String from, String to) throws IOException;

  181. }