Apache HTTPD
readwrite.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define INCL_DOS
18#define INCL_DOSERRORS
19
20#include "apr_arch_file_io.h"
21#include "apr_file_io.h"
22#include "apr_lib.h"
23#include "apr_strings.h"
24
25#include <malloc.h>
26
28{
29 ULONG rc = 0;
31
32 if (!thefile->isopen) {
33 *nbytes = 0;
34 return APR_EBADF;
35 }
36
37 if (thefile->buffered) {
38 char *pos = (char *)buf;
40 ULONG size = *nbytes;
41
43
44 if (thefile->direction == 1) {
45 int rv = apr_file_flush(thefile);
46
47 if (rv != APR_SUCCESS) {
49 return rv;
50 }
51
52 thefile->bufpos = 0;
53 thefile->direction = 0;
54 thefile->dataRead = 0;
55 }
56
57 while (rc == 0 && size > 0) {
58 if (thefile->bufpos >= thefile->dataRead) {
62
63 if (bytesread == 0) {
64 if (rc == 0)
66 break;
67 }
68
71 thefile->bufpos = 0;
72 }
73
77 pos += blocksize;
78 size -= blocksize;
79 }
80
81 *nbytes = rc == 0 ? pos - (char *)buf : 0;
83
84 if (*nbytes == 0 && rc == 0 && thefile->eof_hit) {
85 return APR_EOF;
86 }
87
88 return APR_FROM_OS_ERROR(rc);
89 } else {
90 if (thefile->pipe)
92
94
95 if (rc == ERROR_NO_DATA && thefile->timeout != 0) {
97
98 if (rcwait == 0) {
100 }
101 else if (rcwait == ERROR_TIMEOUT) {
102 *nbytes = 0;
103 return APR_TIMEUP;
104 }
105 }
106
107 if (rc) {
108 *nbytes = 0;
109 return APR_FROM_OS_ERROR(rc);
110 }
111
112 *nbytes = bytesread;
113
114 if (bytesread == 0) {
116 return APR_EOF;
117 }
118
119 return APR_SUCCESS;
120 }
121}
122
123
124
126{
127 ULONG rc = 0;
129
130 if (!thefile->isopen) {
131 *nbytes = 0;
132 return APR_EBADF;
133 }
134
135 if (thefile->buffered) {
136 char *pos = (char *)buf;
137 int blocksize;
138 int size = *nbytes;
139
141
142 if ( thefile->direction == 0 ) {
143 /* Position file pointer for writing at the offset we are logically reading from */
145 if (offset != thefile->filePtr)
148 thefile->direction = 1;
149 }
150
151 while (rc == 0 && size > 0) {
152 if (thefile->bufpos == thefile->bufsize) /* write buffer is full */
153 /* XXX bug; - rc is double-transformed os->apr below */
155
159 pos += blocksize;
160 size -= blocksize;
161 }
162
164 return APR_FROM_OS_ERROR(rc);
165 } else {
167 FILELOCK all = { 0, 0x7fffffff };
170
171 if (rc == 0) {
173
174 if (rc == 0) {
176 }
177
179 }
180 } else {
182 }
183
184 if (rc) {
185 *nbytes = 0;
186 return APR_FROM_OS_ERROR(rc);
187 }
188
190 return APR_SUCCESS;
191 }
192}
193
194
195
196#ifdef HAVE_WRITEV
197
199{
200 int bytes;
201
202 if (thefile->buffered) {
204 if (rv != APR_SUCCESS) {
205 return rv;
206 }
207 }
208
209 if ((bytes = writev(thefile->filedes, vec, nvec)) < 0) {
210 *nbytes = 0;
211 return errno;
212 }
213 else {
214 *nbytes = bytes;
215 return APR_SUCCESS;
216 }
217}
218#endif
219
220
221
223{
224 ULONG rc;
226
227 if (!thefile->isopen) {
228 return APR_EBADF;
229 }
230
232
233 if (rc) {
234 return APR_FROM_OS_ERROR(rc);
235 }
236
237 return APR_SUCCESS;
238}
239
240
241
243{
244 apr_off_t offset = -1;
246}
247
248
250{
251 ULONG rc;
253
254 if (!thefile->isopen) {
255 return APR_EBADF;
256 }
257
258 bytesread = 1;
260
261 if (rc) {
262 return rc;
263 }
264
265 if (bytesread == 0) {
267 return APR_EOF;
268 }
269
270 return APR_SUCCESS;
271}
272
273
274
276{
278
279 len = strlen(str);
280 return apr_file_write(thefile, str, &len);
281}
282
283
285{
286 if (thefile->buffered) {
287 ULONG written = 0;
288 int rc = 0;
289
290 if (thefile->direction == 1 && thefile->bufpos) {
292 thefile->filePtr += written;
293
294 if (rc == 0)
295 thefile->bufpos = 0;
296 }
297
298 return APR_FROM_OS_ERROR(rc);
299 } else {
300 /* There isn't anything to do if we aren't buffering the output
301 * so just return success.
302 */
303 return APR_SUCCESS;
304 }
305}
306
308{
309 return APR_ENOTIMPL;
310}
311
313{
314 return APR_ENOTIMPL;
315}
316
318{
321 int i;
322
323 for (i = 0; i < len-1; i++) {
324 readlen = 1;
326
327 if (rv != APR_SUCCESS) {
328 break;
329 }
330
331 if (readlen != 1) {
332 rv = APR_EOF;
333 break;
334 }
335
336 if (str[i] == '\n') {
337 i++;
338 break;
339 }
340 }
341 str[i] = 0;
342 if (i > 0) {
343 /* we stored chars; don't report EOF or any other errors;
344 * the app will find out about that on the next call
345 */
346 return APR_SUCCESS;
347 }
348 return rv;
349}
350
351
352
354 const char *format, ...)
355{
356 int cc;
357 va_list ap;
358 char *buf;
359 int len;
360
362 if (buf == NULL) {
363 return 0;
364 }
367 cc = apr_file_puts(buf, fptr);
368 va_end(ap);
369 free(buf);
370 return (cc == APR_SUCCESS) ? len : -1;
371}
372
373
374
376{
377 int rc;
378
379 if (!fd->pipe)
380 return APR_SUCCESS; /* Not a pipe, assume no waiting */
381
383
384 if (rc == ERROR_TIMEOUT)
385 return APR_TIMEUP;
386
387 return APR_FROM_OS_ERROR(rc);
388}
const char apr_size_t len
Definition ap_regex.h:187
#define TRUE
Definition abts.h:38
#define writev
APR File I/O Handling.
APR general purpose library routines.
APR Strings library.
#define HUGE_STRING_LEN
Definition httpd.h:303
const unsigned char * buf
Definition util_md5.h:50
#define APR_EBADF
Definition apr_errno.h:704
#define APR_EOF
Definition apr_errno.h:461
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_TIMEUP
Definition apr_errno.h:450
apr_file_t * fd
apr_redis_t * rc
Definition apr_redis.h:173
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
#define APR_FROM_OS_ERROR(e)
Definition apr_errno.h:1214
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_file_t * thefile
void apr_size_t * nbytes
const struct iovec * vec
apr_seek_where_t apr_off_t * offset
const char * format
const struct iovec apr_size_t nvec
#define APR_FOPEN_APPEND
Definition apr_file_io.h:57
#define APR_CUR
apr_vformatter_buff_t const char va_list ap
Definition apr_lib.h:176
APR_DECLARE_NONSTD(void) apr_terminate(void)
Definition start.c:173
const void apr_size_t bytes
Definition apr_random.h:91
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
apr_status_t apr_file_check_read(apr_file_t *fd)
Definition readwrite.c:375
apr_int32_t flags
apr_interval_time_t timeout
apr_size_t bufpos
apr_off_t filePtr
apr_thread_mutex_t * mutex
apr_off_t dataRead
apr_size_t bufsize
#define str