From: Herv� Poussineau <hpous...@reactos.org> When transfering a file, keep it open during the whole transfer, instead of opening/closing it for each block.
Signed-off-by: Herv� Poussineau <hpous...@reactos.org> --- slirp/tftp.c | 20 ++++++++++++-------- slirp/tftp.h | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/slirp/tftp.c b/slirp/tftp.c index 7e63269..48748f1 100644 --- a/slirp/tftp.c +++ b/slirp/tftp.c @@ -37,6 +37,10 @@ static inline void tftp_session_update(struct tftp_session *spt) static void tftp_session_terminate(struct tftp_session *spt) { + if (spt->fd >= 0) { + close(spt->fd); + spt->fd = -1; + } qemu_free(spt->filename); spt->slirp = NULL; } @@ -54,7 +58,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp) /* sessions time out after 5 inactive seconds */ if ((int)(curtime - spt->timestamp) > 5000) { - qemu_free(spt->filename); + tftp_session_terminate(spt); goto found; } } @@ -64,6 +68,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp) found: memset(spt, 0, sizeof(*spt)); memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip)); + spt->fd = -1; spt->client_port = tp->udp.uh_sport; spt->slirp = slirp; @@ -95,23 +100,22 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp) static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr, uint8_t *buf, int len) { - int fd; int bytes_read = 0; - fd = open(spt->filename, O_RDONLY | O_BINARY); + if (spt->fd < 0) { + spt->fd = open(spt->filename, O_RDONLY | O_BINARY); + } - if (fd < 0) { + if (spt->fd < 0) { return -1; } if (len) { - lseek(fd, block_nr * 512, SEEK_SET); + lseek(spt->fd, block_nr * 512, SEEK_SET); - bytes_read = read(fd, buf, len); + bytes_read = read(spt->fd, buf, len); } - close(fd); - return bytes_read; } diff --git a/slirp/tftp.h b/slirp/tftp.h index 471f22e..51704e4 100644 --- a/slirp/tftp.h +++ b/slirp/tftp.h @@ -33,6 +33,7 @@ struct tftp_t { struct tftp_session { Slirp *slirp; char *filename; + int fd; struct in_addr client_ip; uint16_t client_port; -- 1.6.0.2.GIT