apr_file_io.h 39 KB

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