3 #include "network_backends.h"
8 #include "stat_cache.h"
10 #include "sys-socket.h"
22 int network_open_file_chunk(server
*srv
, connection
*con
, chunkqueue
*cq
) {
23 chunk
* const c
= cq
->first
;
28 force_assert(NULL
!= c
);
29 force_assert(FILE_CHUNK
== c
->type
);
30 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
32 offset
= c
->file
.start
+ c
->offset
;
33 toSend
= c
->file
.length
- c
->offset
;
35 if (-1 == c
->file
.fd
) {
36 if (-1 == (c
->file
.fd
= fdevent_open_cloexec(c
->file
.name
->ptr
, O_RDONLY
, 0))) {
37 log_error_write(srv
, __FILE__
, __LINE__
, "ssb", "open failed:", strerror(errno
), c
->file
.name
);
42 /*(skip file size checks if file is temp file created by lighttpd)*/
43 if (c
->file
.is_temp
) return 0;
45 if (-1 == fstat(c
->file
.fd
, &st
)) {
46 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fstat failed:", strerror(errno
));
50 if (offset
> st
.st_size
|| toSend
> st
.st_size
|| offset
> st
.st_size
- toSend
) {
51 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "file shrunk:", c
->file
.name
);
58 int network_write_file_chunk_no_mmap(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
, off_t
*p_max_bytes
) {
59 chunk
* const c
= cq
->first
;
63 force_assert(NULL
!= c
);
64 force_assert(FILE_CHUNK
== c
->type
);
65 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
67 offset
= c
->file
.start
+ c
->offset
;
68 toSend
= c
->file
.length
- c
->offset
;
69 if (toSend
> 64*1024) toSend
= 64*1024; /* max read 64kb in one step */
70 if (toSend
> *p_max_bytes
) toSend
= *p_max_bytes
;
73 chunkqueue_remove_finished_chunks(cq
);
77 if (0 != network_open_file_chunk(srv
, con
, cq
)) return -1;
79 buffer_string_prepare_copy(srv
->tmp_buf
, toSend
);
81 if (-1 == lseek(c
->file
.fd
, offset
, SEEK_SET
)) {
82 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "lseek: ", strerror(errno
));
85 if (-1 == (toSend
= read(c
->file
.fd
, srv
->tmp_buf
->ptr
, toSend
))) {
86 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "read: ", strerror(errno
));
91 if ((r
= send(fd
, srv
->tmp_buf
->ptr
, toSend
, 0)) < 0) {
92 int lastError
= WSAGetLastError();
102 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
103 "send failed: ", lastError
, fd
);
108 if ((r
= write(fd
, srv
->tmp_buf
->ptr
, toSend
)) < 0) {
117 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
118 "write failed:", strerror(errno
), fd
);
126 chunkqueue_mark_written(cq
, r
);
129 return (r
> 0 && r
== toSend
) ? 0 : -3;