diff options
Diffstat (limited to 'qacp.c')
-rw-r--r-- | qacp.c | 48 |
1 files changed, 19 insertions, 29 deletions
@@ -30,6 +30,7 @@ #include <unistd.h> #include <fcntl.h> #include <errno.h> +#include <assert.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/socket.h> @@ -186,6 +187,7 @@ qacp_sendonefile(const char *host, const char *srcfile, const char *destfile) } send_packet(qacp_fd, qp); offset += nbytes; + qpfree(qp); } while (nbytes > 0); sendone_failure: @@ -219,14 +221,8 @@ void qacp_recvonefile(const char *host, const char *srcfile, const char *destfile) { struct qa_packet *qp; - struct sockaddr_in daddr; - socklen_t dlen; - int sd; - int port; - int insd; int outfd; - char buf[BUFSIZ]; - ssize_t nread, nwrote; + ssize_t nwrote; off_t nleft; struct qp_rstat_pkt *rstatp; int rstaterrno; @@ -245,38 +241,35 @@ qacp_recvonefile(const char *host, const char *srcfile, const char *destfile) fchmod(outfd, rstatp->qp_st_mode); - sd = bind_any(QARSH_MINPORT, qarsh_ss_family); - port = getsockport(sd); - /* Recall that the packet types are qarshd-centric, so if we want * to recv a file from the host running qarshd we have to tell * qarshd to send a file. */ - qp = make_qp_sendfile(srcfile, port); + qp = make_qp_sendfile(srcfile, outfd); send_packet(qacp_fd, qp); qpfree(qp); - dlen = sizeof daddr; - insd = accept(sd, (struct sockaddr *) &daddr, &dlen); - /* Read/write file */ nleft = rstatp->qp_st_size; while (nleft > 0) { - nread = read(insd, buf, BUFSIZ); - if (nread < 0) { - fprintf(stderr, "read() error: %s\n", strerror(errno)); - break; - } else if (nread == 0) { /* EOF */ - break; - } - - nwrote = write(outfd, buf, nread); - if (nwrote < 0) { - fprintf(stderr, "write() error: %s\n", strerror(errno)); + qp = recv_packet(qacp_fd); + if (qp) { + assert(qp->qp_type == QP_DATA); + assert(qp->qp_data.qp_remfd == outfd); + assert(rstatp->qp_st_size - nleft == qp->qp_data.qp_offset); + nwrote = write(outfd, qp->qp_data.qp_blob, qp->qp_data.qp_count); + if (nwrote < 0) { + fprintf(stderr, "write() error: %s\n", strerror(errno)); + break; + } + assert(nwrote == qp->qp_data.qp_count); + nleft -= qp->qp_data.qp_count; + qpfree(qp); + } else { + fprintf(stderr, "Did not receive a packet\n"); break; } - nleft -= nread; } if (nleft != 0) { @@ -286,9 +279,6 @@ qacp_recvonefile(const char *host, const char *srcfile, const char *destfile) (long long int)nleft, (long long int)rstatp->qp_st_size); } - - close(sd); - close(insd); close(outfd); /* Await our return code from qarshd */ |