Moved the post-done transfer reset responsibility to transfer.c.
[doldaconnect.git] / daemon / transfer.c
index f23a3ed..32f3e28 100644 (file)
@@ -133,7 +133,10 @@ static void localread(struct socket *sk, struct transfer *transfer)
     
     if((transfer->datapipe != NULL) && (sockqueueleft(transfer->datapipe) > 0)) {
        buf = sockgetinbuf(sk, &blen);
+       if((transfer->endpos >= 0) && (transfer->curpos + blen > transfer->endpos))
+           blen = transfer->endpos - transfer->curpos;
        sockqueue(transfer->datapipe, buf, blen);
+       free(buf);
        time(&transfer->activity);
        transfer->curpos += blen;
        bytesupload += blen;
@@ -148,7 +151,10 @@ static void dataread(struct socket *sk, struct transfer *transfer)
     
     if((transfer->localend != NULL) && (sockqueueleft(transfer->localend) > 0)) {
        buf = sockgetinbuf(sk, &blen);
+       if((transfer->endpos >= 0) && (transfer->curpos + blen > transfer->endpos))
+           blen = transfer->endpos - transfer->curpos;
        sockqueue(transfer->localend, buf, blen);
+       free(buf);
        transfer->curpos += blen;
        bytesdownload += blen;
        CBCHAINDOCB(transfer, trans_p, transfer);
@@ -177,11 +183,16 @@ static void dataerr(struct socket *sk, int errno, struct transfer *transfer)
 {
     if(transfer->curpos >= transfer->size) {
        transfersetstate(transfer, TRNS_DONE);
-       closesock(transfer->localend);
-       quitsock(transfer->localend);
-       transfer->localend = NULL;
+       if(transfer->localend != NULL) {
+           closesock(transfer->localend);
+           quitsock(transfer->localend);
+           transfer->localend = NULL;
+       }
     } else {
-       resettransfer(transfer);
+       if(transfer->dir == TRNSD_DOWN)
+           resettransfer(transfer);
+       else if(transfer->dir == TRNSD_UP)
+           transfer->close = 1;
     }
 }