commit 09a1b978967c316e9c40caed8fe63a9a55565b9f from: Omar Polo date: Wed Oct 07 15:59:31 2020 UTC put back the missing lseek, and change variables name (I know, changing variables names AND introducing changes is better done in more commits, but…) Added back an lseek that was missing. If TLS_WANT_POLL{IN,OUT}, we need to re-send that block, but we need also to rewind the file, in order to read(2) that chunk again. This doesn’t solve the corruption in transferring big files, but reduces them. I still haven’t tracked down the corruption :/ commit - cc68fe70fcb31b9acebab97ecad2c84c4c80fc02 commit + 09a1b978967c316e9c40caed8fe63a9a55565b9f blob - 3cc158e760e3cd1814083427f1070f750ba074ba blob + c140616a6a3b2db7158be6f0ca4a4fa31d141a41 --- gmid.c +++ gmid.c @@ -281,8 +281,8 @@ send_file(char *path, struct pollfd *fds, struct clien { char fpath[PATHBUF]; char buf[FILEBUF]; - size_t i; - ssize_t t, w; + size_t off; + ssize_t ret, len; if (client->fd == -1) { assert(path != NULL); @@ -314,20 +314,18 @@ send_file(char *path, struct pollfd *fds, struct clien } while (1) { - w = read(client->fd, buf, sizeof(buf)); - if (w == -1) + len = read(client->fd, buf, sizeof(buf)); + if (len == -1) warn("read"); - if (w == 0 || w == -1) { + if (len == 0 || len == -1) { goodbye(fds, client); return; } - t = w; - i = 0; - - while (w > 0) { - t = tls_write(client->ctx, buf+i, w); - switch (t) { + off = 0; + while (len > 0) { + ret = tls_write(client->ctx, buf+off, len); + switch (ret) { case -1: warnx("tls_write: %s", tls_error(client->ctx)); goodbye(fds, client); @@ -335,14 +333,16 @@ send_file(char *path, struct pollfd *fds, struct clien case TLS_WANT_POLLIN: case TLS_WANT_POLLOUT: - fds->events = (t == TLS_WANT_POLLIN) - ? POLLIN - : POLLOUT; + fds->events = ret == TLS_WANT_POLLIN ? POLLIN : POLLOUT; + if (lseek(client->fd, -1 * len, SEEK_CUR) == -1) { + warnx("lseek"); + goodbye(fds, client); + } return; default: - w -= t; - i += t; + off += ret; + len -= ret; break; } }