From dc0208b08c7df7c0e559b021bdffcebdb2dc221e Mon Sep 17 00:00:00 2001 From: Nate Straz Date: Tue, 13 Sep 2005 19:47:14 +0000 Subject: Merge qarsh revisions 1727:1738 from sistina-test branch djansa-qarsh. --- qarshd.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 6 deletions(-) (limited to 'qarshd.c') diff --git a/qarshd.c b/qarshd.c index ee9e556..4392db8 100644 --- a/qarshd.c +++ b/qarshd.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -137,7 +138,7 @@ recvfile(const char *path, int if_port, size_t count, mode_t mode) if (nleft != 0) { unlink(path); syslog(LOG_WARNING, "Short file transfer in recvfile(), " - "%d bytes lost, wanted %d\n", nleft, count); + "%zd bytes lost, wanted %zd\n", nleft, count); } close(sd); @@ -146,6 +147,46 @@ recvfile(const char *path, int if_port, size_t count, mode_t mode) return count - nleft; } +ssize_t +pushfile(const char *path, int of_port) +{ + int outsd; + int infd; + off_t offset = 0; + ssize_t nbytes; + struct stat sb; + + outsd = connect_to_peer(&peername, of_port); + if (outsd == -1) { + syslog(LOG_WARNING, "Connect to of_port (%d) failed: %s\n", + of_port, strerror(errno)); + return -1; + } + + infd = open(path, O_RDONLY); + if (infd == -1) { + syslog(LOG_WARNING, "Could not open %s: %s\n", + path, strerror(errno)); + close(outsd); + return -1; + } + + if (fstat(infd, &sb) < 0) { + syslog(LOG_WARNING, "Could not stat %s: %s\n", + path, strerror(errno)); + close(infd); + close(outsd); + return -1; + } + + nbytes = sendfile(outsd, infd, &offset, sb.st_size); + + close(infd); + close(outsd); + + return nbytes; +} + struct qa_packet * rstat(const char *path) { @@ -186,6 +227,7 @@ handle_packets(int infd) for (;;) { FD_SET(infd, &rfds); timeout.tv_sec = 3; + timeout.tv_nsec = 0; nfd = pselect(infd+1, &rfds, NULL, NULL, &timeout, &orig_sigmask); @@ -238,7 +280,7 @@ handle_packets(int infd) break; case QP_RECVFILE: syslog(LOG_INFO, "Got a QP_RECVFILE with path = %s, " - "ifd = %d, count = %d, mode = %o\n", + "ifd = %d, count = %zd, mode = %o\n", qp->qp_recvfile.qp_path, qp->qp_recvfile.qp_if_port, qp->qp_recvfile.qp_count, @@ -251,7 +293,7 @@ handle_packets(int infd) rp = make_qp_returncode(-1, errno, strerror(errno)); } else if (nbytes < qp->qp_recvfile.qp_count) { char tmpstr[512]; - sprintf(tmpstr, "Excpected %d, wrote %d\n", + sprintf(tmpstr, "Excpected %zd, wrote %zd\n", qp->qp_recvfile.qp_count, nbytes); rp = make_qp_returncode(-1, 0, tmpstr); } else { @@ -262,10 +304,18 @@ handle_packets(int infd) break; case QP_SENDFILE: syslog(LOG_INFO, "Got a QP_SENDFILE with path = %s, " - "ofd = %d, count = %d\n", + "ofd = %d\n", qp->qp_sendfile.qp_path, - qp->qp_sendfile.qp_of_port, - qp->qp_sendfile.qp_count); + qp->qp_sendfile.qp_of_port); + nbytes = pushfile(qp->qp_sendfile.qp_path, + qp->qp_sendfile.qp_of_port); + if (nbytes < 0) { + rp = make_qp_returncode(-1, errno, strerror(errno)); + } else { + rp = make_qp_returncode(0, 0, "Transfer Complete"); + } + send_packet(fileno(stdout), rp); + qpfree(rp); break; case QP_RSTAT: syslog(LOG_INFO, "Got a QP_RSTAT with path = %s\n", -- cgit