GME  13
apr_arch_file_io.h
Go to the documentation of this file.
00001 /* Licensed to the Apache Software Foundation (ASF) under one or more
00002  * contributor license agreements.  See the NOTICE file distributed with
00003  * this work for additional information regarding copyright ownership.
00004  * The ASF licenses this file to You under the Apache License, Version 2.0
00005  * (the "License"); you may not use this file except in compliance with
00006  * the License.  You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef FILE_IO_H
00018 #define FILE_IO_H
00019 
00020 #include "apr.h"
00021 #include "apr_private.h"
00022 #include "apr_pools.h"
00023 #include "apr_general.h"
00024 #include "apr_tables.h"
00025 #include "apr_thread_mutex.h"
00026 #include "apr_file_io.h"
00027 #include "apr_file_info.h"
00028 #include "apr_errno.h"
00029 #include "apr_arch_misc.h"
00030 #include "apr_poll.h"
00031 
00032 #ifdef HAVE_SYS_STAT_H
00033 #include <sys/stat.h>
00034 #endif
00035 #if APR_HAVE_SYS_TYPES_H
00036 #include <sys/types.h>
00037 #endif
00038 #ifdef HAVE_SYS_FCNTL_H
00039 #include <fcntl.h>
00040 #endif
00041 #ifdef HAVE_TIME_H
00042 #include <time.h>
00043 #endif
00044 #if APR_HAVE_DIRENT_H
00045 #include <dirent.h>
00046 #endif
00047 #ifdef HAVE_MALLOC_H
00048 #include <malloc.h>
00049 #endif
00050 
00051 #if APR_HAS_UNICODE_FS
00052 #include "arch/win32/apr_arch_utf8.h"
00053 #include <wchar.h>
00054 
00055 /* Helper functions for the WinNT ApiW() functions.  APR treats all
00056  * resource identifiers (files, etc) by their UTF-8 name, to provide 
00057  * access to all named identifiers.  [UTF-8 completely maps Unicode 
00058  * into char type strings.]
00059  *
00060  * The _path flavors below provide us fast mappings of the
00061  * Unicode filename //?/D:/path and //?/UNC/mach/share/path mappings,
00062  * which allow unlimited (well, 32000 wide character) length names.
00063  * These prefixes may appear in Unicode, but must not appear in the
00064  * Ascii API calls.  So we tack them on in utf8_to_unicode_path, and
00065  * strip them right back off in unicode_to_utf8_path.
00066  */
00067 apr_status_t utf8_to_unicode_path(apr_wchar_t* dststr, apr_size_t dstchars, 
00068                                   const char* srcstr);
00069 apr_status_t unicode_to_utf8_path(char* dststr, apr_size_t dstchars, 
00070                                   const apr_wchar_t* srcstr);
00071 
00072 #endif /* APR_HAS_UNICODE_FS */
00073 
00074 /* Another Helper functions for the WinNT ApiW() functions.  We need to
00075  * derive some 'resource' names (max length 255 characters, prefixed with
00076  * Global/ or Local/ on WinNT) from something that looks like a filename.
00077  * Since 'resource' names never contain slashes, convert these to '_'s
00078  * and return the appropriate char* or wchar* for ApiA or ApiW calls.
00079  */
00080 
00081 void *res_name_from_filename(const char *file, int global, apr_pool_t *pool);
00082 
00083 #define APR_FILE_MAX MAX_PATH
00084 
00085 #define APR_FILE_DEFAULT_BUFSIZE 4096
00086 /* For backwards-compat */
00087 #define APR_FILE_BUFSIZE APR_FILE_DEFAULT_BUFSIZE
00088 
00089 /* obscure ommissions from msvc's sys/stat.h */
00090 #ifdef _MSC_VER
00091 #define S_IFIFO        _S_IFIFO /* pipe */
00092 #define S_IFBLK        0060000  /* Block Special */
00093 #define S_IFLNK        0120000  /* Symbolic Link */
00094 #define S_IFSOCK       0140000  /* Socket */
00095 #define S_IFWHT        0160000  /* Whiteout */
00096 #endif
00097 
00098 /* Internal Flags for apr_file_open */
00099 #define APR_OPENINFO     0x00100000 /* Open without READ or WRITE access */
00100 #define APR_OPENLINK     0x00200000 /* Open a link itself, if supported */
00101 #define APR_READCONTROL  0x00400000 /* Read the file's owner/perms */
00102 #define APR_WRITECONTROL 0x00800000 /* Modify the file's owner/perms */
00103 /* #define APR_INHERIT   0x01000000 -- Defined in apr_arch_inherit.h! */
00104 #define APR_STDIN_FLAG   0x02000000 /* Obtained via apr_file_open_stdin() */
00105 #define APR_STDOUT_FLAG  0x04000000 /* Obtained via apr_file_open_stdout() */
00106 #define APR_STDERR_FLAG  0x06000000 /* Obtained via apr_file_open_stderr() */
00107 #define APR_STD_FLAGS    (APR_STDIN_FLAG | APR_STDOUT_FLAG | APR_STDERR_FLAG)
00108 #define APR_WRITEATTRS   0x08000000 /* Modify the file's attributes */
00109 
00110 /* Entries missing from the MSVC 5.0 Win32 SDK:
00111  */
00112 #ifndef FILE_ATTRIBUTE_DEVICE
00113 #define FILE_ATTRIBUTE_DEVICE        0x00000040
00114 #endif
00115 #ifndef FILE_ATTRIBUTE_REPARSE_POINT
00116 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
00117 #endif
00118 #ifndef FILE_FLAG_OPEN_NO_RECALL
00119 #define FILE_FLAG_OPEN_NO_RECALL     0x00100000
00120 #endif
00121 #ifndef FILE_FLAG_OPEN_REPARSE_POINT
00122 #define FILE_FLAG_OPEN_REPARSE_POINT 0x00200000
00123 #endif
00124 #ifndef TRUSTEE_IS_WELL_KNOWN_GROUP
00125 #define TRUSTEE_IS_WELL_KNOWN_GROUP  5
00126 #endif
00127 
00128 /* Information bits available from the WIN32 FindFirstFile function */
00129 #define APR_FINFO_WIN32_DIR (APR_FINFO_NAME  | APR_FINFO_TYPE \
00130                            | APR_FINFO_CTIME | APR_FINFO_ATIME \
00131                            | APR_FINFO_MTIME | APR_FINFO_SIZE)
00132 
00133 /* Sneak the Readonly bit through finfo->protection for internal use _only_ */
00134 #define APR_FREADONLY 0x10000000 
00135 
00136 /* Private function for apr_stat/lstat/getfileinfo/dir_read */
00137 int fillin_fileinfo(apr_finfo_t *finfo, WIN32_FILE_ATTRIBUTE_DATA *wininfo, 
00138                     int byhandle, apr_int32_t wanted);
00139 
00140 /* Private function that extends apr_stat/lstat/getfileinfo/dir_read */
00141 apr_status_t more_finfo(apr_finfo_t *finfo, const void *ufile, 
00142                         apr_int32_t wanted, int whatfile);
00143 
00144 /* whatfile types for the ufile arg */
00145 #define MORE_OF_HANDLE 0
00146 #define MORE_OF_FSPEC  1
00147 #define MORE_OF_WFSPEC 2
00148 
00149 /* quick run-down of fields in windows' apr_file_t structure that may have 
00150  * obvious uses.
00151  * fname --  the filename as passed to the open call.
00152  * dwFileAttricutes -- Attributes used to open the file.
00153  * append -- Windows doesn't support the append concept when opening files.
00154  *           APR needs to keep track of this, and always make sure we append
00155  *           correctly when writing to a file with this flag set TRUE.
00156  */
00157 
00158 /* for apr_poll.c */
00159 #define filedes filehand
00160 
00161 struct apr_file_t {
00162     apr_pool_t *pool;
00163     HANDLE filehand;
00164     BOOLEAN pipe;              /* Is this a pipe of a file? */
00165     OVERLAPPED *pOverlapped;
00166     apr_interval_time_t timeout;
00167     apr_int32_t flags;
00168 
00169     /* File specific info */
00170     apr_finfo_t *finfo;
00171     char *fname;
00172     DWORD dwFileAttributes;
00173     int eof_hit;
00174     BOOLEAN buffered;          // Use buffered I/O?
00175     int ungetchar;             // Last char provided by an unget op. (-1 = no char)
00176     int append; 
00177 
00178     /* Stuff for buffered mode */
00179     char *buffer;
00180     apr_size_t bufpos;         // Read/Write position in buffer
00181     apr_size_t bufsize;        // The size of the buffer
00182     apr_size_t dataRead;       // amount of valid data read into buffer
00183     int direction;             // buffer being used for 0 = read, 1 = write
00184     apr_off_t filePtr;         // position in file of handle
00185     apr_thread_mutex_t *mutex; // mutex semaphore, must be owned to access the above fields
00186 
00187     /* if there is a timeout set, then this pollset is used */
00188     apr_pollset_t *pollset;
00189 
00190     /* Pipe specific info */    
00191 };
00192 
00193 struct apr_dir_t {
00194     apr_pool_t *pool;
00195     HANDLE dirhand;
00196     apr_size_t rootlen;
00197     char *dirname;
00198     char *name;
00199     union {
00200 #if APR_HAS_UNICODE_FS
00201         struct {
00202             WIN32_FIND_DATAW *entry;
00203         } w;
00204 #endif
00205 #if APR_HAS_ANSI_FS
00206         struct {
00207             WIN32_FIND_DATAA *entry;
00208         } n;
00209 #endif        
00210     };
00211     int bof;
00212 };
00213 
00214 /* There are many goofy characters the filesystem can't accept
00215  * or can confound the cmd.exe shell.  Here's the list
00216  * [declared in filesys.c]
00217  */
00218 extern const char apr_c_is_fnchar[256];
00219 
00220 #define IS_FNCHAR(c) (apr_c_is_fnchar[(unsigned char)(c)] & 1)
00221 #define IS_SHCHAR(c) ((apr_c_is_fnchar[(unsigned char)(c)] & 2) == 2)
00222 
00223 
00224 /* If the user passes APR_FILEPATH_TRUENAME to either
00225  * apr_filepath_root or apr_filepath_merge, this fn determines
00226  * that the root really exists.  It's expensive, wouldn't want
00227  * to do this too frequenly.
00228  */
00229 apr_status_t filepath_root_test(char *path, apr_pool_t *p);
00230 
00231 
00232 /* The apr_filepath_merge wants to canonicalize the cwd to the 
00233  * addpath if the user passes NULL as the old root path (this
00234  * isn't true of an empty string "", which won't be concatenated.
00235  *
00236  * But we need to figure out what the cwd of a given volume is,
00237  * when the user passes D:foo.  This fn will determine D:'s cwd.
00238  *
00239  * If flags includes the bit APR_FILEPATH_NATIVE, the path returned
00240  * is in the os-native format.
00241  */
00242 apr_status_t filepath_drive_get(char **rootpath, char drive, 
00243                                 apr_int32_t flags, apr_pool_t *p);
00244 
00245 
00246 /* If the user passes d: vs. D: (or //mach/share vs. //MACH/SHARE),
00247  * we need to fold the case to canonical form.  This function is
00248  * supposed to do so.
00249  */
00250 apr_status_t filepath_root_case(char **rootpath, char *root, apr_pool_t *p);
00251 
00252 
00253 apr_status_t file_cleanup(void *);
00254 
00255 extern apr_status_t
00256 apr_file_socket_pipe_create(apr_file_t **in,
00257                             apr_file_t **out,
00258                             apr_pool_t *p);
00259 
00260 extern apr_status_t
00261 apr_file_socket_pipe_close(apr_file_t *file);
00262 
00263 #endif  /* ! FILE_IO_H */