api-sftp.texi 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. @c -*-texinfo-*-
  2. @c This file is part of Guile-SSH Reference Manual.
  3. @c Copyright (C) 2015 Artyom V. Poptsov
  4. @c See the file guile-ssh.texi for copying conditions.
  5. @node SFTP
  6. @section SFTP
  7. @cindex SFTP
  8. @cindex file transfer
  9. The @code{(ssh sftp)} module provides procedures for working with SFTP.
  10. @subsection SFTP session
  11. @deffn {Scheme Procedure} make-sftp-session ssh-session
  12. Make a new SFTP session using a @var{ssh-session}, initialize the session with
  13. a server. Return initialized SFTP session or throw @code{guile-ssh-error}
  14. exception on an error.
  15. @end deffn
  16. @deffn {Scheme Procedure} sftp-session? x
  17. Return @code{#t} if @var{x} is a SFTP session, @code{#f} otherwise.
  18. @end deffn
  19. @deffn {Scheme Procedure} sftp-get-session sftp-session
  20. Get the parent SSH session for a @var{sftp-session}.
  21. @end deffn
  22. @deffn {Scheme Procedure} sftp-get-error sftp-session
  23. Get the last SFTP error from a @var{sftp-session}. Return the error name as a
  24. symbol, or throw @code{guile-ssh-error} on if an error occured in the
  25. procedure itself.
  26. The procedure returns one of the following values:
  27. @table @samp
  28. @item fx-ok
  29. No error.
  30. @item fx-eof
  31. End-of-file encountered.
  32. @item fx-no-such-file
  33. File doesn't exist.
  34. @item fx-permission-denied
  35. Permission denied.
  36. @item fx-failure
  37. Generic failure.
  38. @item fx-bad-message
  39. Garbage received from the server.
  40. @item fx-no-connection
  41. No connection has been set up.
  42. @item fx-connection-lost
  43. There was a connection, but we lost it.
  44. @item fx-op-unsupported
  45. Operation not supported by the server.
  46. @item fx-invalid-handle
  47. Invalid file handle.
  48. @item fx-no-such-path
  49. No such file or directory path exists.
  50. @item fx-file-already-exist
  51. An attempt to create an already existing file or directory has been made.
  52. @item fx-write-protect
  53. We are trying to write on a write-protected filesystem.
  54. @item fx-no-media
  55. No media in remote drive.
  56. @end table
  57. @end deffn
  58. @deffn {Scheme Procedure} sftp-mkdir sftp-session dirname [mode=#o777]
  59. Create a directory @var{dirname} using a @var{sftp-session} with a @var{mode}.
  60. If the @var{mode} is omitted, the current umask value is used.
  61. @end deffn
  62. @deffn {Scheme Procedure} sftp-rmdir sftp-session dirname
  63. Remove a directory @var{dirname}. Throw @code{guile-ssh-error} on an error.
  64. Return value is undefined.
  65. @end deffn
  66. @deffn {Scheme Procedure} sftp-mv sftp-session source dest
  67. Move or rename a file @var{source} into a @var{dest}. Throw
  68. @code{guile-ssh-error} on an error. Return value is undefined.
  69. @end deffn
  70. @deffn {Scheme Procedure} sftp-symlink sftp-session target dest
  71. Create a symbolic link to a @var{target} in a @var{dest}. Throw
  72. @code{guile-ssh-error} on an error. Return value is undefined.
  73. @end deffn
  74. @deffn {Scheme Procedure} sftp-readlink sftp-session path
  75. Read the value of a symbolic link pointed by a @var{path}. Return the value
  76. or @code{#f} on an error.
  77. @end deffn
  78. @deffn {Scheme Procedure} sftp-chmod sftp-session filename mode
  79. Change permissions of a remote @var{filename} using @var{sftp-session}.
  80. Permissions are set to @code{mode & ~umask}. Throw @code{guile-ssh-error} on
  81. an error. Return value is undefined.
  82. @end deffn
  83. @deffn {Scheme Procedure} sftp-unlink sftp-session filename
  84. Unlink (delete) a remote @var{filename} using @var{sftp-session}. Throw
  85. @code{guile-ssh-error} on an error. Return value is undefined.
  86. @end deffn
  87. @subsubsection Low-Level API
  88. @deffn {Scheme Procedure} %make-sftp-session ssh-session
  89. Make a new SFTP session using a @var{ssh-session} without initialization of
  90. the session with a server. Throw @code{guile-ssh-error} exception on an
  91. error.
  92. Note that you should call @code{%sftp-init} on the returned SFTP session before
  93. using it.
  94. @end deffn
  95. @deffn {Scheme Procedure} %sftp-init sftp-session
  96. Initialize a @var{sftp-session} with the server. Throw @code{guile-ssh-error}
  97. exception on an error, return value is undefined.
  98. @end deffn
  99. @subsection SFTP file
  100. Remote files are represented as regular Guile ports that allow random access
  101. (@pxref{Input and Output,,, guile, The GNU Guile Reference Manual}.)
  102. @deffn {Scheme Procedure} sftp-open sftp-session filename flags [mode=#o666]
  103. Open a remote @var{filename} using an @var{sftp-session}, return an open file
  104. port. Throw @code{guile-ssh-error} on an error.
  105. @end deffn
  106. @deffn {Scheme Procedure} sftp-file? x
  107. Return @code{#t} if @var{x} is an SFTP file port, @code{#f} otherwise.
  108. @end deffn
  109. @subsection High-level operations on remote files
  110. @deffn {Scheme Procedure} call-with-remote-input-file sftp-session filename proc
  111. Call a @var{proc} with a remote file port opened for input using an
  112. @var{sftp-session}. @var{proc} should be a procedure of one argument,
  113. @var{filename} should be a string naming a file. The behaviour is unspecified
  114. if a file already exists.
  115. The procedure calls @var{proc} with one argument: the port obtained by opening
  116. the named remote file for input.
  117. If the procedure returns, then the port is closed automatically and the values
  118. yielded by the procedure are returned. If the procedure does not return, then
  119. the port will not be closed automatically unless it is possible to prove that
  120. the port will never again be used for a read or write operation.
  121. @end deffn
  122. @deffn {Scheme Procedure} call-with-remote-output-file sftp-session filename proc
  123. Call a @var{proc} with a remote file port opened for output using an
  124. @var{sftp-session}. @var{proc} should be a procedure of one argument,
  125. @var{filename} should be a string naming a file. The behaviour is unspecified
  126. if a file already exists.
  127. The procedure calls @var{proc} with one argument: the port obtained by opening
  128. the named remote file for output.
  129. If the procedure returns, then the port is closed automatically and the values
  130. yielded by the procedure are returned. If the procedure does not return, then
  131. the port will not be closed automatically unless it is possible to prove that
  132. the port will never again be used for a read or write operation.
  133. @end deffn
  134. @deffn {Scheme Procedure} with-input-from-remote-file sftp-session filename thunk
  135. @var{thunk} must be a procedure of no arguments, and @var{filename} must be a
  136. string naming a file. The file must already exist. The file is opened for
  137. input, an input port connected to it is made the default value returned by
  138. @code{current-input-port}, and the @var{thunk} is called with no arguments.
  139. When the @var{thunk} returns, the port is closed and the previous default is
  140. restored. Returns the values yielded by @var{thunk}. If an escape procedure
  141. is used to escape from the continuation of these procedures, their behavior is
  142. implementation dependent.
  143. Example:
  144. @lisp
  145. (define (rcat user host filename)
  146. "Print contents of a remote file on a HOST pointed by a FILENAME to
  147. stdout."
  148. (let ((session (make-session #:user user #:host host)))
  149. ;; Connect to an SSH server.
  150. (connect! session)
  151. (authenticate-server session)
  152. ;; Authenticate with an SSH server using a SSH agent.
  153. (userauth-agent! session)
  154. (let ((sftp-session (make-sftp-session session)))
  155. ;; Read read a file line-by-line and print it to stdout.
  156. (with-input-from-remote-file sftp-session filename
  157. (lambda ()
  158. (do ((line (read-line) (read-line)))
  159. ((eof-object? line))
  160. (write-line line)))))))
  161. @end lisp
  162. @end deffn
  163. @deffn {Scheme Procedure} with-output-to-remote-file sftp-session filename thunk
  164. @var{thunk} must be a procedure of no arguments, and @var{filename} must be a
  165. string naming a file. The effect is unspecified if the file already exists.
  166. The file is opened for output, an output port connected to it is made the
  167. default value returned by @code{current-output-port}, and the @var{thunk} is
  168. called with no arguments. When the @var{thunk} returns, the port is closed
  169. and the previous default is restored. Returns the values yielded by
  170. @var{thunk}. If an escape procedure is used to escape from the continuation
  171. of these procedures, their behavior is implementation dependent.
  172. @end deffn
  173. @c Local Variables:
  174. @c TeX-master: "guile-ssh.texi"
  175. @c End: