X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=knetfile.c;h=d3e628371270fffe07f44de0af31e30c7479a959;hb=17d79b44a564aa4d5853bbc0b5f97ec2cefc927b;hp=994babb6a384e2757098bd6b042970a4dd54a801;hpb=317f5e8dd22cc9e1e5e05fbcaeb3b9aca7447351;p=samtools.git diff --git a/knetfile.c b/knetfile.c index 994babb..d3e6283 100644 --- a/knetfile.c +++ b/knetfile.c @@ -1,6 +1,7 @@ /* The MIT License - Copyright (c) 2008 Genome Research Ltd (GRL). + Copyright (c) 2008 by Genome Research Ltd (GRL). + 2010 by Attractive Chaos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,11 +24,9 @@ SOFTWARE. */ -/* Contact: Heng Li */ - /* Probably I will not do socket programming in the next few years and therefore I decide to heavily annotate this file, for Linux and - Windows as well. -lh3 */ + Windows as well. -ac */ #include #include @@ -38,9 +37,7 @@ #include #include -#ifdef _WIN32 -#include -#else +#ifndef _WIN32 #include #include #include @@ -92,7 +89,7 @@ static int socket_connect(const char *host, const char *port) int on = 1, fd; struct linger lng = { 0, 0 }; - struct addrinfo hints, *res; + struct addrinfo hints, *res = 0; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -236,7 +233,10 @@ static int kftp_get_response(knetFile *ftp) static int kftp_send_cmd(knetFile *ftp, const char *cmd, int is_get) { if (socket_wait(ftp->ctrl_fd, 0) <= 0) return -1; // socket is not ready for writing - netwrite(ftp->ctrl_fd, cmd, strlen(cmd)); + if (netwrite(ftp->ctrl_fd, cmd, strlen(cmd)) < 0) { + fprintf(stderr, "[%s] error sending command to socket.\n", __func__); + return -1; + } return is_get? kftp_get_response(ftp) : 0; } @@ -415,7 +415,10 @@ int khttp_connect_file(knetFile *fp) l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host); l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset); l += sprintf(buf + l, "\r\n"); - netwrite(fp->fd, buf, l); + if (netwrite(fp->fd, buf, l) < 0) { + fprintf(stderr, "[%s] fail write GET line.\n", __func__); + return -1; + } l = 0; while (netread(fp->fd, buf + l, 1)) { // read HTTP header; FIXME: bad efficiency if (buf[l] == '\n' && l >= 3) @@ -519,7 +522,10 @@ off_t knet_read(knetFile *fp, void *buf, off_t len) if (fp->type == KNF_TYPE_LOCAL) { // on Windows, the following block is necessary; not on UNIX off_t rest = len, curr; while (rest) { - curr = read(fp->fd, buf + l, rest); + do { + curr = read(fp->fd, buf + l, rest); + } while (curr < 0 && EINTR == errno); + if (curr < 0) return -1; if (curr == 0) break; l += curr; rest -= curr; } @@ -566,7 +572,7 @@ off_t knet_seek(knetFile *fp, int64_t off, int whence) else if (whence==SEEK_SET) fp->offset = off; fp->is_ready = 0; - return fp->offset; + return 0; } errno = EINVAL; fprintf(stderr,"[knet_seek] %s\n", strerror(errno));