Apache Portable Runtime
apr_file_io.h
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 #ifndef APR_FILE_IO_H
18 #define APR_FILE_IO_H
19 
20 /**
21  * @file apr_file_io.h
22  * @brief APR File I/O Handling
23  */
24 
25 #include "apr.h"
26 #include "apr_pools.h"
27 #include "apr_time.h"
28 #include "apr_errno.h"
29 #include "apr_file_info.h"
30 #include "apr_inherit.h"
31 
32 #define APR_WANT_STDIO /**< for SEEK_* */
33 #define APR_WANT_IOVEC /**< for apr_file_writev */
34 #include "apr_want.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 
40 /**
41  * @defgroup apr_file_io File I/O Handling Functions
42  * @ingroup APR
43  * @{
44  */
45 
46 /**
47  * @defgroup apr_file_open_flags File Open Flags/Routines
48  * @{
49  */
50 
51 /* Note to implementors: Values in the range 0x00100000--0x80000000
52  are reserved for platform-specific values. */
53 
54 #define APR_FOPEN_READ 0x00001 /**< Open the file for reading */
55 #define APR_FOPEN_WRITE 0x00002 /**< Open the file for writing */
56 #define APR_FOPEN_CREATE 0x00004 /**< Create the file if not there */
57 #define APR_FOPEN_APPEND 0x00008 /**< Append to the end of the file */
58 #define APR_FOPEN_TRUNCATE 0x00010 /**< Open the file and truncate
59  to 0 length */
60 #define APR_FOPEN_BINARY 0x00020 /**< Open the file in binary mode
61  (This flag is ignored on UNIX
62  because it has no meaning)*/
63 #define APR_FOPEN_EXCL 0x00040 /**< Open should fail if #APR_FOPEN_CREATE
64  and file exists. */
65 #define APR_FOPEN_BUFFERED 0x00080 /**< Open the file for buffered I/O */
66 #define APR_FOPEN_DELONCLOSE 0x00100 /**< Delete the file after close */
67 #define APR_FOPEN_XTHREAD 0x00200 /**< Platform dependent tag to open
68  the file for use across multiple
69  threads */
70 #define APR_FOPEN_SHARELOCK 0x00400 /**< Platform dependent support for
71  higher level locked read/write
72  access to support writes across
73  process/machines */
74 #define APR_FOPEN_NOCLEANUP 0x00800 /**< Do not register a cleanup
75  when the file is opened. The
76  apr_os_file_t handle in apr_file_t
77  will not be closed when the pool
78  is destroyed. */
79 #define APR_FOPEN_SENDFILE_ENABLED 0x01000 /**< Advisory flag that this
80  file should support
81  apr_socket_sendfile operation */
82 #define APR_FOPEN_LARGEFILE 0x04000 /**< Platform dependent flag to enable
83  * large file support, see WARNING below
84  */
85 
86 #define APR_FOPEN_SPARSE 0x08000 /**< Platform dependent flag to enable
87  * sparse file support, see WARNING below
88  */
89 
90 #define APR_FOPEN_ROTATING 0x10000 /**< Do file file rotation checking */
91 
92 #define APR_FOPEN_MANUAL_ROTATE 0x20000 /**< Enable Manual rotation */
93 
94 #define APR_FOPEN_NONBLOCK 0x40000 /**< Platform dependent flag to enable
95  * non blocking file io */
96 
97 
98 
99 /* backcompat */
100 #define APR_READ APR_FOPEN_READ /**< @deprecated @see APR_FOPEN_READ */
101 #define APR_WRITE APR_FOPEN_WRITE /**< @deprecated @see APR_FOPEN_WRITE */
102 #define APR_CREATE APR_FOPEN_CREATE /**< @deprecated @see APR_FOPEN_CREATE */
103 #define APR_APPEND APR_FOPEN_APPEND /**< @deprecated @see APR_FOPEN_APPEND */
104 #define APR_TRUNCATE APR_FOPEN_TRUNCATE /**< @deprecated @see APR_FOPEN_TRUNCATE */
105 #define APR_BINARY APR_FOPEN_BINARY /**< @deprecated @see APR_FOPEN_BINARY */
106 #define APR_EXCL APR_FOPEN_EXCL /**< @deprecated @see APR_FOPEN_EXCL */
107 #define APR_BUFFERED APR_FOPEN_BUFFERED /**< @deprecated @see APR_FOPEN_BUFFERED */
108 #define APR_DELONCLOSE APR_FOPEN_DELONCLOSE /**< @deprecated @see APR_FOPEN_DELONCLOSE */
109 #define APR_XTHREAD APR_FOPEN_XTHREAD /**< @deprecated @see APR_FOPEN_XTHREAD */
110 #define APR_SHARELOCK APR_FOPEN_SHARELOCK /**< @deprecated @see APR_FOPEN_SHARELOCK */
111 #define APR_FILE_NOCLEANUP APR_FOPEN_NOCLEANUP /**< @deprecated @see APR_FOPEN_NOCLEANUP */
112 #define APR_SENDFILE_ENABLED APR_FOPEN_SENDFILE_ENABLED /**< @deprecated @see APR_FOPEN_SENDFILE_ENABLED */
113 #define APR_LARGEFILE APR_FOPEN_LARGEFILE /**< @deprecated @see APR_FOPEN_LARGEFILE */
114 
115 /** @def APR_FOPEN_LARGEFILE
116  * @warning APR_FOPEN_LARGEFILE flag only has effect on some
117  * platforms where sizeof(apr_off_t) == 4. Where implemented, it
118  * allows opening and writing to a file which exceeds the size which
119  * can be represented by apr_off_t (2 gigabytes). When a file's size
120  * does exceed 2Gb, apr_file_info_get() will fail with an error on the
121  * descriptor, likewise apr_stat()/apr_lstat() will fail on the
122  * filename. apr_dir_read() will fail with #APR_INCOMPLETE on a
123  * directory entry for a large file depending on the particular
124  * APR_FINFO_* flags. Generally, it is not recommended to use this
125  * flag.
126  *
127  * @def APR_FOPEN_SPARSE
128  * @warning APR_FOPEN_SPARSE may, depending on platform, convert a
129  * normal file to a sparse file. Some applications may be unable
130  * to decipher a sparse file, so it's critical that the sparse file
131  * flag should only be used for files accessed only by APR or other
132  * applications known to be able to decipher them. APR does not
133  * guarantee that it will compress the file into sparse segments
134  * if it was previously created and written without the sparse flag.
135  * On platforms which do not understand, or on file systems which
136  * cannot handle sparse files, the flag is ignored by apr_file_open().
137  *
138  * @def APR_FOPEN_NONBLOCK
139  * @warning APR_FOPEN_NONBLOCK is not implemented on all platforms.
140  * Callers should be prepared for it to fail with #APR_ENOTIMPL.
141  */
142 
143 /** @} */
144 
145 /**
146  * @defgroup apr_file_seek_flags File Seek Flags
147  * @{
148  */
149 
150 /* flags for apr_file_seek */
151 /** Set the file position */
152 #define APR_SET SEEK_SET
153 /** Current */
154 #define APR_CUR SEEK_CUR
155 /** Go to end of file */
156 #define APR_END SEEK_END
157 /** @} */
158 
159 /**
160  * @defgroup apr_file_attrs_set_flags File Attribute Flags
161  * @{
162  */
163 
164 /* flags for apr_file_attrs_set */
165 #define APR_FILE_ATTR_READONLY 0x01 /**< File is read-only */
166 #define APR_FILE_ATTR_EXECUTABLE 0x02 /**< File is executable */
167 #define APR_FILE_ATTR_HIDDEN 0x04 /**< File is hidden */
168 /** @} */
169 
170 /**
171  * @defgroup apr_file_writev{_full} max iovec size
172  * @{
173  */
174 #if defined(DOXYGEN)
175 #define APR_MAX_IOVEC_SIZE 1024 /**< System dependent maximum
176  size of an iovec array */
177 #elif defined(IOV_MAX)
178 #define APR_MAX_IOVEC_SIZE IOV_MAX
179 #elif defined(MAX_IOVEC)
180 #define APR_MAX_IOVEC_SIZE MAX_IOVEC
181 #else
182 #define APR_MAX_IOVEC_SIZE 1024
183 #endif
184 /** @} */
186 /** File attributes */
187 typedef apr_uint32_t apr_fileattrs_t;
188 
189 /** Type to pass as whence argument to apr_file_seek. */
190 typedef int apr_seek_where_t;
191 
192 /**
193  * Structure for referencing files.
194  */
195 typedef struct apr_file_t apr_file_t;
196 
197 /* File lock types/flags */
198 /**
199  * @defgroup apr_file_lock_types File Lock Types
200  * @{
201  */
202 
203 #define APR_FLOCK_SHARED 1 /**< Shared lock. More than one process
204  or thread can hold a shared lock
205  at any given time. Essentially,
206  this is a "read lock", preventing
207  writers from establishing an
208  exclusive lock. */
209 #define APR_FLOCK_EXCLUSIVE 2 /**< Exclusive lock. Only one process
210  may hold an exclusive lock at any
211  given time. This is analogous to
212  a "write lock". */
213 
214 #define APR_FLOCK_TYPEMASK 0x000F /**< mask to extract lock type */
215 #define APR_FLOCK_NONBLOCK 0x0010 /**< do not block while acquiring the
216  file lock */
217 /** @} */
218 
219 /**
220  * Open the specified file.
221  * @param newf The opened file descriptor.
222  * @param fname The full path to the file (using / on all systems)
223  * @param flag Or'ed value of:
224  * @li #APR_FOPEN_READ open for reading
225  * @li #APR_FOPEN_WRITE open for writing
226  * @li #APR_FOPEN_CREATE create the file if not there
227  * @li #APR_FOPEN_APPEND file ptr is set to end prior to all writes
228  * @li #APR_FOPEN_TRUNCATE set length to zero if file exists
229  * @li #APR_FOPEN_BINARY not a text file
230  * @li #APR_FOPEN_BUFFERED buffer the data. Default is non-buffered
231  * @li #APR_FOPEN_EXCL return error if #APR_FOPEN_CREATE and file exists
232  * @li #APR_FOPEN_DELONCLOSE delete the file after closing
233  * @li #APR_FOPEN_XTHREAD Platform dependent tag to open the file
234  * for use across multiple threads
235  * @li #APR_FOPEN_SHARELOCK Platform dependent support for higher
236  * level locked read/write access to support
237  * writes across process/machines
238  * @li #APR_FOPEN_NOCLEANUP Do not register a cleanup with the pool
239  * passed in on the @a pool argument (see below)
240  * @li #APR_FOPEN_SENDFILE_ENABLED Open with appropriate platform semantics
241  * for sendfile operations. Advisory only,
242  * apr_socket_sendfile does not check this flag
243  * @li #APR_FOPEN_LARGEFILE Platform dependent flag to enable large file
244  * support, see WARNING below
245  * @li #APR_FOPEN_SPARSE Platform dependent flag to enable sparse file
246  * support, see WARNING below
247  * @li #APR_FOPEN_ROTATING Do file file rotation checking
248  * @li #APR_FOPEN_MANUAL_ROTATE Enable Manual rotation
249  * @li #APR_FOPEN_NONBLOCK Platform dependent flag to enable
250  * non blocking file io
251  * @param perm Access permissions for file.
252  * @param pool The pool to use.
253  * @remark If perm is #APR_FPROT_OS_DEFAULT and the file is being created,
254  * appropriate default permissions will be used.
255  * @remark By default, the returned file descriptor will not be
256  * inherited by child processes created by apr_proc_create(). This
257  * can be changed using apr_file_inherit_set().
258  */
259 APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **newf, const char *fname,
260  apr_int32_t flag, apr_fileperms_t perm,
261  apr_pool_t *pool);
262 
263 /**
264  * Close the specified file.
265  * @param file The file descriptor to close.
266  */
268 
269 /**
270  * Delete the specified file.
271  * @param path The full path to the file (using / on all systems)
272  * @param pool The pool to use.
273  * @remark If the file is open, it won't be removed until all
274  * instances are closed.
275  */
276 APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool);
277 
278 /**
279  * Rename the specified file.
280  * @param from_path The full path to the original file (using / on all systems)
281  * @param to_path The full path to the new file (using / on all systems)
282  * @param pool The pool to use.
283  * @warning If a file exists at the new location, then it will be
284  * overwritten. Moving files or directories across devices may not be
285  * possible.
286  */
287 APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path,
288  const char *to_path,
289  apr_pool_t *pool);
290 
291 /**
292  * Create a hard link to the specified file.
293  * @param from_path The full path to the original file (using / on all systems)
294  * @param to_path The full path to the new file (using / on all systems)
295  * @remark Both files must reside on the same device.
296  */
297 APR_DECLARE(apr_status_t) apr_file_link(const char *from_path,
298  const char *to_path);
299 
300 /**
301  * Copy the specified file to another file.
302  * @param from_path The full path to the original file (using / on all systems)
303  * @param to_path The full path to the new file (using / on all systems)
304  * @param perms Access permissions for the new file if it is created.
305  * In place of the usual or'd combination of file permissions, the
306  * value #APR_FPROT_FILE_SOURCE_PERMS may be given, in which case the source
307  * file's permissions are copied.
308  * @param pool The pool to use.
309  * @remark The new file does not need to exist, it will be created if required.
310  * @warning If the new file already exists, its contents will be overwritten.
311  */
312 APR_DECLARE(apr_status_t) apr_file_copy(const char *from_path,
313  const char *to_path,
314  apr_fileperms_t perms,
315  apr_pool_t *pool);
316 
317 /**
318  * Append the specified file to another file.
319  * @param from_path The full path to the source file (use / on all systems)
320  * @param to_path The full path to the destination file (use / on all systems)
321  * @param perms Access permissions for the destination file if it is created.
322  * In place of the usual or'd combination of file permissions, the
323  * value #APR_FPROT_FILE_SOURCE_PERMS may be given, in which case the source
324  * file's permissions are copied.
325  * @param pool The pool to use.
326  * @remark The new file does not need to exist, it will be created if required.
327  */
328 APR_DECLARE(apr_status_t) apr_file_append(const char *from_path,
329  const char *to_path,
330  apr_fileperms_t perms,
331  apr_pool_t *pool);
332 
333 /**
334  * Are we at the end of the file
335  * @param fptr The apr file we are testing.
336  * @remark Returns #APR_EOF if we are at the end of file, #APR_SUCCESS otherwise.
337  */
339 
340 /**
341  * Open standard error as an apr file pointer.
342  * @param thefile The apr file to use as stderr.
343  * @param pool The pool to allocate the file out of.
344  *
345  * @remark The only reason that the apr_file_open_std* functions exist
346  * is that you may not always have a stderr/out/in on Windows. This
347  * is generally a problem with newer versions of Windows and services.
348  *
349  * @remark The other problem is that the C library functions generally work
350  * differently on Windows and Unix. So, by using apr_file_open_std*
351  * functions, you can get a handle to an APR struct that works with
352  * the APR functions which are supposed to work identically on all
353  * platforms.
354  */
356  apr_pool_t *pool);
357 
358 /**
359  * open standard output as an apr file pointer.
360  * @param thefile The apr file to use as stdout.
361  * @param pool The pool to allocate the file out of.
362  *
363  * @remark See remarks for apr_file_open_stderr().
364  */
366  apr_pool_t *pool);
367 
368 /**
369  * open standard input as an apr file pointer.
370  * @param thefile The apr file to use as stdin.
371  * @param pool The pool to allocate the file out of.
372  *
373  * @remark See remarks for apr_file_open_stderr().
374  */
376  apr_pool_t *pool);
377 
378 /**
379  * open standard error as an apr file pointer, with flags.
380  * @param thefile The apr file to use as stderr.
381  * @param flags The flags to open the file with. Only the
382  * @li #APR_FOPEN_EXCL
383  * @li #APR_FOPEN_BUFFERED
384  * @li #APR_FOPEN_XTHREAD
385  * @li #APR_FOPEN_SHARELOCK
386  * @li #APR_FOPEN_SENDFILE_ENABLED
387  * @li #APR_FOPEN_LARGEFILE
388  *
389  * flags should be used. The #APR_FOPEN_WRITE flag will
390  * be set unconditionally.
391  * @param pool The pool to allocate the file out of.
392  *
393  * @remark See remarks for apr_file_open_stderr().
394  */
396  apr_int32_t flags,
397  apr_pool_t *pool);
398 
399 /**
400  * open standard output as an apr file pointer, with flags.
401  * @param thefile The apr file to use as stdout.
402  * @param flags The flags to open the file with. Only the
403  * @li #APR_FOPEN_EXCL
404  * @li #APR_FOPEN_BUFFERED
405  * @li #APR_FOPEN_XTHREAD
406  * @li #APR_FOPEN_SHARELOCK
407  * @li #APR_FOPEN_SENDFILE_ENABLED
408  * @li #APR_FOPEN_LARGEFILE
409  *
410  * flags should be used. The #APR_FOPEN_WRITE flag will
411  * be set unconditionally.
412  * @param pool The pool to allocate the file out of.
413  *
414  * @remark See remarks for apr_file_open_stderr().
415  */
417  apr_int32_t flags,
418  apr_pool_t *pool);
419 
420 /**
421  * open standard input as an apr file pointer, with flags.
422  * @param thefile The apr file to use as stdin.
423  * @param flags The flags to open the file with. Only the
424  * @li #APR_FOPEN_EXCL
425  * @li #APR_FOPEN_BUFFERED
426  * @li #APR_FOPEN_XTHREAD
427  * @li #APR_FOPEN_SHARELOCK
428  * @li #APR_FOPEN_SENDFILE_ENABLED
429  * @li #APR_FOPEN_LARGEFILE
430  *
431  * flags should be used. The #APR_FOPEN_WRITE flag will
432  * be set unconditionally.
433  * @param pool The pool to allocate the file out of.
434  *
435  * @remark See remarks for apr_file_open_stderr().
436  */
438  apr_int32_t flags,
439  apr_pool_t *pool);
440 
441 /**
442  * Read data from the specified file.
443  * @param thefile The file descriptor to read from.
444  * @param buf The buffer to store the data to.
445  * @param nbytes On entry, the number of bytes to read; on exit, the number
446  * of bytes read.
447  *
448  * @remark apr_file_read() will read up to the specified number of
449  * bytes, but never more. If there isn't enough data to fill that
450  * number of bytes, all of the available data is read. The third
451  * argument is modified to reflect the number of bytes read. If a
452  * char was put back into the stream via ungetc, it will be the first
453  * character returned.
454  *
455  * @remark It is not possible for both bytes to be read and an #APR_EOF
456  * or other error to be returned. #APR_EINTR is never returned.
457  */
458 APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf,
459  apr_size_t *nbytes);
460 
461 /**
462  * Write data to the specified file.
463  * @param thefile The file descriptor to write to.
464  * @param buf The buffer which contains the data.
465  * @param nbytes On entry, the number of bytes to write; on exit, the number
466  * of bytes written.
467  *
468  * @remark apr_file_write() will write up to the specified number of
469  * bytes, but never more. If the OS cannot write that many bytes, it
470  * will write as many as it can. The third argument is modified to
471  * reflect the * number of bytes written.
472  *
473  * @remark It is possible for both bytes to be written and an error to
474  * be returned. #APR_EINTR is never returned.
475  */
476 APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf,
477  apr_size_t *nbytes);
478 
479 /**
480  * Write data from iovec array to the specified file.
481  * @param thefile The file descriptor to write to.
482  * @param vec The array from which to get the data to write to the file.
483  * @param nvec The number of elements in the struct iovec array. This must
484  * be smaller than #APR_MAX_IOVEC_SIZE. If it isn't, the function
485  * will fail with #APR_EINVAL.
486  * @param nbytes The number of bytes written.
487  *
488  * @remark It is possible for both bytes to be written and an error to
489  * be returned. #APR_EINTR is never returned.
490  *
491  * @remark apr_file_writev() is available even if the underlying
492  * operating system doesn't provide writev().
493  */
495  const struct iovec *vec,
496  apr_size_t nvec, apr_size_t *nbytes);
497 
498 /**
499  * Read data from the specified file, ensuring that the buffer is filled
500  * before returning.
501  * @param thefile The file descriptor to read from.
502  * @param buf The buffer to store the data to.
503  * @param nbytes The number of bytes to read.
504  * @param bytes_read If non-NULL, this will contain the number of bytes read.
505  *
506  * @remark apr_file_read_full() will read up to the specified number of
507  * bytes, but never more. If there isn't enough data to fill that
508  * number of bytes, then the process/thread will block until it is
509  * available or EOF is reached. If a char was put back into the
510  * stream via ungetc, it will be the first character returned.
511  *
512  * @remark It is possible for both bytes to be read and an error to be
513  * returned. And if *bytes_read is less than nbytes, an accompanying
514  * error is _always_ returned.
515  *
516  * @remark #APR_EINTR is never returned.
517  */
519  apr_size_t nbytes,
520  apr_size_t *bytes_read);
521 
522 /**
523  * Write data to the specified file, ensuring that all of the data is
524  * written before returning.
525  * @param thefile The file descriptor to write to.
526  * @param buf The buffer which contains the data.
527  * @param nbytes The number of bytes to write.
528  * @param bytes_written If non-NULL, set to the number of bytes written.
529  *
530  * @remark apr_file_write_full() will write up to the specified number of
531  * bytes, but never more. If the OS cannot write that many bytes, the
532  * process/thread will block until they can be written. Exceptional
533  * error such as "out of space" or "pipe closed" will terminate with
534  * an error.
535  *
536  * @remark It is possible for both bytes to be written and an error to
537  * be returned. And if *bytes_written is less than nbytes, an
538  * accompanying error is _always_ returned.
539  *
540  * @remark #APR_EINTR is never returned.
541  */
543  const void *buf,
544  apr_size_t nbytes,
545  apr_size_t *bytes_written);
546 
547 
548 /**
549  * Write data from iovec array to the specified file, ensuring that all of the
550  * data is written before returning.
551  * @param thefile The file descriptor to write to.
552  * @param vec The array from which to get the data to write to the file.
553  * @param nvec The number of elements in the struct iovec array. This must
554  * be smaller than #APR_MAX_IOVEC_SIZE. If it isn't, the function
555  * will fail with #APR_EINVAL.
556  * @param nbytes The number of bytes written.
557  *
558  * @remark apr_file_writev_full() is available even if the underlying
559  * operating system doesn't provide writev().
560  */
562  const struct iovec *vec,
563  apr_size_t nvec,
564  apr_size_t *nbytes);
565 /**
566  * Write a character into the specified file.
567  * @param ch The character to write.
568  * @param thefile The file descriptor to write to
569  */
571 
572 /**
573  * Read a character from the specified file.
574  * @param ch The character to read into
575  * @param thefile The file descriptor to read from
576  */
577 APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile);
578 
579 /**
580  * Put a character back onto a specified stream.
581  * @param ch The character to write.
582  * @param thefile The file descriptor to write to
583  */
585 
586 /**
587  * Read a line from the specified file
588  * @param str The buffer to store the string in.
589  * @param len The length of the string
590  * @param thefile The file descriptor to read from
591  * @remark The buffer will be NUL-terminated if any characters are stored.
592  * The newline at the end of the line will not be stripped.
593  */
594 APR_DECLARE(apr_status_t) apr_file_gets(char *str, int len,
595  apr_file_t *thefile);
596 
597 /**
598  * Write the string into the specified file.
599  * @param str The string to write.
600  * @param thefile The file descriptor to write to
601  */
602 APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile);
603 
604 /**
605  * Wait for a pipe to be ready for input or output
606  * @param thepipe the pipe to wait on
607  * @param direction whether to wait for reading or writing to be ready
608  * Can be either #APR_WAIT_READ or #APR_WAIT_WRITE
609  * @remark Will time out if thepipe has a time out set for it
610  */
612  apr_wait_type_t direction);
613 
614 /**
615  * Flush the file's buffer.
616  * @param thefile The file descriptor to flush
617  */
619 
620 /**
621  * Transfer all file modified data and metadata to disk.
622  * @param thefile The file descriptor to sync
623  */
625 
626 /**
627  * Transfer all file modified data to disk.
628  * @param thefile The file descriptor to sync
629  */
631 
632 /**
633  * Duplicate the specified file descriptor.
634  * @param new_file The structure to duplicate into.
635  * @param old_file The file to duplicate.
636  * @param p The pool to use for the new file.
637  * @remark *new_file must point to a valid apr_file_t, or point to NULL.
638  */
640  apr_file_t *old_file,
641  apr_pool_t *p);
642 
643 /**
644  * Duplicate the specified file descriptor and close the original
645  * @param new_file The old file that is to be closed and reused
646  * @param old_file The file to duplicate
647  * @param p The pool to use for the new file
648  *
649  * @remark new_file MUST point at a valid apr_file_t. It cannot be NULL.
650  */
652  apr_file_t *old_file,
653  apr_pool_t *p);
654 
655 /**
656  * Move the specified file descriptor to a new pool
657  * @param new_file Pointer in which to return the new apr_file_t
658  * @param old_file The file to move
659  * @param p The pool to which the descriptor is to be moved
660  * @remark Unlike apr_file_dup2(), this function doesn't do an
661  * OS dup() operation on the underlying descriptor; it just
662  * moves the descriptor's apr_file_t wrapper to a new pool.
663  * @remark The new pool need not be an ancestor of old_file's pool.
664  * @remark After calling this function, old_file may not be used
665  */
667  apr_file_t *old_file,
668  apr_pool_t *p);
669 
670 /**
671  * Give the specified apr file handle a new buffer
672  * @param thefile The file handle that is to be modified
673  * @param buffer The buffer
674  * @param bufsize The size of the buffer
675  * @remark It is possible to add a buffer to previously unbuffered
676  * file handles, the #APR_FOPEN_BUFFERED flag will be added to
677  * the file handle's flags. Likewise, with buffer=NULL and
678  * bufsize=0 arguments it is possible to make a previously
679  * buffered file handle unbuffered.
680  */
682  char * buffer,
683  apr_size_t bufsize);
684 
685 /**
686  * Get the size of any buffer for the specified apr file handle
687  * @param thefile The file handle
688  */
689 APR_DECLARE(apr_size_t) apr_file_buffer_size_get(apr_file_t *thefile);
690 
691 /**
692  * Move the read/write file offset to a specified byte within a file.
693  * @param thefile The file descriptor
694  * @param where How to move the pointer, one of:
695  * @li #APR_SET -- set the offset to offset
696  * @li #APR_CUR -- add the offset to the current position
697  * @li #APR_END -- add the offset to the current file size
698  * @param offset The offset to move the pointer to.
699  * @remark The third argument is modified to be the offset the pointer
700  was actually moved to.
701  */
703  apr_seek_where_t where,
704  apr_off_t *offset);
705 
706 /**
707  * Create an anonymous pipe.
708  * @param in The newly created pipe's file for reading.
709  * @param out The newly created pipe's file for writing.
710  * @param pool The pool to operate on.
711  * @remark By default, the returned file descriptors will be inherited
712  * by child processes created using apr_proc_create(). This can be
713  * changed using apr_file_inherit_unset().
714  * @bug Some platforms cannot toggle between blocking and nonblocking,
715  * and when passing a pipe as a standard handle to an application which
716  * does not expect it, a non-blocking stream will fluxor the client app.
717  * @deprecated @see apr_file_pipe_create_pools()
718  */
720  apr_file_t **out,
721  apr_pool_t *pool);
722 
723 /**
724  * Create an anonymous pipe which portably supports async timeout options.
725  * @param in The newly created pipe's file for reading.
726  * @param out The newly created pipe's file for writing.
727  * @param blocking one of these values defined in apr_thread_proc.h;
728  * @li #APR_FULL_BLOCK
729  * @li #APR_READ_BLOCK
730  * @li #APR_WRITE_BLOCK
731  * @li #APR_FULL_NONBLOCK
732  * @param pool The pool to operate on.
733  * @remark By default, the returned file descriptors will be inherited
734  * by child processes created using apr_proc_create(). This can be
735  * changed using apr_file_inherit_unset().
736  * @remark Some platforms cannot toggle between blocking and nonblocking,
737  * and when passing a pipe as a standard handle to an application which
738  * does not expect it, a non-blocking stream will fluxor the client app.
739  * Use this function rather than apr_file_pipe_create() to create pipes
740  * where one or both ends require non-blocking semantics.
741  * @deprecated @see apr_file_pipe_create_pools()
742  */
744  apr_file_t **out,
745  apr_int32_t blocking,
746  apr_pool_t *pool);
747 
748 /**
749  * Create an anonymous pipe which portably supports async timeout options,
750  * placing each side of the pipe in a different pool.
751  * @param in The newly created pipe's file for reading.
752  * @param out The newly created pipe's file for writing.
753  * @param blocking one of these values defined in apr_thread_proc.h;
754  * @li #APR_FULL_BLOCK
755  * @li #APR_READ_BLOCK
756  * @li #APR_WRITE_BLOCK
757  * @li #APR_FULL_NONBLOCK
758  * @param pool_in The pool for the reading pipe.
759  * @param pool_out The pool for the writing pipe.
760  * @remark By default, the returned file descriptors will be inherited
761  * by child processes created using apr_proc_create(). This can be
762  * changed using apr_file_inherit_unset().
763  * @remark Some platforms cannot toggle between blocking and nonblocking,
764  * and when passing a pipe as a standard handle to an application which
765  * does not expect it, a non-blocking stream will fluxor the client app.
766  * Use this function rather than apr_file_pipe_create() to create pipes
767  * where one or both ends require non-blocking semantics.
768  */
770  apr_file_t **out,
771  apr_int32_t blocking,
772  apr_pool_t *pool_in,
773  apr_pool_t *pool_out);
774 
775 /**
776  * Create a named pipe.
777  * @param filename The filename of the named pipe
778  * @param perm The permissions for the newly created pipe.
779  * @param pool The pool to operate on.
780  */
781 APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
782  apr_fileperms_t perm,
783  apr_pool_t *pool);
784 
785 /**
786  * Get the timeout value for a pipe or manipulate the blocking state.
787  * @param thepipe The pipe we are getting a timeout for.
788  * @param timeout The current timeout value in microseconds.
789  */
791  apr_interval_time_t *timeout);
792 
793 /**
794  * Set the timeout value for a pipe or manipulate the blocking state.
795  * @param thepipe The pipe we are setting a timeout on.
796  * @param timeout The timeout value in microseconds. Values < 0 mean wait
797  * forever, 0 means do not wait at all.
798  */
800  apr_interval_time_t timeout);
801 
802 /** file (un)locking functions. */
803 
804 /**
805  * Establish a lock on the specified, open file. The lock may be advisory
806  * or mandatory, at the discretion of the platform. The lock applies to
807  * the file as a whole, rather than a specific range. Locks are established
808  * on a per-thread/process basis; a second lock by the same thread will not
809  * block.
810  * @param thefile The file to lock.
811  * @param type The type of lock to establish on the file.
812  */
813 APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type);
814 
815 /**
816  * Remove any outstanding locks on the file.
817  * @param thefile The file to unlock.
818  */
820 
821 /**accessor and general file_io functions. */
822 
823 /**
824  * return the file name of the current file.
825  * @param new_path The path of the file.
826  * @param thefile The currently open file.
827  */
828 APR_DECLARE(apr_status_t) apr_file_name_get(const char **new_path,
829  apr_file_t *thefile);
830 
831 /**
832  * Return the data associated with the current file.
833  * @param data The user data associated with the file.
834  * @param key The key to use for retrieving data associated with this file.
835  * @param file The currently open file.
836  */
837 APR_DECLARE(apr_status_t) apr_file_data_get(void **data, const char *key,
838  apr_file_t *file);
839 
840 /**
841  * Set the data associated with the current file.
842  * @param file The currently open file.
843  * @param data The user data to associate with the file.
844  * @param key The key to use for associating data with the file.
845  * @param cleanup The cleanup routine to use when the file is destroyed.
846  */
848  const char *key,
849  apr_status_t (*cleanup)(void *));
850 
851 /**
852  * Write a string to a file using a printf format.
853  * @param fptr The file to write to.
854  * @param format The format string
855  * @param ... The values to substitute in the format string
856  * @return The number of bytes written
857  */
859  const char *format, ...)
860  __attribute__((format(printf,2,3)));
861 
862 /**
863  * set the specified file's permission bits.
864  * @param fname The file (name) to apply the permissions to.
865  * @param perms The permission bits to apply to the file.
866  *
867  * @warning Some platforms may not be able to apply all of the
868  * available permission bits; #APR_INCOMPLETE will be returned if some
869  * permissions are specified which could not be set.
870  *
871  * @warning Platforms which do not implement this feature will return
872  * #APR_ENOTIMPL.
873  */
874 APR_DECLARE(apr_status_t) apr_file_perms_set(const char *fname,
875  apr_fileperms_t perms);
876 
877 /**
878  * Set attributes of the specified file.
879  * @param fname The full path to the file (using / on all systems)
880  * @param attributes Or'd combination of
881  * @li #APR_FILE_ATTR_READONLY - make the file readonly
882  * @li #APR_FILE_ATTR_EXECUTABLE - make the file executable
883  * @li #APR_FILE_ATTR_HIDDEN - make the file hidden
884  * @param attr_mask Mask of valid bits in attributes.
885  * @param pool the pool to use.
886  * @remark This function should be used in preference to explicit manipulation
887  * of the file permissions, because the operations to provide these
888  * attributes are platform specific and may involve more than simply
889  * setting permission bits.
890  * @warning Platforms which do not implement this feature will return
891  * #APR_ENOTIMPL.
892  */
893 APR_DECLARE(apr_status_t) apr_file_attrs_set(const char *fname,
894  apr_fileattrs_t attributes,
895  apr_fileattrs_t attr_mask,
896  apr_pool_t *pool);
897 
898 /**
899  * Set the mtime of the specified file.
900  * @param fname The full path to the file (using / on all systems)
901  * @param mtime The mtime to apply to the file.
902  * @param pool The pool to use.
903  * @warning Platforms which do not implement this feature will return
904  * #APR_ENOTIMPL.
905  */
906 APR_DECLARE(apr_status_t) apr_file_mtime_set(const char *fname,
907  apr_time_t mtime,
908  apr_pool_t *pool);
909 
910 /**
911  * Create a new directory on the file system.
912  * @param path the path for the directory to be created. (use / on all systems)
913  * @param perm Permissions for the new directory.
914  * @param pool the pool to use.
915  */
916 APR_DECLARE(apr_status_t) apr_dir_make(const char *path, apr_fileperms_t perm,
917  apr_pool_t *pool);
918 
919 /** Creates a new directory on the file system, but behaves like
920  * 'mkdir -p'. Creates intermediate directories as required. No error
921  * will be reported if PATH already exists.
922  * @param path the path for the directory to be created. (use / on all systems)
923  * @param perm Permissions for the new directory.
924  * @param pool the pool to use.
925  */
927  apr_fileperms_t perm,
928  apr_pool_t *pool);
929 
930 /**
931  * Remove directory from the file system.
932  * @param path the path for the directory to be removed. (use / on all systems)
933  * @param pool the pool to use.
934  * @remark Removing a directory which is in-use (e.g., the current working
935  * directory, or during apr_dir_read, or with an open file) is not portable.
936  */
937 APR_DECLARE(apr_status_t) apr_dir_remove(const char *path, apr_pool_t *pool);
938 
939 /**
940  * get the specified file's stats.
941  * @param finfo Where to store the information about the file.
942  * @param wanted The desired apr_finfo_t fields, as a bit flag of APR_FINFO_* values
943  * @param thefile The file to get information about.
944  */
946  apr_int32_t wanted,
947  apr_file_t *thefile);
948 
949 
950 /**
951  * Truncate the file's length to the specified offset
952  * @param fp The file to truncate
953  * @param offset The offset to truncate to.
954  * @remark The read/write file offset is repositioned to offset.
955  */
956 APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *fp, apr_off_t offset);
957 
958 /**
959  * Retrieve the flags that were passed into apr_file_open()
960  * when the file was opened.
961  * @return apr_int32_t the flags
962  */
963 APR_DECLARE(apr_int32_t) apr_file_flags_get(apr_file_t *f);
964 
965 /**
966  * Get the pool used by the file.
967  */
969 
970 /**
971  * Set a file to be inherited by child processes.
972  *
973  */
975 
976 /**
977  * Unset a file from being inherited by child processes.
978  */
980 
981 /**
982  * Open a temporary file
983  * @param fp The apr file to use as a temporary file.
984  * @param templ The template to use when creating a temp file.
985  * @param flags The flags to open the file with. If this is zero,
986  * the file is opened with
987  * #APR_FOPEN_CREATE | #APR_FOPEN_READ | #APR_FOPEN_WRITE |
988  * #APR_FOPEN_EXCL | #APR_FOPEN_DELONCLOSE
989  * @param p The pool to allocate the file out of.
990  * @remark
991  * This function generates a unique temporary file name from template.
992  * The last six characters of template must be XXXXXX and these are replaced
993  * with a string that makes the filename unique. Since it will be modified,
994  * template must not be a string constant, but should be declared as a character
995  * array.
996  *
997  */
999  apr_int32_t flags, apr_pool_t *p);
1000 
1001 
1002 /**
1003  * Find an existing directory suitable as a temporary storage location.
1004  * @param temp_dir The temp directory.
1005  * @param p The pool to use for any necessary allocations.
1006  * @remark
1007  * This function uses an algorithm to search for a directory that an
1008  * an application can use for temporary storage.
1009  *
1010  */
1011 APR_DECLARE(apr_status_t) apr_temp_dir_get(const char **temp_dir,
1012  apr_pool_t *p);
1013 
1014 
1015 APR_DECLARE(apr_status_t) apr_file_rotating_check(apr_file_t *thefile);
1016 APR_DECLARE(apr_status_t) apr_file_rotating_manual_check(apr_file_t *thefile, apr_time_t time);
1017 
1018 /** @} */
1019 
1020 
1021 #ifdef __cplusplus
1022 }
1023 #endif
1024 
1025 #endif /* ! APR_FILE_IO_H */
apr_status_t apr_file_flush(apr_file_t *thefile)
apr_status_t apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *pool)
apr_status_t apr_file_open_flags_stdout(apr_file_t **thefile, apr_int32_t flags, apr_pool_t *pool)
apr_status_t apr_file_lock(apr_file_t *thefile, int type)
apr_status_t apr_file_copy(const char *from_path, const char *to_path, apr_fileperms_t perms, apr_pool_t *pool)
apr_status_t apr_file_name_get(const char **new_path, apr_file_t *thefile)
apr_status_t apr_file_pipe_wait(apr_file_t *thepipe, apr_wait_type_t direction)
apr_status_t apr_file_remove(const char *path, apr_pool_t *pool)
apr_status_t apr_file_mtime_set(const char *fname, apr_time_t mtime, apr_pool_t *pool)
apr_status_t apr_file_eof(apr_file_t *fptr)
APR File Information.
apr_status_t apr_temp_dir_get(const char **temp_dir, apr_pool_t *p)
apr_status_t apr_dir_remove(const char *path, apr_pool_t *pool)
apr_status_t apr_file_open_flags_stderr(apr_file_t **thefile, apr_int32_t flags, apr_pool_t *pool)
apr_status_t apr_file_sync(apr_file_t *thefile)
apr_status_t apr_file_info_get(apr_finfo_t *finfo, apr_int32_t wanted, apr_file_t *thefile)
APR File Handle Inheritance Helpers.
apr_status_t apr_file_read_full(apr_file_t *thefile, void *buf, apr_size_t nbytes, apr_size_t *bytes_read)
apr_int32_t apr_file_flags_get(apr_file_t *f)
APR Standard Headers Support.
apr_status_t apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
apr_status_t apr_file_datasync(apr_file_t *thefile)
apr_status_t apr_file_unlock(apr_file_t *thefile)
apr_status_t apr_file_puts(const char *str, apr_file_t *thefile)
apr_status_t apr_file_setaside(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
apr_int64_t apr_interval_time_t
Definition: apr_time.h:55
#define APR_DECLARE_INHERIT_SET(type)
Definition: apr_inherit.h:35
apr_status_t apr_file_write_full(apr_file_t *thefile, const void *buf, apr_size_t nbytes, apr_size_t *bytes_written)
apr_status_t apr_file_dup2(apr_file_t *new_file, apr_file_t *old_file, apr_pool_t *p)
apr_status_t apr_file_open_flags_stdin(apr_file_t **thefile, apr_int32_t flags, apr_pool_t *pool)
apr_status_t apr_file_append(const char *from_path, const char *to_path, apr_fileperms_t perms, apr_pool_t *pool)
apr_status_t apr_dir_make(const char *path, apr_fileperms_t perm, apr_pool_t *pool)
struct apr_file_t apr_file_t
Definition: apr_file_io.h:216
apr_int64_t apr_time_t
Definition: apr_time.h:45
APR memory allocation.
apr_status_t apr_file_namedpipe_create(const char *filename, apr_fileperms_t perm, apr_pool_t *pool)
apr_status_t apr_file_trunc(apr_file_t *fp, apr_off_t offset)
apr_status_t apr_file_open_stderr(apr_file_t **thefile, apr_pool_t *pool)
apr_int32_t apr_fileperms_t
Definition: apr_file_info.h:125
apr_status_t apr_file_gets(char *str, int len, apr_file_t *thefile)
apr_status_t apr_file_open_stdout(apr_file_t **thefile, apr_pool_t *pool)
apr_status_t apr_file_data_set(apr_file_t *file, void *data, const char *key, apr_status_t(*cleanup)(void *))
APR Error Codes.
apr_status_t apr_file_open_stdin(apr_file_t **thefile, apr_pool_t *pool)
#define APR_DECLARE(type)
Definition: apr.h:500
APR Platform Definitions.
apr_status_t apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
apr_status_t apr_file_mktemp(apr_file_t **fp, char *templ, apr_int32_t flags, apr_pool_t *p)
apr_status_t apr_file_dup(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
apr_status_t apr_file_seek(apr_file_t *thefile, apr_seek_where_t where, apr_off_t *offset)
apr_status_t apr_file_pipe_create_ex(apr_file_t **in, apr_file_t **out, apr_int32_t blocking, apr_pool_t *pool)
apr_uint32_t apr_fileattrs_t
Definition: apr_file_io.h:208
apr_status_t apr_file_writev(apr_file_t *thefile, const struct iovec *vec, apr_size_t nvec, apr_size_t *nbytes)
apr_status_t apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
apr_status_t apr_file_data_get(void **data, const char *key, apr_file_t *file)
int apr_file_printf(apr_file_t *fptr, const char *format,...)
apr_status_t apr_file_close(apr_file_t *file)
#define APR_DECLARE_INHERIT_UNSET(type)
Definition: apr_inherit.h:47
apr_status_t apr_file_rename(const char *from_path, const char *to_path, apr_pool_t *pool)
apr_status_t apr_file_getc(char *ch, apr_file_t *thefile)
#define APR_DECLARE_NONSTD(type)
Definition: apr.h:513
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
apr_status_t apr_file_perms_set(const char *fname, apr_fileperms_t perms)
int apr_status_t
Definition: apr_errno.h:44
apr_status_t apr_dir_make_recursive(const char *path, apr_fileperms_t perm, apr_pool_t *pool)
apr_status_t apr_file_pipe_create_pools(apr_file_t **in, apr_file_t **out, apr_int32_t blocking, apr_pool_t *pool_in, apr_pool_t *pool_out)
APR Time Library.
apr_size_t apr_file_buffer_size_get(apr_file_t *thefile)
apr_status_t apr_file_writev_full(apr_file_t *thefile, const struct iovec *vec, apr_size_t nvec, apr_size_t *nbytes)
apr_status_t apr_file_open(apr_file_t **newf, const char *fname, apr_int32_t flag, apr_fileperms_t perm, apr_pool_t *pool)
#define APR_POOL_DECLARE_ACCESSOR(type)
Definition: apr_pools.h:81
apr_status_t apr_file_ungetc(char ch, apr_file_t *thefile)
apr_status_t apr_file_attrs_set(const char *fname, apr_fileattrs_t attributes, apr_fileattrs_t attr_mask, apr_pool_t *pool)
Definition: apr_file_info.h:174
apr_status_t apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t *nbytes)
int apr_seek_where_t
Definition: apr_file_io.h:211
apr_status_t apr_file_putc(char ch, apr_file_t *thefile)
apr_status_t apr_file_link(const char *from_path, const char *to_path)
apr_status_t apr_file_buffer_set(apr_file_t *thefile, char *buffer, apr_size_t bufsize)