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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_file_io.h"
18 #include "apr_strings.h"
19 #include "apr_thread_mutex.h"
20 #include "apr_support.h"
22 /* The only case where we don't use wait_for_io_or_timeout is on
23 * pre-BONE BeOS, so this check should be sufficient and simpler */
25 #define USE_WAIT_FOR_IO
28 static apr_status_t file_read_buffered(apr_file_t *thefile, void *buf,
32 char *pos = (char *)buf;
33 apr_uint64_t blocksize;
34 apr_uint64_t size = *nbytes;
36 if (thefile->direction == 1) {
37 rv = apr_file_flush_locked(thefile);
42 thefile->direction = 0;
43 thefile->dataRead = 0;
47 if (thefile->ungetchar != -1) {
48 *pos = (char)thefile->ungetchar;
51 thefile->ungetchar = -1;
53 while (rv == 0 && size > 0) {
54 if (thefile->bufpos >= thefile->dataRead) {
55 int bytesread = read(thefile->filedes, thefile->buffer,
58 thefile->eof_hit = TRUE;
62 else if (bytesread == -1) {
66 thefile->dataRead = bytesread;
67 thefile->filePtr += thefile->dataRead;
71 blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
72 memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
73 thefile->bufpos += blocksize;
78 *nbytes = pos - (char *)buf;
85 APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
88 apr_size_t bytes_read;
95 if (thefile->buffered) {
97 rv = file_read_buffered(thefile, buf, nbytes);
103 if (thefile->ungetchar != -1) {
105 *(char *)buf = (char)thefile->ungetchar;
106 buf = (char *)buf + 1;
108 thefile->ungetchar = -1;
110 *nbytes = bytes_read;
116 rv = read(thefile->filedes, buf, *nbytes);
117 } while (rv == -1 && errno == EINTR);
118 #ifdef USE_WAIT_FOR_IO
120 (errno == EAGAIN || errno == EWOULDBLOCK) &&
121 thefile->timeout != 0) {
122 apr_status_t arv = apr_wait_for_io_or_timeout(thefile, NULL, 1);
123 if (arv != APR_SUCCESS) {
124 *nbytes = bytes_read;
129 rv = read(thefile->filedes, buf, *nbytes);
130 } while (rv == -1 && errno == EINTR);
134 *nbytes = bytes_read;
136 thefile->eof_hit = TRUE;
147 APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t *nbytes)
151 if (thefile->buffered) {
152 char *pos = (char *)buf;
158 if ( thefile->direction == 0 ) {
159 /* Position file pointer for writing at the offset we are
160 * logically reading from
162 apr_int64_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
163 if (offset != thefile->filePtr)
164 lseek(thefile->filedes, offset, SEEK_SET);
165 thefile->bufpos = thefile->dataRead = 0;
166 thefile->direction = 1;
170 while (rv == 0 && size > 0) {
171 if (thefile->bufpos == thefile->bufsize) /* write buffer is full*/
172 rv = apr_file_flush_locked(thefile);
174 blocksize = size > thefile->bufsize - thefile->bufpos ?
175 thefile->bufsize - thefile->bufpos : size;
176 memcpy(thefile->buffer + thefile->bufpos, pos, blocksize);
177 thefile->bufpos += blocksize;
182 file_unlock(thefile);
188 rv = write(thefile->filedes, buf, *nbytes);
189 } while (rv == (apr_size_t)-1 && errno == EINTR);
190 #ifdef USE_WAIT_FOR_IO
191 if (rv == (apr_size_t)-1 &&
192 (errno == EAGAIN || errno == EWOULDBLOCK) &&
193 thefile->timeout != 0) {
194 apr_status_t arv = apr_wait_for_io_or_timeout(thefile, NULL, 0);
195 if (arv != APR_SUCCESS) {
202 rv = write(thefile->filedes, buf, *nbytes);
203 } while (rv == (apr_size_t)-1 && errno == EINTR);
204 if (rv == (apr_size_t)-1 &&
205 (errno == EAGAIN || errno == EWOULDBLOCK)) {
206 *nbytes /= 2; /* yes, we'll loop if kernel lied
207 * and we can't even write 1 byte
217 if (rv == (apr_size_t)-1) {
226 APR_DECLARE(apr_status_t) apr_file_writev(apr_file_t *thefile, const struct iovec *vec,
227 apr_size_t nvec, apr_size_t *nbytes)
233 if (thefile->buffered) {
236 rv = apr_file_flush_locked(thefile);
237 if (rv != APR_SUCCESS) {
238 file_unlock(thefile);
241 if (thefile->direction == 0) {
242 /* Position file pointer for writing at the offset we are
243 * logically reading from
245 apr_int64_t offset = thefile->filePtr - thefile->dataRead +
247 if (offset != thefile->filePtr)
248 lseek(thefile->filedes, offset, SEEK_SET);
249 thefile->bufpos = thefile->dataRead = 0;
252 file_unlock(thefile);
255 if ((bytes = writev(thefile->filedes, vec, nvec)) < 0) {
266 * The problem with trying to output the entire iovec is that we cannot
267 * maintain the behaviour that a real writev would have. If we iterate
268 * over the iovec one at a time, we lose the atomic properties of
269 * writev(). The other option is to combine the entire iovec into one
270 * buffer that we could then send in one call to write(). This is not
271 * reasonable since we do not know how much data an iovec could contain.
273 * The only reasonable option, that maintains the semantics of a real
274 * writev(), is to only write the first iovec. Callers of file_writev()
275 * must deal with partial writes as they normally would. If you want to
276 * ensure an entire iovec is written, use apr_file_writev_full().
279 *nbytes = vec[0].iov_len;
280 return apr_file_write(thefile, vec[0].iov_base, nbytes);
284 APR_DECLARE(apr_status_t) apr_file_putc(char ch, apr_file_t *thefile)
286 apr_size_t nbytes = 1;
288 return apr_file_write(thefile, &ch, &nbytes);
291 APR_DECLARE(apr_status_t) apr_file_ungetc(char ch, apr_file_t *thefile)
293 thefile->ungetchar = (unsigned char)ch;
297 APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile)
299 apr_size_t nbytes = 1;
301 return apr_file_read(thefile, ch, &nbytes);
304 APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile)
306 return apr_file_write_full(thefile, str, strlen(str), NULL);
309 apr_status_t apr_file_flush_locked(apr_file_t *thefile)
311 apr_status_t rv = APR_SUCCESS;
313 if (thefile->direction == 1 && thefile->bufpos) {
314 apr_ssize_t written = 0, ret;
317 ret = write(thefile->filedes, thefile->buffer + written,
318 thefile->bufpos - written);
321 } while (written < thefile->bufpos &&
322 (ret > 0 || (ret == -1 && errno == EINTR)));
326 thefile->filePtr += written;
334 APR_DECLARE(apr_status_t) apr_file_flush(apr_file_t *thefile)
336 apr_status_t rv = APR_SUCCESS;
338 if (thefile->buffered) {
340 rv = apr_file_flush_locked(thefile);
341 file_unlock(thefile);
343 /* There isn't anything to do if we aren't buffering the output
344 * so just return success.
349 APR_DECLARE(apr_status_t) apr_file_sync(apr_file_t *thefile)
351 apr_status_t rv = APR_SUCCESS;
355 if (thefile->buffered) {
356 rv = apr_file_flush_locked(thefile);
358 if (rv != APR_SUCCESS) {
359 file_unlock(thefile);
364 if (fsync(thefile->filedes)) {
365 rv = apr_get_os_error();
368 file_unlock(thefile);
373 APR_DECLARE(apr_status_t) apr_file_datasync(apr_file_t *thefile)
375 apr_status_t rv = APR_SUCCESS;
379 if (thefile->buffered) {
380 rv = apr_file_flush_locked(thefile);
382 if (rv != APR_SUCCESS) {
383 file_unlock(thefile);
388 #ifdef HAVE_FDATASYNC
389 if (fdatasync(thefile->filedes)) {
391 if (fsync(thefile->filedes)) {
393 rv = apr_get_os_error();
396 file_unlock(thefile);
401 APR_DECLARE(apr_status_t) apr_file_gets(char *str, int len, apr_file_t *thefile)
403 apr_status_t rv = APR_SUCCESS; /* get rid of gcc warning */
405 const char *str_start = str;
406 char *final = str + len - 1;
409 /* sort of like fgets(), which returns NULL and stores no bytes
414 /* If we have an underlying buffer, we can be *much* more efficient
415 * and skip over the apr_file_read calls.
417 if (thefile->buffered) {
420 if (thefile->direction == 1) {
421 rv = apr_file_flush_locked(thefile);
423 file_unlock(thefile);
427 thefile->direction = 0;
429 thefile->dataRead = 0;
432 while (str < final) { /* leave room for trailing '\0' */
433 /* Force ungetc leftover to call apr_file_read. */
434 if (thefile->bufpos < thefile->dataRead &&
435 thefile->ungetchar == -1) {
436 *str = thefile->buffer[thefile->bufpos++];
440 rv = file_read_buffered(thefile, str, &nbytes);
441 if (rv != APR_SUCCESS) {
451 file_unlock(thefile);
454 while (str < final) { /* leave room for trailing '\0' */
456 rv = apr_file_read(thefile, str, &nbytes);
457 if (rv != APR_SUCCESS) {
468 /* We must store a terminating '\0' if we've stored any chars. We can
469 * get away with storing it if we hit an error first.
472 if (str > str_start) {
473 /* we stored chars; don't report EOF or any other errors;
474 * the app will find out about that on the next call
481 struct apr_file_printf_data {
482 apr_vformatter_buff_t vbuff;
487 static int file_printf_flush(apr_vformatter_buff_t *buff)
489 struct apr_file_printf_data *data = (struct apr_file_printf_data *)buff;
491 if (apr_file_write_full(data->fptr, data->buf,
492 data->vbuff.curpos - data->buf, NULL)) {
496 data->vbuff.curpos = data->buf;
500 APR_DECLARE_NONSTD(int) apr_file_printf(apr_file_t *fptr,
501 const char *format, ...)
503 struct apr_file_printf_data data;
507 /* don't really need a HUGE_STRING_LEN anymore */
508 data.buf = malloc(HUGE_STRING_LEN);
509 if (data.buf == NULL) {
512 data.vbuff.curpos = data.buf;
513 data.vbuff.endpos = data.buf + HUGE_STRING_LEN;
515 va_start(ap, format);
516 count = apr_vformatter(file_printf_flush,
517 (apr_vformatter_buff_t *)&data, format, ap);
518 /* apr_vformatter does not call flush for the last bits */
519 if (count >= 0) file_printf_flush((apr_vformatter_buff_t *)&data);