- Add copyright + GPL
- > #define DONT_DEFINE_VOID
- > #include <my_global.h>
- > #include <my_getopt.h>
- > #include <my_sys.h>
- > #include <m_string.h>
- >
- > #include <stdlib.h>
- > #include <stdio.h>
- You can remove stdlib.h, stdio.h and assert.h (included by my_global.h)
- > #include <windows.h>
- > #include <assert.h>
- > #include <shellapi.h>
- > #include <accctrl.h>
- > #include <aclapi.h>
- >
- > #define USAGETEXT \
- > "mysql_install_db.exe Ver 1.42 for Windows\n" \
- > "This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n" \
- > "and you are welcome to modify and redistribute it under the GPL v2 license\n" \
- > "Usage: mysql_install_db.exe [OPTIONS]\n" \
- > "OPTIONS:"
- Add copyright
- Version number to 1.0
- <cut>
- > static struct my_option my_long_options[]=
- > {
- > {"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
- > NO_ARG, 0, 0, 0, 0, 0, 0},
- > {"datadir", 'd', "Data directory of the new database",
- > &opt_datadir, &opt_datadir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
- > {"service", 's', "Name of the Windows service",
- Change to use -S as we want to reserver -s for silent
- > &opt_service, &opt_service, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
- > {"password", 'p', "Root password",
- > &opt_password, &opt_password, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
- > {"port", 'P', "mysql port",
- > &opt_port, &opt_port, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
- > {"socket", 'S',
- > "named pipe name (if missing, it will be set the same as service)",
- Change to use 'W'
- <cut>
- Add verbose and silent to the above options.
- (You already have the variables, but users can't use them)
- > static void die(const char *fmt, ...)
- > {
- > va_list args;
- > DBUG_ENTER("die");
- >
- > /* Print the error message */
- > va_start(args, fmt);
- > if (fmt)
- - Remove test for 'fmt'
- > {
- > fprintf(stderr, "FATAL ERROR: ");
- > vfprintf(stderr, fmt, args);
- > fprintf(stderr, "\n");
- Use: fputc(stderr, '\n');
- > fflush(stderr);
- > }
- Add here (before fflush())
- if (!opt_verbose)
- fprintf(stderr, "Use --verbose to get more information of where things geos wrong\n");
- fprintf(
- "http://kb.askmonty.org/v/installation-issues-on-windows contains some help\n"
- "for solving the most common problems. If this doesn't help you, please\n"
- "leave a comment in the knowledge base or file a bug report at\n"
- "https://bugs.launchpad.net/maria"
- > va_end(args);
- > my_end(0);
- > exit(1);
- > }
- >
- >
- > static void verbose(const char *fmt, ...)
- > {
- > va_list args;
- >
- > if (opt_silent)
- > return;
- > /* Print the verbose message */
- > va_start(args, fmt);
- > if (fmt)
- > {
- Assume fmt is given.
- > vfprintf(stdout, fmt, args);
- > fprintf(stdout, "\n");
- > fflush(stdout);
- > }
- > va_end(args);
- > }
- >
- >
- > int main(int argc, char **argv)
- > {
- > int error;
- > MY_INIT(argv[0]);
- > char self_name[FN_REFLEN];
- > char *p;
- >
- > GetModuleFileName(NULL, self_name, FN_REFLEN);
- > strcpy(mysqld_path,self_name);
- > p = strrchr(mysqld_path, FN_LIBCHAR);
- > if(p)
- > {
- > strcpy(p, "\\mysqld.exe");
- > }
- >
- > if ((error= handle_options(&argc, &argv, my_long_options, get_one_option)))
- > exit(error);
- > if(!opt_datadir)
- > {
- > my_print_help(my_long_options);
- > die("parameter datadir is mandatory");
- Change to:
- die("--datadir=... parameter is mandatory");
- <cut>
- >
- > /**
- > Convert slashes in paths into MySQL-compatible form
- > */
- > static void convert_slashes(char *s)
- > {
- > for(size_t i=0; i< strlen(s); i++)
- > if(s[i] == '\\')
- > s[i] = '/';
- Argh! (You know better ;)
- Change to:
- for (; *s ; s++)
- if (*s == '\\')
- *s= '/';
- <cut>
- > /**
- > Calculate basedir from mysqld.exe path
- Add to comment:
- The assumption is that basedir is one level up from mysqld.exe.
- basedir for C:\my\bin\mysqld.exe would thus be C:\my
- > */
- Add empty line between function comment and function
- > static void get_basedir(char *basedir, int size, const char *mysqld_path)
- > {
- > strcpy_s(basedir, size, mysqld_path);
- > convert_slashes(basedir);
- > char *p = strrchr(basedir,'/');
- > if(p)
- > {
- > *p = 0;
- > p=strrchr(basedir, '/');
- > if(p)
- > *p=0;
- > }
- > }
- >
- >
- >
- Remove one empty line here (2 empty lines to separate functions from
- next function comment)
- > /**
- > Allocate and initialize command line for mysqld --bootstrap.
- > The resulting string is passed to popen, so it has a lot of quoting
- > quoting around the full string plus quoting around parameters with spaces.
- > */
- <cut>
- >
- > static int create_myini()
- > {
- > my_bool enable_named_pipe= FALSE;
- > printf("Creating my.ini file\n");
- >
- > char path_buf[MAX_PATH];
- > GetCurrentDirectory(MAX_PATH, path_buf);
- >
- > /* Create ini file. */
- > FILE *myini = fopen("my.ini","wt");
- > if(!myini)
- > {
- > die("Cannot create my.ini in data directory");
- Change to:
- die(Cannot create my.ini in data directory '%s'", path_buf);
- > }
- > /*
- > Write out server settings. datadir and basedir are calculated,
- > using path to mysqld.exe.
- > */
- remove comment about basedir as it's not stored.
- > fprintf(myini, "[mysqld]\n");
- > convert_slashes(path_buf);
- > fprintf(myini, "datadir=%s\n", path_buf);
- > if (opt_skip_networking)
- > {
- > fprintf(myini,"skip-networking\n");
- > if(!opt_socket)
- > opt_socket= opt_service;
- > }
- > enable_named_pipe= (my_bool)
- > ((opt_socket && opt_socket[0]) || opt_skip_networking);
- >
- > if(enable_named_pipe)
- > {
- > fprintf(myini,"enable-named-pipe\n");
- > }
- >
- > if(opt_socket && opt_socket[0])
- > {
- > fprintf(myini, "socket=%s\n", opt_socket);
- > }
- Add a comment that the above is used for named pipes.
- > if (opt_port)
- > {
- > fprintf(myini,"port=%d\n", opt_port);
- > }
- Add if (!opt_skip_networking) to the above
- >
- > /* Write out client settings. */
- > fprintf(myini, "[client]\n");
- > if(opt_socket && opt_socket[0])
- > fprintf(myini,"socket=%s\n",opt_socket);
- > if(opt_skip_networking)
- > fprintf(myini,"protocol=pipe\n");
- > if(opt_port)
- -> if(!opt_port && opt_skip_networking)
- > fprintf(myini,"port=%d\n",opt_port);
- > fclose(myini);
- > return 0;
- > }
- >
- >
- > static const char update_root_passwd_part1[]=
- > "UPDATE mysql.user SET Password = PASSWORD('";
- > static const char update_root_passwd_part2[]=
- > "') where User='root';\n";
- > static const char remove_default_user_cmd[]=
- > "DELETE FROM mysql.user where User='';\n";
- > static const char allow_remote_root_access_cmd[]=
- > "CREATE TEMPORARY TABLE tmp_user LIKE user;\n"
- You could add here:
- engine=memory
- > "INSERT INTO tmp_user SELECT * from user where user='root' "
- > " AND host='localhost';\n"
- > "UPDATE tmp_user SET host='%';\n"
- > "INSERT INTO user SELECT * FROM tmp_user;\n"
- > "DROP TABLE tmp_user;\n";
- > static const char end_of_script[]="-- end.";
- <cut>
- > /* Get a handle to the SCM database. */
- > sc_manager= OpenSCManager(
- > NULL,
- > NULL,
- > SC_MANAGER_ALL_ACCESS);
- Fix indentation
- >
- > if (!sc_manager)
- > {
- > die("OpenSCManager failed (%d)\n", GetLastError());
- > }
- Add cast to (int)
- >
- > /* Create the service. */
- > sc_service = CreateServiceA(sc_manager, opt_service, opt_service,
- > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
- > SERVICE_ERROR_NORMAL, buf, NULL, NULL, NULL, opt_os_user, opt_os_password);
- >
- > if (!sc_service)
- > {
- > CloseServiceHandle(sc_manager);
- > die("CreateService failed (%d)", GetLastError());
- > }
- Add cast to (int)
- > SERVICE_DESCRIPTION sd = { "MariaDB database server" };
- > ChangeServiceConfig2(sc_service, SERVICE_CONFIG_DESCRIPTION, &sd);
- > CloseServiceHandle(sc_service);
- > CloseServiceHandle(sc_manager);
- > return 0;
- > }
- >
- >
- > static void clean_directory(const char *dir)
- > {
- > char dir2[MAX_PATH+2];
- > size_t len = strlen(dir);
- >
- > strcpy_s(dir2, MAX_PATH+2, dir);
- > dir2[len+1] = 0;
- A better way would be:
- *(strmake(dir2, dir, MAX_PATH+1)+1)= 0;
- <cut>
- > /* Create database instance (including registering as service etc) .*/
- > static int create_db_instance()
- > {
- > int ret=0;
- > char cwd[MAX_PATH];
- > DWORD cwd_len= MAX_PATH;
- > char cmdline[3*MAX_PATH];
- > FILE *in;
- >
- > verbose("Running bootstrap");
- >
- > GetCurrentDirectory(cwd_len, cwd);
- > CreateDirectory(opt_datadir, NULL); /*ignore error, it might already exist */
- >
- > if(!SetCurrentDirectory(opt_datadir))
- > {
- > die("Cannot set current directory to %s\n",opt_datadir);
- Add quotes around '%s' so that we can detect empty names and space.
- > return -1;
- > }
- >
- > CreateDirectory("mysql",NULL);
- > CreateDirectory("test", NULL);
- >
- > set_directory_permissions(opt_datadir, NULL);
- > set_directory_permissions(opt_datadir, default_os_user);
- Please add a comment that the above gives permissions for both the current
- user and default_os_user.
- >
- > /* Create mysqld --bootstrap process */
- > init_bootstrap_command_line(cmdline, sizeof(cmdline));
- > /* verbose("Executing %s", cmdline); */
- >
- > in= popen(cmdline, "wt");
- > if(!in)
- > goto end;
- >
- > if (fwrite("use mysql;\n",11,1, in) != 1)
- > {
- > verbose("ERROR: Cannot write to mysqld's stdin");
- > ret = 1;
- > goto end;
- > }
- >
- > /* Write the bootstrap script to stdin. */
- > if (fwrite(mysql_bootstrap_sql, strlen(mysql_bootstrap_sql), 1, in) != 1)
- > {
- > verbose("ERROR: Cannot write to mysqld's stdin");
- > ret= 1;
- > goto end;
- > }
- >
- >
- > /* Remove default user, if requested. */
- > if(!opt_default_user)
- > {
- > verbose("Removing default user",remove_default_user_cmd);
- > fputs(remove_default_user_cmd, in);
- > fflush(in);
- > }
- >
- > if(opt_allow_remote_root_access)
- > {
- > verbose("Allowing remote access for user root",remove_default_user_cmd);
- > fputs(allow_remote_root_access_cmd,in);
- > fflush(in);
- > }
- >
- > /* Change root password if requested. */
- > if (opt_password)
- > {
- > verbose("Changing root password",remove_default_user_cmd);
- > fputs(update_root_passwd_part1, in);
- > fputs(opt_password, in);
- > fputs(update_root_passwd_part2, in);
- > fflush(in);
- > }
- Here you should end with 'flush privileges' to enable the above changes.
- > /*
- > On some reason, bootstrap chokes if last command sent via stdin ends with
- > newline, so we supply a dummy comment, that does not end with newline.
- > */
- > fputs(end_of_script, in);
- > fflush(in);
- >
- > /* Check if bootstrap has completed successfully. */
- > ret= pclose(in);
- > if (ret)
- > {
- > verbose("mysqld returned an error in pclose");
- Please print error code
- > goto end;
- > }
- >
- > /* Create my.ini file in data directory.*/
- > ret= create_myini();
- > if(ret)
- > goto end;
- >
- > /* Register service if requested. */
- > if(opt_service && opt_service[0])
- > {
- Add here
- verbose("Registering service '%s', opt_service);
- <cut>