fspr_arch_file_io.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 FILE_IO_H
  17. #define FILE_IO_H
  18. #include "fspr.h"
  19. #include "fspr_private.h"
  20. #include "fspr_pools.h"
  21. #include "fspr_general.h"
  22. #include "fspr_tables.h"
  23. #include "fspr_thread_mutex.h"
  24. #include "fspr_file_io.h"
  25. #include "fspr_file_info.h"
  26. #include "fspr_errno.h"
  27. #include "fspr_arch_misc.h"
  28. #include "fspr_poll.h"
  29. #ifdef HAVE_SYS_STAT_H
  30. #include <sys/stat.h>
  31. #endif
  32. #if APR_HAVE_SYS_TYPES_H
  33. #include <sys/types.h>
  34. #endif
  35. #ifdef HAVE_SYS_FCNTL_H
  36. #include <fcntl.h>
  37. #endif
  38. #ifdef HAVE_TIME_H
  39. #include <time.h>
  40. #endif
  41. #if APR_HAVE_DIRENT_H
  42. #include <dirent.h>
  43. #endif
  44. #ifdef HAVE_MALLOC_H
  45. #include <malloc.h>
  46. #endif
  47. #if APR_HAS_UNICODE_FS
  48. #include "arch/win32/fspr_arch_utf8.h"
  49. #include <wchar.h>
  50. typedef fspr_uint16_t fspr_wchar_t;
  51. /* Helper functions for the WinNT ApiW() functions. APR treats all
  52. * resource identifiers (files, etc) by their UTF-8 name, to provide
  53. * access to all named identifiers. [UTF-8 completely maps Unicode
  54. * into char type strings.]
  55. *
  56. * The _path flavors below provide us fast mappings of the
  57. * Unicode filename //?/D:/path and //?/UNC/mach/share/path mappings,
  58. * which allow unlimited (well, 32000 wide character) length names.
  59. * These prefixes may appear in Unicode, but must not appear in the
  60. * Ascii API calls. So we tack them on in utf8_to_unicode_path, and
  61. * strip them right back off in unicode_to_utf8_path.
  62. */
  63. fspr_status_t utf8_to_unicode_path(fspr_wchar_t* dststr, fspr_size_t dstchars,
  64. const char* srcstr);
  65. fspr_status_t unicode_to_utf8_path(char* dststr, fspr_size_t dstchars,
  66. const fspr_wchar_t* srcstr);
  67. #endif /* APR_HAS_UNICODE_FS */
  68. /* Another Helper functions for the WinNT ApiW() functions. We need to
  69. * derive some 'resource' names (max length 255 characters, prefixed with
  70. * Global/ or Local/ on WinNT) from something that looks like a filename.
  71. * Since 'resource' names never contain slashes, convert these to '_'s
  72. * and return the appropriate char* or wchar* for ApiA or ApiW calls.
  73. */
  74. void *res_name_from_filename(const char *file, int global, fspr_pool_t *pool);
  75. #define APR_FILE_MAX MAX_PATH
  76. #define APR_FILE_BUFSIZE 4096
  77. /* obscure ommissions from msvc's sys/stat.h */
  78. #ifdef _MSC_VER
  79. #define S_IFIFO _S_IFIFO /* pipe */
  80. #define S_IFBLK 0060000 /* Block Special */
  81. #define S_IFLNK 0120000 /* Symbolic Link */
  82. #define S_IFSOCK 0140000 /* Socket */
  83. #define S_IFWHT 0160000 /* Whiteout */
  84. #endif
  85. /* Internal Flags for fspr_file_open */
  86. #define APR_OPENINFO 0x00100000 /* Open without READ or WRITE access */
  87. #define APR_OPENLINK 0x00200000 /* Open a link itself, if supported */
  88. #define APR_READCONTROL 0x00400000 /* Read the file's owner/perms */
  89. #define APR_WRITECONTROL 0x00800000 /* Modifythe file's owner/perms */
  90. #define APR_WRITEATTRS 0x01000000 /* Modify the file's attributes */
  91. /* Entries missing from the MSVC 5.0 Win32 SDK:
  92. */
  93. #ifndef FILE_ATTRIBUTE_DEVICE
  94. #define FILE_ATTRIBUTE_DEVICE 0x00000040
  95. #endif
  96. #ifndef FILE_ATTRIBUTE_REPARSE_POINT
  97. #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
  98. #endif
  99. #ifndef FILE_FLAG_OPEN_NO_RECALL
  100. #define FILE_FLAG_OPEN_NO_RECALL 0x00100000
  101. #endif
  102. #ifndef FILE_FLAG_OPEN_REPARSE_POINT
  103. #define FILE_FLAG_OPEN_REPARSE_POINT 0x00200000
  104. #endif
  105. #ifndef TRUSTEE_IS_WELL_KNOWN_GROUP
  106. #define TRUSTEE_IS_WELL_KNOWN_GROUP 5
  107. #endif
  108. /* Information bits available from the WIN32 FindFirstFile function */
  109. #define APR_FINFO_WIN32_DIR (APR_FINFO_NAME | APR_FINFO_TYPE \
  110. | APR_FINFO_CTIME | APR_FINFO_ATIME \
  111. | APR_FINFO_MTIME | APR_FINFO_SIZE)
  112. /* Sneak the Readonly bit through finfo->protection for internal use _only_ */
  113. #define APR_FREADONLY 0x10000000
  114. /* Private function for fspr_stat/lstat/getfileinfo/dir_read */
  115. int fillin_fileinfo(fspr_finfo_t *finfo, WIN32_FILE_ATTRIBUTE_DATA *wininfo,
  116. int byhandle, fspr_int32_t wanted);
  117. /* Private function that extends fspr_stat/lstat/getfileinfo/dir_read */
  118. fspr_status_t more_finfo(fspr_finfo_t *finfo, const void *ufile,
  119. fspr_int32_t wanted, int whatfile);
  120. /* whatfile types for the ufile arg */
  121. #define MORE_OF_HANDLE 0
  122. #define MORE_OF_FSPEC 1
  123. #define MORE_OF_WFSPEC 2
  124. /* quick run-down of fields in windows' fspr_file_t structure that may have
  125. * obvious uses.
  126. * fname -- the filename as passed to the open call.
  127. * dwFileAttricutes -- Attributes used to open the file.
  128. * append -- Windows doesn't support the append concept when opening files.
  129. * APR needs to keep track of this, and always make sure we append
  130. * correctly when writing to a file with this flag set TRUE.
  131. */
  132. // for fspr_poll.c;
  133. #define filedes filehand
  134. struct fspr_file_t {
  135. fspr_pool_t *pool;
  136. HANDLE filehand;
  137. BOOLEAN pipe; // Is this a pipe of a file?
  138. OVERLAPPED *pOverlapped;
  139. fspr_interval_time_t timeout;
  140. fspr_int32_t flags;
  141. /* File specific info */
  142. fspr_finfo_t *finfo;
  143. char *fname;
  144. DWORD dwFileAttributes;
  145. int eof_hit;
  146. BOOLEAN buffered; // Use buffered I/O?
  147. int ungetchar; // Last char provided by an unget op. (-1 = no char)
  148. int append;
  149. /* Stuff for buffered mode */
  150. char *buffer;
  151. fspr_size_t bufpos; // Read/Write position in buffer
  152. fspr_size_t dataRead; // amount of valid data read into buffer
  153. int direction; // buffer being used for 0 = read, 1 = write
  154. fspr_off_t filePtr; // position in file of handle
  155. fspr_thread_mutex_t *mutex; // mutex semaphore, must be owned to access the above fields
  156. /* if there is a timeout set, then this pollset is used */
  157. fspr_pollset_t *pollset;
  158. /* Pipe specific info */
  159. };
  160. struct fspr_dir_t {
  161. fspr_pool_t *pool;
  162. HANDLE dirhand;
  163. fspr_size_t rootlen;
  164. char *dirname;
  165. char *name;
  166. union {
  167. #if APR_HAS_UNICODE_FS
  168. struct {
  169. WIN32_FIND_DATAW *entry;
  170. } w;
  171. #endif
  172. #if APR_HAS_ANSI_FS
  173. struct {
  174. WIN32_FIND_DATAA *entry;
  175. } n;
  176. #endif
  177. };
  178. int bof;
  179. };
  180. /* There are many goofy characters the filesystem can't accept
  181. * or can confound the cmd.exe shell. Here's the list
  182. * [declared in filesys.c]
  183. */
  184. extern const char fspr_c_is_fnchar[256];
  185. #define IS_FNCHAR(c) (fspr_c_is_fnchar[(unsigned char)(c)] & 1)
  186. #define IS_SHCHAR(c) ((fspr_c_is_fnchar[(unsigned char)(c)] & 2) == 2)
  187. /* If the user passes APR_FILEPATH_TRUENAME to either
  188. * fspr_filepath_root or fspr_filepath_merge, this fn determines
  189. * that the root really exists. It's expensive, wouldn't want
  190. * to do this too frequenly.
  191. */
  192. fspr_status_t filepath_root_test(char *path, fspr_pool_t *p);
  193. /* The fspr_filepath_merge wants to canonicalize the cwd to the
  194. * addpath if the user passes NULL as the old root path (this
  195. * isn't true of an empty string "", which won't be concatenated.
  196. *
  197. * But we need to figure out what the cwd of a given volume is,
  198. * when the user passes D:foo. This fn will determine D:'s cwd.
  199. *
  200. * If flags includes the bit APR_FILEPATH_NATIVE, the path returned
  201. * is in the os-native format.
  202. */
  203. fspr_status_t filepath_drive_get(char **rootpath, char drive,
  204. fspr_int32_t flags, fspr_pool_t *p);
  205. /* If the user passes d: vs. D: (or //mach/share vs. //MACH/SHARE),
  206. * we need to fold the case to canonical form. This function is
  207. * supposed to do so.
  208. */
  209. fspr_status_t filepath_root_case(char **rootpath, char *root, fspr_pool_t *p);
  210. fspr_status_t file_cleanup(void *);
  211. /**
  212. * Internal function to create a Win32/NT pipe that respects some async
  213. * timeout options.
  214. * @param in new read end of the created pipe
  215. * @param out new write end of the created pipe
  216. * @param blocking_mode one of
  217. * <pre>
  218. * APR_FULL_BLOCK
  219. * APR_READ_BLOCK
  220. * APR_WRITE_BLOCK
  221. * APR_FULL_NONBLOCK
  222. * </pre>
  223. * @remark It so happens that APR_FULL_BLOCK and APR_FULL_NONBLOCK
  224. * are common to fspr_procattr_io_set() in, out and err modes.
  225. * Because APR_CHILD_BLOCK and APR_WRITE_BLOCK share the same value,
  226. * as do APR_PARENT_BLOCK and APR_READ_BLOCK, it's possible to use
  227. * that value directly for creating the stdout/stderr pipes. When
  228. * creating the stdin pipe, the values must be transposed.
  229. * @see fspr_procattr_io_set
  230. */
  231. fspr_status_t fspr_create_nt_pipe(fspr_file_t **in, fspr_file_t **out,
  232. fspr_int32_t blocking_mode,
  233. fspr_pool_t *p);
  234. /** @see fspr_create_nt_pipe */
  235. #define APR_READ_BLOCK 3
  236. /** @see fspr_create_nt_pipe */
  237. #define APR_WRITE_BLOCK 4
  238. #endif /* ! FILE_IO_H */