static void read_loop(int fd, char *buf, size_t len);
-struct redo_list {
- struct redo_list *next;
- int num;
+struct flist_ndx_item {
+ struct flist_ndx_item *next;
+ int ndx;
};
-static struct redo_list *redo_list_head;
-static struct redo_list *redo_list_tail;
+struct flist_ndx_list {
+ struct flist_ndx_item *head, *tail;
+};
+
+static struct flist_ndx_list redo_list;
struct msg_list {
struct msg_list *next;
static struct msg_list *msg_list_head;
static struct msg_list *msg_list_tail;
-static void redo_list_add(int num)
+static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
{
- struct redo_list *rl;
-
- if (!(rl = new(struct redo_list)))
- exit_cleanup(RERR_MALLOC);
- rl->next = NULL;
- rl->num = num;
- if (redo_list_tail)
- redo_list_tail->next = rl;
+ struct flist_ndx_item *item;
+
+ if (!(item = new(struct flist_ndx_item)))
+ out_of_memory("flist_ndx_push");
+ item->next = NULL;
+ item->ndx = ndx;
+ if (lp->tail)
+ lp->tail->next = item;
else
- redo_list_head = rl;
- redo_list_tail = rl;
+ lp->head = item;
+ lp->tail = item;
+}
+
+static int flist_ndx_pop(struct flist_ndx_list *lp)
+{
+ struct flist_ndx_item *next;
+ int ndx;
+
+ if (!lp->head)
+ return -1;
+
+ ndx = lp->head->ndx;
+ next = lp->head->next;
+ free(lp->head);
+ lp->head = next;
+ if (!next)
+ lp->tail = NULL;
+
+ return ndx;
}
static void check_timeout(void)
struct msg_list *ml;
if (!(ml = new(struct msg_list)))
- exit_cleanup(RERR_MALLOC);
+ out_of_memory("msg_list_add");
ml->next = NULL;
if (!(ml->buf = new_array(char, len+4)))
- exit_cleanup(RERR_MALLOC);
+ out_of_memory("msg_list_add");
SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
memcpy(ml->buf+4, buf, len);
ml->len = len+4;
rprintf(FERROR, "invalid message %d:%d\n", tag, len);
exit_cleanup(RERR_STREAMIO);
}
- redo_list_add(-1);
+ flist_ndx_push(&redo_list, -1);
break;
case MSG_REDO:
if (len != 4 || !am_generator) {
exit_cleanup(RERR_STREAMIO);
}
read_loop(fd, buf, 4);
- redo_list_add(IVAL(buf,0));
+ flist_ndx_push(&redo_list, IVAL(buf,0));
break;
case MSG_DELETED:
if (len >= (int)sizeof buf || !am_generator) {
int get_redo_num(void)
{
- struct redo_list *next;
- int num;
-
- while (!redo_list_head)
+ while (!redo_list.head) {
read_msg_fd();
+ }
- num = redo_list_head->num;
- next = redo_list_head->next;
- free(redo_list_head);
- redo_list_head = next;
- if (!next)
- redo_list_tail = NULL;
-
- return num;
+ return flist_ndx_pop(&redo_list);
}
/**
return c;
}
+int read_vstring(int f, char *buf, int bufsize)
+{
+ int len = read_byte(f);
+
+ if (len & 0x80)
+ len = (len & ~0x80) * 0x100 + read_byte(f);
+
+ if (len >= bufsize) {
+ rprintf(FERROR, "over-long vstring received (%d > %d)\n",
+ len, bufsize - 1);
+ exit_cleanup(RERR_PROTOCOL);
+ }
+
+ if (len)
+ readfd(f, buf, len);
+ buf[len] = '\0';
+ return len;
+}
+
/* Populate a sum_struct with values from the socket. This is
* called by both the sender and the receiver. */
void read_sum_head(int f, struct sum_struct *sum)
writefd(f, (char *)&c, 1);
}
+void write_vstring(int f, char *str, int len)
+{
+ uchar lenbuf[3], *lb = lenbuf;
+
+ if (len > 0x7F) {
+ if (len > 0x7FFF) {
+ rprintf(FERROR,
+ "attempting to send over-long vstring (%d > %d)\n",
+ len, 0x7FFF);
+ exit_cleanup(RERR_PROTOCOL);
+ }
+ *lb++ = len / 0x100 + 0x80;
+ }
+ *lb = len;
+
+ writefd(f, (char*)lenbuf, lb - lenbuf + 1);
+ if (len)
+ writefd(f, str, len);
+}
+
/**
* Read a line of up to @p maxlen characters into @p buf (not counting