+/*
+ * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
+ * 64-bit types on this platform.
+ */
+void write_longint(int f, int64 x)
+{
+ char b[8];
+
+ if (x <= 0x7FFFFFFF) {
+ write_int(f, (int)x);
+ return;
+ }
+
+#if SIZEOF_INT64 < 8
+ rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
+ exit_cleanup(RERR_UNSUPPORTED);
+#else
+ write_int(f, (int32)0xFFFFFFFF);
+ SIVAL(b,0,(x&0xFFFFFFFF));
+ SIVAL(b,4,((x>>32)&0xFFFFFFFF));
+
+ writefd(f,b,8);
+#endif
+}
+
+void write_buf(int f,char *buf,size_t len)
+{
+ writefd(f,buf,len);
+}
+
+/** Write a string to the connection */
+void write_sbuf(int f, char *buf)
+{
+ writefd(f, buf, strlen(buf));
+}
+
+void write_byte(int f, uchar c)
+{
+ writefd(f, (char *)&c, 1);
+}
+
+void write_vstring(int f, char *str, int len)
+{
+ uchar lenbuf[3], *lb = lenbuf;
+
+ if (len > 0x7F) {
+ if (len > 0x7FFF) {
+ rprintf(FERROR,
+ "attempting to send over-long vstring (%d > %d)\n",
+ len, 0x7FFF);
+ exit_cleanup(RERR_PROTOCOL);
+ }
+ *lb++ = len / 0x100 + 0x80;
+ }
+ *lb = len;
+
+ writefd(f, (char*)lenbuf, lb - lenbuf + 1);
+ if (len)
+ writefd(f, str, len);
+}
+
+/**
+ * Read a line of up to @p maxlen characters into @p buf (not counting
+ * the trailing null). Strips the (required) trailing newline and all
+ * carriage returns.
+ *
+ * @return 1 for success; 0 for I/O error or truncation.
+ **/
+int read_line(int f, char *buf, size_t maxlen)
+{
+ while (maxlen) {
+ buf[0] = 0;
+ read_buf(f, buf, 1);
+ if (buf[0] == 0)
+ return 0;
+ if (buf[0] == '\n')
+ break;
+ if (buf[0] != '\r') {
+ buf++;
+ maxlen--;
+ }
+ }
+ *buf = '\0';
+ return maxlen > 0;
+}
+
+void io_printf(int fd, const char *format, ...)
+{
+ va_list ap;
+ char buf[BIGPATHBUFLEN];
+ int len;
+
+ va_start(ap, format);
+ len = vsnprintf(buf, sizeof buf, format, ap);
+ va_end(ap);
+
+ if (len < 0)
+ exit_cleanup(RERR_STREAMIO);
+
+ if (len > (int)sizeof buf) {
+ rprintf(FERROR, "io_printf() was too long for the buffer.\n");
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ write_sbuf(fd, buf);
+}
+
+/** Setup for multiplexing a MSG_* stream with the data stream. */
+void io_start_multiplex_out(void)
+{
+ io_flush(NORMAL_FLUSH);
+ io_start_buffering_out();
+ io_multiplexing_out = 1;
+}
+
+/** Setup for multiplexing a MSG_* stream with the data stream. */
+void io_start_multiplex_in(void)
+{
+ io_flush(NORMAL_FLUSH);
+ io_start_buffering_in();
+ io_multiplexing_in = 1;
+}
+
+/** Write an message to the multiplexed data stream. */
+int io_multiplex_write(enum msgcode code, char *buf, size_t len)
+{
+ if (!io_multiplexing_out)
+ return 0;
+
+ io_flush(NORMAL_FLUSH);
+ stats.total_written += (len+4);
+ mplex_write(code, buf, len);
+ return 1;
+}
+
+void close_multiplexing_in(void)
+{
+ io_multiplexing_in = 0;
+}
+
+/** Stop output multiplexing. */
+void close_multiplexing_out(void)
+{
+ io_multiplexing_out = 0;
+}
+
+void start_write_batch(int fd)
+{
+ write_stream_flags(batch_fd);
+
+ /* Some communication has already taken place, but we don't
+ * enable batch writing until here so that we can write a
+ * canonical record of the communication even though the
+ * actual communication so far depends on whether a daemon
+ * is involved. */
+ write_int(batch_fd, protocol_version);
+ write_int(batch_fd, checksum_seed);
+
+ if (am_sender)
+ write_batch_monitor_out = fd;
+ else
+ write_batch_monitor_in = fd;
+}
+
+void stop_write_batch(void)
+{
+ write_batch_monitor_out = -1;
+ write_batch_monitor_in = -1;
+}