2
0

fspr_file_io.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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 fspr_file_io.h
  20. * @brief APR File I/O Handling
  21. */
  22. #include "fspr.h"
  23. #include "fspr_pools.h"
  24. #include "fspr_time.h"
  25. #include "fspr_errno.h"
  26. #include "fspr_file_info.h"
  27. #include "fspr_inherit.h"
  28. #define APR_WANT_STDIO /**< for SEEK_* */
  29. #define APR_WANT_IOVEC /**< for fspr_file_writev */
  30. #include "fspr_want.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif /* __cplusplus */
  34. /**
  35. * @defgroup fspr_file_io File I/O Handling Functions
  36. * @ingroup APR
  37. * @{
  38. */
  39. /**
  40. * @defgroup fspr_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. fspr_socket_sendfile operation */
  68. #define APR_FOPEN_LARGEFILE 0x04000 /**< Platform dependent flag to enable
  69. large file support; WARNING see
  70. below. */
  71. /* backcompat */
  72. #define APR_READ APR_FOPEN_READ /**< @deprecated @see APR_FOPEN_READ */
  73. #define APR_WRITE APR_FOPEN_WRITE /**< @deprecated @see APR_FOPEN_WRITE */
  74. #define APR_CREATE APR_FOPEN_CREATE /**< @deprecated @see APR_FOPEN_CREATE */
  75. #define APR_APPEND APR_FOPEN_APPEND /**< @deprecated @see APR_FOPEN_APPEND */
  76. #define APR_TRUNCATE APR_FOPEN_TRUNCATE /**< @deprecated @see APR_FOPEN_TRUNCATE */
  77. #define APR_BINARY APR_FOPEN_BINARY /**< @deprecated @see APR_FOPEN_BINARY */
  78. #define APR_EXCL APR_FOPEN_EXCL /**< @deprecated @see APR_FOPEN_EXCL */
  79. #define APR_BUFFERED APR_FOPEN_BUFFERED /**< @deprecated @see APR_FOPEN_BUFFERED */
  80. #define APR_DELONCLOSE APR_FOPEN_DELONCLOSE /**< @deprecated @see APR_FOPEN_DELONCLOSE */
  81. #define APR_XTHREAD APR_FOPEN_XTHREAD /**< @deprecated @see APR_FOPEN_XTHREAD */
  82. #define APR_SHARELOCK APR_FOPEN_SHARELOCK /**< @deprecated @see APR_FOPEN_SHARELOCK */
  83. #define APR_FILE_NOCLEANUP APR_FOPEN_NOCLEANUP /**< @deprecated @see APR_FOPEN_NOCLEANUP */
  84. #define APR_SENDFILE_ENABLED APR_FOPEN_SENDFILE_ENABLED /**< @deprecated @see APR_FOPEN_SENDFILE_ENABLED */
  85. #define APR_LARGEFILE APR_FOPEN_LARGEFILE /**< @deprecated @see APR_FOPEN_LARGEFILE */
  86. /** @warning The APR_LARGEFILE flag only has effect on some platforms
  87. * where sizeof(fspr_off_t) == 4. Where implemented, it allows opening
  88. * and writing to a file which exceeds the size which can be
  89. * represented by fspr_off_t (2 gigabytes). When a file's size does
  90. * exceed 2Gb, fspr_file_info_get() will fail with an error on the
  91. * descriptor, likewise fspr_stat()/fspr_lstat() will fail on the
  92. * filename. fspr_dir_read() will fail with APR_INCOMPLETE on a
  93. * directory entry for a large file depending on the particular
  94. * APR_FINFO_* flags. Generally, it is not recommended to use this
  95. * flag. */
  96. /** @} */
  97. /**
  98. * @defgroup fspr_file_seek_flags File Seek Flags
  99. * @{
  100. */
  101. /* flags for fspr_file_seek */
  102. /** Set the file position */
  103. #define APR_SET SEEK_SET
  104. /** Current */
  105. #define APR_CUR SEEK_CUR
  106. /** Go to end of file */
  107. #define APR_END SEEK_END
  108. /** @} */
  109. /**
  110. * @defgroup fspr_file_attrs_set_flags File Attribute Flags
  111. * @{
  112. */
  113. /* flags for fspr_file_attrs_set */
  114. #define APR_FILE_ATTR_READONLY 0x01 /**< File is read-only */
  115. #define APR_FILE_ATTR_EXECUTABLE 0x02 /**< File is executable */
  116. #define APR_FILE_ATTR_HIDDEN 0x04 /**< File is hidden */
  117. /** @} */
  118. /**
  119. * @defgroup fspr_file_writev{_full} max iovec size
  120. * @{
  121. */
  122. #if defined(DOXYGEN)
  123. #define APR_MAX_IOVEC_SIZE 1024 /**< System dependent maximum
  124. size of an iovec array */
  125. #elif defined(IOV_MAX)
  126. #define APR_MAX_IOVEC_SIZE IOV_MAX
  127. #elif defined(MAX_IOVEC)
  128. #define APR_MAX_IOVEC_SIZE MAX_IOVEC
  129. #else
  130. #define APR_MAX_IOVEC_SIZE 1024
  131. #endif
  132. /** @} */
  133. /** File attributes */
  134. typedef fspr_uint32_t fspr_fileattrs_t;
  135. /** Type to pass as whence argument to fspr_file_seek. */
  136. typedef int fspr_seek_where_t;
  137. /**
  138. * Structure for referencing files.
  139. */
  140. typedef struct fspr_file_t fspr_file_t;
  141. /* File lock types/flags */
  142. /**
  143. * @defgroup fspr_file_lock_types File Lock Types
  144. * @{
  145. */
  146. #define APR_FLOCK_SHARED 1 /**< Shared lock. More than one process
  147. or thread can hold a shared lock
  148. at any given time. Essentially,
  149. this is a "read lock", preventing
  150. writers from establishing an
  151. exclusive lock. */
  152. #define APR_FLOCK_EXCLUSIVE 2 /**< Exclusive lock. Only one process
  153. may hold an exclusive lock at any
  154. given time. This is analogous to
  155. a "write lock". */
  156. #define APR_FLOCK_TYPEMASK 0x000F /**< mask to extract lock type */
  157. #define APR_FLOCK_NONBLOCK 0x0010 /**< do not block while acquiring the
  158. file lock */
  159. /** @} */
  160. /**
  161. * Open the specified file.
  162. * @param newf The opened file descriptor.
  163. * @param fname The full path to the file (using / on all systems)
  164. * @param flag Or'ed value of:
  165. * <PRE>
  166. * APR_READ open for reading
  167. * APR_WRITE open for writing
  168. * APR_CREATE create the file if not there
  169. * APR_APPEND file ptr is set to end prior to all writes
  170. * APR_TRUNCATE set length to zero if file exists
  171. * APR_BINARY not a text file (This flag is ignored on
  172. * UNIX because it has no meaning)
  173. * APR_BUFFERED buffer the data. Default is non-buffered
  174. * APR_EXCL return error if APR_CREATE and file exists
  175. * APR_DELONCLOSE delete the file after closing.
  176. * APR_XTHREAD Platform dependent tag to open the file
  177. * for use across multiple threads
  178. * APR_SHARELOCK Platform dependent support for higher
  179. * level locked read/write access to support
  180. * writes across process/machines
  181. * APR_FILE_NOCLEANUP Do not register a cleanup with the pool
  182. * passed in on the <EM>pool</EM> argument (see below).
  183. * The fspr_os_file_t handle in fspr_file_t will not
  184. * be closed when the pool is destroyed.
  185. * APR_SENDFILE_ENABLED Open with appropriate platform semantics
  186. * for sendfile operations. Advisory only,
  187. * fspr_socket_sendfile does not check this flag.
  188. * </PRE>
  189. * @param perm Access permissions for file.
  190. * @param pool The pool to use.
  191. * @remark If perm is APR_OS_DEFAULT and the file is being created,
  192. * appropriate default permissions will be used.
  193. * @remark By default, the returned file descriptor will not be
  194. * inherited by child processes created by fspr_proc_create(). This
  195. * can be changed using fspr_file_inherit_set().
  196. */
  197. APR_DECLARE(fspr_status_t) fspr_file_open(fspr_file_t **newf, const char *fname,
  198. fspr_int32_t flag, fspr_fileperms_t perm,
  199. fspr_pool_t *pool);
  200. /**
  201. * Close the specified file.
  202. * @param file The file descriptor to close.
  203. */
  204. APR_DECLARE(fspr_status_t) fspr_file_close(fspr_file_t *file);
  205. /**
  206. * Delete the specified file.
  207. * @param path The full path to the file (using / on all systems)
  208. * @param pool The pool to use.
  209. * @remark If the file is open, it won't be removed until all
  210. * instances are closed.
  211. */
  212. APR_DECLARE(fspr_status_t) fspr_file_remove(const char *path, fspr_pool_t *pool);
  213. /**
  214. * Rename the specified file.
  215. * @param from_path The full path to the original file (using / on all systems)
  216. * @param to_path The full path to the new file (using / on all systems)
  217. * @param pool The pool to use.
  218. * @warning If a file exists at the new location, then it will be
  219. * overwritten. Moving files or directories across devices may not be
  220. * possible.
  221. */
  222. APR_DECLARE(fspr_status_t) fspr_file_rename(const char *from_path,
  223. const char *to_path,
  224. fspr_pool_t *pool);
  225. /**
  226. * Copy the specified file to another file.
  227. * @param from_path The full path to the original file (using / on all systems)
  228. * @param to_path The full path to the new file (using / on all systems)
  229. * @param perms Access permissions for the new file if it is created.
  230. * In place of the usual or'd combination of file permissions, the
  231. * value APR_FILE_SOURCE_PERMS may be given, in which case the source
  232. * file's permissions are copied.
  233. * @param pool The pool to use.
  234. * @remark The new file does not need to exist, it will be created if required.
  235. * @warning If the new file already exists, its contents will be overwritten.
  236. */
  237. APR_DECLARE(fspr_status_t) fspr_file_copy(const char *from_path,
  238. const char *to_path,
  239. fspr_fileperms_t perms,
  240. fspr_pool_t *pool);
  241. /**
  242. * Append the specified file to another file.
  243. * @param from_path The full path to the source file (use / on all systems)
  244. * @param to_path The full path to the destination file (use / on all systems)
  245. * @param perms Access permissions for the destination file if it is created.
  246. * In place of the usual or'd combination of file permissions, the
  247. * value APR_FILE_SOURCE_PERMS may be given, in which case the source
  248. * file's permissions are copied.
  249. * @param pool The pool to use.
  250. * @remark The new file does not need to exist, it will be created if required.
  251. */
  252. APR_DECLARE(fspr_status_t) fspr_file_append(const char *from_path,
  253. const char *to_path,
  254. fspr_fileperms_t perms,
  255. fspr_pool_t *pool);
  256. /**
  257. * Are we at the end of the file
  258. * @param fptr The apr file we are testing.
  259. * @remark Returns APR_EOF if we are at the end of file, APR_SUCCESS otherwise.
  260. */
  261. APR_DECLARE(fspr_status_t) fspr_file_eof(fspr_file_t *fptr);
  262. /**
  263. * Open standard error as an apr file pointer.
  264. * @param thefile The apr file to use as stderr.
  265. * @param pool The pool to allocate the file out of.
  266. *
  267. * @remark The only reason that the fspr_file_open_std* functions exist
  268. * is that you may not always have a stderr/out/in on Windows. This
  269. * is generally a problem with newer versions of Windows and services.
  270. *
  271. * @remark The other problem is that the C library functions generally work
  272. * differently on Windows and Unix. So, by using fspr_file_open_std*
  273. * functions, you can get a handle to an APR struct that works with
  274. * the APR functions which are supposed to work identically on all
  275. * platforms.
  276. */
  277. APR_DECLARE(fspr_status_t) fspr_file_open_stderr(fspr_file_t **thefile,
  278. fspr_pool_t *pool);
  279. /**
  280. * open standard output as an apr file pointer.
  281. * @param thefile The apr file to use as stdout.
  282. * @param pool The pool to allocate the file out of.
  283. *
  284. * @remark See remarks for fspr_file_open_stdout.
  285. */
  286. APR_DECLARE(fspr_status_t) fspr_file_open_stdout(fspr_file_t **thefile,
  287. fspr_pool_t *pool);
  288. /**
  289. * open standard input as an apr file pointer.
  290. * @param thefile The apr file to use as stdin.
  291. * @param pool The pool to allocate the file out of.
  292. *
  293. * @remark See remarks for fspr_file_open_stdout.
  294. */
  295. APR_DECLARE(fspr_status_t) fspr_file_open_stdin(fspr_file_t **thefile,
  296. fspr_pool_t *pool);
  297. /**
  298. * Read data from the specified file.
  299. * @param thefile The file descriptor to read from.
  300. * @param buf The buffer to store the data to.
  301. * @param nbytes On entry, the number of bytes to read; on exit, the number
  302. * of bytes read.
  303. *
  304. * @remark fspr_file_read will read up to the specified number of
  305. * bytes, but never more. If there isn't enough data to fill that
  306. * number of bytes, all of the available data is read. The third
  307. * argument is modified to reflect the number of bytes read. If a
  308. * char was put back into the stream via ungetc, it will be the first
  309. * character returned.
  310. *
  311. * @remark It is not possible for both bytes to be read and an APR_EOF
  312. * or other error to be returned. APR_EINTR is never returned.
  313. */
  314. APR_DECLARE(fspr_status_t) fspr_file_read(fspr_file_t *thefile, void *buf,
  315. fspr_size_t *nbytes);
  316. /**
  317. * Write data to the specified file.
  318. * @param thefile The file descriptor to write to.
  319. * @param buf The buffer which contains the data.
  320. * @param nbytes On entry, the number of bytes to write; on exit, the number
  321. * of bytes written.
  322. *
  323. * @remark fspr_file_write will write up to the specified number of
  324. * bytes, but never more. If the OS cannot write that many bytes, it
  325. * will write as many as it can. The third argument is modified to
  326. * reflect the * number of bytes written.
  327. *
  328. * @remark It is possible for both bytes to be written and an error to
  329. * be returned. APR_EINTR is never returned.
  330. */
  331. APR_DECLARE(fspr_status_t) fspr_file_write(fspr_file_t *thefile, const void *buf,
  332. fspr_size_t *nbytes);
  333. /**
  334. * Write data from iovec array to the specified file.
  335. * @param thefile The file descriptor to write to.
  336. * @param vec The array from which to get the data to write to the file.
  337. * @param nvec The number of elements in the struct iovec array. This must
  338. * be smaller than APR_MAX_IOVEC_SIZE. If it isn't, the function
  339. * will fail with APR_EINVAL.
  340. * @param nbytes The number of bytes written.
  341. *
  342. * @remark It is possible for both bytes to be written and an error to
  343. * be returned. APR_EINTR is never returned.
  344. *
  345. * @remark fspr_file_writev is available even if the underlying
  346. * operating system doesn't provide writev().
  347. */
  348. APR_DECLARE(fspr_status_t) fspr_file_writev(fspr_file_t *thefile,
  349. const struct iovec *vec,
  350. fspr_size_t nvec, fspr_size_t *nbytes);
  351. /**
  352. * Read data from the specified file, ensuring that the buffer is filled
  353. * before returning.
  354. * @param thefile The file descriptor to read from.
  355. * @param buf The buffer to store the data to.
  356. * @param nbytes The number of bytes to read.
  357. * @param bytes_read If non-NULL, this will contain the number of bytes read.
  358. *
  359. * @remark fspr_file_read will read up to the specified number of
  360. * bytes, but never more. If there isn't enough data to fill that
  361. * number of bytes, then the process/thread will block until it is
  362. * available or EOF is reached. If a char was put back into the
  363. * stream via ungetc, it will be the first character returned.
  364. *
  365. * @remark It is possible for both bytes to be read and an error to be
  366. * returned. And if *bytes_read is less than nbytes, an accompanying
  367. * error is _always_ returned.
  368. *
  369. * @remark APR_EINTR is never returned.
  370. */
  371. APR_DECLARE(fspr_status_t) fspr_file_read_full(fspr_file_t *thefile, void *buf,
  372. fspr_size_t nbytes,
  373. fspr_size_t *bytes_read);
  374. /**
  375. * Write data to the specified file, ensuring that all of the data is
  376. * written before returning.
  377. * @param thefile The file descriptor to write to.
  378. * @param buf The buffer which contains the data.
  379. * @param nbytes The number of bytes to write.
  380. * @param bytes_written If non-NULL, set to the number of bytes written.
  381. *
  382. * @remark fspr_file_write will write up to the specified number of
  383. * bytes, but never more. If the OS cannot write that many bytes, the
  384. * process/thread will block until they can be written. Exceptional
  385. * error such as "out of space" or "pipe closed" will terminate with
  386. * an error.
  387. *
  388. * @remark It is possible for both bytes to be written and an error to
  389. * be returned. And if *bytes_written is less than nbytes, an
  390. * accompanying error is _always_ returned.
  391. *
  392. * @remark APR_EINTR is never returned.
  393. */
  394. APR_DECLARE(fspr_status_t) fspr_file_write_full(fspr_file_t *thefile,
  395. const void *buf,
  396. fspr_size_t nbytes,
  397. fspr_size_t *bytes_written);
  398. /**
  399. * Write data from iovec array to the specified file, ensuring that all of the
  400. * data is written before returning.
  401. * @param thefile The file descriptor to write to.
  402. * @param vec The array from which to get the data to write to the file.
  403. * @param nvec The number of elements in the struct iovec array. This must
  404. * be smaller than APR_MAX_IOVEC_SIZE. If it isn't, the function
  405. * will fail with APR_EINVAL.
  406. * @param nbytes The number of bytes written.
  407. *
  408. * @remark fspr_file_writev_full is available even if the underlying
  409. * operating system doesn't provide writev().
  410. */
  411. APR_DECLARE(fspr_status_t) fspr_file_writev_full(fspr_file_t *thefile,
  412. const struct iovec *vec,
  413. fspr_size_t nvec,
  414. fspr_size_t *nbytes);
  415. /**
  416. * Write a character into the specified file.
  417. * @param ch The character to write.
  418. * @param thefile The file descriptor to write to
  419. */
  420. APR_DECLARE(fspr_status_t) fspr_file_putc(char ch, fspr_file_t *thefile);
  421. /**
  422. * Read a character from the specified file.
  423. * @param ch The character to read into
  424. * @param thefile The file descriptor to read from
  425. */
  426. APR_DECLARE(fspr_status_t) fspr_file_getc(char *ch, fspr_file_t *thefile);
  427. /**
  428. * Put a character back onto a specified stream.
  429. * @param ch The character to write.
  430. * @param thefile The file descriptor to write to
  431. */
  432. APR_DECLARE(fspr_status_t) fspr_file_ungetc(char ch, fspr_file_t *thefile);
  433. /**
  434. * Read a string from the specified file.
  435. * @param str The buffer to store the string in.
  436. * @param len The length of the string
  437. * @param thefile The file descriptor to read from
  438. * @remark The buffer will be NUL-terminated if any characters are stored.
  439. */
  440. APR_DECLARE(fspr_status_t) fspr_file_gets(char *str, int len,
  441. fspr_file_t *thefile);
  442. /**
  443. * Write the string into the specified file.
  444. * @param str The string to write.
  445. * @param thefile The file descriptor to write to
  446. */
  447. APR_DECLARE(fspr_status_t) fspr_file_puts(const char *str, fspr_file_t *thefile);
  448. /**
  449. * Flush the file's buffer.
  450. * @param thefile The file descriptor to flush
  451. */
  452. APR_DECLARE(fspr_status_t) fspr_file_flush(fspr_file_t *thefile);
  453. /**
  454. * Duplicate the specified file descriptor.
  455. * @param new_file The structure to duplicate into.
  456. * @param old_file The file to duplicate.
  457. * @param p The pool to use for the new file.
  458. * @remark *new_file must point to a valid fspr_file_t, or point to NULL.
  459. */
  460. APR_DECLARE(fspr_status_t) fspr_file_dup(fspr_file_t **new_file,
  461. fspr_file_t *old_file,
  462. fspr_pool_t *p);
  463. /**
  464. * Duplicate the specified file descriptor and close the original
  465. * @param new_file The old file that is to be closed and reused
  466. * @param old_file The file to duplicate
  467. * @param p The pool to use for the new file
  468. *
  469. * @remark new_file MUST point at a valid fspr_file_t. It cannot be NULL.
  470. */
  471. APR_DECLARE(fspr_status_t) fspr_file_dup2(fspr_file_t *new_file,
  472. fspr_file_t *old_file,
  473. fspr_pool_t *p);
  474. /**
  475. * Move the specified file descriptor to a new pool
  476. * @param new_file Pointer in which to return the new fspr_file_t
  477. * @param old_file The file to move
  478. * @param p The pool to which the descriptor is to be moved
  479. * @remark Unlike fspr_file_dup2(), this function doesn't do an
  480. * OS dup() operation on the underlying descriptor; it just
  481. * moves the descriptor's fspr_file_t wrapper to a new pool.
  482. * @remark The new pool need not be an ancestor of old_file's pool.
  483. * @remark After calling this function, old_file may not be used
  484. */
  485. APR_DECLARE(fspr_status_t) fspr_file_setaside(fspr_file_t **new_file,
  486. fspr_file_t *old_file,
  487. fspr_pool_t *p);
  488. /**
  489. * Move the read/write file offset to a specified byte within a file.
  490. * @param thefile The file descriptor
  491. * @param where How to move the pointer, one of:
  492. * <PRE>
  493. * APR_SET -- set the offset to offset
  494. * APR_CUR -- add the offset to the current position
  495. * APR_END -- add the offset to the current file size
  496. * </PRE>
  497. * @param offset The offset to move the pointer to.
  498. * @remark The third argument is modified to be the offset the pointer
  499. was actually moved to.
  500. */
  501. APR_DECLARE(fspr_status_t) fspr_file_seek(fspr_file_t *thefile,
  502. fspr_seek_where_t where,
  503. fspr_off_t *offset);
  504. /**
  505. * Create an anonymous pipe.
  506. * @param in The file descriptor to use as input to the pipe.
  507. * @param out The file descriptor to use as output from the pipe.
  508. * @param pool The pool to operate on.
  509. * @remark By default, the returned file descriptors will be inherited
  510. * by child processes created using fspr_proc_create(). This can be
  511. * changed using fspr_file_inherit_unset().
  512. */
  513. APR_DECLARE(fspr_status_t) fspr_file_pipe_create(fspr_file_t **in,
  514. fspr_file_t **out,
  515. fspr_pool_t *pool);
  516. /**
  517. * Create a named pipe.
  518. * @param filename The filename of the named pipe
  519. * @param perm The permissions for the newly created pipe.
  520. * @param pool The pool to operate on.
  521. */
  522. APR_DECLARE(fspr_status_t) fspr_file_namedpipe_create(const char *filename,
  523. fspr_fileperms_t perm,
  524. fspr_pool_t *pool);
  525. /**
  526. * Get the timeout value for a pipe or manipulate the blocking state.
  527. * @param thepipe The pipe we are getting a timeout for.
  528. * @param timeout The current timeout value in microseconds.
  529. */
  530. APR_DECLARE(fspr_status_t) fspr_file_pipe_timeout_get(fspr_file_t *thepipe,
  531. fspr_interval_time_t *timeout);
  532. /**
  533. * Set the timeout value for a pipe or manipulate the blocking state.
  534. * @param thepipe The pipe we are setting a timeout on.
  535. * @param timeout The timeout value in microseconds. Values < 0 mean wait
  536. * forever, 0 means do not wait at all.
  537. */
  538. APR_DECLARE(fspr_status_t) fspr_file_pipe_timeout_set(fspr_file_t *thepipe,
  539. fspr_interval_time_t timeout);
  540. /** file (un)locking functions. */
  541. /**
  542. * Establish a lock on the specified, open file. The lock may be advisory
  543. * or mandatory, at the discretion of the platform. The lock applies to
  544. * the file as a whole, rather than a specific range. Locks are established
  545. * on a per-thread/process basis; a second lock by the same thread will not
  546. * block.
  547. * @param thefile The file to lock.
  548. * @param type The type of lock to establish on the file.
  549. */
  550. APR_DECLARE(fspr_status_t) fspr_file_lock(fspr_file_t *thefile, int type);
  551. /**
  552. * Remove any outstanding locks on the file.
  553. * @param thefile The file to unlock.
  554. */
  555. APR_DECLARE(fspr_status_t) fspr_file_unlock(fspr_file_t *thefile);
  556. /**accessor and general file_io functions. */
  557. /**
  558. * return the file name of the current file.
  559. * @param new_path The path of the file.
  560. * @param thefile The currently open file.
  561. */
  562. APR_DECLARE(fspr_status_t) fspr_file_name_get(const char **new_path,
  563. fspr_file_t *thefile);
  564. /**
  565. * Return the data associated with the current file.
  566. * @param data The user data associated with the file.
  567. * @param key The key to use for retreiving data associated with this file.
  568. * @param file The currently open file.
  569. */
  570. APR_DECLARE(fspr_status_t) fspr_file_data_get(void **data, const char *key,
  571. fspr_file_t *file);
  572. /**
  573. * Set the data associated with the current file.
  574. * @param file The currently open file.
  575. * @param data The user data to associate with the file.
  576. * @param key The key to use for assocaiteing data with the file.
  577. * @param cleanup The cleanup routine to use when the file is destroyed.
  578. */
  579. APR_DECLARE(fspr_status_t) fspr_file_data_set(fspr_file_t *file, void *data,
  580. const char *key,
  581. fspr_status_t (*cleanup)(void *));
  582. /**
  583. * Write a string to a file using a printf format.
  584. * @param fptr The file to write to.
  585. * @param format The format string
  586. * @param ... The values to substitute in the format string
  587. * @return The number of bytes written
  588. */
  589. APR_DECLARE_NONSTD(int) fspr_file_printf(fspr_file_t *fptr,
  590. const char *format, ...)
  591. __attribute__((format(printf,2,3)));
  592. /**
  593. * set the specified file's permission bits.
  594. * @param fname The file (name) to apply the permissions to.
  595. * @param perms The permission bits to apply to the file.
  596. *
  597. * @warning Some platforms may not be able to apply all of the
  598. * available permission bits; APR_INCOMPLETE will be returned if some
  599. * permissions are specified which could not be set.
  600. *
  601. * @warning Platforms which do not implement this feature will return
  602. * APR_ENOTIMPL.
  603. */
  604. APR_DECLARE(fspr_status_t) fspr_file_perms_set(const char *fname,
  605. fspr_fileperms_t perms);
  606. /**
  607. * Set attributes of the specified file.
  608. * @param fname The full path to the file (using / on all systems)
  609. * @param attributes Or'd combination of
  610. * <PRE>
  611. * APR_FILE_ATTR_READONLY - make the file readonly
  612. * APR_FILE_ATTR_EXECUTABLE - make the file executable
  613. * APR_FILE_ATTR_HIDDEN - make the file hidden
  614. * </PRE>
  615. * @param attr_mask Mask of valid bits in attributes.
  616. * @param pool the pool to use.
  617. * @remark This function should be used in preference to explict manipulation
  618. * of the file permissions, because the operations to provide these
  619. * attributes are platform specific and may involve more than simply
  620. * setting permission bits.
  621. * @warning Platforms which do not implement this feature will return
  622. * APR_ENOTIMPL.
  623. */
  624. APR_DECLARE(fspr_status_t) fspr_file_attrs_set(const char *fname,
  625. fspr_fileattrs_t attributes,
  626. fspr_fileattrs_t attr_mask,
  627. fspr_pool_t *pool);
  628. /**
  629. * Set the mtime of the specified file.
  630. * @param fname The full path to the file (using / on all systems)
  631. * @param mtime The mtime to apply to the file.
  632. * @param pool The pool to use.
  633. * @warning Platforms which do not implement this feature will return
  634. * APR_ENOTIMPL.
  635. */
  636. APR_DECLARE(fspr_status_t) fspr_file_mtime_set(const char *fname,
  637. fspr_time_t mtime,
  638. fspr_pool_t *pool);
  639. /**
  640. * Create a new directory on the file system.
  641. * @param path the path for the directory to be created. (use / on all systems)
  642. * @param perm Permissions for the new direcoty.
  643. * @param pool the pool to use.
  644. */
  645. APR_DECLARE(fspr_status_t) fspr_dir_make(const char *path, fspr_fileperms_t perm,
  646. fspr_pool_t *pool);
  647. /** Creates a new directory on the file system, but behaves like
  648. * 'mkdir -p'. Creates intermediate directories as required. No error
  649. * will be reported if PATH already exists.
  650. * @param path the path for the directory to be created. (use / on all systems)
  651. * @param perm Permissions for the new direcoty.
  652. * @param pool the pool to use.
  653. */
  654. APR_DECLARE(fspr_status_t) fspr_dir_make_recursive(const char *path,
  655. fspr_fileperms_t perm,
  656. fspr_pool_t *pool);
  657. /**
  658. * Remove directory from the file system.
  659. * @param path the path for the directory to be removed. (use / on all systems)
  660. * @param pool the pool to use.
  661. */
  662. APR_DECLARE(fspr_status_t) fspr_dir_remove(const char *path, fspr_pool_t *pool);
  663. /**
  664. * get the specified file's stats.
  665. * @param finfo Where to store the information about the file.
  666. * @param wanted The desired fspr_finfo_t fields, as a bit flag of APR_FINFO_ values
  667. * @param thefile The file to get information about.
  668. */
  669. APR_DECLARE(fspr_status_t) fspr_file_info_get(fspr_finfo_t *finfo,
  670. fspr_int32_t wanted,
  671. fspr_file_t *thefile);
  672. /**
  673. * Truncate the file's length to the specified offset
  674. * @param fp The file to truncate
  675. * @param offset The offset to truncate to.
  676. */
  677. APR_DECLARE(fspr_status_t) fspr_file_trunc(fspr_file_t *fp, fspr_off_t offset);
  678. /**
  679. * Retrieve the flags that were passed into fspr_file_open()
  680. * when the file was opened.
  681. * @return fspr_int32_t the flags
  682. */
  683. APR_DECLARE(fspr_int32_t) fspr_file_flags_get(fspr_file_t *f);
  684. /**
  685. * Get the pool used by the file.
  686. */
  687. APR_POOL_DECLARE_ACCESSOR(file);
  688. /**
  689. * Set a file to be inherited by child processes.
  690. *
  691. */
  692. APR_DECLARE_INHERIT_SET(file);
  693. /**
  694. * Unset a file from being inherited by child processes.
  695. */
  696. APR_DECLARE_INHERIT_UNSET(file);
  697. /**
  698. * Open a temporary file
  699. * @param fp The apr file to use as a temporary file.
  700. * @param templ The template to use when creating a temp file.
  701. * @param flags The flags to open the file with. If this is zero,
  702. * the file is opened with
  703. * APR_CREATE | APR_READ | APR_WRITE | APR_EXCL | APR_DELONCLOSE
  704. * @param p The pool to allocate the file out of.
  705. * @remark
  706. * This function generates a unique temporary file name from template.
  707. * The last six characters of template must be XXXXXX and these are replaced
  708. * with a string that makes the filename unique. Since it will be modified,
  709. * template must not be a string constant, but should be declared as a character
  710. * array.
  711. *
  712. */
  713. APR_DECLARE(fspr_status_t) fspr_file_mktemp(fspr_file_t **fp, char *templ,
  714. fspr_int32_t flags, fspr_pool_t *p);
  715. /**
  716. * Find an existing directory suitable as a temporary storage location.
  717. * @param temp_dir The temp directory.
  718. * @param p The pool to use for any necessary allocations.
  719. * @remark
  720. * This function uses an algorithm to search for a directory that an
  721. * an application can use for temporary storage. Once such a
  722. * directory is found, that location is cached by the library. Thus,
  723. * callers only pay the cost of this algorithm once if that one time
  724. * is successful.
  725. *
  726. */
  727. APR_DECLARE(fspr_status_t) fspr_temp_dir_get(const char **temp_dir,
  728. fspr_pool_t *p);
  729. /** @} */
  730. #ifdef __cplusplus
  731. }
  732. #endif
  733. #endif /* ! APR_FILE_IO_H */