Elijah Newren | 5e3f94d | 2023-04-22 20:17:23 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | d5fff46 | 2023-04-22 20:17:12 +0000 | [diff] [blame] | 2 | #include "copy.h" |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 3 | #include "pkt-line.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 4 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 5 | #include "hex.h" |
Jeff King | fd89433 | 2015-09-01 16:24:13 -0400 | [diff] [blame] | 6 | #include "run-command.h" |
Elijah Newren | 545f7b5 | 2023-12-23 17:14:56 +0000 | [diff] [blame] | 7 | #include "sideband.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 8 | #include "trace.h" |
Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 9 | #include "write-or-die.h" |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 10 | |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 11 | char packet_buffer[LARGE_PACKET_MAX]; |
Stephen Boyd | 1e4cd68 | 2011-04-03 00:06:54 -0700 | [diff] [blame] | 12 | static const char *packet_trace_prefix = "git"; |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 13 | static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET); |
Jeff King | 3235983 | 2015-06-16 13:23:20 -0400 | [diff] [blame] | 14 | static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE); |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 15 | |
| 16 | void packet_trace_identity(const char *prog) |
| 17 | { |
| 18 | packet_trace_prefix = xstrdup(prog); |
| 19 | } |
| 20 | |
Jeff King | fd89433 | 2015-09-01 16:24:13 -0400 | [diff] [blame] | 21 | static const char *get_trace_prefix(void) |
| 22 | { |
| 23 | return in_async() ? "sideband" : packet_trace_prefix; |
| 24 | } |
| 25 | |
Jeff King | 3235983 | 2015-06-16 13:23:20 -0400 | [diff] [blame] | 26 | static int packet_trace_pack(const char *buf, unsigned int len, int sideband) |
| 27 | { |
| 28 | if (!sideband) { |
| 29 | trace_verbatim(&trace_pack, buf, len); |
| 30 | return 1; |
| 31 | } else if (len && *buf == '\1') { |
| 32 | trace_verbatim(&trace_pack, buf + 1, len - 1); |
| 33 | return 1; |
| 34 | } else { |
| 35 | /* it's another non-pack sideband */ |
| 36 | return 0; |
| 37 | } |
| 38 | } |
| 39 | |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 40 | static void packet_trace(const char *buf, unsigned int len, int write) |
| 41 | { |
| 42 | int i; |
| 43 | struct strbuf out; |
Jeff King | 3235983 | 2015-06-16 13:23:20 -0400 | [diff] [blame] | 44 | static int in_pack, sideband; |
| 45 | |
| 46 | if (!trace_want(&trace_packet) && !trace_want(&trace_pack)) |
| 47 | return; |
| 48 | |
| 49 | if (in_pack) { |
| 50 | if (packet_trace_pack(buf, len, sideband)) |
| 51 | return; |
| 52 | } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) { |
| 53 | in_pack = 1; |
| 54 | sideband = *buf == '\1'; |
| 55 | packet_trace_pack(buf, len, sideband); |
| 56 | |
| 57 | /* |
| 58 | * Make a note in the human-readable trace that the pack data |
| 59 | * started. |
| 60 | */ |
| 61 | buf = "PACK ..."; |
| 62 | len = strlen(buf); |
| 63 | } |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 64 | |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 65 | if (!trace_want(&trace_packet)) |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 66 | return; |
| 67 | |
| 68 | /* +32 is just a guess for header + quoting */ |
| 69 | strbuf_init(&out, len+32); |
| 70 | |
| 71 | strbuf_addf(&out, "packet: %12s%c ", |
Jeff King | fd89433 | 2015-09-01 16:24:13 -0400 | [diff] [blame] | 72 | get_trace_prefix(), write ? '>' : '<'); |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 73 | |
Jeff King | 3235983 | 2015-06-16 13:23:20 -0400 | [diff] [blame] | 74 | /* XXX we should really handle printable utf8 */ |
| 75 | for (i = 0; i < len; i++) { |
| 76 | /* suppress newlines */ |
| 77 | if (buf[i] == '\n') |
| 78 | continue; |
| 79 | if (buf[i] >= 0x20 && buf[i] <= 0x7e) |
| 80 | strbuf_addch(&out, buf[i]); |
| 81 | else |
| 82 | strbuf_addf(&out, "\\%o", buf[i]); |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | strbuf_addch(&out, '\n'); |
Karsten Blees | 6aa3085 | 2014-07-12 02:00:06 +0200 | [diff] [blame] | 86 | trace_strbuf(&trace_packet, &out); |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 87 | strbuf_release(&out); |
| 88 | } |
| 89 | |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 90 | /* |
| 91 | * If we buffered things up above (we don't, but we should), |
| 92 | * we'd flush it here |
| 93 | */ |
| 94 | void packet_flush(int fd) |
| 95 | { |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 96 | packet_trace("0000", 4, 1); |
Jeff King | 37c8001 | 2019-03-04 23:11:39 -0500 | [diff] [blame] | 97 | if (write_in_full(fd, "0000", 4) < 0) |
| 98 | die_errno(_("unable to write flush packet")); |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Brandon Williams | a4cfd41 | 2018-03-14 11:31:40 -0700 | [diff] [blame] | 101 | void packet_delim(int fd) |
| 102 | { |
| 103 | packet_trace("0001", 4, 1); |
Jeff King | 37c8001 | 2019-03-04 23:11:39 -0500 | [diff] [blame] | 104 | if (write_in_full(fd, "0001", 4) < 0) |
| 105 | die_errno(_("unable to write delim packet")); |
Brandon Williams | a4cfd41 | 2018-03-14 11:31:40 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Denton Liu | 0181b60 | 2020-05-19 06:53:59 -0400 | [diff] [blame] | 108 | void packet_response_end(int fd) |
| 109 | { |
| 110 | packet_trace("0002", 4, 1); |
| 111 | if (write_in_full(fd, "0002", 4) < 0) |
Denton Liu | 8232a0f | 2021-07-08 19:27:22 -0700 | [diff] [blame] | 112 | die_errno(_("unable to write response end packet")); |
Denton Liu | 0181b60 | 2020-05-19 06:53:59 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Lars Schneider | 038ce90 | 2016-10-16 16:20:32 -0700 | [diff] [blame] | 115 | int packet_flush_gently(int fd) |
| 116 | { |
| 117 | packet_trace("0000", 4, 1); |
Jeff King | 4c95e3d | 2017-09-13 13:17:30 -0400 | [diff] [blame] | 118 | if (write_in_full(fd, "0000", 4) < 0) |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 119 | return error(_("flush packet write failed")); |
Jeff King | 4c95e3d | 2017-09-13 13:17:30 -0400 | [diff] [blame] | 120 | return 0; |
Lars Schneider | 038ce90 | 2016-10-16 16:20:32 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 123 | void packet_buf_flush(struct strbuf *buf) |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 124 | { |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 125 | packet_trace("0000", 4, 1); |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 126 | strbuf_add(buf, "0000", 4); |
| 127 | } |
| 128 | |
Brandon Williams | a4cfd41 | 2018-03-14 11:31:40 -0700 | [diff] [blame] | 129 | void packet_buf_delim(struct strbuf *buf) |
| 130 | { |
| 131 | packet_trace("0001", 4, 1); |
| 132 | strbuf_add(buf, "0001", 4); |
| 133 | } |
| 134 | |
Junio C Hamano | 56bea28 | 2019-05-15 10:42:35 +0900 | [diff] [blame] | 135 | void set_packet_header(char *buf, int size) |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 136 | { |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 137 | static char hexchar[] = "0123456789abcdef"; |
Lars Schneider | 2f60bdd | 2016-10-16 16:20:30 -0700 | [diff] [blame] | 138 | |
| 139 | #define hex(a) (hexchar[(a) & 15]) |
| 140 | buf[0] = hex(size >> 12); |
| 141 | buf[1] = hex(size >> 8); |
| 142 | buf[2] = hex(size >> 4); |
| 143 | buf[3] = hex(size); |
| 144 | #undef hex |
| 145 | } |
| 146 | |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 147 | static void format_packet(struct strbuf *out, const char *prefix, |
| 148 | const char *fmt, va_list args) |
Lars Schneider | 2f60bdd | 2016-10-16 16:20:30 -0700 | [diff] [blame] | 149 | { |
Jeff King | 8e9faf2 | 2014-12-10 04:47:02 -0500 | [diff] [blame] | 150 | size_t orig_len, n; |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 151 | |
Jeff King | 8e9faf2 | 2014-12-10 04:47:02 -0500 | [diff] [blame] | 152 | orig_len = out->len; |
| 153 | strbuf_addstr(out, "0000"); |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 154 | strbuf_addstr(out, prefix); |
Jeff King | 8e9faf2 | 2014-12-10 04:47:02 -0500 | [diff] [blame] | 155 | strbuf_vaddf(out, fmt, args); |
| 156 | n = out->len - orig_len; |
| 157 | |
| 158 | if (n > LARGE_PACKET_MAX) |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 159 | die(_("protocol error: impossibly long line")); |
Jeff King | 8e9faf2 | 2014-12-10 04:47:02 -0500 | [diff] [blame] | 160 | |
Lars Schneider | 2f60bdd | 2016-10-16 16:20:30 -0700 | [diff] [blame] | 161 | set_packet_header(&out->buf[orig_len], n); |
Jeff King | 8e9faf2 | 2014-12-10 04:47:02 -0500 | [diff] [blame] | 162 | packet_trace(out->buf + orig_len + 4, n - 4, 1); |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 165 | static int packet_write_fmt_1(int fd, int gently, const char *prefix, |
Lars Schneider | 70428d1 | 2016-10-16 16:20:31 -0700 | [diff] [blame] | 166 | const char *fmt, va_list args) |
| 167 | { |
Martin Ågren | 150efef | 2017-08-27 09:37:32 +0200 | [diff] [blame] | 168 | static struct strbuf buf = STRBUF_INIT; |
Lars Schneider | 70428d1 | 2016-10-16 16:20:31 -0700 | [diff] [blame] | 169 | |
Martin Ågren | 150efef | 2017-08-27 09:37:32 +0200 | [diff] [blame] | 170 | strbuf_reset(&buf); |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 171 | format_packet(&buf, prefix, fmt, args); |
Jeff King | 4c95e3d | 2017-09-13 13:17:30 -0400 | [diff] [blame] | 172 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
| 173 | if (!gently) { |
| 174 | check_pipe(errno); |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 175 | die_errno(_("packet write with format failed")); |
Jeff King | 4c95e3d | 2017-09-13 13:17:30 -0400 | [diff] [blame] | 176 | } |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 177 | return error(_("packet write with format failed")); |
Lars Schneider | 70428d1 | 2016-10-16 16:20:31 -0700 | [diff] [blame] | 178 | } |
Jeff King | 4c95e3d | 2017-09-13 13:17:30 -0400 | [diff] [blame] | 179 | |
| 180 | return 0; |
Lars Schneider | 70428d1 | 2016-10-16 16:20:31 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 183 | void packet_write_fmt(int fd, const char *fmt, ...) |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 184 | { |
| 185 | va_list args; |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 186 | |
| 187 | va_start(args, fmt); |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 188 | packet_write_fmt_1(fd, 0, "", fmt, args); |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 189 | va_end(args); |
Lars Schneider | 70428d1 | 2016-10-16 16:20:31 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int packet_write_fmt_gently(int fd, const char *fmt, ...) |
| 193 | { |
| 194 | int status; |
| 195 | va_list args; |
| 196 | |
| 197 | va_start(args, fmt); |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 198 | status = packet_write_fmt_1(fd, 1, "", fmt, args); |
Lars Schneider | 70428d1 | 2016-10-16 16:20:31 -0700 | [diff] [blame] | 199 | va_end(args); |
| 200 | return status; |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Matheus Tavares | 332ec96 | 2021-04-15 18:57:52 -0300 | [diff] [blame] | 203 | static int do_packet_write(const int fd_out, const char *buf, size_t size, |
| 204 | struct strbuf *err) |
Lars Schneider | edfb780 | 2016-10-16 16:20:33 -0700 | [diff] [blame] | 205 | { |
Jeff Hostetler | 7455e05 | 2021-03-15 21:08:18 +0000 | [diff] [blame] | 206 | char header[4]; |
Lars Schneider | edfb780 | 2016-10-16 16:20:33 -0700 | [diff] [blame] | 207 | size_t packet_size; |
| 208 | |
Matheus Tavares | 332ec96 | 2021-04-15 18:57:52 -0300 | [diff] [blame] | 209 | if (size > LARGE_PACKET_DATA_MAX) { |
| 210 | strbuf_addstr(err, _("packet write failed - data exceeds max packet size")); |
| 211 | return -1; |
| 212 | } |
Lars Schneider | edfb780 | 2016-10-16 16:20:33 -0700 | [diff] [blame] | 213 | |
| 214 | packet_trace(buf, size, 1); |
| 215 | packet_size = size + 4; |
Jeff Hostetler | 7455e05 | 2021-03-15 21:08:18 +0000 | [diff] [blame] | 216 | |
| 217 | set_packet_header(header, packet_size); |
| 218 | |
| 219 | /* |
| 220 | * Write the header and the buffer in 2 parts so that we do |
| 221 | * not need to allocate a buffer or rely on a static buffer. |
| 222 | * This also avoids putting a large buffer on the stack which |
| 223 | * might have multi-threading issues. |
| 224 | */ |
| 225 | |
| 226 | if (write_in_full(fd_out, header, 4) < 0 || |
Matheus Tavares | 332ec96 | 2021-04-15 18:57:52 -0300 | [diff] [blame] | 227 | write_in_full(fd_out, buf, size) < 0) { |
| 228 | strbuf_addf(err, _("packet write failed: %s"), strerror(errno)); |
| 229 | return -1; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int packet_write_gently(const int fd_out, const char *buf, size_t size) |
| 235 | { |
| 236 | struct strbuf err = STRBUF_INIT; |
| 237 | if (do_packet_write(fd_out, buf, size, &err)) { |
| 238 | error("%s", err.buf); |
| 239 | strbuf_release(&err); |
| 240 | return -1; |
| 241 | } |
Jeff King | 4c95e3d | 2017-09-13 13:17:30 -0400 | [diff] [blame] | 242 | return 0; |
Lars Schneider | edfb780 | 2016-10-16 16:20:33 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Brandon Williams | 5d2124b | 2017-10-16 10:55:23 -0700 | [diff] [blame] | 245 | void packet_write(int fd_out, const char *buf, size_t size) |
| 246 | { |
Matheus Tavares | 332ec96 | 2021-04-15 18:57:52 -0300 | [diff] [blame] | 247 | struct strbuf err = STRBUF_INIT; |
| 248 | if (do_packet_write(fd_out, buf, size, &err)) |
| 249 | die("%s", err.buf); |
Brandon Williams | 5d2124b | 2017-10-16 10:55:23 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Jacob Vosmaer | 9632839 | 2021-09-01 14:54:41 +0200 | [diff] [blame] | 252 | void packet_fwrite(FILE *f, const char *buf, size_t size) |
| 253 | { |
| 254 | size_t packet_size; |
| 255 | char header[4]; |
| 256 | |
| 257 | if (size > LARGE_PACKET_DATA_MAX) |
| 258 | die(_("packet write failed - data exceeds max packet size")); |
| 259 | |
| 260 | packet_trace(buf, size, 1); |
| 261 | packet_size = size + 4; |
| 262 | |
| 263 | set_packet_header(header, packet_size); |
| 264 | fwrite_or_die(f, header, 4); |
| 265 | fwrite_or_die(f, buf, size); |
| 266 | } |
| 267 | |
| 268 | void packet_fwrite_fmt(FILE *fh, const char *fmt, ...) |
| 269 | { |
| 270 | static struct strbuf buf = STRBUF_INIT; |
| 271 | va_list args; |
| 272 | |
| 273 | strbuf_reset(&buf); |
| 274 | |
| 275 | va_start(args, fmt); |
| 276 | format_packet(&buf, "", fmt, args); |
| 277 | va_end(args); |
| 278 | |
| 279 | fwrite_or_die(fh, buf.buf, buf.len); |
| 280 | } |
| 281 | |
| 282 | void packet_fflush(FILE *f) |
| 283 | { |
| 284 | packet_trace("0000", 4, 1); |
| 285 | fwrite_or_die(f, "0000", 4); |
| 286 | fflush_or_die(f); |
| 287 | } |
| 288 | |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 289 | void packet_buf_write(struct strbuf *buf, const char *fmt, ...) |
| 290 | { |
| 291 | va_list args; |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 292 | |
| 293 | va_start(args, fmt); |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 294 | format_packet(buf, "", fmt, args); |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 295 | va_end(args); |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Johannes Schindelin | 3a63c6a | 2021-03-15 21:08:19 +0000 | [diff] [blame] | 298 | int write_packetized_from_fd_no_flush(int fd_in, int fd_out) |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 299 | { |
Jeff Hostetler | 7455e05 | 2021-03-15 21:08:18 +0000 | [diff] [blame] | 300 | char *buf = xmalloc(LARGE_PACKET_DATA_MAX); |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 301 | int err = 0; |
| 302 | ssize_t bytes_to_write; |
| 303 | |
| 304 | while (!err) { |
Jeff Hostetler | 7455e05 | 2021-03-15 21:08:18 +0000 | [diff] [blame] | 305 | bytes_to_write = xread(fd_in, buf, LARGE_PACKET_DATA_MAX); |
| 306 | if (bytes_to_write < 0) { |
| 307 | free(buf); |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 308 | return COPY_READ_ERROR; |
Jeff Hostetler | 7455e05 | 2021-03-15 21:08:18 +0000 | [diff] [blame] | 309 | } |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 310 | if (bytes_to_write == 0) |
| 311 | break; |
| 312 | err = packet_write_gently(fd_out, buf, bytes_to_write); |
| 313 | } |
Jeff Hostetler | 7455e05 | 2021-03-15 21:08:18 +0000 | [diff] [blame] | 314 | free(buf); |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 315 | return err; |
| 316 | } |
| 317 | |
Matheus Tavares | 52917a9 | 2022-08-14 22:06:37 -0300 | [diff] [blame] | 318 | int write_packetized_from_buf_no_flush_count(const char *src_in, size_t len, |
| 319 | int fd_out, int *packet_counter) |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 320 | { |
| 321 | int err = 0; |
| 322 | size_t bytes_written = 0; |
| 323 | size_t bytes_to_write; |
| 324 | |
| 325 | while (!err) { |
| 326 | if ((len - bytes_written) > LARGE_PACKET_DATA_MAX) |
| 327 | bytes_to_write = LARGE_PACKET_DATA_MAX; |
| 328 | else |
| 329 | bytes_to_write = len - bytes_written; |
| 330 | if (bytes_to_write == 0) |
| 331 | break; |
| 332 | err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write); |
| 333 | bytes_written += bytes_to_write; |
Matheus Tavares | 52917a9 | 2022-08-14 22:06:37 -0300 | [diff] [blame] | 334 | if (packet_counter) |
| 335 | (*packet_counter)++; |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 336 | } |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 337 | return err; |
| 338 | } |
| 339 | |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 340 | static int get_packet_data(int fd, char **src_buf, size_t *src_size, |
| 341 | void *dst, unsigned size, int options) |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 342 | { |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 343 | ssize_t ret; |
| 344 | |
| 345 | if (fd >= 0 && src_buf && *src_buf) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 346 | BUG("multiple sources given to packet_read"); |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 347 | |
| 348 | /* Read up to "size" bytes from our source, whatever it is. */ |
| 349 | if (src_buf && *src_buf) { |
| 350 | ret = size < *src_size ? size : *src_size; |
| 351 | memcpy(dst, *src_buf, ret); |
| 352 | *src_buf += ret; |
| 353 | *src_size -= ret; |
| 354 | } else { |
| 355 | ret = read_in_full(fd, dst, size); |
Johannes Schindelin | c4ba579 | 2021-03-15 21:08:20 +0000 | [diff] [blame] | 356 | if (ret < 0) { |
| 357 | if (options & PACKET_READ_GENTLE_ON_READ_ERROR) |
| 358 | return error_errno(_("read error")); |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 359 | die_errno(_("read error")); |
Johannes Schindelin | c4ba579 | 2021-03-15 21:08:20 +0000 | [diff] [blame] | 360 | } |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* And complain if we didn't get enough bytes to satisfy the read. */ |
Jeff King | 61d3633 | 2017-09-27 02:00:28 -0400 | [diff] [blame] | 364 | if (ret != size) { |
Jeff King | 0380942 | 2013-02-20 15:02:10 -0500 | [diff] [blame] | 365 | if (options & PACKET_READ_GENTLE_ON_EOF) |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 366 | return -1; |
| 367 | |
Johannes Schindelin | c4ba579 | 2021-03-15 21:08:20 +0000 | [diff] [blame] | 368 | if (options & PACKET_READ_GENTLE_ON_READ_ERROR) |
| 369 | return error(_("the remote end hung up unexpectedly")); |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 370 | die(_("the remote end hung up unexpectedly")); |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | return ret; |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 374 | } |
| 375 | |
René Scharfe | 3e81b89 | 2023-07-07 23:47:55 +0200 | [diff] [blame] | 376 | int packet_length(const char lenbuf_hex[4], size_t size) |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 377 | { |
René Scharfe | 3e81b89 | 2023-07-07 23:47:55 +0200 | [diff] [blame] | 378 | if (size < 4) |
| 379 | BUG("buffer too small"); |
| 380 | return hexval(lenbuf_hex[0]) << 12 | |
| 381 | hexval(lenbuf_hex[1]) << 8 | |
| 382 | hexval(lenbuf_hex[2]) << 4 | |
| 383 | hexval(lenbuf_hex[3]); |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Ivan Frade | 88e9b1e | 2021-11-10 23:51:28 +0000 | [diff] [blame] | 386 | static char *find_packfile_uri_path(const char *buffer) |
| 387 | { |
| 388 | const char *URI_MARK = "://"; |
| 389 | char *path; |
| 390 | int len; |
| 391 | |
| 392 | /* First char is sideband mark */ |
| 393 | buffer += 1; |
| 394 | |
| 395 | len = strspn(buffer, "0123456789abcdefABCDEF"); |
| 396 | /* size of SHA1 and SHA256 hash */ |
| 397 | if (!(len == 40 || len == 64) || buffer[len] != ' ') |
| 398 | return NULL; /* required "<hash>SP" not seen */ |
| 399 | |
| 400 | path = strstr(buffer + len + 1, URI_MARK); |
| 401 | if (!path) |
| 402 | return NULL; |
| 403 | |
| 404 | path = strchr(path + strlen(URI_MARK), '/'); |
| 405 | if (!path || !*(path + 1)) |
| 406 | return NULL; |
| 407 | |
| 408 | /* position after '/' */ |
| 409 | return ++path; |
| 410 | } |
| 411 | |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 412 | enum packet_read_status packet_read_with_status(int fd, char **src_buffer, |
| 413 | size_t *src_len, char *buffer, |
| 414 | unsigned size, int *pktlen, |
| 415 | int options) |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 416 | { |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 417 | int len; |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 418 | char linelen[4]; |
Ivan Frade | 88e9b1e | 2021-11-10 23:51:28 +0000 | [diff] [blame] | 419 | char *uri_path_start; |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 420 | |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 421 | if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) { |
| 422 | *pktlen = -1; |
| 423 | return PACKET_READ_EOF; |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 424 | } |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 425 | |
René Scharfe | 3e81b89 | 2023-07-07 23:47:55 +0200 | [diff] [blame] | 426 | len = packet_length(linelen, sizeof(linelen)); |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 427 | |
| 428 | if (len < 0) { |
Johannes Schindelin | c4ba579 | 2021-03-15 21:08:20 +0000 | [diff] [blame] | 429 | if (options & PACKET_READ_GENTLE_ON_READ_ERROR) |
| 430 | return error(_("protocol error: bad line length " |
| 431 | "character: %.4s"), linelen); |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 432 | die(_("protocol error: bad line length character: %.4s"), linelen); |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 433 | } else if (!len) { |
| 434 | packet_trace("0000", 4, 0); |
| 435 | *pktlen = 0; |
| 436 | return PACKET_READ_FLUSH; |
Brandon Williams | a4cfd41 | 2018-03-14 11:31:40 -0700 | [diff] [blame] | 437 | } else if (len == 1) { |
| 438 | packet_trace("0001", 4, 0); |
| 439 | *pktlen = 0; |
| 440 | return PACKET_READ_DELIM; |
Denton Liu | 0181b60 | 2020-05-19 06:53:59 -0400 | [diff] [blame] | 441 | } else if (len == 2) { |
| 442 | packet_trace("0002", 4, 0); |
| 443 | *pktlen = 0; |
| 444 | return PACKET_READ_RESPONSE_END; |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 445 | } else if (len < 4) { |
Johannes Schindelin | c4ba579 | 2021-03-15 21:08:20 +0000 | [diff] [blame] | 446 | if (options & PACKET_READ_GENTLE_ON_READ_ERROR) |
| 447 | return error(_("protocol error: bad line length %d"), |
| 448 | len); |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 449 | die(_("protocol error: bad line length %d"), len); |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | len -= 4; |
Johannes Schindelin | c4ba579 | 2021-03-15 21:08:20 +0000 | [diff] [blame] | 453 | if ((unsigned)len >= size) { |
| 454 | if (options & PACKET_READ_GENTLE_ON_READ_ERROR) |
| 455 | return error(_("protocol error: bad line length %d"), |
| 456 | len); |
Nguyễn Thái Ngọc Duy | c60d769 | 2018-07-21 09:49:34 +0200 | [diff] [blame] | 457 | die(_("protocol error: bad line length %d"), len); |
Johannes Schindelin | c4ba579 | 2021-03-15 21:08:20 +0000 | [diff] [blame] | 458 | } |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 459 | |
| 460 | if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) { |
| 461 | *pktlen = -1; |
| 462 | return PACKET_READ_EOF; |
| 463 | } |
Jeff King | 819b929 | 2013-02-20 15:02:28 -0500 | [diff] [blame] | 464 | |
| 465 | if ((options & PACKET_READ_CHOMP_NEWLINE) && |
Jiang Xin | 7033d54 | 2023-12-17 22:41:38 +0800 | [diff] [blame] | 466 | len && buffer[len-1] == '\n') { |
| 467 | if (options & PACKET_READ_USE_SIDEBAND) { |
| 468 | int band = *buffer & 0xff; |
| 469 | switch (band) { |
| 470 | case 1: |
| 471 | /* Chomp newline for payload */ |
| 472 | len--; |
| 473 | break; |
| 474 | case 2: |
| 475 | case 3: |
| 476 | /* |
| 477 | * Do not chomp newline for progress and error |
| 478 | * message. |
| 479 | */ |
| 480 | break; |
| 481 | default: |
| 482 | /* |
| 483 | * Bad sideband, let's leave it to |
| 484 | * demultiplex_sideband() to catch this error. |
| 485 | */ |
| 486 | break; |
| 487 | } |
| 488 | } else { |
| 489 | len--; |
| 490 | } |
| 491 | } |
Jeff King | 819b929 | 2013-02-20 15:02:28 -0500 | [diff] [blame] | 492 | |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 493 | buffer[len] = 0; |
Ivan Frade | 88e9b1e | 2021-11-10 23:51:28 +0000 | [diff] [blame] | 494 | if (options & PACKET_READ_REDACT_URI_PATH && |
| 495 | (uri_path_start = find_packfile_uri_path(buffer))) { |
| 496 | const char *redacted = "<redacted>"; |
| 497 | struct strbuf tracebuf = STRBUF_INIT; |
| 498 | strbuf_insert(&tracebuf, 0, buffer, len); |
| 499 | strbuf_splice(&tracebuf, uri_path_start - buffer, |
| 500 | strlen(uri_path_start), redacted, strlen(redacted)); |
| 501 | packet_trace(tracebuf.buf, tracebuf.len, 0); |
| 502 | strbuf_release(&tracebuf); |
| 503 | } else { |
| 504 | packet_trace(buffer, len, 0); |
| 505 | } |
Jeff King | 533ddba | 2019-04-13 01:54:02 -0400 | [diff] [blame] | 506 | |
| 507 | if ((options & PACKET_READ_DIE_ON_ERR_PACKET) && |
| 508 | starts_with(buffer, "ERR ")) |
| 509 | die(_("remote error: %s"), buffer + 4); |
| 510 | |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 511 | *pktlen = len; |
| 512 | return PACKET_READ_NORMAL; |
| 513 | } |
| 514 | |
Ævar Arnfjörð Bjarmason | ec9a37d | 2021-10-14 22:15:12 +0200 | [diff] [blame] | 515 | int packet_read(int fd, char *buffer, unsigned size, int options) |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 516 | { |
| 517 | int pktlen = -1; |
| 518 | |
Ævar Arnfjörð Bjarmason | ec9a37d | 2021-10-14 22:15:12 +0200 | [diff] [blame] | 519 | packet_read_with_status(fd, NULL, NULL, buffer, size, &pktlen, |
| 520 | options); |
Brandon Williams | 2153d47 | 2018-03-14 11:31:38 -0700 | [diff] [blame] | 521 | |
| 522 | return pktlen; |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 523 | } |
Shawn O. Pearce | f5615d2 | 2009-10-30 17:47:21 -0700 | [diff] [blame] | 524 | |
Ævar Arnfjörð Bjarmason | ec9a37d | 2021-10-14 22:15:12 +0200 | [diff] [blame] | 525 | char *packet_read_line(int fd, int *dst_len) |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 526 | { |
Ævar Arnfjörð Bjarmason | ec9a37d | 2021-10-14 22:15:12 +0200 | [diff] [blame] | 527 | int len = packet_read(fd, packet_buffer, sizeof(packet_buffer), |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 528 | PACKET_READ_CHOMP_NEWLINE); |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 529 | if (dst_len) |
| 530 | *dst_len = len; |
Ben Peart | 974b50c | 2017-05-05 11:27:54 -0400 | [diff] [blame] | 531 | return (len > 0) ? packet_buffer : NULL; |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Ben Peart | 825b922 | 2017-05-05 11:27:55 -0400 | [diff] [blame] | 534 | int packet_read_line_gently(int fd, int *dst_len, char **dst_line) |
| 535 | { |
Ævar Arnfjörð Bjarmason | ec9a37d | 2021-10-14 22:15:12 +0200 | [diff] [blame] | 536 | int len = packet_read(fd, packet_buffer, sizeof(packet_buffer), |
Ben Peart | 825b922 | 2017-05-05 11:27:55 -0400 | [diff] [blame] | 537 | PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF); |
| 538 | if (dst_len) |
| 539 | *dst_len = len; |
| 540 | if (dst_line) |
| 541 | *dst_line = (len > 0) ? packet_buffer : NULL; |
| 542 | return len; |
| 543 | } |
| 544 | |
Johannes Schindelin | 8c2efa5 | 2021-03-15 21:08:21 +0000 | [diff] [blame] | 545 | ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options) |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 546 | { |
| 547 | int packet_len; |
| 548 | |
| 549 | size_t orig_len = sb_out->len; |
| 550 | size_t orig_alloc = sb_out->alloc; |
| 551 | |
| 552 | for (;;) { |
| 553 | strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX); |
Ævar Arnfjörð Bjarmason | ec9a37d | 2021-10-14 22:15:12 +0200 | [diff] [blame] | 554 | packet_len = packet_read(fd_in, |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 555 | /* strbuf_grow() above always allocates one extra byte to |
| 556 | * store a '\0' at the end of the string. packet_read() |
| 557 | * writes a '\0' extra byte at the end, too. Let it know |
| 558 | * that there is already room for the extra byte. |
| 559 | */ |
| 560 | sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1, |
Johannes Schindelin | 8c2efa5 | 2021-03-15 21:08:21 +0000 | [diff] [blame] | 561 | options); |
Lars Schneider | bb643d8 | 2016-10-16 16:20:34 -0700 | [diff] [blame] | 562 | if (packet_len <= 0) |
| 563 | break; |
| 564 | sb_out->len += packet_len; |
| 565 | } |
| 566 | |
| 567 | if (packet_len < 0) { |
| 568 | if (orig_alloc == 0) |
| 569 | strbuf_release(sb_out); |
| 570 | else |
| 571 | strbuf_setlen(sb_out, orig_len); |
| 572 | return packet_len; |
| 573 | } |
| 574 | return sb_out->len - orig_len; |
| 575 | } |
Brandon Williams | 77dabc1 | 2018-03-14 11:31:39 -0700 | [diff] [blame] | 576 | |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 577 | int recv_sideband(const char *me, int in_stream, int out) |
| 578 | { |
| 579 | char buf[LARGE_PACKET_MAX + 1]; |
| 580 | int len; |
| 581 | struct strbuf scratch = STRBUF_INIT; |
| 582 | enum sideband_type sideband_type; |
| 583 | |
| 584 | while (1) { |
Jeff King | af22a63 | 2020-10-28 05:33:24 -0400 | [diff] [blame] | 585 | int status = packet_read_with_status(in_stream, NULL, NULL, |
| 586 | buf, LARGE_PACKET_MAX, |
| 587 | &len, |
| 588 | PACKET_READ_GENTLE_ON_EOF); |
| 589 | if (!demultiplex_sideband(me, status, buf, len, 0, &scratch, |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 590 | &sideband_type)) |
| 591 | continue; |
| 592 | switch (sideband_type) { |
| 593 | case SIDEBAND_PRIMARY: |
| 594 | write_or_die(out, buf + 1, len - 1); |
| 595 | break; |
| 596 | default: /* errors: message already written */ |
Johannes Schindelin | 8e86cf6 | 2020-10-19 19:35:41 +0000 | [diff] [blame] | 597 | if (scratch.len > 0) |
| 598 | BUG("unhandled incomplete sideband: '%s'", |
| 599 | scratch.buf); |
Jonathan Tan | fbd76cd | 2019-01-16 11:28:13 -0800 | [diff] [blame] | 600 | return sideband_type; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
Brandon Williams | 77dabc1 | 2018-03-14 11:31:39 -0700 | [diff] [blame] | 605 | /* Packet Reader Functions */ |
| 606 | void packet_reader_init(struct packet_reader *reader, int fd, |
| 607 | char *src_buffer, size_t src_len, |
| 608 | int options) |
| 609 | { |
| 610 | memset(reader, 0, sizeof(*reader)); |
| 611 | |
| 612 | reader->fd = fd; |
| 613 | reader->src_buffer = src_buffer; |
| 614 | reader->src_len = src_len; |
| 615 | reader->buffer = packet_buffer; |
| 616 | reader->buffer_size = sizeof(packet_buffer); |
| 617 | reader->options = options; |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 618 | reader->me = "git"; |
brian m. carlson | 9a9f0d3 | 2020-05-25 19:58:54 +0000 | [diff] [blame] | 619 | reader->hash_algo = &hash_algos[GIT_HASH_SHA1]; |
Jiang Xin | 64220dc | 2023-12-17 22:41:37 +0800 | [diff] [blame] | 620 | strbuf_init(&reader->scratch, 0); |
Brandon Williams | 77dabc1 | 2018-03-14 11:31:39 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | enum packet_read_status packet_reader_read(struct packet_reader *reader) |
| 624 | { |
| 625 | if (reader->line_peeked) { |
| 626 | reader->line_peeked = 0; |
| 627 | return reader->status; |
| 628 | } |
| 629 | |
Jiang Xin | 7033d54 | 2023-12-17 22:41:38 +0800 | [diff] [blame] | 630 | if (reader->use_sideband) |
| 631 | reader->options |= PACKET_READ_USE_SIDEBAND; |
| 632 | |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 633 | /* |
| 634 | * Consume all progress packets until a primary payload packet is |
| 635 | * received |
| 636 | */ |
| 637 | while (1) { |
| 638 | enum sideband_type sideband_type; |
| 639 | reader->status = packet_read_with_status(reader->fd, |
| 640 | &reader->src_buffer, |
| 641 | &reader->src_len, |
| 642 | reader->buffer, |
| 643 | reader->buffer_size, |
| 644 | &reader->pktlen, |
| 645 | reader->options); |
| 646 | if (!reader->use_sideband) |
| 647 | break; |
Jeff King | af22a63 | 2020-10-28 05:33:24 -0400 | [diff] [blame] | 648 | if (demultiplex_sideband(reader->me, reader->status, |
| 649 | reader->buffer, reader->pktlen, 1, |
Jiang Xin | 64220dc | 2023-12-17 22:41:37 +0800 | [diff] [blame] | 650 | &reader->scratch, &sideband_type)) |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 651 | break; |
| 652 | } |
Brandon Williams | 77dabc1 | 2018-03-14 11:31:39 -0700 | [diff] [blame] | 653 | |
| 654 | if (reader->status == PACKET_READ_NORMAL) |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 655 | /* Skip the sideband designator if sideband is used */ |
| 656 | reader->line = reader->use_sideband ? |
| 657 | reader->buffer + 1 : reader->buffer; |
Brandon Williams | 77dabc1 | 2018-03-14 11:31:39 -0700 | [diff] [blame] | 658 | else |
| 659 | reader->line = NULL; |
| 660 | |
| 661 | return reader->status; |
| 662 | } |
| 663 | |
| 664 | enum packet_read_status packet_reader_peek(struct packet_reader *reader) |
| 665 | { |
| 666 | /* Only allow peeking a single line */ |
| 667 | if (reader->line_peeked) |
| 668 | return reader->status; |
| 669 | |
| 670 | /* Peek a line by reading it and setting peeked flag */ |
| 671 | packet_reader_read(reader); |
| 672 | reader->line_peeked = 1; |
| 673 | return reader->status; |
| 674 | } |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 675 | |
| 676 | void packet_writer_init(struct packet_writer *writer, int dest_fd) |
| 677 | { |
| 678 | writer->dest_fd = dest_fd; |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 679 | writer->use_sideband = 0; |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | void packet_writer_write(struct packet_writer *writer, const char *fmt, ...) |
| 683 | { |
| 684 | va_list args; |
| 685 | |
| 686 | va_start(args, fmt); |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 687 | packet_write_fmt_1(writer->dest_fd, 0, |
| 688 | writer->use_sideband ? "\001" : "", fmt, args); |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 689 | va_end(args); |
| 690 | } |
| 691 | |
| 692 | void packet_writer_error(struct packet_writer *writer, const char *fmt, ...) |
| 693 | { |
| 694 | va_list args; |
| 695 | |
| 696 | va_start(args, fmt); |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 697 | packet_write_fmt_1(writer->dest_fd, 0, |
| 698 | writer->use_sideband ? "\003" : "ERR ", fmt, args); |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 699 | va_end(args); |
| 700 | } |
| 701 | |
| 702 | void packet_writer_delim(struct packet_writer *writer) |
| 703 | { |
| 704 | packet_delim(writer->dest_fd); |
| 705 | } |
| 706 | |
| 707 | void packet_writer_flush(struct packet_writer *writer) |
| 708 | { |
| 709 | packet_flush(writer->dest_fd); |
| 710 | } |