size_t dirname_part(char *to, const char *name, size_t *to_res_length) { size_t length; DBUG_ENTER("dirname_part"); DBUG_PRINT("enter",("'%s'",name)); length=dirname_length(name); *to_res_length= (size_t) (convert_dirname(to, name, name+length) - to); DBUG_RETURN(length); } /* dirname */
uint dirname_part(my_string to, const char *name) { uint length; DBUG_ENTER("dirname_part"); DBUG_PRINT("enter",("'%s'",name)); length=dirname_length(name); convert_dirname(to, name, name+length); DBUG_RETURN(length); } /* dirname */
void print_defaults(const char *conf_file, const char **groups) { #ifdef _WIN32 bool have_ext=fn_ext(conf_file)[0] != 0; #endif char name[FN_REFLEN]; const char **dirs; puts("\nDefault options are read from the following files in the given order:"); if (dirname_length(conf_file)) fputs(conf_file,stdout); else { #ifdef _WIN32 GetWindowsDirectory(name,sizeof(name)); printf("%s\\%s%s ",name,conf_file,have_ext ? "" : windows_ext); #endif #if defined(__EMX__) || defined(OS2) if (getenv("ETC")) printf("%s\\%s%s ", getenv("ETC"), conf_file, default_ext); #endif for (dirs=default_directories ; *dirs; dirs++) { if (**dirs) strmov(name,*dirs); else if (defaults_extra_file) strmov(name,defaults_extra_file); else continue; convert_dirname(name); if (name[0] == FN_HOMELIB) /* Add . to filenames in home */ strcat(name,"."); strxmov(strend(name),conf_file,default_ext," ",NullS); fputs(name,stdout); } puts(""); } fputs("The following groups are read:",stdout); for ( ; *groups ; groups++) { fputc(' ',stdout); fputs(*groups,stdout); } puts("\nThe following options may be given as the first argument:\n\ --print-defaults Print the program argument list and exit\n\ --no-defaults Don't read default options from any options file\n\ --defaults-file=# Only read default options from the given file #\n\ --defaults-extra-file=# Read this file after the global files are read"); }
void my_print_default_files(const char *conf_file) { const char *empty_list[]= { "", 0 }; my_bool have_ext= fn_ext(conf_file)[0] != 0; const char **exts_to_use= have_ext ? empty_list : f_extensions; char name[FN_REFLEN], **ext; puts("\nDefault options are read from the following files in the given order:"); if (dirname_length(conf_file)) fputs(conf_file,stdout); else { const char **dirs; MEM_ROOT alloc; init_alloc_root(&alloc, 512, 0, MYF(0)); if ((dirs= init_default_directories(&alloc)) == NULL) { fputs("Internal error initializing default directories list", stdout); } else { for ( ; *dirs; dirs++) { for (ext= (char**) exts_to_use; *ext; ext++) { const char *pos; char *end; if (**dirs) pos= *dirs; else if (my_defaults_extra_file) pos= my_defaults_extra_file; else continue; end= convert_dirname(name, pos, NullS); if (name[0] == FN_HOMELIB) /* Add . to filenames in home */ *end++= '.'; strxmov(end, conf_file, *ext, " ", NullS); fputs(name, stdout); } } } free_root(&alloc, MYF(0)); } puts(""); }
char *get_charsets_dir(char *buf) { const char *sharedir= SHAREDIR; char *res; DBUG_ENTER("get_charsets_dir"); if (charsets_dir != NULL) strmake(buf, charsets_dir, FN_REFLEN-1); else { if (test_if_hard_path(sharedir) || is_prefix(sharedir, DEFAULT_CHARSET_HOME)) strxmov(buf, sharedir, "/", CHARSET_DIR, NullS); else strxmov(buf, DEFAULT_CHARSET_HOME, "/", sharedir, "/", CHARSET_DIR, NullS); } res= convert_dirname(buf,buf,NullS); DBUG_PRINT("info",("charsets dir: '%s'", buf)); DBUG_RETURN(res); }
void my_print_default_files(const char *conf_file) { const char *empty_list[]= { "", 0 }; my_bool have_ext= fn_ext(conf_file)[0] != 0; const char **exts_to_use= have_ext ? empty_list : f_extensions; char name[FN_REFLEN], **ext; const char **dirs; init_default_directories(); puts("\nDefault options are read from the following files in the given order:"); if (dirname_length(conf_file)) fputs(conf_file,stdout); else { for (dirs=default_directories ; *dirs; dirs++) { for (ext= (char**) exts_to_use; *ext; ext++) { const char *pos; char *end; if (**dirs) pos= *dirs; else if (my_defaults_extra_file) pos= my_defaults_extra_file; else continue; end= convert_dirname(name, pos, NullS); if (name[0] == FN_HOMELIB) /* Add . to filenames in home */ *end++='.'; strxmov(end, conf_file, *ext, " ", NullS); fputs(name,stdout); } } } puts(""); }
my_string intern_filename(my_string to, const char *from) { #ifndef VMS { uint length; char buff[FN_REFLEN]; if (from == to) { /* Dirname may destroy from */ strmov(buff,from); from=buff; } length=dirname_part(to,from); /* Copy dirname & fix chars */ (void) strcat(to,from+length); return (to); } #else /* VMS */ /* change 'dev:[lib]xxx' to 'dev:lib/xxx' */ /* change 'dev:xxx' to 'dev:xxx' */ /* change 'dev:x/y/[.lib]' to 'dev:x/y/lib/ */ /* change '[.lib]' to './lib/' */ /* change '[x.y]' or '[x.][y]' or '[x][.y]' to '/x/y/' */ /* change '[000000.x] or [x.000000]' to '/x/' */ int par_length,root_length; my_string pos,from_pos,to_pos,end_pos; char buff[FN_REFLEN]; convert_dirname(buff,from,NullS); /* change '<>' to '[]' */ from_pos=buff; if ((pos=strrchr(from_pos,FN_DEVCHAR))) /* Skipp device part */ { pos++; to_pos=strnmov(to,from_pos,(size_s) (pos-from_pos)); from_pos=pos; } else to_pos=to; root_length=strlen(FN_C_ROOT_DIR); if ((pos = strchr(from_pos,FN_C_BEFORE_DIR)) && (end_pos = strrchr(pos+1,FN_C_AFTER_DIR))) { to_pos=strnmov(to_pos,from_pos,(size_s) (pos-from_pos)); /* Copy all between ':' and '[' */ from_pos=pos+1; if (strinstr(from_pos,FN_C_ROOT_DIR) == 1 && (from_pos[root_length] == FN_C_DIR_SEP || from_pos[root_length] == FN_C_AFTER_DIR)) { from_pos+=root_length+1; } else if (*from_pos == FN_C_DIR_SEP) *(to_pos++) = FN_CURLIB; /* Set ./ first */ *(to_pos++) = FN_LIBCHAR; par_length=strlen(FN_C_PARENT_DIR); pos=to_pos; for (; from_pos <= end_pos ; from_pos++) { switch (*from_pos) { case FN_C_DIR_SEP: case FN_C_AFTER_DIR: if (pos != to_pos) { if ((int) (to_pos-pos) == root_length && is_suffix(pos,FN_C_ROOT_DIR)) to_pos=pos; /* remove root-pos */ else { *(to_pos++)=FN_LIBCHAR; /* Find lib */ pos=to_pos; } } break; case FN_C_BEFORE_DIR: break; case '-': /* *(FN_C_PARENT_DIR): */ if (to_pos[-1] == FN_LIBCHAR && strncmp(from_pos,FN_C_PARENT_DIR,par_length) == 0) { /* Change '-' to '..' */ to_pos=strmov(to_pos,FN_PARENTDIR); *(to_pos++)=FN_LIBCHAR; pos=to_pos; from_pos+=par_length-1; break; } /* Fall through */ default: *(to_pos++)= *from_pos; break; } } } (void) strmov(to_pos,from_pos); return (to); #endif /* VMS */ } /* intern_filename */
char * fn_format(char * to, const char *name, const char *dir, const char *extension, uint flag) { char dev[FN_REFLEN], buff[FN_REFLEN], *pos, *startpos; const char *ext; reg1 size_t length; size_t dev_length; my_bool not_used; DBUG_ENTER("fn_format"); DBUG_ASSERT(name != NULL); DBUG_ASSERT(extension != NULL); DBUG_PRINT("enter",("name: %s dir: %s extension: %s flag: %d", name,dir,extension,flag)); /* Copy and skip directory */ name+=(length=dirname_part(dev, (startpos=(char *) name), &dev_length)); if (length == 0 || (flag & MY_REPLACE_DIR)) { DBUG_ASSERT(dir != NULL); /* Use given directory */ convert_dirname(dev,dir,NullS); /* Fix to this OS */ } else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev)) { DBUG_ASSERT(dir != NULL); /* Put 'dir' before the given path */ strmake(buff,dev,sizeof(buff)-1); pos=convert_dirname(dev,dir,NullS); strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev)); } if (flag & MY_PACK_FILENAME) pack_dirname(dev,dev); /* Put in ./.. and ~/.. */ if (flag & MY_UNPACK_FILENAME) (void) unpack_dirname(dev, dev, ¬_used); /* Replace ~/.. with dir */ if (!(flag & MY_APPEND_EXT) && (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS) { if ((flag & MY_REPLACE_EXT) == 0) /* If we should keep old ext */ { length=strlength(name); /* Use old extension */ ext = ""; } else { length= (size_t) (pos-(char*) name); /* Change extension */ ext= extension; } } else { length=strlength(name); /* No ext, use the now one */ ext=extension; } if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN ) { /* To long path, return original or NULL */ size_t tmp_length; if (flag & MY_SAFE_PATH) DBUG_RETURN(NullS); tmp_length= strlength(startpos); DBUG_PRINT("error",("dev: '%s' ext: '%s' length: %u",dev,ext, (uint) length)); (void) strmake(to, startpos, MY_MIN(tmp_length, FN_REFLEN-1)); } else { if (to == startpos) { bmove(buff,(uchar*) name,length); /* Save name for last copy */ name=buff; } pos=strmake(strmov(to,dev),name,length); (void) strmov(pos,ext); /* Don't convert extension */ } /* If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do realpath if the file is a symbolic link */ if (flag & MY_RETURN_REAL_PATH) (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ? MY_RESOLVE_LINK: 0)); else if (flag & MY_RESOLVE_SYMLINKS) { strmov(buff,to); (void) my_readlink(to, buff, MYF(0)); } DBUG_RETURN(to); } /* fn_format */
static int search_default_file_with_ext(Process_option_func opt_handler, void *handler_ctx, const char *dir, const char *ext, const char *config_file, int recursion_level) { char name[FN_REFLEN + 10], buff[4096], curr_gr[4096], *ptr, *end, **tmp_ext; char *value, option[4096+2], tmp[FN_REFLEN]; static const char includedir_keyword[]= "includedir"; static const char include_keyword[]= "include"; const int max_recursion_level= 10; MYSQL_FILE *fp; uint line=0; my_bool found_group=0; uint i; MY_DIR *search_dir; FILEINFO *search_file; if ((dir ? strlen(dir) : 0 )+strlen(config_file) >= FN_REFLEN-3) return 0; /* Ignore wrong paths */ if (dir) { end=convert_dirname(name, dir, NullS); if (dir[0] == FN_HOMELIB) /* Add . to filenames in home */ *end++='.'; strxmov(end,config_file,ext,NullS); } else { strmov(name,config_file); } fn_format(name,name,"","",4); #if !defined(__WIN__) { MY_STAT stat_info; if (!my_stat(name,&stat_info,MYF(0))) return 1; /* Ignore world-writable regular files. This is mainly done to protect us to not read a file created by the mysqld server, but the check is still valid in most context. */ if ((stat_info.st_mode & S_IWOTH) && (stat_info.st_mode & S_IFMT) == S_IFREG) { fprintf(stderr, "Warning: World-writable config file '%s' is ignored\n", name); return 0; } } #endif if (!(fp= mysql_file_fopen(key_file_cnf, name, O_RDONLY, MYF(0)))) return 1; /* Ignore wrong files */ while (mysql_file_fgets(buff, sizeof(buff) - 1, fp)) { line++; /* Ignore comment and empty lines */ for (ptr= buff; my_isspace(&my_charset_latin1, *ptr); ptr++) {} if (*ptr == '#' || *ptr == ';' || !*ptr) continue; /* Configuration File Directives */ if (*ptr == '!') { if (recursion_level >= max_recursion_level) { for (end= ptr + strlen(ptr) - 1; my_isspace(&my_charset_latin1, *(end - 1)); end--) {} end[0]= 0; fprintf(stderr, "Warning: skipping '%s' directive as maximum include" "recursion level was reached in file %s at line %d\n", ptr, name, line); continue; } /* skip over `!' and following whitespace */ for (++ptr; my_isspace(&my_charset_latin1, ptr[0]); ptr++) {} if ((!strncmp(ptr, includedir_keyword, sizeof(includedir_keyword) - 1)) && my_isspace(&my_charset_latin1, ptr[sizeof(includedir_keyword) - 1])) { if (!(ptr= get_argument(includedir_keyword, sizeof(includedir_keyword), ptr, name, line))) goto err; if (!(search_dir= my_dir(ptr, MYF(MY_WME)))) goto err; for (i= 0; i < (uint) search_dir->number_of_files; i++) { search_file= search_dir->dir_entry + i; ext= fn_ext(search_file->name); /* check extension */ for (tmp_ext= (char**) f_extensions; *tmp_ext; tmp_ext++) { if (!strcmp(ext, *tmp_ext)) break; } if (*tmp_ext) { fn_format(tmp, search_file->name, ptr, "", MY_UNPACK_FILENAME | MY_SAFE_PATH); search_default_file_with_ext(opt_handler, handler_ctx, "", "", tmp, recursion_level + 1); } } my_dirend(search_dir); } else if ((!strncmp(ptr, include_keyword, sizeof(include_keyword) - 1)) && my_isspace(&my_charset_latin1, ptr[sizeof(include_keyword)-1])) { if (!(ptr= get_argument(include_keyword, sizeof(include_keyword), ptr, name, line))) goto err; search_default_file_with_ext(opt_handler, handler_ctx, "", "", ptr, recursion_level + 1); } continue; } if (*ptr == '[') /* Group name */ { found_group=1; if (!(end=(char *) strchr(++ptr,']'))) { fprintf(stderr, "error: Wrong group definition in config file: %s at line %d\n", name,line); goto err; } /* Remove end space */ for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ; end[0]=0; strmake(curr_gr, ptr, MY_MIN((size_t) (end-ptr)+1, sizeof(curr_gr)-1)); /* signal that a new group is found */ opt_handler(handler_ctx, curr_gr, NULL); continue; } if (!found_group) { fprintf(stderr, "error: Found option without preceding group in config file: %s at line: %d\n", name,line); goto err; } end= remove_end_comment(ptr); if ((value= strchr(ptr, '='))) end= value; /* Option without argument */ for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ; if (!value) { strmake(strmov(option,"--"),ptr, (size_t) (end-ptr)); if (opt_handler(handler_ctx, curr_gr, option)) goto err; } else { /* Remove pre- and end space */ char *value_end; for (value++ ; my_isspace(&my_charset_latin1,*value); value++) ; value_end=strend(value); /* We don't have to test for value_end >= value as we know there is an '=' before */ for ( ; my_isspace(&my_charset_latin1,value_end[-1]) ; value_end--) ; if (value_end < value) /* Empty string */ value_end=value; /* remove quotes around argument */ if ((*value == '\"' || *value == '\'') && /* First char is quote */ (value + 1 < value_end ) && /* String is longer than 1 */ *value == value_end[-1] ) /* First char is equal to last char */ { value++; value_end--; } ptr=strnmov(strmov(option,"--"),ptr,(size_t) (end-ptr)); *ptr++= '='; for ( ; value != value_end; value++) { if (*value == '\\' && value != value_end-1) { switch(*++value) { case 'n': *ptr++='\n'; break; case 't': *ptr++= '\t'; break; case 'r': *ptr++ = '\r'; break; case 'b': *ptr++ = '\b'; break; case 's': *ptr++= ' '; /* space */ break; case '\"': *ptr++= '\"'; break; case '\'': *ptr++= '\''; break; case '\\': *ptr++= '\\'; break; default: /* Unknown; Keep '\' */ *ptr++= '\\'; *ptr++= *value; break; } } else *ptr++= *value; } *ptr=0; if (opt_handler(handler_ctx, curr_gr, option)) goto err; } } mysql_file_fclose(fp, MYF(0)); return(0); err: mysql_file_fclose(fp, MYF(0)); return -1; /* Fatal error */ }
File create_temp_file(char *to, const char *dir, const char *prefix, int mode __attribute__((unused)), myf MyFlags __attribute__((unused))) { File file= -1; #ifdef __WIN__ TCHAR path_buf[MAX_PATH-14]; #endif DBUG_ENTER("create_temp_file"); DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix)); #if defined (__WIN__) /* Use GetTempPath to determine path for temporary files. This is because the documentation for GetTempFileName has the following to say about this parameter: "If this parameter is NULL, the function fails." */ if (!dir) { if (GetTempPath(sizeof(path_buf), path_buf) > 0) dir = path_buf; } /* Use GetTempFileName to generate a unique filename, create the file and release it's handle - uses up to the first three letters from prefix */ if (GetTempFileName(dir, prefix, 0, to) == 0) DBUG_RETURN(-1); DBUG_PRINT("info", ("name: %s", to)); /* Open the file without the "open only if file doesn't already exist" since the file has already been created by GetTempFileName */ if ((file= my_open(to, (mode & ~O_EXCL), MyFlags)) < 0) { /* Open failed, remove the file created by GetTempFileName */ int tmp= my_errno; (void) my_delete(to, MYF(0)); my_errno= tmp; } #elif defined(HAVE_MKSTEMP) { char prefix_buff[30]; uint pfx_len; File org_file; pfx_len= (uint) (strmov(strnmov(prefix_buff, prefix ? prefix : "tmp.", sizeof(prefix_buff)-7),"XXXXXX") - prefix_buff); if (!dir && ! (dir =getenv("TMPDIR"))) dir=P_tmpdir; if (strlen(dir)+ pfx_len > FN_REFLEN-2) { errno=my_errno= ENAMETOOLONG; DBUG_RETURN(file); } strmov(convert_dirname(to,dir,NullS),prefix_buff); org_file=mkstemp(to); if (mode & O_TEMPORARY) (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); file=my_register_filename(org_file, to, FILE_BY_MKSTEMP, EE_CANTCREATEFILE, MyFlags); /* If we didn't manage to register the name, remove the temp file */ if (org_file >= 0 && file < 0) { int tmp=my_errno; close(org_file); (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); my_errno=tmp; } } #elif defined(HAVE_TEMPNAM) { extern char **environ; char *res,**old_env,*temp_env[1]; if (dir && !dir[0]) { /* Change empty string to current dir */ to[0]= FN_CURLIB; to[1]= 0; dir=to; } old_env= (char**) environ; if (dir) { /* Don't use TMPDIR if dir is given */ environ=(const char**) temp_env; temp_env[0]=0; } if ((res=tempnam((char*) dir, (char*) prefix))) { strmake(to,res,FN_REFLEN-1); (*free)(res); file=my_create(to,0, (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW | O_TEMPORARY | O_SHORT_LIVED), MYF(MY_WME)); } else { DBUG_PRINT("error",("Got error: %d from tempnam",errno)); } environ=(const char**) old_env; } #else #error No implementation found for create_temp_file #endif if (file >= 0) thread_safe_increment(my_tmp_file_created,&THR_LOCK_open); DBUG_RETURN(file); }
static int examine_log(my_string file_name, char **table_names) { uint command,result,files_open; ulong access_time,length; my_off_t filepos; int lock_command,mi_result; char isam_file_name[FN_REFLEN],llbuff[21],llbuff2[21]; uchar head[20]; gptr buff; struct test_if_open_param open_param; IO_CACHE cache; File file; FILE *write_file; enum ha_extra_function extra_command; TREE tree; struct file_info file_info,*curr_file_info; DBUG_ENTER("examine_log"); if ((file=my_open(file_name,O_RDONLY,MYF(MY_WME))) < 0) DBUG_RETURN(1); write_file=0; if (write_filename) { if (!(write_file=my_fopen(write_filename,O_WRONLY,MYF(MY_WME)))) { my_close(file,MYF(0)); DBUG_RETURN(1); } } init_io_cache(&cache,file,0,READ_CACHE,start_offset,0,MYF(0)); bzero((gptr) com_count,sizeof(com_count)); init_tree(&tree,0,0,sizeof(file_info),(qsort_cmp2) file_info_compare,1, (tree_element_free) file_info_free, NULL); VOID(init_key_cache(KEY_CACHE_SIZE)); files_open=0; access_time=0; while (access_time++ != number_of_commands && !my_b_read(&cache,(byte*) head,9)) { isamlog_filepos=my_b_tell(&cache)-9L; file_info.filenr= mi_uint2korr(head+1); isamlog_process=file_info.process=(long) mi_uint4korr(head+3); if (!opt_processes) file_info.process=0; result= mi_uint2korr(head+7); if ((curr_file_info=(struct file_info*) tree_search(&tree,&file_info))) { curr_file_info->accessed=access_time; if (update && curr_file_info->used && curr_file_info->closed) { if (reopen_closed_file(&tree,curr_file_info)) { command=sizeof(com_count)/sizeof(com_count[0][0])/3; result=0; goto com_err; } } } command=(uint) head[0]; if (command < sizeof(com_count)/sizeof(com_count[0][0])/3 && (!table_names[0] || (curr_file_info && curr_file_info->used))) { com_count[command][0]++; if (result) com_count[command][1]++; } switch ((enum myisam_log_commands) command) { case MI_LOG_OPEN: if (!table_names[0]) { com_count[command][0]--; /* Must be counted explicite */ if (result) com_count[command][1]--; } if (curr_file_info) printf("\nWarning: %s is opened with same process and filenumber\nMaybe you should use the -P option ?\n", curr_file_info->show_name); if (my_b_read(&cache,(byte*) head,2)) goto err; file_info.name=0; file_info.show_name=0; file_info.record=0; if (read_string(&cache,(gptr*) &file_info.name, (uint) mi_uint2korr(head))) goto err; { uint i; char *pos,*to; /* Fix if old DOS files to new format */ for (pos=file_info.name; (pos=strchr(pos,'\\')) ; pos++) *pos= '/'; pos=file_info.name; for (i=0 ; i < prefix_remove ; i++) { char *next; if (!(next=strchr(pos,'/'))) break; pos=next+1; } to=isam_file_name; if (filepath) to=convert_dirname(isam_file_name,filepath,NullS); strmov(to,pos); fn_ext(isam_file_name)[0]=0; /* Remove extension */ } open_param.name=file_info.name; open_param.max_id=0; VOID(tree_walk(&tree,(tree_walk_action) test_if_open,(void*) &open_param, left_root_right)); file_info.id=open_param.max_id+1; /* * In the line below +10 is added to accomodate '<' and '>' chars * plus '\0' at the end, so that there is place for 7 digits. * It is improbable that same table can have that many entries in * the table cache. * The additional space is needed for the sprintf commands two lines * below. */ file_info.show_name=my_memdup(isam_file_name, (uint) strlen(isam_file_name)+10, MYF(MY_WME)); if (file_info.id > 1) sprintf(strend(file_info.show_name),"<%d>",file_info.id); file_info.closed=1; file_info.accessed=access_time; file_info.used=1; if (table_names[0]) { char **name; file_info.used=0; for (name=table_names ; *name ; name++) { if (!strcmp(*name,isam_file_name)) file_info.used=1; /* Update/log only this */ } } if (update && file_info.used) { if (files_open >= max_files) { if (close_some_file(&tree)) goto com_err; files_open--; } if (!(file_info.isam= mi_open(isam_file_name,O_RDWR, HA_OPEN_WAIT_IF_LOCKED))) goto com_err; if (!(file_info.record=my_malloc(file_info.isam->s->base.reclength, MYF(MY_WME)))) goto end; files_open++; file_info.closed=0; if (opt_myisam_with_debug) file_info.isam->s->rnd= 0; else file_info.isam->s->rnd= isamlog_process; } VOID(tree_insert(&tree,(gptr) &file_info,0)); if (file_info.used) { if (verbose && !record_pos_file) printf_log("%s: open -> %d",file_info.show_name, file_info.filenr); com_count[command][0]++; if (result) com_count[command][1]++; } break; case MI_LOG_CLOSE: if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s -> %d",FILENAME(curr_file_info), command_name[command],result); if (curr_file_info) { if (!curr_file_info->closed) files_open--; VOID(tree_delete(&tree,(gptr) curr_file_info)); } break; case MI_LOG_EXTRA: if (my_b_read(&cache,(byte*) head,1)) goto err; extra_command=(enum ha_extra_function) head[0]; if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s(%d) -> %d",FILENAME(curr_file_info), command_name[command], (int) extra_command,result); if (update && curr_file_info && !curr_file_info->closed) { if (mi_extra(curr_file_info->isam, extra_command, 0) != (int) result) { fflush(stdout); VOID(fprintf(stderr, "Warning: error %d, expected %d on command %s at %s\n", my_errno,result,command_name[command], llstr(isamlog_filepos,llbuff))); fflush(stderr); } } break; case MI_LOG_DELETE: if (my_b_read(&cache,(byte*) head,8)) goto err; filepos=mi_sizekorr(head); if (verbose && (!record_pos_file || ((record_pos == filepos || record_pos == NO_FILEPOS) && !cmp_filename(curr_file_info,record_pos_file))) && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s at %ld -> %d",FILENAME(curr_file_info), command_name[command],(long) filepos,result); if (update && curr_file_info && !curr_file_info->closed) { if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos)) { if (!recover) goto com_err; if (verbose) printf_log("error: Didn't find row to delete with mi_rrnd"); com_count[command][2]++; /* Mark error */ } mi_result=mi_delete(curr_file_info->isam,curr_file_info->record); if ((mi_result == 0 && result) || (mi_result && (uint) my_errno != result)) { if (!recover) goto com_err; if (mi_result) com_count[command][2]++; /* Mark error */ if (verbose) printf_log("error: Got result %d from mi_delete instead of %d", mi_result, result); } } break; case MI_LOG_WRITE: case MI_LOG_UPDATE: if (my_b_read(&cache,(byte*) head,12)) goto err; filepos=mi_sizekorr(head); length=mi_uint4korr(head+8); buff=0; if (read_string(&cache,&buff,(uint) length)) goto err; if ((!record_pos_file || ((record_pos == filepos || record_pos == NO_FILEPOS) && !cmp_filename(curr_file_info,record_pos_file))) && (!table_names[0] || (curr_file_info && curr_file_info->used))) { if (write_file && (my_fwrite(write_file,buff,length,MYF(MY_WAIT_IF_FULL | MY_NABP)))) goto end; if (verbose) printf_log("%s: %s at %ld, length=%ld -> %d", FILENAME(curr_file_info), command_name[command], filepos,length,result); } if (update && curr_file_info && !curr_file_info->closed) { if (curr_file_info->isam->s->base.blobs) fix_blob_pointers(curr_file_info->isam,buff); if ((enum myisam_log_commands) command == MI_LOG_UPDATE) { if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos)) { if (!recover) { result=0; goto com_err; } if (verbose) printf_log("error: Didn't find row to update with mi_rrnd"); if (recover == 1 || result || find_record_with_key(curr_file_info,buff)) { com_count[command][2]++; /* Mark error */ break; } } mi_result=mi_update(curr_file_info->isam,curr_file_info->record, buff); if ((mi_result == 0 && result) || (mi_result && (uint) my_errno != result)) { if (!recover) goto com_err; if (verbose) printf_log("error: Got result %d from mi_update instead of %d", mi_result, result); if (mi_result) com_count[command][2]++; /* Mark error */ } } else { mi_result=mi_write(curr_file_info->isam,buff); if ((mi_result == 0 && result) || (mi_result && (uint) my_errno != result)) { if (!recover) goto com_err; if (verbose) printf_log("error: Got result %d from mi_write instead of %d", mi_result, result); if (mi_result) com_count[command][2]++; /* Mark error */ } if (!recover && filepos != curr_file_info->isam->lastpos) { printf("error: Wrote at position: %s, should have been %s", llstr(curr_file_info->isam->lastpos,llbuff), llstr(filepos,llbuff2)); goto end; } } } my_free(buff,MYF(0)); break; case MI_LOG_LOCK: if (my_b_read(&cache,(byte*) head,sizeof(lock_command))) goto err; memcpy_fixed(&lock_command,head,sizeof(lock_command)); if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s(%d) -> %d\n",FILENAME(curr_file_info), command_name[command],lock_command,result); if (update && curr_file_info && !curr_file_info->closed) { if (mi_lock_database(curr_file_info->isam,lock_command) != (int) result) goto com_err; } break; case MI_LOG_DELETE_ALL: if (verbose && !record_pos_file && (!table_names[0] || (curr_file_info && curr_file_info->used))) printf_log("%s: %s -> %d\n",FILENAME(curr_file_info), command_name[command],result); break; default: fflush(stdout); VOID(fprintf(stderr, "Error: found unknown command %d in logfile, aborted\n", command)); fflush(stderr); goto end; } } end_key_cache(); delete_tree(&tree); VOID(end_io_cache(&cache)); VOID(my_close(file,MYF(0))); if (write_file && my_fclose(write_file,MYF(MY_WME))) DBUG_RETURN(1); DBUG_RETURN(0); err: fflush(stdout); VOID(fprintf(stderr,"Got error %d when reading from logfile\n",my_errno)); fflush(stderr); goto end; com_err: fflush(stdout); VOID(fprintf(stderr,"Got error %d, expected %d on command %s at %s\n", my_errno,result,command_name[command], llstr(isamlog_filepos,llbuff))); fflush(stderr); end: end_key_cache(); delete_tree(&tree); VOID(end_io_cache(&cache)); VOID(my_close(file,MYF(0))); if (write_file) VOID(my_fclose(write_file,MYF(MY_WME))); DBUG_RETURN(1); }
static my_bool search_default_file(DYNAMIC_ARRAY *args, MEM_ROOT *alloc, const char *dir, const char *config_file, const char *ext, TYPELIB *group) { char name[FN_REFLEN+10],buff[4096],*ptr,*end,*value,*tmp; FILE *fp; uint line=0; my_bool read_values=0,found_group=0; if ((dir ? strlen(dir) : 0 )+strlen(config_file) >= FN_REFLEN-3) return 0; /* Ignore wrong paths */ if (dir) { strmov(name,dir); convert_dirname(name); if (dir[0] == FN_HOMELIB) /* Add . to filenames in home */ strcat(name,"."); strxmov(strend(name),config_file,ext,NullS); } else { strmov(name,config_file); } fn_format(name,name,"","",4); #if !defined(_WIN32) && !defined(OS2) { MY_STAT stat_info; if (!my_stat(name,&stat_info,MYF(0))) return 0; if (stat_info.st_mode & S_IWOTH) /* ignore world-writeable files */ { fprintf(stderr, "warning: World-writeable config file %s is ignored\n", name); return 0; } } #endif if (!(fp = my_fopen(fn_format(name,name,"","",4),O_RDONLY,MYF(0)))) return 0; /* Ignore wrong files */ while (fgets(buff,sizeof(buff)-1,fp)) { line++; /* Ignore comment and empty lines */ for (ptr=buff ; isspace(*ptr) ; ptr++ ) ; if (*ptr == '#' || *ptr == ';' || !*ptr) continue; if (*ptr == '[') /* Group name */ { found_group=1; if (!(end=(char *) strchr(++ptr,']'))) { fprintf(stderr, "error: Wrong group definition in config file: %s at line %d\n", name,line); goto err; } for ( ; isspace(end[-1]) ; end--) ; /* Remove end space */ end[0]=0; read_values=find_type(ptr,group,3) > 0; continue; } if (!found_group) { fprintf(stderr, "error: Found option without preceding group in config file: %s at line: %d\n", name,line); goto err; } if (!read_values) continue; if (!(end=value=strchr(ptr,'='))) end=strend(ptr); /* Option without argument */ for ( ; isspace(end[-1]) ; end--) ; if (!value) { if (!(tmp=alloc_root(alloc,(uint) (end-ptr)+3))) goto err; strmake(strmov(tmp,"--"),ptr,(uint) (end-ptr)); if (insert_dynamic(args,(gptr) &tmp)) goto err; } else { /* Remove pre- and end space */ char *value_end; for (value++ ; isspace(*value); value++) ; value_end=strend(value); for ( ; isspace(value_end[-1]) ; value_end--) ; if (value_end < value) /* Empty string */ value_end=value; if (!(tmp=alloc_root(alloc,(uint) (end-ptr)+3 + (uint) (value_end-value)+1))) goto err; if (insert_dynamic(args,(gptr) &tmp)) goto err; ptr=strnmov(strmov(tmp,"--"),ptr,(uint) (end-ptr)); *ptr++= '='; for ( ; value != value_end; value++) { if (*value == '\\' && value != value_end-1) { switch(*++value) { case 'n': *ptr++='\n'; break; case 't': *ptr++= '\t'; break; case 'r': *ptr++ = '\r'; break; case 'b': *ptr++ = '\b'; break; case 's': *ptr++= ' '; /* space */ break; case '\\': *ptr++= '\\'; break; default: /* Unknown; Keep '\' */ *ptr++= '\\'; *ptr++= *value; break; } } else *ptr++= *value; } *ptr=0; } } my_fclose(fp,MYF(0)); return(0); err: my_fclose(fp,MYF(0)); return 1; }
File create_temp_file(char *to, const char *dir, const char *prefix, int mode __attribute__((unused)), myf MyFlags __attribute__((unused))) { File file= -1; DBUG_ENTER("create_temp_file"); #if defined(_MSC_VER) { char temp[FN_REFLEN],*end,*res,**old_env,*temp_env[1]; old_env=environ; if (dir) { end=strend(dir)-1; if (!dir[0]) { /* Change empty string to current dir */ to[0]= FN_CURLIB; to[1]= 0; dir=to; } else if (*end == FN_DEVCHAR) { /* Get current dir for drive */ _fullpath(temp,dir,FN_REFLEN); dir=to; } else if (*end == FN_LIBCHAR && dir < end && end[-1] != FN_DEVCHAR) { strmake(to,dir,(uint) (end-dir)); /* Copy and remove last '\' */ dir=to; } environ=temp_env; /* Force use of dir (dir not checked) */ temp_env[0]=0; } if ((res=tempnam((char*) dir,(char *) prefix))) { strmake(to,res,FN_REFLEN-1); (*free)(res); file=my_create(to,0, mode, MyFlags); } environ=old_env; } #elif defined(_ZTC__) if (!dir) dir=getenv("TMPDIR"); if ((res=tempnam((char*) dir,(char *) prefix))) { strmake(to,res,FN_REFLEN-1); (*free)(res); file=my_create(to, 0, mode, MyFlags); } #elif defined(HAVE_MKSTEMP) { char prefix_buff[30]; uint pfx_len; File org_file; pfx_len=(strmov(strnmov(prefix_buff, prefix ? prefix : "tmp.", sizeof(prefix_buff)-7),"XXXXXX") - prefix_buff); if (!dir && ! (dir =getenv("TMPDIR"))) dir=P_tmpdir; if (strlen(dir)+ pfx_len > FN_REFLEN-2) { errno=my_errno= ENAMETOOLONG; return 1; } strmov(to,dir); strmov(convert_dirname(to),prefix_buff); org_file=mkstemp(to); file=my_register_filename(org_file, to, FILE_BY_MKSTEMP, EE_CANTCREATEFILE, MyFlags); /* If we didn't manage to register the name, remove the temp file */ if (org_file >= 0 && file < 0) { int tmp=my_errno; (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); my_errno=tmp; } } #elif defined(HAVE_TEMPNAM) { char *res,**old_env,*temp_env[1]; if (dir && !dir[0]) { /* Change empty string to current dir */ to[0]= FN_CURLIB; to[1]= 0; dir=to; } #ifdef OS2 // changing environ variable doesn't work with VACPP char buffer[256]; sprintf( buffer, "TMP=%s", dir); // remove ending backslash if (buffer[strlen(buffer)-1] == '\\') buffer[strlen(buffer)-1] = '\0'; putenv( buffer); #else old_env= (char**) environ; if (dir) { /* Don't use TMPDIR if dir is given */ environ=(const char**) temp_env; temp_env[0]=0; } #endif if ((res=tempnam((char*) dir, (char*) prefix))) { strmake(to,res,FN_REFLEN-1); (*free)(res); file=my_create(to,0, (int) (O_RDWR | O_BINARY | O_TRUNC | O_TEMPORARY | O_SHORT_LIVED), MYF(MY_WME)); } else { DBUG_PRINT("error",("Got error: %d from tempnam",errno)); } #ifndef OS2 environ=(const char**) old_env; #endif } #else { register long uniq; register int length; my_string pos,end_pos; /* Make an unique number */ pthread_mutex_lock(&THR_LOCK_open); uniq= ((long) getpid() << 20) + (long) _my_tempnam_used++ ; pthread_mutex_unlock(&THR_LOCK_open); if (!dir && !(dir=getenv("TMPDIR"))) /* Use this if possibly */ dir=P_tmpdir; /* Use system default */ length=strlen(dir)+strlen(pfx)+1; DBUG_PRINT("test",("mallocing %d byte",length+8+sizeof(TMP_EXT)+1)); if (length+8+sizeof(TMP_EXT)+1 > FN_REFLENGTH) errno=my_errno= ENAMETOOLONG; else { end_pos=strmov(to,dir); if (end_pos != to && end_pos[-1] != FN_LIBCHAR) *end_pos++=FN_LIBCHAR; end_pos=strmov(end_pos,pfx); for (length=0 ; length < 8 && uniq ; length++) { *end_pos++= _dig_vec[(int) (uniq & 31)]; uniq >>= 5; } (void) strmov(end_pos,TMP_EXT); file=my_create(to,0, (int) (O_RDWR | O_BINARY | O_TRUNC | O_TEMPORARY | O_SHORT_LIVED), MYF(MY_WME)); } } #endif if (file >= 0) thread_safe_increment(my_tmp_file_created,&THR_LOCK_open); DBUG_RETURN(file); }
File create_temp_file(char *to, const char *dir, const char *prefix, int mode, myf MyFlags) { File file= -1; #ifdef _WIN32 TCHAR path_buf[MAX_PATH-14]; #endif DBUG_ENTER("create_temp_file"); DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix)); #if defined(_WIN32) /* Use GetTempPath to determine path for temporary files. This is because the documentation for GetTempFileName has the following to say about this parameter: "If this parameter is NULL, the function fails." */ if (!dir) { if(GetTempPath(sizeof(path_buf), path_buf) > 0) dir = path_buf; } /* Use GetTempFileName to generate a unique filename, create the file and release it's handle - uses up to the first three letters from prefix */ if (GetTempFileName(dir, prefix, 0, to) == 0) DBUG_RETURN(-1); DBUG_PRINT("info", ("name: %s", to)); /* Open the file without the "open only if file doesn't already exist" since the file has already been created by GetTempFileName */ if ((file= my_open(to, (mode & ~O_EXCL), MyFlags)) < 0) { /* Open failed, remove the file created by GetTempFileName */ int tmp= my_errno(); (void) my_delete(to, MYF(0)); set_my_errno(tmp); } #else /* mkstemp() is available on all non-Windows supported platforms. */ { char prefix_buff[30]; uint pfx_len; File org_file; pfx_len= (uint) (my_stpcpy(my_stpnmov(prefix_buff, prefix ? prefix : "tmp.", sizeof(prefix_buff)-7),"XXXXXX") - prefix_buff); if (!dir && ! (dir =getenv("TMPDIR"))) dir= DEFAULT_TMPDIR; if (strlen(dir)+ pfx_len > FN_REFLEN-2) { errno=ENAMETOOLONG; set_my_errno(ENAMETOOLONG); DBUG_RETURN(file); } my_stpcpy(convert_dirname(to,dir,NullS),prefix_buff); org_file=mkstemp(to); if (mode & O_TEMPORARY) (void) my_delete(to, MYF(MY_WME)); file=my_register_filename(org_file, to, FILE_BY_MKSTEMP, EE_CANTCREATEFILE, MyFlags); /* If we didn't manage to register the name, remove the temp file */ if (org_file >= 0 && file < 0) { int tmp=my_errno(); close(org_file); (void) my_delete(to, MYF(MY_WME)); set_my_errno(tmp); } } #endif if (file >= 0) { mysql_mutex_lock(&THR_LOCK_open); my_tmp_file_created++; mysql_mutex_unlock(&THR_LOCK_open); } DBUG_RETURN(file); }
my_string fn_format(my_string to, const char *name, const char *dsk, const char *form, int flag) { reg1 uint length; char dev[FN_REFLEN], buff[BUFF_LEN], *pos, *startpos; const char *ext; DBUG_ENTER("fn_format"); DBUG_PRINT("enter",("name: %s dsk: %s form: %s flag: %d", name,dsk,form,flag)); /* Kopiera & skippa enheten */ name+=(length=dirname_part(dev,(startpos=(my_string) name))); if (length == 0 || flag & 1) { (void) strmake(dev,dsk, sizeof(dev) - 2); /* Use given directory */ convert_dirname(dev); /* Fix to this OS */ } if (flag & 8) pack_dirname(dev,dev); /* Put in ./.. and ~/.. */ if (flag & 4) (void) unpack_dirname(dev,dev); /* Replace ~/.. with dir */ if ((pos=(char*)strchr(name,FN_EXTCHAR)) != NullS) { if ((flag & 2) == 0) /* Skall vi byta extension ? */ { length=strlength(name); /* Old extension */ ext = ""; } else { length=(uint) (pos-(char*) name); /* Change extension */ ext= form; } } else { length=strlength(name); /* Har ingen ext- tag nya */ ext=form; } if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN ) { /* To long path, return original */ uint tmp_length; if (flag & 64) return 0; tmp_length=strlength(startpos); DBUG_PRINT("error",("dev: '%s' ext: '%s' length: %d",dev,ext,length)); (void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1)); } else { if (to == startpos) { bmove(buff,(char*) name,length); /* Save name for last copy */ name=buff; } pos=strmake(strmov(to,dev),name,length); #ifdef FN_UPPER_CASE caseup_str(to); #endif #ifdef FN_LOWER_CASE casedn_str(to); #endif (void) strmov(pos,ext); /* Don't convert extension */ } /* Purify gives a lot of UMR errors when using realpath */ #if defined(HAVE_REALPATH) && !defined(HAVE_purify) && !defined(HAVE_BROKEN_REALPATH) if (flag & 16) { struct stat stat_buff; if (flag & 32 || (!lstat(to,&stat_buff) && S_ISLNK(stat_buff.st_mode))) { if (realpath(to,buff)) strmake(to,buff,FN_REFLEN-1); } } #endif DBUG_RETURN (to); } /* fn_format */
void print_defaults(const char *conf_file, const char **groups) { #ifdef __WIN__ my_bool have_ext= fn_ext(conf_file)[0] != 0; #endif char name[FN_REFLEN], **ext; const char **dirs; puts("\nDefault options are read from the following files in the given order:"); if (dirname_length(conf_file)) fputs(conf_file,stdout); else { #ifdef __WIN__ GetWindowsDirectory(name,sizeof(name)); if (!have_ext) { for (ext= (char**) f_extensions; *ext; *ext++) printf("%s\\%s%s ", name, conf_file, *ext); } else printf("%s\\%s ", name, conf_file); #endif #if defined(__EMX__) || defined(OS2) { const char *etc; if ((etc= getenv("ETC"))) { for (ext= (char**) f_extensions; *ext; *ext++) printf("%s\\%s%s ", etc, conf_file, *ext); } } #endif for (dirs=default_directories ; *dirs; dirs++) { for (ext= (char**) f_extensions; *ext; *ext++) { const char *pos; char *end; if (**dirs) pos= *dirs; else if (defaults_extra_file) pos= defaults_extra_file; else continue; end= convert_dirname(name, pos, NullS); if (name[0] == FN_HOMELIB) /* Add . to filenames in home */ *end++='.'; strxmov(end, conf_file, *ext, " ", NullS); fputs(name,stdout); } } puts(""); } fputs("The following groups are read:",stdout); for ( ; *groups ; groups++) { fputc(' ',stdout); fputs(*groups,stdout); } puts("\nThe following options may be given as the first argument:\n\ --print-defaults Print the program argument list and exit\n\ --no-defaults Don't read default options from any options file\n\ --defaults-file=# Only read default options from the given file #\n\ --defaults-extra-file=# Read this file after the global files are read"); }