-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_stream.c
124 lines (111 loc) · 3.56 KB
/
io_stream.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* ryshttpd -- simple filesharing http server.
*
* ryshttpd is copyrighted:
* Copyright (C) 2018 Andrey Rys. All rights reserved.
*
* ryshttpd is licensed to you under the terms of std. MIT/X11 license:
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* That's like sendfile, but much more complex.
* The aim is to avoid using sendfile completely,
* doing all I/O as much portably as possible.
* Original code is from my tfcrypt I/O layer.
*/
#include "httpd.h"
static size_t BLK_LEN_ADJ(rh_fsize filelen, rh_fsize read_already, size_t blklen)
{
if (filelen == NOFSIZE) return blklen;
return ((filelen - read_already) >= blklen) ? blklen : (filelen - read_already);
}
rh_yesno io_stream_file(struct io_stream_args *ios_args)
{
char *pblk;
rh_fsize total_to_read;
size_t ld, lr, lb, li;
rh_yesno do_stop = NO;
if (!ios_args) return NO;
if (!ios_args->rdfn || !ios_args->wrfn || !ios_args->mgfn || !ios_args->skfn) {
ios_args->error = EINVAL;
return NO;
}
if (ios_args->file_size != NOFSIZE) {
if ((ios_args->read_to > ios_args->file_size)
|| ios_args->read_to == 0)
ios_args->read_to = ios_args->file_size;
if (ios_args->start_from > ios_args->file_size)
ios_args->start_from = 0;
if (ios_args->start_from > ios_args->read_to)
ios_args->start_from = 0;
if (ios_args->start_from > 0) {
if (ios_args->skfn(ios_args->fn_args, ios_args->start_from) == NOFSIZE) {
ios_args->error = errno;
ios_args->status = IOS_SEEK_ERROR;
return NO;
}
}
total_to_read = ios_args->read_to - ios_args->start_from;
}
else total_to_read = NOFSIZE;
/* tfcrypt fault tolerant io code */
while (1) {
if (do_stop) break;
pblk = ios_args->workbuf;
ld = 0;
lr = lb = BLK_LEN_ADJ(total_to_read, ios_args->nr_written, ios_args->wkbufsz);
_ragain: li = ios_args->rdfn(ios_args->fn_args, pblk, lr);
if (li == 0) do_stop = YES;
if (li == NOSIZE) {
ios_args->error = errno;
ios_args->status = IOS_READ_ERROR;
return NO;
}
else ld += li;
if (li && li < lr) {
pblk += li;
lr -= li;
goto _ragain;
}
ios_args->mgfn(ios_args->fn_args, pblk, lr);
pblk = ios_args->workbuf;
lr = ld;
ld = 0;
_wagain: li = ios_args->wrfn(ios_args->fn_args, pblk, lr);
if (li == NOSIZE) {
ios_args->error = errno;
ios_args->status = IOS_WRITE_ERROR;
return NO;
}
else ld += li;
if (li < lr) {
pblk += li;
lr -= li;
goto _wagain;
}
ios_args->nr_written += ld;
if (total_to_read != NOFSIZE
&& ios_args->nr_written >= total_to_read) break;
}
ios_args->error = 0;
ios_args->status = IOS_ALL_OK;
return YES;
}