Skip to content

Commit c229178

Browse files
committed
Check size while writing to file (Fix #8)
1 parent 8279258 commit c229178

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/recv_buffer.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@ int recv_buffer_read(char *data, int len)
2121
return buffer_read(&buffer, data, len);
2222
}
2323

24-
int64_t recv_file_buffer_read(int fd, int64_t *offset, int64_t size, int64_t blocksize)
24+
int64_t recv_file_buffer_read(int fd, int64_t *offset, int64_t size,
25+
int64_t blocksize)
2526
{
2627
char data[PACKET_DATA_SIZE];
2728
int retval = 0;
2829
int len = 0;
2930
int read;
31+
int bytes_to_write;
3032

3133
if (fd < 0) return -1;
3234

3335
while (size > 0) {
3436

3537
read = buffer_read(&buffer, data, PACKET_DATA_SIZE);
36-
if (read == 0) continue;
38+
if (read == 0) continue; /* keep polling until there is data */
3739

38-
len = pwrite(fd, &data, PACKET_DATA_SIZE, *offset);
40+
bytes_to_write = (size > PACKET_DATA_SIZE) ? PACKET_DATA_SIZE : size;
41+
len = pwrite(fd, &data, bytes_to_write, *offset);
3942
if (len < 1) return len;
4043

4144
*offset += len;

0 commit comments

Comments
 (0)