apr_file_io.h 40 KB

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