apr_file_io.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. * @remark Note that advanced filesystem permissions such as ACLs are not
  288. * duplicated by this API. The target permissions (including duplicating the
  289. * source file permissions) are assigned only when the target file does not yet
  290. * exist.
  291. */
  292. APR_DECLARE(apr_status_t) apr_file_append(const char *from_path,
  293. const char *to_path,
  294. apr_fileperms_t perms,
  295. apr_pool_t *pool);
  296. /**
  297. * Are we at the end of the file
  298. * @param fptr The apr file we are testing.
  299. * @remark Returns #APR_EOF if we are at the end of file, #APR_SUCCESS otherwise.
  300. */
  301. APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr);
  302. /**
  303. * Open standard error as an apr file pointer.
  304. * @param thefile The apr file to use as stderr.
  305. * @param pool The pool to allocate the file out of.
  306. *
  307. * @remark The only reason that the apr_file_open_std* functions exist
  308. * is that you may not always have a stderr/out/in on Windows. This
  309. * is generally a problem with newer versions of Windows and services.
  310. *
  311. * @remark The other problem is that the C library functions generally work
  312. * differently on Windows and Unix. So, by using apr_file_open_std*
  313. * functions, you can get a handle to an APR struct that works with
  314. * the APR functions which are supposed to work identically on all
  315. * platforms.
  316. */
  317. APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile,
  318. apr_pool_t *pool);
  319. /**
  320. * open standard output as an apr file pointer.
  321. * @param thefile The apr file to use as stdout.
  322. * @param pool The pool to allocate the file out of.
  323. *
  324. * @remark See remarks for apr_file_open_stderr().
  325. */
  326. APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile,
  327. apr_pool_t *pool);
  328. /**
  329. * open standard input as an apr file pointer.
  330. * @param thefile The apr file to use as stdin.
  331. * @param pool The pool to allocate the file out of.
  332. *
  333. * @remark See remarks for apr_file_open_stderr().
  334. */
  335. APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile,
  336. apr_pool_t *pool);
  337. /**
  338. * open standard error as an apr file pointer, with flags.
  339. * @param thefile The apr file to use as stderr.
  340. * @param flags The flags to open the file with. Only the
  341. * @li #APR_FOPEN_EXCL
  342. * @li #APR_FOPEN_BUFFERED
  343. * @li #APR_FOPEN_XTHREAD
  344. * @li #APR_FOPEN_SHARELOCK
  345. * @li #APR_FOPEN_SENDFILE_ENABLED
  346. * @li #APR_FOPEN_LARGEFILE
  347. *
  348. * flags should be used. The #APR_FOPEN_WRITE flag will
  349. * be set unconditionally.
  350. * @param pool The pool to allocate the file out of.
  351. *
  352. * @remark See remarks for apr_file_open_stderr().
  353. */
  354. APR_DECLARE(apr_status_t) apr_file_open_flags_stderr(apr_file_t **thefile,
  355. apr_int32_t flags,
  356. apr_pool_t *pool);
  357. /**
  358. * open standard output as an apr file pointer, with flags.
  359. * @param thefile The apr file to use as stdout.
  360. * @param flags The flags to open the file with. Only the
  361. * @li #APR_FOPEN_EXCL
  362. * @li #APR_FOPEN_BUFFERED
  363. * @li #APR_FOPEN_XTHREAD
  364. * @li #APR_FOPEN_SHARELOCK
  365. * @li #APR_FOPEN_SENDFILE_ENABLED
  366. * @li #APR_FOPEN_LARGEFILE
  367. *
  368. * flags should be used. The #APR_FOPEN_WRITE flag will
  369. * be set unconditionally.
  370. * @param pool The pool to allocate the file out of.
  371. *
  372. * @remark See remarks for apr_file_open_stderr().
  373. */
  374. APR_DECLARE(apr_status_t) apr_file_open_flags_stdout(apr_file_t **thefile,
  375. apr_int32_t flags,
  376. apr_pool_t *pool);
  377. /**
  378. * open standard input as an apr file pointer, with flags.
  379. * @param thefile The apr file to use as stdin.
  380. * @param flags The flags to open the file with. Only the
  381. * @li #APR_FOPEN_EXCL
  382. * @li #APR_FOPEN_BUFFERED
  383. * @li #APR_FOPEN_XTHREAD
  384. * @li #APR_FOPEN_SHARELOCK
  385. * @li #APR_FOPEN_SENDFILE_ENABLED
  386. * @li #APR_FOPEN_LARGEFILE
  387. *
  388. * flags should be used. The #APR_FOPEN_WRITE flag will
  389. * be set unconditionally.
  390. * @param pool The pool to allocate the file out of.
  391. *
  392. * @remark See remarks for apr_file_open_stderr().
  393. */
  394. APR_DECLARE(apr_status_t) apr_file_open_flags_stdin(apr_file_t **thefile,
  395. apr_int32_t flags,
  396. apr_pool_t *pool);
  397. /**
  398. * Read data from the specified file.
  399. * @param thefile The file descriptor to read from.
  400. * @param buf The buffer to store the data to.
  401. * @param nbytes On entry, the number of bytes to read; on exit, the number
  402. * of bytes read.
  403. *
  404. * @remark apr_file_read() will read up to the specified number of
  405. * bytes, but never more. If there isn't enough data to fill that
  406. * number of bytes, all of the available data is read. The third
  407. * argument is modified to reflect the number of bytes read. If a
  408. * char was put back into the stream via ungetc, it will be the first
  409. * character returned.
  410. *
  411. * @remark It is not possible for both bytes to be read and an #APR_EOF
  412. * or other error to be returned. #APR_EINTR is never returned.
  413. */
  414. APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf,
  415. apr_size_t *nbytes);
  416. /**
  417. * Write data to the specified file.
  418. * @param thefile The file descriptor to write to.
  419. * @param buf The buffer which contains the data.
  420. * @param nbytes On entry, the number of bytes to write; on exit, the number
  421. * of bytes written.
  422. *
  423. * @remark apr_file_write() will write up to the specified number of
  424. * bytes, but never more. If the OS cannot write that many bytes, it
  425. * will write as many as it can. The third argument is modified to
  426. * reflect the * number of bytes written.
  427. *
  428. * @remark It is possible for both bytes to be written and an error to
  429. * be returned. #APR_EINTR is never returned.
  430. */
  431. APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf,
  432. apr_size_t *nbytes);
  433. /**
  434. * Write data from iovec array to the specified file.
  435. * @param thefile The file descriptor to write to.
  436. * @param vec The array from which to get the data to write to the file.
  437. * @param nvec The number of elements in the struct iovec array. This must
  438. * be smaller than #APR_MAX_IOVEC_SIZE. If it isn't, the function
  439. * will fail with #APR_EINVAL.
  440. * @param nbytes The number of bytes written.
  441. *
  442. * @remark It is possible for both bytes to be written and an error to
  443. * be returned. #APR_EINTR is never returned.
  444. *
  445. * @remark apr_file_writev() is available even if the underlying
  446. * operating system doesn't provide writev().
  447. */
  448. APR_DECLARE(apr_status_t) apr_file_writev(apr_file_t *thefile,
  449. const struct iovec *vec,
  450. apr_size_t nvec, apr_size_t *nbytes);
  451. /**
  452. * Read data from the specified file, ensuring that the buffer is filled
  453. * before returning.
  454. * @param thefile The file descriptor to read from.
  455. * @param buf The buffer to store the data to.
  456. * @param nbytes The number of bytes to read.
  457. * @param bytes_read If non-NULL, this will contain the number of bytes read.
  458. *
  459. * @remark apr_file_read_full() will read up to the specified number of
  460. * bytes, but never more. If there isn't enough data to fill that
  461. * number of bytes, then the process/thread will block until it is
  462. * available or EOF is reached. If a char was put back into the
  463. * stream via ungetc, it will be the first character returned.
  464. *
  465. * @remark It is possible for both bytes to be read and an error to be
  466. * returned. And if *bytes_read is less than nbytes, an accompanying
  467. * error is _always_ returned.
  468. *
  469. * @remark #APR_EINTR is never returned.
  470. */
  471. APR_DECLARE(apr_status_t) apr_file_read_full(apr_file_t *thefile, void *buf,
  472. apr_size_t nbytes,
  473. apr_size_t *bytes_read);
  474. /**
  475. * Write data to the specified file, ensuring that all of the data is
  476. * written before returning.
  477. * @param thefile The file descriptor to write to.
  478. * @param buf The buffer which contains the data.
  479. * @param nbytes The number of bytes to write.
  480. * @param bytes_written If non-NULL, set to the number of bytes written.
  481. *
  482. * @remark apr_file_write_full() will write up to the specified number of
  483. * bytes, but never more. If the OS cannot write that many bytes, the
  484. * process/thread will block until they can be written. Exceptional
  485. * error such as "out of space" or "pipe closed" will terminate with
  486. * an error.
  487. *
  488. * @remark It is possible for both bytes to be written and an error to
  489. * be returned. And if *bytes_written is less than nbytes, an
  490. * accompanying error is _always_ returned.
  491. *
  492. * @remark #APR_EINTR is never returned.
  493. */
  494. APR_DECLARE(apr_status_t) apr_file_write_full(apr_file_t *thefile,
  495. const void *buf,
  496. apr_size_t nbytes,
  497. apr_size_t *bytes_written);
  498. /**
  499. * Write data from iovec array to the specified file, ensuring that all of the
  500. * data is written before returning.
  501. * @param thefile The file descriptor to write to.
  502. * @param vec The array from which to get the data to write to the file.
  503. * @param nvec The number of elements in the struct iovec array. This must
  504. * be smaller than #APR_MAX_IOVEC_SIZE. If it isn't, the function
  505. * will fail with #APR_EINVAL.
  506. * @param nbytes The number of bytes written.
  507. *
  508. * @remark apr_file_writev_full() is available even if the underlying
  509. * operating system doesn't provide writev().
  510. */
  511. APR_DECLARE(apr_status_t) apr_file_writev_full(apr_file_t *thefile,
  512. const struct iovec *vec,
  513. apr_size_t nvec,
  514. apr_size_t *nbytes);
  515. /**
  516. * Write a character into the specified file.
  517. * @param ch The character to write.
  518. * @param thefile The file descriptor to write to
  519. */
  520. APR_DECLARE(apr_status_t) apr_file_putc(char ch, apr_file_t *thefile);
  521. /**
  522. * Read a character from the specified file.
  523. * @param ch The character to read into
  524. * @param thefile The file descriptor to read from
  525. */
  526. APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile);
  527. /**
  528. * Put a character back onto a specified stream.
  529. * @param ch The character to write.
  530. * @param thefile The file descriptor to write to
  531. */
  532. APR_DECLARE(apr_status_t) apr_file_ungetc(char ch, apr_file_t *thefile);
  533. /**
  534. * Read a line from the specified file
  535. * @param str The buffer to store the string in.
  536. * @param len The length of the string
  537. * @param thefile The file descriptor to read from
  538. * @remark The buffer will be NUL-terminated if any characters are stored.
  539. * The newline at the end of the line will not be stripped.
  540. */
  541. APR_DECLARE(apr_status_t) apr_file_gets(char *str, int len,
  542. apr_file_t *thefile);
  543. /**
  544. * Write the string into the specified file.
  545. * @param str The string to write.
  546. * @param thefile The file descriptor to write to
  547. */
  548. APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile);
  549. /**
  550. * Flush the file's buffer.
  551. * @param thefile The file descriptor to flush
  552. */
  553. APR_DECLARE(apr_status_t) apr_file_flush(apr_file_t *thefile);
  554. /**
  555. * Transfer all file modified data and metadata to disk.
  556. * @param thefile The file descriptor to sync
  557. */
  558. APR_DECLARE(apr_status_t) apr_file_sync(apr_file_t *thefile);
  559. /**
  560. * Transfer all file modified data to disk.
  561. * @param thefile The file descriptor to sync
  562. */
  563. APR_DECLARE(apr_status_t) apr_file_datasync(apr_file_t *thefile);
  564. /**
  565. * Duplicate the specified file descriptor.
  566. * @param new_file The structure to duplicate into.
  567. * @param old_file The file to duplicate.
  568. * @param p The pool to use for the new file.
  569. * @remark *new_file must point to a valid apr_file_t, or point to NULL.
  570. */
  571. APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file,
  572. apr_file_t *old_file,
  573. apr_pool_t *p);
  574. /**
  575. * Duplicate the specified file descriptor and close the original
  576. * @param new_file The old file that is to be closed and reused
  577. * @param old_file The file to duplicate
  578. * @param p The pool to use for the new file
  579. *
  580. * @remark new_file MUST point at a valid apr_file_t. It cannot be NULL.
  581. */
  582. APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
  583. apr_file_t *old_file,
  584. apr_pool_t *p);
  585. /**
  586. * Move the specified file descriptor to a new pool
  587. * @param new_file Pointer in which to return the new apr_file_t
  588. * @param old_file The file to move
  589. * @param p The pool to which the descriptor is to be moved
  590. * @remark Unlike apr_file_dup2(), this function doesn't do an
  591. * OS dup() operation on the underlying descriptor; it just
  592. * moves the descriptor's apr_file_t wrapper to a new pool.
  593. * @remark The new pool need not be an ancestor of old_file's pool.
  594. * @remark After calling this function, old_file may not be used
  595. */
  596. APR_DECLARE(apr_status_t) apr_file_setaside(apr_file_t **new_file,
  597. apr_file_t *old_file,
  598. apr_pool_t *p);
  599. /**
  600. * Give the specified apr file handle a new buffer
  601. * @param thefile The file handle that is to be modified
  602. * @param buffer The buffer
  603. * @param bufsize The size of the buffer
  604. * @remark It is possible to add a buffer to previously unbuffered
  605. * file handles, the #APR_FOPEN_BUFFERED flag will be added to
  606. * the file handle's flags. Likewise, with buffer=NULL and
  607. * bufsize=0 arguments it is possible to make a previously
  608. * buffered file handle unbuffered.
  609. */
  610. APR_DECLARE(apr_status_t) apr_file_buffer_set(apr_file_t *thefile,
  611. char * buffer,
  612. apr_size_t bufsize);
  613. /**
  614. * Get the size of any buffer for the specified apr file handle
  615. * @param thefile The file handle
  616. */
  617. APR_DECLARE(apr_size_t) apr_file_buffer_size_get(apr_file_t *thefile);
  618. /**
  619. * Move the read/write file offset to a specified byte within a file.
  620. * @param thefile The file descriptor
  621. * @param where How to move the pointer, one of:
  622. * @li #APR_SET -- set the offset to offset
  623. * @li #APR_CUR -- add the offset to the current position
  624. * @li #APR_END -- add the offset to the current file size
  625. * @param offset The offset to move the pointer to.
  626. * @remark The third argument is modified to be the offset the pointer
  627. was actually moved to.
  628. */
  629. APR_DECLARE(apr_status_t) apr_file_seek(apr_file_t *thefile,
  630. apr_seek_where_t where,
  631. apr_off_t *offset);
  632. /**
  633. * Create an anonymous pipe.
  634. * @param in The newly created pipe's file for reading.
  635. * @param out The newly created pipe's file for writing.
  636. * @param pool The pool to operate on.
  637. * @remark By default, the returned file descriptors will be inherited
  638. * by child processes created using apr_proc_create(). This can be
  639. * changed using apr_file_inherit_unset().
  640. * @bug Some platforms cannot toggle between blocking and nonblocking,
  641. * and when passing a pipe as a standard handle to an application which
  642. * does not expect it, a non-blocking stream will fluxor the client app.
  643. * @deprecated @see apr_file_pipe_create_pools()
  644. */
  645. APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in,
  646. apr_file_t **out,
  647. apr_pool_t *pool);
  648. /**
  649. * Create an anonymous pipe which portably supports async timeout options.
  650. * @param in The newly created pipe's file for reading.
  651. * @param out The newly created pipe's file for writing.
  652. * @param blocking one of these values defined in apr_thread_proc.h;
  653. * @li #APR_FULL_BLOCK
  654. * @li #APR_READ_BLOCK
  655. * @li #APR_WRITE_BLOCK
  656. * @li #APR_FULL_NONBLOCK
  657. * @param pool The pool to operate on.
  658. * @remark By default, the returned file descriptors will be inherited
  659. * by child processes created using apr_proc_create(). This can be
  660. * changed using apr_file_inherit_unset().
  661. * @remark Some platforms cannot toggle between blocking and nonblocking,
  662. * and when passing a pipe as a standard handle to an application which
  663. * does not expect it, a non-blocking stream will fluxor the client app.
  664. * Use this function rather than apr_file_pipe_create() to create pipes
  665. * where one or both ends require non-blocking semantics.
  666. * @deprecated @see apr_file_pipe_create_pools()
  667. */
  668. APR_DECLARE(apr_status_t) apr_file_pipe_create_ex(apr_file_t **in,
  669. apr_file_t **out,
  670. apr_int32_t blocking,
  671. apr_pool_t *pool);
  672. /**
  673. * Create an anonymous pipe which portably supports async timeout options,
  674. * placing each side of the pipe in a different pool.
  675. * @param in The newly created pipe's file for reading.
  676. * @param out The newly created pipe's file for writing.
  677. * @param blocking one of these values defined in apr_thread_proc.h;
  678. * @li #APR_FULL_BLOCK
  679. * @li #APR_READ_BLOCK
  680. * @li #APR_WRITE_BLOCK
  681. * @li #APR_FULL_NONBLOCK
  682. * @param pool_in The pool for the reading pipe.
  683. * @param pool_out The pool for the writing pipe.
  684. * @remark By default, the returned file descriptors will be inherited
  685. * by child processes created using apr_proc_create(). This can be
  686. * changed using apr_file_inherit_unset().
  687. * @remark Some platforms cannot toggle between blocking and nonblocking,
  688. * and when passing a pipe as a standard handle to an application which
  689. * does not expect it, a non-blocking stream will fluxor the client app.
  690. * Use this function rather than apr_file_pipe_create() to create pipes
  691. * where one or both ends require non-blocking semantics.
  692. */
  693. APR_DECLARE(apr_status_t) apr_file_pipe_create_pools(apr_file_t **in,
  694. apr_file_t **out,
  695. apr_int32_t blocking,
  696. apr_pool_t *pool_in,
  697. apr_pool_t *pool_out);
  698. /**
  699. * Create a named pipe.
  700. * @param filename The filename of the named pipe
  701. * @param perm The permissions for the newly created pipe.
  702. * @param pool The pool to operate on.
  703. */
  704. APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
  705. apr_fileperms_t perm,
  706. apr_pool_t *pool);
  707. /**
  708. * Get the timeout value for a pipe or manipulate the blocking state.
  709. * @param thepipe The pipe we are getting a timeout for.
  710. * @param timeout The current timeout value in microseconds.
  711. */
  712. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe,
  713. apr_interval_time_t *timeout);
  714. /**
  715. * Set the timeout value for a pipe or manipulate the blocking state.
  716. * @param thepipe The pipe we are setting a timeout on.
  717. * @param timeout The timeout value in microseconds. Values < 0 mean wait
  718. * forever, 0 means do not wait at all.
  719. */
  720. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe,
  721. apr_interval_time_t timeout);
  722. /** file (un)locking functions. */
  723. /**
  724. * Establish a lock on the specified, open file. The lock may be advisory
  725. * or mandatory, at the discretion of the platform. The lock applies to
  726. * the file as a whole, rather than a specific range. Locks are established
  727. * on a per-thread/process basis; a second lock by the same thread will not
  728. * block.
  729. * @param thefile The file to lock.
  730. * @param type The type of lock to establish on the file.
  731. */
  732. APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type);
  733. /**
  734. * Remove any outstanding locks on the file.
  735. * @param thefile The file to unlock.
  736. */
  737. APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *thefile);
  738. /**accessor and general file_io functions. */
  739. /**
  740. * return the file name of the current file.
  741. * @param new_path The path of the file.
  742. * @param thefile The currently open file.
  743. */
  744. APR_DECLARE(apr_status_t) apr_file_name_get(const char **new_path,
  745. apr_file_t *thefile);
  746. /**
  747. * Return the data associated with the current file.
  748. * @param data The user data associated with the file.
  749. * @param key The key to use for retrieving data associated with this file.
  750. * @param file The currently open file.
  751. */
  752. APR_DECLARE(apr_status_t) apr_file_data_get(void **data, const char *key,
  753. apr_file_t *file);
  754. /**
  755. * Set the data associated with the current file.
  756. * @param file The currently open file.
  757. * @param data The user data to associate with the file.
  758. * @param key The key to use for associating data with the file.
  759. * @param cleanup The cleanup routine to use when the file is destroyed.
  760. */
  761. APR_DECLARE(apr_status_t) apr_file_data_set(apr_file_t *file, void *data,
  762. const char *key,
  763. apr_status_t (*cleanup)(void *));
  764. /**
  765. * Write a string to a file using a printf format.
  766. * @param fptr The file to write to.
  767. * @param format The format string
  768. * @param ... The values to substitute in the format string
  769. * @return The number of bytes written
  770. */
  771. APR_DECLARE_NONSTD(int) apr_file_printf(apr_file_t *fptr,
  772. const char *format, ...)
  773. __attribute__((format(printf,2,3)));
  774. /**
  775. * set the specified file's permission bits.
  776. * @param fname The file (name) to apply the permissions to.
  777. * @param perms The permission bits to apply to the file.
  778. *
  779. * @warning Some platforms may not be able to apply all of the
  780. * available permission bits; #APR_INCOMPLETE will be returned if some
  781. * permissions are specified which could not be set.
  782. *
  783. * @warning Platforms which do not implement this feature will return
  784. * #APR_ENOTIMPL.
  785. */
  786. APR_DECLARE(apr_status_t) apr_file_perms_set(const char *fname,
  787. apr_fileperms_t perms);
  788. /**
  789. * Set attributes of the specified file.
  790. * @param fname The full path to the file (using / on all systems)
  791. * @param attributes Or'd combination of
  792. * @li #APR_FILE_ATTR_READONLY - make the file readonly
  793. * @li #APR_FILE_ATTR_EXECUTABLE - make the file executable
  794. * @li #APR_FILE_ATTR_HIDDEN - make the file hidden
  795. * @param attr_mask Mask of valid bits in attributes.
  796. * @param pool the pool to use.
  797. * @remark This function should be used in preference to explicit manipulation
  798. * of the file permissions, because the operations to provide these
  799. * attributes are platform specific and may involve more than simply
  800. * setting permission bits.
  801. * @warning Platforms which do not implement this feature will return
  802. * #APR_ENOTIMPL.
  803. */
  804. APR_DECLARE(apr_status_t) apr_file_attrs_set(const char *fname,
  805. apr_fileattrs_t attributes,
  806. apr_fileattrs_t attr_mask,
  807. apr_pool_t *pool);
  808. /**
  809. * Set the mtime of the specified file.
  810. * @param fname The full path to the file (using / on all systems)
  811. * @param mtime The mtime to apply to the file.
  812. * @param pool The pool to use.
  813. * @warning Platforms which do not implement this feature will return
  814. * #APR_ENOTIMPL.
  815. */
  816. APR_DECLARE(apr_status_t) apr_file_mtime_set(const char *fname,
  817. apr_time_t mtime,
  818. apr_pool_t *pool);
  819. /**
  820. * Create a new directory on the file system.
  821. * @param path the path for the directory to be created. (use / on all systems)
  822. * @param perm Permissions for the new directory.
  823. * @param pool the pool to use.
  824. */
  825. APR_DECLARE(apr_status_t) apr_dir_make(const char *path, apr_fileperms_t perm,
  826. apr_pool_t *pool);
  827. /** Creates a new directory on the file system, but behaves like
  828. * 'mkdir -p'. Creates intermediate directories as required. No error
  829. * will be reported if PATH already exists.
  830. * @param path the path for the directory to be created. (use / on all systems)
  831. * @param perm Permissions for the new directory.
  832. * @param pool the pool to use.
  833. */
  834. APR_DECLARE(apr_status_t) apr_dir_make_recursive(const char *path,
  835. apr_fileperms_t perm,
  836. apr_pool_t *pool);
  837. /**
  838. * Remove directory from the file system.
  839. * @param path the path for the directory to be removed. (use / on all systems)
  840. * @param pool the pool to use.
  841. * @remark Removing a directory which is in-use (e.g., the current working
  842. * directory, or during apr_dir_read, or with an open file) is not portable.
  843. */
  844. APR_DECLARE(apr_status_t) apr_dir_remove(const char *path, apr_pool_t *pool);
  845. /**
  846. * get the specified file's stats.
  847. * @param finfo Where to store the information about the file.
  848. * @param wanted The desired apr_finfo_t fields, as a bit flag of APR_FINFO_* values
  849. * @param thefile The file to get information about.
  850. */
  851. APR_DECLARE(apr_status_t) apr_file_info_get(apr_finfo_t *finfo,
  852. apr_int32_t wanted,
  853. apr_file_t *thefile);
  854. /**
  855. * Truncate the file's length to the specified offset
  856. * @param fp The file to truncate
  857. * @param offset The offset to truncate to.
  858. * @remark The read/write file offset is repositioned to offset.
  859. */
  860. APR_DECLARE(apr_status_t) apr_file_trunc(apr_file_t *fp, apr_off_t offset);
  861. /**
  862. * Retrieve the flags that were passed into apr_file_open()
  863. * when the file was opened.
  864. * @return apr_int32_t the flags
  865. */
  866. APR_DECLARE(apr_int32_t) apr_file_flags_get(apr_file_t *f);
  867. /**
  868. * Get the pool used by the file.
  869. */
  870. APR_POOL_DECLARE_ACCESSOR(file);
  871. /**
  872. * Set a file to be inherited by child processes.
  873. *
  874. */
  875. APR_DECLARE_INHERIT_SET(file);
  876. /**
  877. * Unset a file from being inherited by child processes.
  878. */
  879. APR_DECLARE_INHERIT_UNSET(file);
  880. /**
  881. * Open a temporary file
  882. * @param fp The apr file to use as a temporary file.
  883. * @param templ The template to use when creating a temp file.
  884. * @param flags The flags to open the file with. If this is zero,
  885. * the file is opened with
  886. * #APR_FOPEN_CREATE | #APR_FOPEN_READ | #APR_FOPEN_WRITE |
  887. * #APR_FOPEN_EXCL | #APR_FOPEN_DELONCLOSE
  888. * @param p The pool to allocate the file out of.
  889. * @remark
  890. * This function generates a unique temporary file name from template.
  891. * The last six characters of template must be XXXXXX and these are replaced
  892. * with a string that makes the filename unique. Since it will be modified,
  893. * template must not be a string constant, but should be declared as a character
  894. * array.
  895. *
  896. */
  897. APR_DECLARE(apr_status_t) apr_file_mktemp(apr_file_t **fp, char *templ,
  898. apr_int32_t flags, apr_pool_t *p);
  899. /**
  900. * Find an existing directory suitable as a temporary storage location.
  901. * @param temp_dir The temp directory.
  902. * @param p The pool to use for any necessary allocations.
  903. * @remark
  904. * This function uses an algorithm to search for a directory that an
  905. * an application can use for temporary storage.
  906. *
  907. */
  908. APR_DECLARE(apr_status_t) apr_temp_dir_get(const char **temp_dir,
  909. apr_pool_t *p);
  910. /** @} */
  911. #ifdef __cplusplus
  912. }
  913. #endif
  914. #endif /* ! APR_FILE_IO_H */