]>
git.ipfire.org Git - thirdparty/git.git/blob - pkt-line.c
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
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
16 * The writing side could use stdio, but since the reading
17 * side can't, we stay with pure read/write interfaces.
19 static void safe_write(int fd
, const void *buf
, unsigned n
)
22 int ret
= write(fd
, buf
, n
);
29 die("write error (disk full?)");
30 if (errno
== EAGAIN
|| errno
== EINTR
)
32 die("write error (%s)", strerror(errno
));
37 * If we buffered things up above (we don't, but we should),
40 void packet_flush(int fd
)
42 safe_write(fd
, "0000", 4);
45 #define hex(a) (hexchar[(a) & 15])
46 void packet_write(int fd
, const char *fmt
, ...)
48 static char buffer
[1000];
49 static char hexchar
[] = "0123456789abcdef";
54 n
= vsnprintf(buffer
+ 4, sizeof(buffer
) - 4, fmt
, args
);
56 if (n
>= sizeof(buffer
)-4)
57 die("protocol error: impossibly long line");
59 buffer
[0] = hex(n
>> 12);
60 buffer
[1] = hex(n
>> 8);
61 buffer
[2] = hex(n
>> 4);
63 safe_write(fd
, buffer
, n
);
66 static void safe_read(int fd
, void *buffer
, unsigned size
)
71 int ret
= read(fd
, buffer
+ n
, size
- n
);
73 if (errno
== EINTR
|| errno
== EAGAIN
)
75 die("read error (%s)", strerror(errno
));
78 die("unexpected EOF");
83 int packet_read_line(int fd
, char *buffer
, unsigned size
)
89 safe_read(fd
, linelen
, 4);
92 for (n
= 0; n
< 4; n
++) {
93 unsigned char c
= linelen
[n
];
95 if (c
>= '0' && c
<= '9') {
99 if (c
>= 'a' && c
<= 'f') {
103 if (c
>= 'A' && c
<= 'F') {
107 die("protocol error: bad line length character");
113 die("protocol error: bad line length %d", len
);
114 safe_read(fd
, buffer
, len
);