+ LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sent: %zd", written);
+ if (ret != 0)
+ return -1;
+ return written;
+}
+#endif
+
+
+/*
+ * Essentially a loop around buf_read() to ensure "length" bytes are read
+ * from dsi->buffer and/or the socket.
+ *
+ * @returns length on success, some value smaller then length indicates an error
+ */
+size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
+{
+ size_t stored;
+ ssize_t len;
+
+ if (dsi->flags & DSI_DISCONNECTED)
+ return 0;
+
+ LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length);
+
+ stored = 0;
+ while (stored < length) {
+ len = buf_read(dsi, (u_int8_t *) data + stored, length - stored);
+ if (len == -1 && (errno == EINTR || errno == EAGAIN)) {
+ LOG(log_maxdebug, logtype_dsi, "dsi_stream_read: select read loop");
+ continue;
+ } else if (len > 0) {
+ stored += len;
+ } else { /* eof or error */
+ /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
+#if 0
+ if (errno == ECONNRESET)
+ dsi->flags |= DSI_GOT_ECONNRESET;
+#endif
+ if (len || stored || dsi->read_count) {
+ if (! (dsi->flags & DSI_DISCONNECTED)) {
+ LOG(log_error, logtype_dsi, "dsi_stream_read: len:%d, %s",
+ len, (len < 0) ? strerror(errno) : "unexpected EOF");
+ }
+ return 0;
+ }
+ break;
+ }
+ }
+
+ dsi->read_count += stored;
+
+ LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes): got: %u", length, stored);
+ return stored;