summaryrefslogtreecommitdiff
path: root/pkt-line.c
diff options
context:
space:
mode:
Diffstat (limited to 'pkt-line.c')
-rw-r--r--pkt-line.c80
1 files changed, 43 insertions, 37 deletions
diff --git a/pkt-line.c b/pkt-line.c
index 9f63eae..2dc8ac2 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -243,29 +243,50 @@ void packet_write(int fd_out, const char *buf, size_t size)
die("%s", err.buf);
}
-void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
+void packet_fwrite(FILE *f, const char *buf, size_t size)
{
- va_list args;
+ size_t packet_size;
+ char header[4];
- va_start(args, fmt);
- format_packet(buf, "", fmt, args);
- va_end(args);
+ if (size > LARGE_PACKET_DATA_MAX)
+ die(_("packet write failed - data exceeds max packet size"));
+
+ packet_trace(buf, size, 1);
+ packet_size = size + 4;
+
+ set_packet_header(header, packet_size);
+ fwrite_or_die(f, header, 4);
+ fwrite_or_die(f, buf, size);
}
-void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
+void packet_fwrite_fmt(FILE *fh, const char *fmt, ...)
{
- size_t orig_len, n;
+ static struct strbuf buf = STRBUF_INIT;
+ va_list args;
- orig_len = buf->len;
- strbuf_addstr(buf, "0000");
- strbuf_add(buf, data, len);
- n = buf->len - orig_len;
+ strbuf_reset(&buf);
- if (n > LARGE_PACKET_MAX)
- die(_("protocol error: impossibly long line"));
+ va_start(args, fmt);
+ format_packet(&buf, "", fmt, args);
+ va_end(args);
+
+ fwrite_or_die(fh, buf.buf, buf.len);
+}
- set_packet_header(&buf->buf[orig_len], n);
- packet_trace(data, len, 1);
+void packet_fflush(FILE *f)
+{
+ packet_trace("0000", 4, 1);
+ fwrite_or_die(f, "0000", 4);
+ fflush_or_die(f);
+}
+
+void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ format_packet(buf, "", fmt, args);
+ va_end(args);
}
int write_packetized_from_fd_no_flush(int fd_in, int fd_out)
@@ -416,38 +437,28 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
return PACKET_READ_NORMAL;
}
-int packet_read(int fd, char **src_buffer, size_t *src_len,
- char *buffer, unsigned size, int options)
+int packet_read(int fd, char *buffer, unsigned size, int options)
{
int pktlen = -1;
- packet_read_with_status(fd, src_buffer, src_len, buffer, size,
- &pktlen, options);
+ packet_read_with_status(fd, NULL, NULL, buffer, size, &pktlen,
+ options);
return pktlen;
}
-static char *packet_read_line_generic(int fd,
- char **src, size_t *src_len,
- int *dst_len)
+char *packet_read_line(int fd, int *dst_len)
{
- int len = packet_read(fd, src, src_len,
- packet_buffer, sizeof(packet_buffer),
+ int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
PACKET_READ_CHOMP_NEWLINE);
if (dst_len)
*dst_len = len;
return (len > 0) ? packet_buffer : NULL;
}
-char *packet_read_line(int fd, int *len_p)
-{
- return packet_read_line_generic(fd, NULL, NULL, len_p);
-}
-
int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
{
- int len = packet_read(fd, NULL, NULL,
- packet_buffer, sizeof(packet_buffer),
+ int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
if (dst_len)
*dst_len = len;
@@ -456,11 +467,6 @@ int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
return len;
}
-char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
-{
- return packet_read_line_generic(-1, src, src_len, dst_len);
-}
-
ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
{
int packet_len;
@@ -470,7 +476,7 @@ ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
for (;;) {
strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
- packet_len = packet_read(fd_in, NULL, NULL,
+ packet_len = packet_read(fd_in,
/* strbuf_grow() above always allocates one extra byte to
* store a '\0' at the end of the string. packet_read()
* writes a '\0' extra byte at the end, too. Let it know