static struct errors *generate_empty_message(uint d_code) { struct errors *new_error; struct message message; /* create a new element */ if (!(new_error= (struct errors *) my_malloc(sizeof(*new_error), MYF(MY_WME)))) return(0); if (my_init_dynamic_array(&new_error->msg, sizeof(struct message), 0, 1, MYF(0))) return(0); /* OOM: Fatal error */ new_error->er_name= NULL; new_error->d_code= d_code; new_error->sql_code1= empty_string; new_error->sql_code2= empty_string; if (!(message.lang_short_name= my_strdup(default_language, MYF(MY_WME))) || !(message.text= my_strdup("", MYF(MY_WME)))) return(0); /* Can't fail as msg is preallocated */ (void) insert_dynamic(&new_error->msg, (uchar*) &message); return(new_error); }
/** @brief Initialize the hash @details Initialize the hash, by defining and giving valid values for its elements. The failure to allocate memory for the hash->array element will not result in a fatal failure. The dynamic array that is part of the hash will allocate memory as required during insertion. @param[in,out] hash The hash that is initialized @param[in] charset The charater set information @param[in] hash_function Hash function to be used. NULL - use standard hash from character set. @param[in] size The hash size @param[in] key_offest The key offset for the hash @param[in] key_length The length of the key used in the hash @param[in] get_key get the key for the hash @param[in] free_element pointer to the function that does cleanup @param[in] flags flags set in the hash @return inidicates success or failure of initialization @retval 0 success @retval 1 failure */ my_bool _my_hash_init(HASH *hash, uint growth_size, CHARSET_INFO *charset, my_hash_function hash_function, ulong size, size_t key_offset, size_t key_length, my_hash_get_key get_key, void (*free_element)(void*), uint flags, PSI_memory_key psi_key) { my_bool retval; DBUG_ENTER("my_hash_init"); DBUG_PRINT("enter",("hash: 0x%lx size: %u", (long) hash, (uint) size)); hash->records=0; hash->key_offset=key_offset; hash->key_length=key_length; hash->blength=1; hash->get_key=get_key; hash->free=free_element; hash->flags=flags; hash->charset=charset; hash->hash_function= hash_function ? hash_function : cset_hash_sort_adapter; hash->m_psi_key= psi_key; retval= my_init_dynamic_array(&hash->array, psi_key, sizeof(HASH_LINK), NULL, /* init_buffer */ size, growth_size); DBUG_RETURN(retval); }
my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size, uint init_alloc, uint alloc_increment) { /* placeholder to preserve ABI */ return my_init_dynamic_array(array, element_size, NULL, /* init_buffer */ init_alloc, alloc_increment); }
/** WT_RESOURCE constructor It's called from lf_hash and takes a pointer to an LF_SLIST instance. WT_RESOURCE is located at arg+sizeof(LF_SLIST) */ static void wt_resource_create(uchar *arg) { WT_RESOURCE *rc= (WT_RESOURCE*)(arg+LF_HASH_OVERHEAD); DBUG_ENTER("wt_resource_create"); bzero(rc, sizeof(*rc)); rc_rwlock_init(rc); mysql_cond_init(key_WT_RESOURCE_cond, &rc->cond, 0); my_init_dynamic_array(&rc->owners, sizeof(WT_THD *), 0, 5, MYF(0)); DBUG_VOID_RETURN; }
/* {{{ MADB_DescInit */ MADB_Desc *MADB_DescInit(MADB_Dbc *Dbc,enum enum_madb_desc_type DescType, my_bool isExternal) { MADB_Desc *Desc; if (!(Desc= (MADB_Desc *)MADB_CALLOC(sizeof(MADB_Desc)))) return NULL; Desc->DescType= DescType; if (my_init_dynamic_array(&Desc->Records, sizeof(MADB_DescRecord), 0, 0)) { MADB_FREE(Desc); Desc= NULL; } if (isExternal) { if (my_init_dynamic_array(&Desc->Stmts, sizeof(MADB_Stmt**), 0, 0)) { MADB_DescFree(Desc, FALSE); Desc= NULL; } else { Desc->Dbc= Dbc; EnterCriticalSection(&Dbc->cs); Desc->ListItem.data= (void *)Desc; Dbc->Descrs= list_add(Dbc->Descrs, &Desc->ListItem); LeaveCriticalSection(&Dbc->cs); } } if (Desc) Desc->AppType= isExternal; Desc->Header.ArraySize= 1; return Desc; }
my_bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist) { char *end, *copy; char buff[FN_REFLEN]; DBUG_ENTER("init_tmpdir"); DBUG_PRINT("enter", ("pathlist: %s", pathlist ? pathlist : "NULL")); mysql_mutex_init(key_TMPDIR_mutex, &tmpdir->mutex, MY_MUTEX_INIT_FAST); if (my_init_dynamic_array(&tmpdir->full_list, sizeof(char*), 1, 5)) goto err; if (!pathlist || !pathlist[0]) { /* Get default temporary directory */ pathlist=getenv("TMPDIR"); /* Use this if possible */ #if defined(_WIN32) if (!pathlist) pathlist=getenv("TEMP"); if (!pathlist) pathlist=getenv("TMP"); #endif if (!pathlist || !pathlist[0]) pathlist=(char*) P_tmpdir; } do { size_t length; end=strcend(pathlist, DELIM); strmake(buff, pathlist, (uint) (end-pathlist)); length= cleanup_dirname(buff, buff); if (!(copy= my_strndup(key_memory_MY_TMPDIR_full_list, buff, length, MYF(MY_WME))) || insert_dynamic(&tmpdir->full_list, ©)) DBUG_RETURN(TRUE); pathlist=end+1; } while (*end); freeze_size(&tmpdir->full_list); tmpdir->list=(char **)tmpdir->full_list.buffer; tmpdir->max=tmpdir->full_list.elements-1; tmpdir->cur=0; DBUG_RETURN(FALSE); err: delete_dynamic(&tmpdir->full_list); /* Safe to free */ mysql_mutex_destroy(&tmpdir->mutex); DBUG_RETURN(TRUE); }
/* {{{ MADB_DescCopyDesc */ SQLRETURN MADB_DescCopyDesc(MADB_Desc *SrcDesc, MADB_Desc *DestDesc) { if (!SrcDesc) return SQL_INVALID_HANDLE; if (DestDesc->DescType == MADB_DESC_IRD) { MADB_SetError(&DestDesc->Error, MADB_ERR_HY016, NULL, 0); return SQL_ERROR; } if (!SrcDesc->Header.Count) { MADB_SetError(&DestDesc->Error, MADB_ERR_HY007, NULL, 0); return SQL_ERROR; } /* make sure there aren't old records */ delete_dynamic(&DestDesc->Records); if (my_init_dynamic_array(&DestDesc->Records, sizeof(MADB_DescRecord), SrcDesc->Records.elements, SrcDesc->Records.alloc_increment)) { MADB_SetError(&DestDesc->Error, MADB_ERR_HY001, NULL, 0); return SQL_ERROR; } memcpy(&DestDesc->Header, &SrcDesc->Header, sizeof(MADB_Header)); DestDesc->AppType= SrcDesc->AppType; DestDesc->DescType= SrcDesc->DescType; memcpy(&DestDesc->Error, &SrcDesc->Error, sizeof(MADB_Error)); /* Since we never allocate pointers we can just copy content */ memcpy(DestDesc->Records.buffer, SrcDesc->Records.buffer, SrcDesc->Records.size_of_element * SrcDesc->Records.max_element); /* todo: internal buffer needs to be clearead or we need to move it outside of record structure */ return SQL_SUCCESS; }
MY_DIR *my_dir(const char *path, myf MyFlags) { char *buffer; MY_DIR *result= 0; FILEINFO finfo; DYNAMIC_ARRAY *dir_entries_storage; MEM_ROOT *names_storage; DIR *dirp; struct dirent *dp; char tmp_path[FN_REFLEN+1],*tmp_file; #ifdef THREAD char dirent_tmp[sizeof(struct dirent)+_POSIX_PATH_MAX+1]; #endif DBUG_ENTER("my_dir"); DBUG_PRINT("my",("path: '%s' MyFlags: %d",path,MyFlags)); #if defined(THREAD) && !defined(HAVE_READDIR_R) mysql_mutex_lock(&THR_LOCK_open); #endif dirp = opendir(directory_file_name(tmp_path,(char *) path)); #if defined(__amiga__) if ((dirp->dd_fd) < 0) /* Directory doesn't exists */ goto error; #endif if (dirp == NULL || ! (buffer= my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) + sizeof(MEM_ROOT), MyFlags))) goto error; dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))); if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO), ENTRIES_START_SIZE, ENTRIES_INCREMENT)) { my_free(buffer); goto error; } init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE); /* MY_DIR structure is allocated and completly initialized at this point */ result= (MY_DIR*)buffer; tmp_file=strend(tmp_path); #ifdef THREAD dp= (struct dirent*) dirent_tmp; #else dp=0; #endif while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp))) { if (!(finfo.name= strdup_root(names_storage, dp->d_name))) goto error; if (MyFlags & MY_WANT_STAT) { if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage, sizeof(MY_STAT)))) goto error; bzero(finfo.mystat, sizeof(MY_STAT)); (void) strmov(tmp_file,dp->d_name); (void) my_stat(tmp_path, finfo.mystat, MyFlags); if (!(finfo.mystat->st_mode & MY_S_IREAD)) continue; } else finfo.mystat= NULL; if (push_dynamic(dir_entries_storage, (uchar*)&finfo)) goto error; } (void) closedir(dirp); #if defined(THREAD) && !defined(HAVE_READDIR_R) mysql_mutex_unlock(&THR_LOCK_open); #endif result->dir_entry= (FILEINFO *)dir_entries_storage->buffer; result->number_off_files= dir_entries_storage->elements; if (!(MyFlags & MY_DONT_SORT)) my_qsort((void *) result->dir_entry, result->number_off_files, sizeof(FILEINFO), (qsort_cmp) comp_names); DBUG_RETURN(result); error: #if defined(THREAD) && !defined(HAVE_READDIR_R) mysql_mutex_unlock(&THR_LOCK_open); #endif my_errno=errno; if (dirp) (void) closedir(dirp); my_dirend(result); if (MyFlags & (MY_FAE | MY_WME)) my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,my_errno); DBUG_RETURN((MY_DIR *) NULL); } /* my_dir */
MY_DIR *my_dir(const char *path, myf MyFlags) { char *buffer; MY_DIR *result= 0; FILEINFO finfo; DYNAMIC_ARRAY *dir_entries_storage; MEM_ROOT *names_storage; #ifdef __BORLANDC__ struct ffblk find; #else struct _finddata_t find; #endif ushort mode; char tmp_path[FN_REFLEN],*tmp_file,attrib; #ifdef _WIN64 __int64 handle; #else long handle; #endif DBUG_ENTER("my_dir"); DBUG_PRINT("my",("path: '%s' stat: %d MyFlags: %d",path,MyFlags)); /* Put LIB-CHAR as last path-character if not there */ tmp_file=tmp_path; if (!*path) *tmp_file++ ='.'; /* From current dir */ tmp_file= strnmov(tmp_file, path, FN_REFLEN-5); if (tmp_file[-1] == FN_DEVCHAR) *tmp_file++= '.'; /* From current dev-dir */ if (tmp_file[-1] != FN_LIBCHAR) *tmp_file++ =FN_LIBCHAR; tmp_file[0]='*'; /* Windows needs this !??? */ tmp_file[1]='.'; tmp_file[2]='*'; tmp_file[3]='\0'; if (!(buffer= my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) + sizeof(MEM_ROOT), MyFlags))) goto error; dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))); if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO), ENTRIES_START_SIZE, ENTRIES_INCREMENT)) { my_free(buffer); goto error; } init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE); /* MY_DIR structure is allocated and completly initialized at this point */ result= (MY_DIR*)buffer; #ifdef __BORLANDC__ if ((handle= findfirst(tmp_path,&find,0)) == -1L) #else if ((handle=_findfirst(tmp_path,&find)) == -1L) #endif { DBUG_PRINT("info", ("findfirst returned error, errno: %d", errno)); if (errno != EINVAL) goto error; /* Could not read the directory, no read access. Probably because by "chmod -r". continue and return zero files in dir */ } else { do { #ifdef __BORLANDC__ attrib= find.ff_attrib; #else attrib= find.attrib; /* Do not show hidden and system files which Windows sometimes create. Note. Because Borland's findfirst() is called with the third argument = 0 hidden/system files are excluded from the search. */ if (attrib & (_A_HIDDEN | _A_SYSTEM)) continue; #endif #ifdef __BORLANDC__ if (!(finfo.name= strdup_root(names_storage, find.ff_name))) goto error; #else if (!(finfo.name= strdup_root(names_storage, find.name))) goto error; #endif if (MyFlags & MY_WANT_STAT) { if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage, sizeof(MY_STAT)))) goto error; bzero(finfo.mystat, sizeof(MY_STAT)); #ifdef __BORLANDC__ finfo.mystat->st_size=find.ff_fsize; #else finfo.mystat->st_size=find.size; #endif mode= MY_S_IREAD; if (!(attrib & _A_RDONLY)) mode|= MY_S_IWRITE; if (attrib & _A_SUBDIR) mode|= MY_S_IFDIR; finfo.mystat->st_mode= mode; #ifdef __BORLANDC__ finfo.mystat->st_mtime= ((uint32) find.ff_ftime); #else finfo.mystat->st_mtime= ((uint32) find.time_write); #endif } else finfo.mystat= NULL; if (push_dynamic(dir_entries_storage, (uchar*)&finfo)) goto error; } #ifdef __BORLANDC__ while (findnext(&find) == 0); #else while (_findnext(handle,&find) == 0); _findclose(handle); #endif } result->dir_entry= (FILEINFO *)dir_entries_storage->buffer; result->number_off_files= dir_entries_storage->elements; if (!(MyFlags & MY_DONT_SORT)) my_qsort((void *) result->dir_entry, result->number_off_files, sizeof(FILEINFO), (qsort_cmp) comp_names); DBUG_PRINT("exit", ("found %d files", result->number_off_files)); DBUG_RETURN(result); error: my_errno=errno; #ifndef __BORLANDC__ if (handle != -1) _findclose(handle); #endif my_dirend(result); if (MyFlags & MY_FAE+MY_WME) my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,errno); DBUG_RETURN((MY_DIR *) NULL); } /* my_dir */
static struct errors *parse_error_string(char *str, int er_count) { struct errors *new_error; DBUG_ENTER("parse_error_string"); DBUG_PRINT("enter", ("str: %s", str)); /* create a new element */ new_error= (struct errors *) my_malloc(PSI_NOT_INSTRUMENTED, sizeof(*new_error), MYF(MY_WME)); if (my_init_dynamic_array(&new_error->msg, PSI_NOT_INSTRUMENTED, sizeof(struct message), NULL, 0, 0)) DBUG_RETURN(0); /* OOM: Fatal error */ /* getting the error name */ str= skip_delimiters(str); if (!(new_error->er_name= get_word(&str))) DBUG_RETURN(0); /* OOM: Fatal error */ DBUG_PRINT("info", ("er_name: %s", new_error->er_name)); str= skip_delimiters(str); /* getting the code1 */ new_error->d_code= er_offset + er_count; DBUG_PRINT("info", ("d_code: %d", new_error->d_code)); str= skip_delimiters(str); /* if we reached EOL => no more codes, but this can happen */ if (!*str) { new_error->sql_code1= empty_string; new_error->sql_code2= empty_string; DBUG_PRINT("info", ("str: %s", str)); DBUG_RETURN(new_error); } /* getting the sql_code 1 */ if (!(new_error->sql_code1= get_word(&str))) DBUG_RETURN(0); /* OOM: Fatal error */ DBUG_PRINT("info", ("sql_code1: %s", new_error->sql_code1)); str= skip_delimiters(str); /* if we reached EOL => no more codes, but this can happen */ if (!*str) { new_error->sql_code2= empty_string; DBUG_PRINT("info", ("str: %s", str)); DBUG_RETURN(new_error); } /* getting the sql_code 2 */ if (!(new_error->sql_code2= get_word(&str))) DBUG_RETURN(0); /* OOM: Fatal error */ DBUG_PRINT("info", ("sql_code2: %s", new_error->sql_code2)); str= skip_delimiters(str); if (*str) { fprintf(stderr, "The error line did not end with sql/odbc code: '%s'\n", str); DBUG_RETURN(0); } DBUG_RETURN(new_error); }
int my_load_defaults(const char *conf_file, const char **groups, int *argc, char ***argv, const char ***default_directories) { DYNAMIC_ARRAY args; TYPELIB group; my_bool found_print_defaults= 0; uint args_used= 0; int error= 0; MEM_ROOT alloc; char *ptr,**res; struct handle_option_ctx ctx; const char **dirs; uint args_sep= my_getopt_use_args_separator ? 1 : 0; DBUG_ENTER("load_defaults"); init_alloc_root(&alloc, 512, 0, MYF(0)); if ((dirs= init_default_directories(&alloc)) == NULL) goto err; /* Check if the user doesn't want any default option processing --no-defaults is always the first option */ if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults")) { /* remove the --no-defaults argument and return only the other arguments */ uint i, j; if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+ (*argc + 1)*sizeof(char*)))) goto err; res= (char**) (ptr+sizeof(alloc)); res[0]= **argv; /* Copy program name */ j= 1; /* Start from 1 for the reset result args */ if (my_getopt_use_args_separator) { /* set arguments separator */ set_args_separator(&res[1]); j++; } for (i=2 ; i < (uint) *argc ; i++, j++) res[j]=argv[0][i]; res[j]=0; /* End pointer */ /* Update the argc, if have not added args separator, then we have to decrease argc because we have removed the "--no-defaults". */ if (!my_getopt_use_args_separator) (*argc)--; *argv=res; *(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */ if (default_directories) *default_directories= dirs; DBUG_RETURN(0); } group.count=0; group.name= "defaults"; group.type_names= groups; for (; *groups ; groups++) group.count++; if (my_init_dynamic_array(&args, sizeof(char*),*argc, 32, MYF(0))) goto err; ctx.alloc= &alloc; ctx.args= &args; ctx.group= &group; error= my_search_option_files(conf_file, argc, argv, &args_used, handle_default_option, (void *) &ctx, dirs); /* Here error contains <> 0 only if we have a fully specified conf_file or a forced default file */ if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+ (args.elements + *argc + 1 + args_sep) *sizeof(char*)))) goto err; res= (char**) (ptr+sizeof(alloc)); /* copy name + found arguments + command line arguments to new array */ res[0]= argv[0][0]; /* Name MUST be set, even by embedded library */ memcpy((uchar*) (res+1), args.buffer, args.elements*sizeof(char*)); /* Skip --defaults-xxx options */ (*argc)-= args_used; (*argv)+= args_used; /* Check if we wan't to see the new argument list This options must always be the last of the default options */ if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults")) { found_print_defaults=1; --*argc; ++*argv; /* skip argument */ } if (my_getopt_use_args_separator) { /* set arguments separator for arguments from config file and command line */ set_args_separator(&res[args.elements+1]); } if (*argc) memcpy((uchar*) (res+1+args.elements+args_sep), (char*) ((*argv)+1), (*argc-1)*sizeof(char*)); res[args.elements+ *argc+args_sep]=0; /* last null */ (*argc)+=args.elements+args_sep; *argv= (char**) res; *(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */ delete_dynamic(&args); if (found_print_defaults) { int i; printf("%s would have been started with the following arguments:\n", **argv); for (i=1 ; i < *argc ; i++) if (!my_getopt_is_args_separator((*argv)[i])) /* skip arguments separator */ printf("%s ", (*argv)[i]); puts(""); exit(0); } if (error == 0 && default_directories) *default_directories= dirs; DBUG_RETURN(error); err: fprintf(stderr,"Fatal error in defaults handling. Program aborted\n"); exit(1); return 0; /* Keep compiler happy */ }
MY_DIR *my_dir(const char* path, myf MyFlags) { char *buffer; MY_DIR *result= 0; FILEINFO finfo; DYNAMIC_ARRAY *dir_entries_storage; MEM_ROOT *names_storage; struct find_t find; ushort mode; char tmp_path[FN_REFLEN],*tmp_file,attrib; DBUG_ENTER("my_dir"); DBUG_PRINT("my",("path: '%s' stat: %d MyFlags: %d",path,MyFlags)); /* Put LIB-CHAR as last path-character if not there */ tmp_file=tmp_path; if (!*path) *tmp_file++ ='.'; /* From current dir */ tmp_file= strmov(tmp_file,path); if (tmp_file[-1] == FN_DEVCHAR) *tmp_file++= '.'; /* From current dev-dir */ if (tmp_file[-1] != FN_LIBCHAR) *tmp_file++ =FN_LIBCHAR; tmp_file[0]='*'; /* MSDOS needs this !??? */ tmp_file[1]='.'; tmp_file[2]='*'; tmp_file[3]='\0'; if (_dos_findfirst(tmp_path,_A_NORMAL | _A_SUBDIR, &find)) goto error; if (!(buffer= my_malloc(ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) + sizeof(MEM_ROOT), MyFlags))) goto error; dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))); if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO), ENTRIES_START_SIZE, ENTRIES_INCREMENT)) { my_free((gptr) buffer,MYF(0)); goto error; } init_alloc_root(names_storage, NAMES_START_SIZE, NAMES_START_SIZE); /* MY_DIR structure is allocated and completly initialized at this point */ result= (MY_DIR*)buffer; do { if (!(finfo.name= strdup_root(names_storage, find.name))) goto error; if (MyFlags & MY_WANT_STAT) { if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage, sizeof(MY_STAT)))) goto error; bzero(finfo.mystat, sizeof(MY_STAT)); finfo.mystat->st_size= find.size; mode= MY_S_IREAD; attrib= find.attrib; if (!(attrib & _A_RDONLY)) mode|= MY_S_IWRITE; if (attrib & _A_SUBDIR) mode|= MY_S_IFDIR; finfo.mystat->st_mode= mode; finfo.mystat->st_mtime= ((uint32) find.wr_date << 16) + find.wr_time; } else finfo.mystat= NULL; if (push_dynamic(dir_entries_storage, (gptr)&finfo)) goto error; } while (_dos_findnext(&find) == 0); result->dir_entry= (FILEINFO *)dir_entries_storage->buffer; result->number_off_files= dir_entries_storage->elements; if (!(MyFlags & MY_DONT_SORT)) my_qsort((void *) result->dir_entry, result->number_off_files, sizeof(FILEINFO), (qsort_cmp) comp_names); DBUG_RETURN(result); error: my_dirend(result); if (MyFlags & MY_FAE+MY_WME) my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,errno); DBUG_RETURN((MY_DIR *) NULL); } /* my_dir */
MY_DIR *my_dir(const char *path, myf MyFlags) { char *buffer; MY_DIR *result= 0; FILEINFO finfo; DYNAMIC_ARRAY *dir_entries_storage; MEM_ROOT *names_storage; DIR *dirp; struct dirent *dp; char tmp_path[FN_REFLEN + 2], *tmp_file; char dirent_tmp[sizeof(struct dirent)+_POSIX_PATH_MAX+1]; DBUG_ENTER("my_dir"); DBUG_PRINT("my",("path: '%s' MyFlags: %d",path,MyFlags)); #if !defined(HAVE_READDIR_R) mysql_mutex_lock(&THR_LOCK_open); #endif dirp = opendir(directory_file_name(tmp_path,(char *) path)); if (dirp == NULL || ! (buffer= my_malloc(key_memory_MY_DIR, ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY)) + sizeof(MEM_ROOT), MyFlags))) goto error; dir_entries_storage= (DYNAMIC_ARRAY*)(buffer + ALIGN_SIZE(sizeof(MY_DIR))); names_storage= (MEM_ROOT*)(buffer + ALIGN_SIZE(sizeof(MY_DIR)) + ALIGN_SIZE(sizeof(DYNAMIC_ARRAY))); if (my_init_dynamic_array(dir_entries_storage, sizeof(FILEINFO), NULL, /* init_buffer */ ENTRIES_START_SIZE, ENTRIES_INCREMENT)) { my_free(buffer); goto error; } init_alloc_root(key_memory_MY_DIR, names_storage, NAMES_START_SIZE, NAMES_START_SIZE); /* MY_DIR structure is allocated and completly initialized at this point */ result= (MY_DIR*)buffer; tmp_file=strend(tmp_path); dp= (struct dirent*) dirent_tmp; while (!(READDIR(dirp,(struct dirent*) dirent_tmp,dp))) { if (!(finfo.name= strdup_root(names_storage, dp->d_name))) goto error; if (MyFlags & MY_WANT_STAT) { if (!(finfo.mystat= (MY_STAT*)alloc_root(names_storage, sizeof(MY_STAT)))) goto error; memset(finfo.mystat, 0, sizeof(MY_STAT)); (void) my_stpcpy(tmp_file,dp->d_name); (void) my_stat(tmp_path, finfo.mystat, MyFlags); if (!(finfo.mystat->st_mode & MY_S_IREAD)) continue; } else finfo.mystat= NULL; if (insert_dynamic(dir_entries_storage, (uchar*)&finfo)) goto error; } (void) closedir(dirp); #if !defined(HAVE_READDIR_R) mysql_mutex_unlock(&THR_LOCK_open); #endif result->dir_entry= (FILEINFO *)dir_entries_storage->buffer; result->number_off_files= dir_entries_storage->elements; if (!(MyFlags & MY_DONT_SORT)) my_qsort((void *) result->dir_entry, result->number_off_files, sizeof(FILEINFO), (qsort_cmp) comp_names); DBUG_RETURN(result); error: #if !defined(HAVE_READDIR_R) mysql_mutex_unlock(&THR_LOCK_open); #endif my_errno=errno; if (dirp) (void) closedir(dirp); my_dirend(result); if (MyFlags & (MY_FAE | MY_WME)) { char errbuf[MYSYS_STRERROR_SIZE]; my_error(EE_DIR, MYF(0), path, my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno)); } DBUG_RETURN((MY_DIR *) NULL); } /* my_dir */
void load_defaults(const char *conf_file, const char **groups, int *argc, char ***argv) { DYNAMIC_ARRAY args; const char **dirs, *forced_default_file; TYPELIB group; my_bool found_print_defaults=0; uint args_used=0; MEM_ROOT alloc; char *ptr,**res; DBUG_ENTER("load_defaults"); init_alloc_root(&alloc,128,0); if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults")) { /* remove the --no-defaults argument and return only the other arguments */ uint i; if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+ (*argc + 1)*sizeof(char*)))) goto err; res= (char**) (ptr+sizeof(alloc)); res[0]= **argv; /* Copy program name */ for (i=2 ; i < (uint) *argc ; i++) res[i-1]=argv[0][i]; (*argc)--; *argv=res; *(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */ DBUG_VOID_RETURN; } /* Check if we want to force the use a specific default file */ forced_default_file=0; if (*argc >= 2) { if (is_prefix(argv[0][1],"--defaults-file=")) { forced_default_file=strchr(argv[0][1],'=')+1; args_used++; } else if (is_prefix(argv[0][1],"--defaults-extra-file=")) { defaults_extra_file=strchr(argv[0][1],'=')+1; args_used++; } } group.count=0; group.name= "defaults"; group.type_names= groups; for (; *groups ; groups++) group.count++; if (my_init_dynamic_array(&args, sizeof(char*),*argc, 32)) goto err; if (forced_default_file) { if (search_default_file(&args, &alloc, "", forced_default_file, "", &group)) goto err; } else if (dirname_length(conf_file)) { if (search_default_file(&args, &alloc, NullS, conf_file, default_ext, &group)) goto err; } else { #ifdef _WIN32 char system_dir[FN_REFLEN]; GetWindowsDirectory(system_dir,sizeof(system_dir)); if (search_default_file(&args, &alloc, system_dir, conf_file, windows_ext, &group)) goto err; #endif #if defined(__EMX__) || defined(OS2) if (getenv("ETC") && search_default_file(&args, &alloc, getenv("ETC"), conf_file, default_ext, &group)) goto err; #endif for (dirs=default_directories ; *dirs; dirs++) { int error=0; if (**dirs) error=search_default_file(&args, &alloc, *dirs, conf_file, default_ext, &group); else if (defaults_extra_file) error=search_default_file(&args, &alloc, NullS, defaults_extra_file, default_ext, &group); if (error) goto err; } } if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+ (args.elements + *argc +1) *sizeof(char*)))) goto err; res= (char**) (ptr+sizeof(alloc)); /* copy name + found arguments + command line arguments to new array */ res[0]=argv[0][0]; memcpy((gptr) (res+1), args.buffer, args.elements*sizeof(char*)); /* Skipp --defaults-file and --defaults-extra-file */ (*argc)-= args_used; (*argv)+= args_used; /* Check if we wan't to see the new argument list */ if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults")) { found_print_defaults=1; --*argc; ++*argv; /* skipp argument */ } memcpy((gptr) (res+1+args.elements), (char*) ((*argv)+1), (*argc-1)*sizeof(char*)); res[args.elements+ *argc]=0; /* last null */ (*argc)+=args.elements; *argv= (char**) res; *(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */ delete_dynamic(&args); if (found_print_defaults) { int i; printf("%s would have been started with the following arguments:\n", **argv); for (i=1 ; i < *argc ; i++) printf("%s ", (*argv)[i]); puts(""); exit(1); } DBUG_VOID_RETURN; err: fprintf(stderr,"Program aborted\n"); exit(1); }
int load_defaults(const char *conf_file, const char **groups, int *argc, char ***argv) { DYNAMIC_ARRAY args; const char **dirs, *forced_default_file, *forced_extra_defaults; TYPELIB group; my_bool found_print_defaults=0; uint args_used=0; int error= 0; MEM_ROOT alloc; char *ptr, **res; DBUG_ENTER("load_defaults"); init_alloc_root(&alloc,512,0); if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults")) { /* remove the --no-defaults argument and return only the other arguments */ uint i; if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+ (*argc + 1)*sizeof(char*)))) goto err; res= (char**) (ptr+sizeof(alloc)); res[0]= **argv; /* Copy program name */ for (i=2 ; i < (uint) *argc ; i++) res[i-1]=argv[0][i]; res[i-1]=0; /* End pointer */ (*argc)--; *argv=res; *(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */ DBUG_RETURN(0); } get_defaults_files(*argc, *argv, (char **)&forced_default_file, (char **)&forced_extra_defaults); if (forced_default_file) forced_default_file= strchr(forced_default_file,'=')+1; if (forced_extra_defaults) defaults_extra_file= strchr(forced_extra_defaults,'=')+1; args_used+= (forced_default_file ? 1 : 0) + (forced_extra_defaults ? 1 : 0); group.count=0; group.name= "defaults"; group.type_names= groups; for (; *groups ; groups++) group.count++; if (my_init_dynamic_array(&args, sizeof(char*),*argc, 32)) goto err; if (forced_default_file) { if ((error= search_default_file_with_ext(&args, &alloc, "", "", forced_default_file, &group, 0)) < 0) goto err; if (error > 0) { fprintf(stderr, "Could not open required defaults file: %s\n", forced_default_file); goto err; } } else if (dirname_length(conf_file)) { if ((error= search_default_file(&args, &alloc, NullS, conf_file, &group)) < 0) goto err; } else { #ifdef __WIN__ char system_dir[FN_REFLEN]; GetWindowsDirectory(system_dir,sizeof(system_dir)); if ((search_default_file(&args, &alloc, system_dir, conf_file, &group))) goto err; #endif #if defined(__EMX__) || defined(OS2) { const char *etc; if ((etc= getenv("ETC")) && (search_default_file(&args, &alloc, etc, conf_file, &group)) < 0) goto err; } #endif for (dirs=default_directories ; *dirs; dirs++) { if (**dirs) { if (search_default_file(&args, &alloc, *dirs, conf_file, &group) < 0) goto err; } else if (defaults_extra_file) { if (search_default_file(&args, &alloc, NullS, defaults_extra_file, &group) < 0) goto err; /* Fatal error */ } } } /* Here error contains <> 0 only if we have a fully specified conf_file or a forced default file */ if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+ (args.elements + *argc +1) *sizeof(char*)))) goto err; res= (char**) (ptr+sizeof(alloc)); /* copy name + found arguments + command line arguments to new array */ res[0]= argv[0][0]; /* Name MUST be set, even by embedded library */ memcpy((gptr) (res+1), args.buffer, args.elements*sizeof(char*)); /* Skip --defaults-file and --defaults-extra-file */ (*argc)-= args_used; (*argv)+= args_used; /* Check if we wan't to see the new argument list */ if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults")) { found_print_defaults=1; --*argc; ++*argv; /* skip argument */ } if (*argc) memcpy((gptr) (res+1+args.elements), (char*) ((*argv)+1), (*argc-1)*sizeof(char*)); res[args.elements+ *argc]=0; /* last null */ (*argc)+=args.elements; *argv= (char**) res; *(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */ delete_dynamic(&args); if (found_print_defaults) { int i; printf("%s would have been started with the following arguments:\n", **argv); for (i=1 ; i < *argc ; i++) printf("%s ", (*argv)[i]); puts(""); exit(0); } DBUG_RETURN(error); err: fprintf(stderr,"Fatal error in defaults handling. Program aborted\n"); exit(1); return 0; /* Keep compiler happy */ }