pkt-line.con commit (revert local fix) (200e7fb)
   1#include "cache.h"
   2#include "pkt-line.h"
   3
   4/*
   5 * Write a packetized stream, where each line is preceded by
   6 * its length (including the header) as a 4-byte hex number.
   7 * A length of 'zero' means end of stream (and a length of 1-3
   8 * would be an error). 
   9 *
  10 * This is all pretty stupid, but we use this packetized line
  11 * format to make a streaming format possible without ever
  12 * over-running the read buffers. That way we'll never read
  13 * into what might be the pack data (which should go to another
  14 * process entirely).
  15 *
  16 * The writing side could use stdio, but since the reading
  17 * side can't, we stay with pure read/write interfaces.
  18 */
  19static void safe_write(int fd, const void *buf, unsigned n)
  20{
  21        while (n) {
  22                int ret = write(fd, buf, n);
  23                if (ret > 0) {
  24                        buf += ret;
  25                        n -= ret;
  26                        continue;
  27                }
  28                if (!ret)
  29                        die("write error (disk full?)");
  30                if (errno == EAGAIN || errno == EINTR)
  31                        continue;
  32                die("write error (%s)", strerror(errno));
  33        }
  34}
  35
  36/*
  37 * If we buffered things up above (we don't, but we should),
  38 * we'd flush it here
  39 */
  40void packet_flush(int fd)
  41{
  42        safe_write(fd, "0000", 4);
  43}
  44
  45#define hex(a) (hexchar[(a) & 15])
  46void packet_write(int fd, const char *fmt, ...)
  47{
  48        static char buffer[1000];
  49        static char hexchar[] = "0123456789abcdef";
  50        va_list args;
  51        unsigned n;
  52
  53        va_start(args, fmt);
  54        n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
  55        va_end(args);
  56        if (n >= sizeof(buffer)-4)
  57                die("protocol error: impossibly long line");
  58        n += 4;
  59        buffer[0] = hex(n >> 12);
  60        buffer[1] = hex(n >> 8);
  61        buffer[2] = hex(n >> 4);
  62        buffer[3] = hex(n);
  63        safe_write(fd, buffer, n);
  64}
  65
  66static void safe_read(int fd, void *buffer, unsigned size)
  67{
  68        int n = 0;
  69
  70        while (n < size) {
  71                int ret = read(fd, buffer + n, size - n);
  72                if (ret < 0) {
  73                        if (errno == EINTR || errno == EAGAIN)
  74                                continue;
  75                        die("read error (%s)", strerror(errno));
  76                }
  77                if (!ret)
  78                        die("unexpected EOF");
  79                n += ret;
  80        }
  81}
  82
  83int packet_read_line(int fd, char *buffer, unsigned size)
  84{
  85        int n;
  86        unsigned len;
  87        char linelen[4];
  88
  89        safe_read(fd, linelen, 4);
  90
  91        len = 0;
  92        for (n = 0; n < 4; n++) {
  93                unsigned char c = linelen[n];
  94                len <<= 4;
  95                if (c >= '0' && c <= '9') {
  96                        len += c - '0';
  97                        continue;
  98                }
  99                if (c >= 'a' && c <= 'f') {
 100                        len += c - 'a' + 10;
 101                        continue;
 102                }
 103                if (c >= 'A' && c <= 'F') {
 104                        len += c - 'A' + 10;
 105                        continue;
 106                }
 107                die("protocol error: bad line length character");
 108        }
 109        if (!len)
 110                return 0;
 111        len -= 4;
 112        if (len >= size)
 113                die("protocol error: bad line length %d", len);
 114        safe_read(fd, buffer, len);
 115        buffer[len] = 0;
 116        return len;
 117}