pkt-line.con commit ls-remote: pass ref prefixes when requesting a remote's refs (b4be741)
   1#include "cache.h"
   2#include "pkt-line.h"
   3#include "run-command.h"
   4
   5char packet_buffer[LARGE_PACKET_MAX];
   6static const char *packet_trace_prefix = "git";
   7static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
   8static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
   9
  10void packet_trace_identity(const char *prog)
  11{
  12        packet_trace_prefix = xstrdup(prog);
  13}
  14
  15static const char *get_trace_prefix(void)
  16{
  17        return in_async() ? "sideband" : packet_trace_prefix;
  18}
  19
  20static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
  21{
  22        if (!sideband) {
  23                trace_verbatim(&trace_pack, buf, len);
  24                return 1;
  25        } else if (len && *buf == '\1') {
  26                trace_verbatim(&trace_pack, buf + 1, len - 1);
  27                return 1;
  28        } else {
  29                /* it's another non-pack sideband */
  30                return 0;
  31        }
  32}
  33
  34static void packet_trace(const char *buf, unsigned int len, int write)
  35{
  36        int i;
  37        struct strbuf out;
  38        static int in_pack, sideband;
  39
  40        if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
  41                return;
  42
  43        if (in_pack) {
  44                if (packet_trace_pack(buf, len, sideband))
  45                        return;
  46        } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
  47                in_pack = 1;
  48                sideband = *buf == '\1';
  49                packet_trace_pack(buf, len, sideband);
  50
  51                /*
  52                 * Make a note in the human-readable trace that the pack data
  53                 * started.
  54                 */
  55                buf = "PACK ...";
  56                len = strlen(buf);
  57        }
  58
  59        if (!trace_want(&trace_packet))
  60                return;
  61
  62        /* +32 is just a guess for header + quoting */
  63        strbuf_init(&out, len+32);
  64
  65        strbuf_addf(&out, "packet: %12s%c ",
  66                    get_trace_prefix(), write ? '>' : '<');
  67
  68        /* XXX we should really handle printable utf8 */
  69        for (i = 0; i < len; i++) {
  70                /* suppress newlines */
  71                if (buf[i] == '\n')
  72                        continue;
  73                if (buf[i] >= 0x20 && buf[i] <= 0x7e)
  74                        strbuf_addch(&out, buf[i]);
  75                else
  76                        strbuf_addf(&out, "\\%o", buf[i]);
  77        }
  78
  79        strbuf_addch(&out, '\n');
  80        trace_strbuf(&trace_packet, &out);
  81        strbuf_release(&out);
  82}
  83
  84/*
  85 * If we buffered things up above (we don't, but we should),
  86 * we'd flush it here
  87 */
  88void packet_flush(int fd)
  89{
  90        packet_trace("0000", 4, 1);
  91        write_or_die(fd, "0000", 4);
  92}
  93
  94void packet_delim(int fd)
  95{
  96        packet_trace("0001", 4, 1);
  97        write_or_die(fd, "0001", 4);
  98}
  99
 100int packet_flush_gently(int fd)
 101{
 102        packet_trace("0000", 4, 1);
 103        if (write_in_full(fd, "0000", 4) < 0)
 104                return error("flush packet write failed");
 105        return 0;
 106}
 107
 108void packet_buf_flush(struct strbuf *buf)
 109{
 110        packet_trace("0000", 4, 1);
 111        strbuf_add(buf, "0000", 4);
 112}
 113
 114void packet_buf_delim(struct strbuf *buf)
 115{
 116        packet_trace("0001", 4, 1);
 117        strbuf_add(buf, "0001", 4);
 118}
 119
 120static void set_packet_header(char *buf, const int size)
 121{
 122        static char hexchar[] = "0123456789abcdef";
 123
 124        #define hex(a) (hexchar[(a) & 15])
 125        buf[0] = hex(size >> 12);
 126        buf[1] = hex(size >> 8);
 127        buf[2] = hex(size >> 4);
 128        buf[3] = hex(size);
 129        #undef hex
 130}
 131
 132static void format_packet(struct strbuf *out, const char *fmt, va_list args)
 133{
 134        size_t orig_len, n;
 135
 136        orig_len = out->len;
 137        strbuf_addstr(out, "0000");
 138        strbuf_vaddf(out, fmt, args);
 139        n = out->len - orig_len;
 140
 141        if (n > LARGE_PACKET_MAX)
 142                die("protocol error: impossibly long line");
 143
 144        set_packet_header(&out->buf[orig_len], n);
 145        packet_trace(out->buf + orig_len + 4, n - 4, 1);
 146}
 147
 148static int packet_write_fmt_1(int fd, int gently,
 149                              const char *fmt, va_list args)
 150{
 151        static struct strbuf buf = STRBUF_INIT;
 152
 153        strbuf_reset(&buf);
 154        format_packet(&buf, fmt, args);
 155        if (write_in_full(fd, buf.buf, buf.len) < 0) {
 156                if (!gently) {
 157                        check_pipe(errno);
 158                        die_errno("packet write with format failed");
 159                }
 160                return error("packet write with format failed");
 161        }
 162
 163        return 0;
 164}
 165
 166void packet_write_fmt(int fd, const char *fmt, ...)
 167{
 168        va_list args;
 169
 170        va_start(args, fmt);
 171        packet_write_fmt_1(fd, 0, fmt, args);
 172        va_end(args);
 173}
 174
 175int packet_write_fmt_gently(int fd, const char *fmt, ...)
 176{
 177        int status;
 178        va_list args;
 179
 180        va_start(args, fmt);
 181        status = packet_write_fmt_1(fd, 1, fmt, args);
 182        va_end(args);
 183        return status;
 184}
 185
 186static int packet_write_gently(const int fd_out, const char *buf, size_t size)
 187{
 188        static char packet_write_buffer[LARGE_PACKET_MAX];
 189        size_t packet_size;
 190
 191        if (size > sizeof(packet_write_buffer) - 4)
 192                return error("packet write failed - data exceeds max packet size");
 193
 194        packet_trace(buf, size, 1);
 195        packet_size = size + 4;
 196        set_packet_header(packet_write_buffer, packet_size);
 197        memcpy(packet_write_buffer + 4, buf, size);
 198        if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
 199                return error("packet write failed");
 200        return 0;
 201}
 202
 203void packet_write(int fd_out, const char *buf, size_t size)
 204{
 205        if (packet_write_gently(fd_out, buf, size))
 206                die_errno("packet write failed");
 207}
 208
 209void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
 210{
 211        va_list args;
 212
 213        va_start(args, fmt);
 214        format_packet(buf, fmt, args);
 215        va_end(args);
 216}
 217
 218int write_packetized_from_fd(int fd_in, int fd_out)
 219{
 220        static char buf[LARGE_PACKET_DATA_MAX];
 221        int err = 0;
 222        ssize_t bytes_to_write;
 223
 224        while (!err) {
 225                bytes_to_write = xread(fd_in, buf, sizeof(buf));
 226                if (bytes_to_write < 0)
 227                        return COPY_READ_ERROR;
 228                if (bytes_to_write == 0)
 229                        break;
 230                err = packet_write_gently(fd_out, buf, bytes_to_write);
 231        }
 232        if (!err)
 233                err = packet_flush_gently(fd_out);
 234        return err;
 235}
 236
 237int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
 238{
 239        int err = 0;
 240        size_t bytes_written = 0;
 241        size_t bytes_to_write;
 242
 243        while (!err) {
 244                if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
 245                        bytes_to_write = LARGE_PACKET_DATA_MAX;
 246                else
 247                        bytes_to_write = len - bytes_written;
 248                if (bytes_to_write == 0)
 249                        break;
 250                err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
 251                bytes_written += bytes_to_write;
 252        }
 253        if (!err)
 254                err = packet_flush_gently(fd_out);
 255        return err;
 256}
 257
 258static int get_packet_data(int fd, char **src_buf, size_t *src_size,
 259                           void *dst, unsigned size, int options)
 260{
 261        ssize_t ret;
 262
 263        if (fd >= 0 && src_buf && *src_buf)
 264                die("BUG: multiple sources given to packet_read");
 265
 266        /* Read up to "size" bytes from our source, whatever it is. */
 267        if (src_buf && *src_buf) {
 268                ret = size < *src_size ? size : *src_size;
 269                memcpy(dst, *src_buf, ret);
 270                *src_buf += ret;
 271                *src_size -= ret;
 272        } else {
 273                ret = read_in_full(fd, dst, size);
 274                if (ret < 0)
 275                        die_errno("read error");
 276        }
 277
 278        /* And complain if we didn't get enough bytes to satisfy the read. */
 279        if (ret != size) {
 280                if (options & PACKET_READ_GENTLE_ON_EOF)
 281                        return -1;
 282
 283                die("The remote end hung up unexpectedly");
 284        }
 285
 286        return ret;
 287}
 288
 289static int packet_length(const char *linelen)
 290{
 291        int val = hex2chr(linelen);
 292        return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
 293}
 294
 295enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
 296                                                size_t *src_len, char *buffer,
 297                                                unsigned size, int *pktlen,
 298                                                int options)
 299{
 300        int len;
 301        char linelen[4];
 302
 303        if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
 304                *pktlen = -1;
 305                return PACKET_READ_EOF;
 306        }
 307
 308        len = packet_length(linelen);
 309
 310        if (len < 0) {
 311                die("protocol error: bad line length character: %.4s", linelen);
 312        } else if (!len) {
 313                packet_trace("0000", 4, 0);
 314                *pktlen = 0;
 315                return PACKET_READ_FLUSH;
 316        } else if (len == 1) {
 317                packet_trace("0001", 4, 0);
 318                *pktlen = 0;
 319                return PACKET_READ_DELIM;
 320        } else if (len < 4) {
 321                die("protocol error: bad line length %d", len);
 322        }
 323
 324        len -= 4;
 325        if ((unsigned)len >= size)
 326                die("protocol error: bad line length %d", len);
 327
 328        if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
 329                *pktlen = -1;
 330                return PACKET_READ_EOF;
 331        }
 332
 333        if ((options & PACKET_READ_CHOMP_NEWLINE) &&
 334            len && buffer[len-1] == '\n')
 335                len--;
 336
 337        buffer[len] = 0;
 338        packet_trace(buffer, len, 0);
 339        *pktlen = len;
 340        return PACKET_READ_NORMAL;
 341}
 342
 343int packet_read(int fd, char **src_buffer, size_t *src_len,
 344                char *buffer, unsigned size, int options)
 345{
 346        int pktlen = -1;
 347
 348        packet_read_with_status(fd, src_buffer, src_len, buffer, size,
 349                                &pktlen, options);
 350
 351        return pktlen;
 352}
 353
 354static char *packet_read_line_generic(int fd,
 355                                      char **src, size_t *src_len,
 356                                      int *dst_len)
 357{
 358        int len = packet_read(fd, src, src_len,
 359                              packet_buffer, sizeof(packet_buffer),
 360                              PACKET_READ_CHOMP_NEWLINE);
 361        if (dst_len)
 362                *dst_len = len;
 363        return (len > 0) ? packet_buffer : NULL;
 364}
 365
 366char *packet_read_line(int fd, int *len_p)
 367{
 368        return packet_read_line_generic(fd, NULL, NULL, len_p);
 369}
 370
 371int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
 372{
 373        int len = packet_read(fd, NULL, NULL,
 374                              packet_buffer, sizeof(packet_buffer),
 375                              PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
 376        if (dst_len)
 377                *dst_len = len;
 378        if (dst_line)
 379                *dst_line = (len > 0) ? packet_buffer : NULL;
 380        return len;
 381}
 382
 383char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
 384{
 385        return packet_read_line_generic(-1, src, src_len, dst_len);
 386}
 387
 388ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
 389{
 390        int packet_len;
 391
 392        size_t orig_len = sb_out->len;
 393        size_t orig_alloc = sb_out->alloc;
 394
 395        for (;;) {
 396                strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
 397                packet_len = packet_read(fd_in, NULL, NULL,
 398                        /* strbuf_grow() above always allocates one extra byte to
 399                         * store a '\0' at the end of the string. packet_read()
 400                         * writes a '\0' extra byte at the end, too. Let it know
 401                         * that there is already room for the extra byte.
 402                         */
 403                        sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
 404                        PACKET_READ_GENTLE_ON_EOF);
 405                if (packet_len <= 0)
 406                        break;
 407                sb_out->len += packet_len;
 408        }
 409
 410        if (packet_len < 0) {
 411                if (orig_alloc == 0)
 412                        strbuf_release(sb_out);
 413                else
 414                        strbuf_setlen(sb_out, orig_len);
 415                return packet_len;
 416        }
 417        return sb_out->len - orig_len;
 418}
 419
 420/* Packet Reader Functions */
 421void packet_reader_init(struct packet_reader *reader, int fd,
 422                        char *src_buffer, size_t src_len,
 423                        int options)
 424{
 425        memset(reader, 0, sizeof(*reader));
 426
 427        reader->fd = fd;
 428        reader->src_buffer = src_buffer;
 429        reader->src_len = src_len;
 430        reader->buffer = packet_buffer;
 431        reader->buffer_size = sizeof(packet_buffer);
 432        reader->options = options;
 433}
 434
 435enum packet_read_status packet_reader_read(struct packet_reader *reader)
 436{
 437        if (reader->line_peeked) {
 438                reader->line_peeked = 0;
 439                return reader->status;
 440        }
 441
 442        reader->status = packet_read_with_status(reader->fd,
 443                                                 &reader->src_buffer,
 444                                                 &reader->src_len,
 445                                                 reader->buffer,
 446                                                 reader->buffer_size,
 447                                                 &reader->pktlen,
 448                                                 reader->options);
 449
 450        if (reader->status == PACKET_READ_NORMAL)
 451                reader->line = reader->buffer;
 452        else
 453                reader->line = NULL;
 454
 455        return reader->status;
 456}
 457
 458enum packet_read_status packet_reader_peek(struct packet_reader *reader)
 459{
 460        /* Only allow peeking a single line */
 461        if (reader->line_peeked)
 462                return reader->status;
 463
 464        /* Peek a line by reading it and setting peeked flag */
 465        packet_reader_read(reader);
 466        reader->line_peeked = 1;
 467        return reader->status;
 468}