Skip to content

Instantly share code, notes, and snippets.

@artyom-poptsov
Created February 21, 2016 22:14
Show Gist options
  • Save artyom-poptsov/ea2420dacacdbd176916 to your computer and use it in GitHub Desktop.
Save artyom-poptsov/ea2420dacacdbd176916 to your computer and use it in GitHub Desktop.
SFTP file streams
#!/usr/bin/guile \
-e main -s
!#
;; Copyright (C) 2016 Artyom V. Poptsov <poptsov.artyom@gmail.com>
;;
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(use-modules (srfi srfi-41) ; streams
(ssh session)
(ssh sftp)
(ssh auth))
(define (sftp-file->stream sftp-file)
(stream-let loop ((c (read-char sftp-file)))
(if (eof-object? c)
(begin
(close-input-port sftp-file)
stream-null)
(stream-cons c (loop (read-char sftp-file))))))
(define (main args)
(let ((s (make-session #:host "localhost")))
(connect! s)
(userauth-agent! s)
(let* ((sftp-session (make-sftp-session s))
(remote-file (sftp-open sftp-session "/etc/hosts" O_RDONLY))
(rstream (sftp-file->stream remote-file)))
(stream-for-each display (stream-map char-upcase rstream)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment