summaryrefslogtreecommitdiff
path: root/lib/tread.c
blob: 5dde577d191fb9311319f0525253428769ad0cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "system.h"

#include "rpmio.h"

int timedRead(FD_t fd, void * bufptr, int length) {
    int bytesRead;
    int total = 0;
    char * buf = bufptr;
    fd_set readSet;
    struct timeval tv;

    while  (total < length) {
	FD_ZERO(&readSet);
	FD_SET(fdFileno(fd), &readSet);

	tv.tv_sec = 5;			/* FIXME: this should be configurable */
	tv.tv_usec = 0;

	if (select(fdFileno(fd) + 1, &readSet, NULL, NULL, &tv) != 1) 
	    return total;

	bytesRead = fdRead(fd, buf + total, length - total);

	if (bytesRead < 0)
	    return bytesRead;
	else if (bytesRead == 0) 
	    return total;

	total += bytesRead;
    }

    return length;
}