Skip to content

Commit

Permalink
Add sftp-put for writing a file via SFTP.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Oct 9, 2024
1 parent 36547fe commit 8e97bd5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
:sftp-list-directory
:sftp-get
:sftp-delete
:sftp-put

;; STREAMS API // BLOCKING

Expand Down
17 changes: 17 additions & 0 deletions src/sftp.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,20 @@ It is possible to combine `MAXFILES' and `EXTENSIONS' (retrieve 5 files with ext
(ssh2.debug "Trying to delete remote file ~A" remote-path)
(let ((result (libssh2-sftp-unlink-ex sftp remote-path)))
(ssh2.debug "Deleting ~A resulted in ~A." remote-path result))))

(defun sftp-put (ssh-connection local-path remote-path)
(with-sftp (sftp ssh-connection)
(let ((handle))
(unwind-protect
(progn
(ssh2.debug "Trying to send local file ~A to remote file ~A" local-path remote-path)
(setf handle (libssh2-sftp-open-ex sftp remote-path (foreign-bitfield-value 'sftp-flags '(:write :creat :trunc))
#o644
:file))
(with-open-file (in local-path :direction :input :element-type '(unsigned-byte 8))
(let ((buffer (make-array 1024 :element-type '(unsigned-byte 8))))
(loop for numbytes = (read-sequence buffer in)
until (zerop numbytes)
do (cffi:with-foreign-array (array buffer `(:array :uint8 ,numbytes)) (libssh2-sftp-write handle array numbytes)))))
(ssh2.debug "Local file ~A was written to ~A" local-path remote-path)))
(when handle (libssh2-sftp-close-handle handle)))))

0 comments on commit 8e97bd5

Please sign in to comment.