Drizzled Public API Documentation

my_sys.h
00001 /* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 MySQL
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 
00023 #pragma once
00024 
00025 #ifdef __cplusplus
00026 # include <cstdio>
00027 #else
00028 # include <stdio.h>
00029 #endif
00030 
00031 #include <errno.h>
00032 
00033 #include <drizzled/internal/my_pthread.h>
00034 
00035 #include <drizzled/charset_info.h>                    /* for CHARSET_INFO */
00036 #include <stdarg.h>
00037 #include <drizzled/internal/aio_result.h>
00038 
00039 #include <drizzled/memory/root.h>
00040 #include <drizzled/error.h>
00041 
00042 #ifndef errno       /* did we already get it? */
00043 #ifdef HAVE_ERRNO_AS_DEFINE
00044 #include <errno.h>      /* errno is a define */
00045 #else
00046 extern int errno;     /* declare errno */
00047 #endif
00048 #endif          /* #ifndef errno */
00049 
00050 #ifdef HAVE_SYS_MMAN_H 
00051 #include <sys/mman.h>
00052 #endif
00053 
00054 #include <drizzled/qsort_cmp.h>
00055 
00056 #include <drizzled/visibility.h>
00057 
00058 namespace drizzled
00059 {
00060 namespace internal
00061 {
00062 
00063 #ifndef MAP_NOSYNC
00064 #define MAP_NOSYNC      0
00065 #endif
00066 #ifndef MAP_NORESERVE
00067 #define MAP_NORESERVE 0         /* For irix and AIX */
00068 #endif
00069 
00070 /*
00071   EDQUOT is used only in 3 C files only in mysys/. If it does not exist on
00072   system, we set it to some value which can never happen.
00073 */
00074 #ifndef EDQUOT
00075 #define EDQUOT (-1)
00076 #endif
00077 
00078 /* Sun Studio does not inject this into main namespace yet */
00079 #if defined(__cplusplus)
00080   using std::FILE;
00081 #endif
00082 
00083 #define MY_INIT(name);    { ::drizzled::internal::my_progname= name; ::drizzled::internal::my_init(); }
00084 
00085 
00086   /* General bitmaps for my_func's */
00087 #define MY_FFNF   1 /* Fatal if file not found */
00088 #define MY_FNABP  2 /* Fatal if not all bytes read/writen */
00089 #define MY_NABP   4 /* Error if not all bytes read/writen */
00090 #define MY_FAE    8 /* Fatal if any error */
00091 #define MY_WME    16  /* Write message on error */
00092 #define MY_WAIT_IF_FULL 32  /* Wait and try again if disk full error */
00093 #define MY_IGNORE_BADFD 32      /* my_sync: ignore 'bad descriptor' errors */
00094 #define MY_SYNC_DIR     1024    /* my_create/delete/rename: sync directory */
00095 #define MY_FULL_IO     512      /* For my_read - loop intil I/O is complete */
00096 #define MY_DONT_CHECK_FILESIZE 128 /* Option to init_io_cache() */
00097 #define MY_LINK_WARNING 32  /* my_redel() gives warning if links */
00098 #define MY_COPYTIME 64  /* my_redel() copys time */
00099 #define MY_DELETE_OLD 256 /* my_create_with_symlink() */
00100 #define MY_HOLD_ORIGINAL_MODES 128  /* my_copy() holds to file modes */
00101 #define MY_REDEL_MAKE_BACKUP 256
00102 #define MY_DONT_WAIT  64  /* my_lock() don't wait if can't lock */
00103 #define MY_DONT_OVERWRITE_FILE 1024 /* my_copy: Don't overwrite file */
00104 #define MY_THREADSAFE 2048      /* my_seek(): lock fd mutex */
00105 
00106 #define ME_OLDWIN 2 /* Use old window */
00107 #define ME_BELL   4 /* Ring bell then printing message */
00108 #define ME_HOLDTANG 8 /* Don't delete last keys */
00109 #define ME_WAITTANG 32  /* Wait for a user action  */
00110 #define ME_NOREFRESH  64  /* Dont refresh screen */
00111 #define ME_NOINPUT  128 /* Dont use the input libary */
00112 
00113   /* Bits in last argument to fn_format */
00114 #define MY_REPLACE_DIR    1 /* replace dir in name with 'dir' */
00115 #define MY_REPLACE_EXT    2 /* replace extension with 'ext' */
00116 #define MY_UNPACK_FILENAME  4 /* Unpack name (~ -> home) */
00117 #define MY_RESOLVE_SYMLINKS 16  /* Resolve all symbolic links */
00118 #define MY_RETURN_REAL_PATH 32  /* return full path for file */
00119 #define MY_SAFE_PATH    64  /* Return NULL if too long path */
00120 #define MY_RELATIVE_PATH  128 /* name is relative to 'dir' */
00121 #define MY_APPEND_EXT           256     /* add 'ext' as additional extension*/
00122 
00123 
00124   /* Some constants */
00125 #define MY_WAIT_FOR_USER_TO_FIX_PANIC 60  /* in seconds */
00126 #define MY_WAIT_GIVE_USER_A_MESSAGE 10  /* Every 10 times of prev */
00127 #define DFLT_INIT_HITS  3
00128 
00129   /* Internal error numbers (for assembler functions) */
00130 #define MY_ERRNO_EDOM   33
00131 #define MY_ERRNO_ERANGE   34
00132 
00133   /* Bits for get_date timeflag */
00134 #define GETDATE_DATE_TIME 1
00135 #define GETDATE_SHORT_DATE  2
00136 #define GETDATE_HHMMSSTIME  4
00137 #define GETDATE_GMT   8
00138 #define GETDATE_FIXEDLENGTH 16
00139 
00140 
00141 typedef uint64_t my_off_t;
00142 
00143 #define TRASH(A,B) /* nothing */
00144 
00145 extern char *home_dir;      /* Home directory for user */
00146 extern const char *my_progname;   /* program-name (printed in errors) */
00147 extern uint32_t my_file_limit;
00148 
00149 /* statistics */
00150 extern uint mysys_usage_id;
00151 extern bool my_init_done;
00152 
00153           /* Executed when comming from shell */
00154 extern DRIZZLED_API int my_umask,   /* Default creation mask  */
00155      my_umask_dir,
00156      my_recived_signals,  /* Signals we have got */
00157      my_safe_to_handle_signal, /* Set when allowed to SIGTSTP */
00158      my_dont_interrupt; /* call remember_intr when set */
00159 extern bool mysys_uses_curses, my_use_symdir;
00160 extern uint32_t sf_malloc_cur_memory, sf_malloc_max_memory;
00161 
00162 extern uint32_t my_default_record_cache_size;
00163 extern bool my_disable_async_io,
00164                my_disable_flush_key_blocks, my_disable_symlinks;
00165 extern char wild_many, wild_one, wild_prefix;
00166 extern const char *charsets_dir;
00167 /* from default.c */
00168 extern char *my_defaults_extra_file;
00169 extern const char *my_defaults_group_suffix;
00170 extern const char *my_defaults_file;
00171 
00172 extern bool timed_mutexes;
00173 
00174 typedef class wild_file_pack  /* Struct to hold info when selecting files */
00175 {
00176 public:
00177   uint    wilds;    /* How many wildcards */
00178   uint    not_pos;  /* Start of not-theese-files */
00179   char *  *wild;    /* Pointer to wildcards */
00180 
00181   wild_file_pack():
00182     wilds(0),
00183     not_pos(0),
00184     wild(NULL)
00185   {}
00186 
00187 } WF_PACK;
00188 
00189 enum cache_type
00190 {
00191   TYPE_NOT_SET= 0,
00192   READ_CACHE,
00193   WRITE_CACHE,
00194   READ_FIFO,
00195   READ_NET,
00196   WRITE_NET
00197 };
00198 
00199 typedef struct record_cache /* Used when cacheing records */
00200 {
00201 public:
00202   int file;
00203   int rc_seek,error,inited;
00204   uint  rc_length,read_length,reclength;
00205   my_off_t rc_record_pos,end_of_file;
00206   unsigned char *rc_buff,*rc_buff2,*rc_pos,*rc_end,*rc_request_pos;
00207 #ifdef HAVE_AIOWAIT
00208   int use_async_io;
00209   my_aio_result aio_result;
00210 #endif
00211   enum cache_type type;
00212 
00213   record_cache():
00214     file(0),
00215     rc_seek(0),
00216     error(0),
00217     inited(0),
00218     rc_length(0),
00219     read_length(0),
00220     reclength(0),
00221     rc_record_pos(0),
00222     end_of_file(0),
00223     rc_buff(NULL),
00224     rc_buff2(NULL),
00225     rc_pos(NULL),
00226     rc_end(NULL),
00227     rc_request_pos(NULL)
00228   {}
00229 
00230 } RECORD_CACHE;
00231 
00232 
00233   /* defines for mf_iocache */
00234 
00235   /* Test if buffer is inited */
00236 #define my_b_clear(info) (info)->buffer=0
00237 #define my_b_inited(info) (info)->buffer
00238 #define my_b_EOF INT_MIN
00239 
00240 #define my_b_read(info,Buffer,Count) \
00241   ((info)->read_pos + (Count) <= (info)->read_end ?\
00242    (memcpy(Buffer,(info)->read_pos,(size_t) (Count)), \
00243     ((info)->read_pos+=(Count)),0) :\
00244    (*(info)->read_function)((info),Buffer,Count))
00245 
00246 #define my_b_write(info,Buffer,Count) \
00247  ((info)->write_pos + (Count) <=(info)->write_end ?\
00248   (memcpy((info)->write_pos, (Buffer), (size_t)(Count)),\
00249    ((info)->write_pos+=(Count)),0) : \
00250    (*(info)->write_function)((info),(Buffer),(Count)))
00251 
00252 #define my_b_get(info) \
00253   ((info)->read_pos != (info)->read_end ?\
00254    ((info)->read_pos++, (int) (unsigned char) (info)->read_pos[-1]) :\
00255    _my_b_get(info))
00256 
00257 #define my_b_tell(info) ((info)->pos_in_file + \
00258        (size_t) (*(info)->current_pos - (info)->request_pos))
00259 
00260 #define my_b_bytes_in_cache(info) (size_t) (*(info)->current_end - \
00261             *(info)->current_pos)
00262 
00263 typedef uint32_t ha_checksum;
00264 
00265 /* Define the type of function to be passed to process_default_option_files */
00266 typedef int (*Process_option_func)(void *ctx, const char *group_name,
00267                                    const char *option);
00268 
00269 /* Prototypes for mysys and my_func functions */
00270 
00271 extern int my_copy(const char *from,const char *to,myf MyFlags);
00272 DRIZZLED_API int my_delete(const char *name,myf MyFlags);
00273 DRIZZLED_API int my_open(const char *FileName,int Flags,myf MyFlags);
00274 extern int my_register_filename(int fd, const char *FileName,
00275                                 uint32_t error_message_number, myf MyFlags);
00276 DRIZZLED_API int my_create(const char *FileName,int CreateFlags,
00277                            int AccessFlags, myf MyFlags);
00278 DRIZZLED_API int my_close(int Filedes,myf MyFlags);
00279 extern int my_mkdir(const char *dir, int Flags, myf MyFlags);
00280 extern int my_realpath(char *to, const char *filename, myf MyFlags);
00281 extern int my_create_with_symlink(const char *linkname, const char *filename,
00282                                   int createflags, int access_flags,
00283                                   myf MyFlags);
00284 DRIZZLED_API int my_delete_with_symlink(const char *name, myf MyFlags);
00285 extern int my_rename_with_symlink(const char *from,const char *to,myf MyFlags);
00286 DRIZZLED_API size_t my_read(int Filedes,unsigned char *Buffer,size_t Count,myf MyFlags);
00287 DRIZZLED_API int my_rename(const char *from, const char *to,myf MyFlags);
00288 DRIZZLED_API size_t my_write(int Filedes, const unsigned char *Buffer,
00289                              size_t Count, myf MyFlags);
00290 extern int _sanity(const char *sFile, uint32_t uLine);
00291 
00292 extern int check_if_legal_filename(const char *path);
00293 extern int check_if_legal_tablename(const char *path);
00294 
00295 DRIZZLED_API int my_sync(int fd, myf my_flags);
00296 extern int my_sync_dir(const char *dir_name, myf my_flags);
00297 extern int my_sync_dir_by_file(const char *file_name, myf my_flags);
00298 extern bool my_init(void);
00299 extern void my_end(void);
00300 extern int my_redel(const char *from, const char *to, int MyFlags);
00301 extern int my_copystat(const char *from, const char *to, int MyFlags);
00302 extern char * my_filename(int fd);
00303 
00304 extern void my_remember_signal(int signal_number,void (*func)(int));
00305 extern size_t dirname_part(char * to,const char *name, size_t *to_res_length);
00306 extern size_t dirname_length(const char *name);
00307 #define base_name(A) (A+dirname_length(A))
00308 bool test_if_hard_path(const char *dir_name);
00309 
00310 extern char *convert_dirname(char *to, const char *from, const char *from_end);
00311 extern char * fn_ext(const char *name);
00312 extern char * fn_same(char * toname,const char *name,int flag);
00313 DRIZZLED_API char * fn_format(char * to,const char *name,const char *dir,
00314                               const char *form, uint32_t flag);
00315 extern size_t strlength(const char *str);
00316 extern size_t unpack_dirname(char * to,const char *from);
00317 extern size_t unpack_filename(char * to,const char *from);
00318 extern char * intern_filename(char * to,const char *from);
00319 extern int pack_filename(char * to, const char *name, size_t max_length);
00320 extern char * my_load_path(char * to, const char *path,
00321             const char *own_path_prefix);
00322 extern int wild_compare(const char *str,const char *wildstr,
00323                         bool str_is_pattern);
00324 extern WF_PACK *wf_comp(char * str);
00325 extern int wf_test(wild_file_pack *wf_pack,const char *name);
00326 extern void wf_end(wild_file_pack *buffer);
00327 extern bool array_append_string_unique(const char *str,
00328                                           const char **array, size_t size);
00329 extern void get_date(char * to,int timeflag,time_t use_time);
00330 extern int init_record_cache(RECORD_CACHE *info,size_t cachesize,int file,
00331            size_t reclength,enum cache_type type,
00332            bool use_async_io);
00333 extern int read_cache_record(RECORD_CACHE *info,unsigned char *to);
00334 extern int end_record_cache(RECORD_CACHE *info);
00335 extern int write_cache_record(RECORD_CACHE *info,my_off_t filepos,
00336             const unsigned char *record,size_t length);
00337 extern int flush_write_cache(RECORD_CACHE *info);
00338 extern void sigtstp_handler(int signal_number);
00339 extern void handle_recived_signals(void);
00340 
00341 extern void my_set_alarm_variable(int signo);
00342 extern void my_string_ptr_sort(unsigned char *base,uint32_t items,size_t size);
00343 extern void radixsort_for_str_ptr(unsigned char* base[], uint32_t number_of_elements,
00344           size_t size_of_element,unsigned char *buffer[]);
00345 extern void my_qsort(void *base_ptr, size_t total_elems, size_t size,
00346                      qsort_cmp cmp);
00347 extern void my_qsort2(void *base_ptr, size_t total_elems, size_t size,
00348                       qsort2_cmp cmp, void *cmp_argument);
00349 extern qsort2_cmp get_ptr_compare(size_t);
00350 DRIZZLED_API void my_store_ptr(unsigned char *buff, size_t pack_length, my_off_t pos);
00351 DRIZZLED_API my_off_t my_get_ptr(unsigned char *ptr, size_t pack_length);
00352 int create_temp_file(char *to, const char *dir, const char *pfx, myf MyFlags);
00353 
00354 extern int get_defaults_options(int argc, char **argv,
00355                                 char **defaults, char **extra_defaults,
00356                                 char **group_suffix);
00357 extern int load_defaults(const char *conf_file, const char **groups,
00358        int *argc, char ***argv);
00359 extern int my_search_option_files(const char *conf_file, int *argc,
00360                                   char ***argv, uint32_t *args_used,
00361                                   Process_option_func func, void *func_ctx);
00362 extern void free_defaults(char **argv);
00363 extern void my_print_default_files(const char *conf_file);
00364 extern void print_defaults(const char *conf_file, const char **groups);
00365 extern ha_checksum my_checksum(ha_checksum crc, const unsigned char *mem,
00366                                size_t count);
00367 extern void my_sleep(uint32_t m_seconds);
00368 
00369 
00370 
00371 extern void thd_increment_bytes_sent(uint32_t length);
00372 extern void thd_increment_bytes_received(uint32_t length);
00373 extern void thd_increment_net_big_packet_count(uint32_t length);
00374 
00375 } /* namespace internal */
00376 } /* namespace drizzled */
00377