summaryrefslogtreecommitdiffstats
path: root/qarshd.c
diff options
context:
space:
mode:
authorNate Straz <nstraz@redhat.com>2005-09-13 19:47:14 +0000
committerNathan Straz <nstraz@redhat.com>2008-09-23 09:37:44 -0400
commitdc0208b08c7df7c0e559b021bdffcebdb2dc221e (patch)
tree63c15c2651515f369792ea30141c6286e8de450f /qarshd.c
parent988f9b104087022ecf08ed881aa723fcb33a46b6 (diff)
downloadqarsh-dc0208b08c7df7c0e559b021bdffcebdb2dc221e.tar.gz
qarsh-dc0208b08c7df7c0e559b021bdffcebdb2dc221e.tar.xz
qarsh-dc0208b08c7df7c0e559b021bdffcebdb2dc221e.zip
Merge qarsh revisions 1727:1738 from sistina-test branch djansa-qarsh.
Diffstat (limited to 'qarshd.c')
-rw-r--r--qarshd.c62
1 files changed, 56 insertions, 6 deletions
diff --git a/qarshd.c b/qarshd.c
index ee9e556..4392db8 100644
--- a/qarshd.c
+++ b/qarshd.c
@@ -13,6 +13,7 @@
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
+#include <sys/sendfile.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <grp.h>
@@ -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",