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#ifndef FILE_IO_H
18#define FILE_IO_H
19
20#include "apr.h"
21#include "apr_private.h"
22#include "apr_general.h"
23#include "apr_tables.h"
24#include "apr_file_io.h"
25#include "apr_file_info.h"
26#include "apr_errno.h"
27#include "apr_lib.h"
28#include "apr_thread_mutex.h"
29#ifndef WAITIO_USES_POLL
30#include "apr_poll.h"
31#endif
32
33/* System headers the file I/O library needs */
34#if APR_HAVE_FCNTL_H
35#include <fcntl.h>
36#endif
37#if APR_HAVE_SYS_TYPES_H
38#include <sys/types.h>
39#endif
40#if APR_HAVE_ERRNO_H
41#include <errno.h>
42#endif
43#if APR_HAVE_STRING_H
44#include <string.h>
45#endif
46#if APR_HAVE_STRINGS_H
47#include <strings.h>
48#endif
49#if APR_HAVE_DIRENT_H
50#include <dirent.h>
51#endif
52#ifdef HAVE_SYS_STAT_H
53#include <sys/stat.h>
54#endif
55#if APR_HAVE_UNISTD_H
56#include <unistd.h>
57#endif
58#if APR_HAVE_STDIO_H
59#include <stdio.h>
60#endif
61#if APR_HAVE_STDLIB_H
62#include <stdlib.h>
63#endif
64#if APR_HAVE_SYS_UIO_H
65#include <sys/uio.h>
66#endif
67#if APR_HAVE_SYS_TIME_H
68#include <sys/time.h>
69#endif
70#ifdef BEOS
71#include <kernel/OS.h>
72#endif
73/* Hunting down DEV_BSIZE if not from dirent.h, sys/stat.h etc */
74#ifdef HAVE_SYS_PARAM_H
75#include <sys/param.h>
76#endif
77
78#if BEOS_BONE
79# ifndef BONE7
80  /* prior to BONE/7 fd_set & select were defined in sys/socket.h */
81#  include <sys/socket.h>
82# else
83  /* Be moved the fd_set stuff and also the FIONBIO definition... */
84#  include <sys/ioctl.h>
85# endif
86#endif
87/* End System headers */
88
89#define APR_FILE_DEFAULT_BUFSIZE 4096
90/* For backwards-compat */
91#define APR_FILE_BUFSIZE  APR_FILE_DEFAULT_BUFSIZE
92
93struct apr_file_t {
94    apr_pool_t *pool;
95    int filedes;
96    char *fname;
97    apr_int32_t flags;
98    int eof_hit;
99    int is_pipe;
100    apr_interval_time_t timeout;
101    int buffered;
102    enum {BLK_UNKNOWN, BLK_OFF, BLK_ON } blocking;
103    int ungetchar;    /* Last char provided by an unget op. (-1 = no char)*/
104#ifndef WAITIO_USES_POLL
105    /* if there is a timeout set, then this pollset is used */
106    apr_pollset_t *pollset;
107#endif
108    /* Stuff for buffered mode */
109    char *buffer;
110    apr_size_t bufpos;        /* Read/Write position in buffer */
111    apr_size_t bufsize;       /* The size of the buffer */
112    unsigned long dataRead;   /* amount of valid data read into buffer */
113    int direction;            /* buffer being used for 0 = read, 1 = write */
114    apr_off_t filePtr;        /* position in file of handle */
115#if APR_HAS_THREADS
116    struct apr_thread_mutex_t *thlock;
117#endif
118};
119
120#if APR_HAS_THREADS
121#define file_lock(f)   do { \
122                           if ((f)->thlock) \
123                               apr_thread_mutex_lock((f)->thlock); \
124                       } while (0)
125#define file_unlock(f) do { \
126                           if ((f)->thlock) \
127                               apr_thread_mutex_unlock((f)->thlock); \
128                       } while (0)
129#else
130#define file_lock(f)   do {} while (0)
131#define file_unlock(f) do {} while (0)
132#endif
133
134#if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
135#define stat(f,b) stat64(f,b)
136#define lstat(f,b) lstat64(f,b)
137#define fstat(f,b) fstat64(f,b)
138#define lseek(f,o,w) lseek64(f,o,w)
139#define ftruncate(f,l) ftruncate64(f,l)
140typedef struct stat64 struct_stat;
141#else
142typedef struct stat struct_stat;
143#endif
144
145/* readdir64_r is only used in specific cases: */
146#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) \
147    && !defined(READDIR_IS_THREAD_SAFE) && defined(HAVE_READDIR64_R)
148#define APR_USE_READDIR64_R
149#endif
150
151struct apr_dir_t {
152    apr_pool_t *pool;
153    char *dirname;
154    DIR *dirstruct;
155#ifdef APR_USE_READDIR64_R
156    struct dirent64 *entry;
157#else
158    struct dirent *entry;
159#endif
160};
161
162apr_status_t apr_unix_file_cleanup(void *);
163apr_status_t apr_unix_child_file_cleanup(void *);
164
165mode_t apr_unix_perms2mode(apr_fileperms_t perms);
166apr_fileperms_t apr_unix_mode2perms(mode_t mode);
167
168apr_status_t apr_file_flush_locked(apr_file_t *thefile);
169apr_status_t apr_file_info_get_locked(apr_finfo_t *finfo, apr_int32_t wanted,
170                                      apr_file_t *thefile);
171
172
173#endif  /* ! FILE_IO_H */
174
175