Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 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 |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 8 | * would be an error). |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 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 | */ |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 19 | ssize_t safe_write(int fd, const void *buf, ssize_t n) |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 20 | { |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 21 | ssize_t nn = n; |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 22 | while (n) { |
Junio C Hamano | 1c15afb | 2005-12-19 16:18:28 -0800 | [diff] [blame] | 23 | int ret = xwrite(fd, buf, n); |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 24 | if (ret > 0) { |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 25 | buf = (char *) buf + ret; |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 26 | n -= ret; |
| 27 | continue; |
| 28 | } |
| 29 | if (!ret) |
| 30 | die("write error (disk full?)"); |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 31 | die("write error (%s)", strerror(errno)); |
| 32 | } |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 33 | return nn; |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* |
| 37 | * If we buffered things up above (we don't, but we should), |
| 38 | * we'd flush it here |
| 39 | */ |
| 40 | void packet_flush(int fd) |
| 41 | { |
| 42 | safe_write(fd, "0000", 4); |
| 43 | } |
| 44 | |
| 45 | #define hex(a) (hexchar[(a) & 15]) |
| 46 | void 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 | |
| 66 | static void safe_read(int fd, void *buffer, unsigned size) |
| 67 | { |
Heikki Orsila | c697ad1 | 2008-05-03 16:27:26 +0300 | [diff] [blame] | 68 | ssize_t ret = read_in_full(fd, buffer, size); |
| 69 | if (ret < 0) |
| 70 | die("read error (%s)", strerror(errno)); |
| 71 | else if (ret < size) |
| 72 | die("The remote end hung up unexpectedly"); |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int packet_read_line(int fd, char *buffer, unsigned size) |
| 76 | { |
| 77 | int n; |
| 78 | unsigned len; |
| 79 | char linelen[4]; |
| 80 | |
| 81 | safe_read(fd, linelen, 4); |
| 82 | |
| 83 | len = 0; |
| 84 | for (n = 0; n < 4; n++) { |
| 85 | unsigned char c = linelen[n]; |
| 86 | len <<= 4; |
| 87 | if (c >= '0' && c <= '9') { |
| 88 | len += c - '0'; |
| 89 | continue; |
| 90 | } |
| 91 | if (c >= 'a' && c <= 'f') { |
| 92 | len += c - 'a' + 10; |
| 93 | continue; |
| 94 | } |
| 95 | if (c >= 'A' && c <= 'F') { |
| 96 | len += c - 'A' + 10; |
| 97 | continue; |
| 98 | } |
| 99 | die("protocol error: bad line length character"); |
| 100 | } |
| 101 | if (!len) |
| 102 | return 0; |
| 103 | len -= 4; |
| 104 | if (len >= size) |
| 105 | die("protocol error: bad line length %d", len); |
| 106 | safe_read(fd, buffer, len); |
| 107 | buffer[len] = 0; |
| 108 | return len; |
| 109 | } |