3 #include "network_backends.h"
5 #include "sys-socket.h"
11 int network_write_file_chunk_no_mmap(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
, off_t
*p_max_bytes
) {
12 chunk
* const c
= cq
->first
;
17 force_assert(NULL
!= c
);
18 force_assert(FILE_CHUNK
== c
->type
);
19 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
21 offset
= c
->file
.start
+ c
->offset
;
22 toSend
= c
->file
.length
- c
->offset
;
23 if (toSend
> 64*1024) toSend
= 64*1024; /* max read 64kb in one step */
24 if (toSend
> *p_max_bytes
) toSend
= *p_max_bytes
;
27 chunkqueue_remove_finished_chunks(cq
);
31 if (0 != chunkqueue_open_file_chunk(srv
, cq
)) return -1;
33 buffer_string_prepare_copy(srv
->tmp_buf
, toSend
);
35 if (-1 == lseek(c
->file
.fd
, offset
, SEEK_SET
)) {
36 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "lseek: ", strerror(errno
));
39 if (-1 == (toSend
= read(c
->file
.fd
, srv
->tmp_buf
->ptr
, toSend
))) {
40 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "read: ", strerror(errno
));
45 if ((r
= send(fd
, srv
->tmp_buf
->ptr
, toSend
, 0)) < 0) {
46 int lastError
= WSAGetLastError();
56 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
57 "send failed: ", lastError
, fd
);
62 if ((r
= write(fd
, srv
->tmp_buf
->ptr
, toSend
)) < 0) {
71 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
72 "write failed:", strerror(errno
), fd
);
80 chunkqueue_mark_written(cq
, r
);
83 return (r
> 0 && r
== toSend
) ? 0 : -3;