Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sftp-put for writing a local file to a remote SFTP server. #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@

;; SFTP
:sftp-list-directory
:sftp-get
:sftp-delete
:sftp-put

;; STREAMS API // BLOCKING

Expand Down
18 changes: 18 additions & 0 deletions src/sftp.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,21 @@ 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 &key (modes #o644))
(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))
modes
: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)))))
32 changes: 16 additions & 16 deletions src/types.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,22 @@
(:excl #x20))

(defbitfield (sftp-modes :long)
(:named-pipe #x10000)
(:character-special #x20000)
(:directory #x40000)
(:block-special #x60000)
(:regular #x100000)
(:symbolic-link #x120000)
(:socket #x140000)
(:user-read #x400)
(:user-write #x200)
(:user-execute #x100)
(:group-read #x40)
(:group-write #x20)
(:group-execute #x10)
(:other-read #x4)
(:other-write #x2)
(:other-execute #x1))
(:named-pipe #o10000)
(:character-special #o20000)
(:directory #o40000)
(:block-special #o60000)
(:regular #o100000)
(:symbolic-link #o120000)
(:socket #o140000)
(:user-read #o400)
(:user-write #o200)
(:user-execute #o100)
(:group-read #o40)
(:group-write #o20)
(:group-execute #o10)
(:other-read #o4)
(:other-write #o2)
(:other-execute #o1))

(defcenum (sftp-error-code :unsigned-long)
(:ok 0)
Expand Down