X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fproto.c;h=9701ab45d472f0ca7e30c86215d36f2799ca31af;hb=HEAD;hp=9d548a6ecb900bf61a01b213b21e8b0a0944a7e4;hpb=779888af6bc05f2395e0c9fc0c53067e0674cb27;p=reposerve diff --git a/src/proto.c b/src/proto.c index 9d548a6..9701ab4 100644 --- a/src/proto.c +++ b/src/proto.c @@ -28,7 +28,7 @@ void flist_destroy(struct flist *flist) } } -int flist_add(struct flist *flist, const char *fname, int contents) +int flist_add(struct flist *flist, const char *name, const char *path, int contents) { FILE *fp; struct proto_file_entry fent; @@ -43,8 +43,8 @@ int flist_add(struct flist *flist, const char *fname, int contents) return -1; } - if(!(fp = fopen(fname, "rb"))) { - fprintf(stderr, "flist_add: failed to open file: %s: %s\n", fname, strerror(errno)); + if(!(fp = fopen(path, "rb"))) { + fprintf(stderr, "flist_add: failed to open file: %s: %s\n", path, strerror(errno)); return -1; } fstat(fileno(fp), &st); @@ -52,12 +52,12 @@ int flist_add(struct flist *flist, const char *fname, int contents) fent.size = st.st_size; fent.mtime = st.st_mtime; - namelen = datalen = strlen(fname); + namelen = datalen = strlen(name); if(contents) { datalen += fent.size; } - if(flist->flist->num_files >= flist->max_files) { + if(!flist->flist || flist->flist->num_files >= flist->max_files) { struct proto_flist *tmp; int newsz = flist->max_files ? flist->max_files << 1 : 32; @@ -65,6 +65,9 @@ int flist_add(struct flist *flist, const char *fname, int contents) fprintf(stderr, "flist_add: failed to resize file list to %d entries\n", newsz); return -1; } + if(!flist->flist) { + tmp->num_files = 0; + } flist->flist = tmp; flist->max_files = newsz; } @@ -89,7 +92,7 @@ int flist_add(struct flist *flist, const char *fname, int contents) fent.nameoffs = flist->data_sz; fent.namelen = namelen; dptr = flist->data + flist->data_sz; - memcpy(dptr, fname, namelen); + memcpy(dptr, name, namelen); md5_begin(&md); if(contents) { @@ -139,3 +142,33 @@ int flist_finalize(struct flist *flist) flist->final_size = newsz; return 0; } + +int read_line(int s, char *buf, int bufsz) +{ + char *ptr = buf; + while(bufsz > 1 && read(s, ptr, 1) > 0 && *ptr++ != '\n'); + *ptr = 0; + return ptr == buf ? -1 : 0; +} + +int read_resp(int s) +{ + int sz, msglen = 0; + char buf[64]; + char *ptr; + + while(msglen < 3 && (sz = read(s, buf + msglen, 3 - msglen)) > 0) { + msglen += sz; + } + if(msglen < 3) return -1; + + if(memcmp(buf, "ERR", 3) == 0) { + return -1; + } + + ptr = buf; + while(read(s, ptr, 1) > 0 && *ptr++ != '\n'); + *ptr = 0; + + return atoi(buf); +}