|
| 1 | +(ns clj-libssh2.channel |
| 2 | + (:require [net.n01se.clojure-jna :as jna] |
| 3 | + [clj-libssh2.error :refer [handle-errors]] |
| 4 | + [clj-libssh2.libssh2 :as libssh2] |
| 5 | + [clj-libssh2.libssh2.channel :as libssh2-channel] |
| 6 | + [clj-libssh2.socket :refer [block block-return wait]]) |
| 7 | + (:import [java.io InputStream OutputStream PushbackInputStream] |
| 8 | + [com.sun.jna.ptr IntByReference PointerByReference])) |
| 9 | + |
| 10 | +(defn close |
| 11 | + "Close a channel." |
| 12 | + [session channel] |
| 13 | + (block session |
| 14 | + (handle-errors session (libssh2-channel/close channel)))) |
| 15 | + |
| 16 | +(defn exec |
| 17 | + "Execute a command via a channel." |
| 18 | + [session channel commandline] |
| 19 | + (block session |
| 20 | + (handle-errors session (libssh2-channel/exec channel commandline)))) |
| 21 | + |
| 22 | +(defn exit-signal |
| 23 | + "Collect the exit signal data from a channel." |
| 24 | + [session channel] |
| 25 | + (let [->str (fn [string-ref length-ref] |
| 26 | + (let [string (.getValue string-ref) |
| 27 | + length (.getValue length-ref)] |
| 28 | + (when (< 0 length) |
| 29 | + (String. (.getByteArray string 0 length) "ASCII")))) |
| 30 | + exit-signal (PointerByReference.) |
| 31 | + exit-signal-len (IntByReference.) |
| 32 | + err-msg (PointerByReference.) |
| 33 | + err-msg-len (IntByReference.) |
| 34 | + lang-tag (PointerByReference.) |
| 35 | + lang-tag-len (IntByReference.)] |
| 36 | + (handle-errors session |
| 37 | + (libssh2-channel/get-exit-signal channel |
| 38 | + exit-signal exit-signal-len |
| 39 | + err-msg err-msg-len |
| 40 | + lang-tag lang-tag-len)) |
| 41 | + {:exit-signal (->str exit-signal exit-signal-len) |
| 42 | + :err-msg (->str err-msg err-msg-len) |
| 43 | + :lang-tag (->str lang-tag lang-tag-len)})) |
| 44 | + |
| 45 | +(defn exit-status |
| 46 | + "Get the exit code from the last executed command." |
| 47 | + [channel] |
| 48 | + (libssh2-channel/get-exit-status channel)) |
| 49 | + |
| 50 | +(defn free |
| 51 | + "Free a channel." |
| 52 | + [channel] |
| 53 | + (libssh2-channel/free channel)) |
| 54 | + |
| 55 | +(defn open |
| 56 | + "Create a new channel in a session." |
| 57 | + [session] |
| 58 | + (block-return session |
| 59 | + (libssh2-channel/open-session (:session session)))) |
| 60 | + |
| 61 | +(defn send-eof |
| 62 | + "Tell the remote process that we're done sending input." |
| 63 | + [session channel] |
| 64 | + (block session (handle-errors session (libssh2-channel/send-eof channel)))) |
| 65 | + |
| 66 | +(defn pull |
| 67 | + "Read some output from a given stream on a channel. |
| 68 | +
|
| 69 | + Parameters: |
| 70 | +
|
| 71 | + session The usual session object. |
| 72 | + channel A valid channel for this session. |
| 73 | + ssh-stream-id 0 for STDOUT, 1 for STDERR or any other number that the |
| 74 | + process on the other end wishes to send data on. |
| 75 | + output-stream A java.io.OutputStream to send the data to. |
| 76 | +
|
| 77 | + Return: |
| 78 | +
|
| 79 | + Either :eof, :eagain or :ready. If :eof, then no more data will be sent. If |
| 80 | + :eagain, then more data might be available. You should select on the |
| 81 | + appropriate socket and try again. If :ready, the stream is ready for another |
| 82 | + read immediately." |
| 83 | + [session channel ssh-stream-id ^OutputStream output-stream] |
| 84 | + (let [size (-> session :options :read-chunk-size) |
| 85 | + buf1 (jna/make-cbuf size) |
| 86 | + res (handle-errors session |
| 87 | + (libssh2-channel/read-ex channel ssh-stream-id buf1 size))] |
| 88 | + (when (and (some? output-stream) (< 0 res)) |
| 89 | + (let [buf2 (byte-array size (byte 0))] |
| 90 | + (.get buf1 buf2 0 res) |
| 91 | + (.write output-stream buf2 0 res))) |
| 92 | + (condp = res |
| 93 | + 0 :eof |
| 94 | + libssh2/ERROR_EAGAIN :eagain |
| 95 | + :ready))) |
| 96 | + |
| 97 | +(defn push |
| 98 | + "Write some input to a given stream on a channel. |
| 99 | +
|
| 100 | + Parameters: |
| 101 | +
|
| 102 | + session The usual session object. |
| 103 | + channel A valid channel for this session. |
| 104 | + ssh-stream-id 0 for STDIN or any other number that the process on the other |
| 105 | + end wishes to receive data on. |
| 106 | + input-stream A java.io.PushbackInputStream to grab the data from. This must |
| 107 | + be capable of pushing back as `:write-chunk-size` bytes. |
| 108 | +
|
| 109 | + Return: |
| 110 | +
|
| 111 | + Either :eof or :ready. If :eof, then the input-stream has returned -1 and no |
| 112 | + more data will be read from it nor written to the channel. If :eagain, then |
| 113 | + you should select on the appropriate socket before calling this again. If |
| 114 | + :ready then there are more bytes to be processed and this function should be |
| 115 | + called again." |
| 116 | + [session channel ssh-stream-id ^PushbackInputStream input-stream] |
| 117 | + {:pre [(instance? PushbackInputStream input-stream)]} |
| 118 | + (let [size (-> session :options :write-chunk-size) |
| 119 | + read-size (min size (-> session :options :read-chunk-size)) |
| 120 | + buf (byte-array read-size (byte 0)) |
| 121 | + bytes-read (.read input-stream buf)] |
| 122 | + (if (< -1 bytes-read) |
| 123 | + (if (< 0 bytes-read) |
| 124 | + (let [sent (handle-errors session |
| 125 | + (libssh2-channel/write-ex channel ssh-stream-id buf size))] |
| 126 | + (when (< sent bytes-read) |
| 127 | + (let [bytes-sent (if (< 0 sent) sent 0)] |
| 128 | + (.unread input-stream buf bytes-sent (- bytes-read bytes-sent)))) |
| 129 | + (if (= libssh2/ERROR_EAGAIN sent) |
| 130 | + :eagain |
| 131 | + :ready)) |
| 132 | + :ready) |
| 133 | + (do |
| 134 | + (send-eof session channel) |
| 135 | + :eof)))) |
| 136 | + |
| 137 | +(defn- ensure-pushback |
| 138 | + [size stream] |
| 139 | + (let [s (:stream stream)] |
| 140 | + (if (and (instance? InputStream s) (not (instance? PushbackInputStream s))) |
| 141 | + (assoc stream :stream (PushbackInputStream. s size)) |
| 142 | + stream))) |
| 143 | + |
| 144 | +(defn- make-stream |
| 145 | + [now direction [id stream]] |
| 146 | + (hash-map :id id |
| 147 | + :direction direction |
| 148 | + :stream stream |
| 149 | + :last-read-time now |
| 150 | + :status :ready)) |
| 151 | + |
| 152 | +(defn- pump-stream |
| 153 | + "Do exactly one push/pull on a stream and enforce read timeouts" |
| 154 | + [session channel stream] |
| 155 | + (if (not= :eof (:status stream)) |
| 156 | + (let [pump-fn (if (= :output (:direction stream)) pull push) |
| 157 | + last-read-time (:last-read-time stream) |
| 158 | + new-status (pump-fn session channel (:id stream) (:stream stream)) |
| 159 | + now (System/currentTimeMillis)] |
| 160 | + (when (and (= pump-fn pull) |
| 161 | + (= :eagain new-status) |
| 162 | + (< (-> session :options :read-timeout) (- now last-read-time))) |
| 163 | + (throw (Exception. (format "Read timeout for %s stream %d" |
| 164 | + (-> stream :direction name) |
| 165 | + (-> stream :id))))) |
| 166 | + (assoc stream :status new-status :last-read-time now)) |
| 167 | + stream)) |
| 168 | + |
| 169 | +(defn pump |
| 170 | + "Process a collection of input and output streams all at once." |
| 171 | + [session channel input-streams output-streams] |
| 172 | + (let [now (System/currentTimeMillis) |
| 173 | + write-size (-> session :options :write-chunk-size) |
| 174 | + streams (concat (->> input-streams |
| 175 | + (map (partial make-stream now :input)) |
| 176 | + (map (partial ensure-pushback write-size))) |
| 177 | + (->> output-streams |
| 178 | + (map (partial make-stream now :output))))] |
| 179 | + (when-not (empty? streams) |
| 180 | + (loop [s (map (partial pump-stream session channel) streams)] |
| 181 | + (let [status-set (->> s (map :status) set)] |
| 182 | + (if (not= #{:eof} status-set) |
| 183 | + (do |
| 184 | + (when (contains? status-set :eagain) |
| 185 | + (wait session)) |
| 186 | + (recur (map (partial pump-stream session channel) streams))) |
| 187 | + (->> s |
| 188 | + (filter #(= :output (:direction %))) |
| 189 | + (map #(hash-map (:id %) %)) |
| 190 | + (apply merge)))))))) |
| 191 | + |
| 192 | +(defmacro with-channel |
| 193 | + "Convenience macro for wrapping a bunch of channel operations." |
| 194 | + [session chan & body] |
| 195 | + `(let [~chan (open ~session)] |
| 196 | + (try |
| 197 | + (do ~@body) |
| 198 | + (finally |
| 199 | + (close ~session ~chan) |
| 200 | + (handle-errors ~session |
| 201 | + (free ~chan)))))) |
0 commit comments