Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FtpChannel.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.FileNotFoundException;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.io.OutputStream;
  48. import java.util.Collection;
  49. import java.util.concurrent.TimeUnit;
  50. /**
  51. * An interface providing FTP operations over a {@link RemoteSession}. All
  52. * operations are supposed to throw {@link FtpException} for remote file system
  53. * errors and other IOExceptions on connection errors.
  54. *
  55. * @since 5.2
  56. */
  57. public interface FtpChannel {
  58. /**
  59. * An {@link Exception} for reporting SFTP errors.
  60. */
  61. static class FtpException extends IOException {
  62. private static final long serialVersionUID = 7176525179280330876L;
  63. public static final int OK = 0;
  64. public static final int EOF = 1;
  65. public static final int NO_SUCH_FILE = 2;
  66. public static final int NO_PERMISSION = 3;
  67. public static final int UNSPECIFIED_FAILURE = 4;
  68. public static final int PROTOCOL_ERROR = 5;
  69. public static final int UNSUPPORTED = 8;
  70. private final int status;
  71. public FtpException(String message, int status) {
  72. super(message);
  73. this.status = status;
  74. }
  75. public FtpException(String message, int status, Throwable cause) {
  76. super(message, cause);
  77. this.status = status;
  78. }
  79. public int getStatus() {
  80. return status;
  81. }
  82. }
  83. /**
  84. * Connects the {@link FtpChannel} to the remote end.
  85. *
  86. * @param timeout
  87. * for establishing the FTP connection
  88. * @param unit
  89. * of the {@code timeout}
  90. * @throws IOException
  91. */
  92. void connect(int timeout, TimeUnit unit) throws IOException;
  93. /**
  94. * Disconnects and {@link FtpChannel}.
  95. */
  96. void disconnect();
  97. /**
  98. * @return whether the {@link FtpChannel} is connected
  99. */
  100. boolean isConnected();
  101. /**
  102. * Changes the current remote directory.
  103. *
  104. * @param path
  105. * target directory
  106. * @throws IOException
  107. * if the operation could not be performed remotely
  108. */
  109. void cd(String path) throws IOException;
  110. /**
  111. * @return the current remote directory path
  112. * @throws IOException
  113. */
  114. String pwd() throws IOException;
  115. /**
  116. * Simplified remote directory entry.
  117. */
  118. interface DirEntry {
  119. String getFilename();
  120. long getModifiedTime();
  121. boolean isDirectory();
  122. }
  123. /**
  124. * Lists contents of a remote directory
  125. *
  126. * @param path
  127. * of the directory to list
  128. * @return the directory entries
  129. * @throws IOException
  130. */
  131. Collection<DirEntry> ls(String path) throws IOException;
  132. /**
  133. * Deletes a directory on the remote file system. The directory must be
  134. * empty.
  135. *
  136. * @param path
  137. * to delete
  138. * @throws IOException
  139. */
  140. void rmdir(String path) throws IOException;
  141. /**
  142. * Creates a directory on the remote file system.
  143. *
  144. * @param path
  145. * to create
  146. * @throws IOException
  147. */
  148. void mkdir(String path) throws IOException;
  149. /**
  150. * Obtain an {@link InputStream} to read the contents of a remote file.
  151. *
  152. * @param path
  153. * of the file to read
  154. *
  155. * @return the stream to read from
  156. * @throws IOException
  157. */
  158. InputStream get(String path) throws IOException;
  159. /**
  160. * Obtain an {@link OutputStream} to write to a remote file. If the file
  161. * exists already, it will be overwritten.
  162. *
  163. * @param path
  164. * of the file to read
  165. *
  166. * @return the stream to read from
  167. * @throws IOException
  168. */
  169. OutputStream put(String path) throws IOException;
  170. /**
  171. * Deletes a file on the remote file system.
  172. *
  173. * @param path
  174. * to delete
  175. * @throws IOException
  176. * if the file does not exist or could otherwise not be deleted
  177. */
  178. void rm(String path) throws IOException;
  179. /**
  180. * Deletes a file on the remote file system. If the file does not exist, no
  181. * exception is thrown.
  182. *
  183. * @param path
  184. * to delete
  185. * @throws IOException
  186. * if the file exist but could not be deleted
  187. */
  188. default void delete(String path) throws IOException {
  189. try {
  190. rm(path);
  191. } catch (FileNotFoundException e) {
  192. // Ignore; it's OK if the file doesn't exist
  193. } catch (FtpException f) {
  194. if (f.getStatus() == FtpException.NO_SUCH_FILE) {
  195. return;
  196. }
  197. throw f;
  198. }
  199. }
  200. /**
  201. * Renames a file on the remote file system. If {@code to} exists, it is
  202. * replaced by {@code from}. (POSIX rename() semantics)
  203. *
  204. * @param from
  205. * original name of the file
  206. * @param to
  207. * new name of the file
  208. * @throws IOException
  209. * @see <a href=
  210. * "http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html">stdio.h:
  211. * rename()</a>
  212. */
  213. void rename(String from, String to) throws IOException;
  214. }