Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 27th, 2012  |  syntax: None  |  size: 11.16 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. Add copyright + GPL
  3.  
  4. > #define DONT_DEFINE_VOID
  5. > #include <my_global.h>
  6. > #include <my_getopt.h>
  7. > #include <my_sys.h>
  8. > #include <m_string.h>
  9. >
  10. > #include <stdlib.h>
  11. > #include <stdio.h>
  12.  
  13. You can remove stdlib.h, stdio.h and assert.h (included by my_global.h)
  14.  
  15. > #include <windows.h>
  16. > #include <assert.h>
  17. > #include <shellapi.h>
  18. > #include <accctrl.h>
  19. > #include <aclapi.h>
  20. >
  21. > #define USAGETEXT \
  22. > "mysql_install_db.exe  Ver 1.42 for Windows\n" \
  23. > "This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n" \
  24. > "and you are welcome to modify and redistribute it under the GPL v2 license\n" \
  25. > "Usage: mysql_install_db.exe [OPTIONS]\n" \
  26. > "OPTIONS:"
  27.  
  28. Add copyright
  29. Version number to 1.0
  30.  
  31. <cut>
  32.  
  33. > static struct my_option my_long_options[]=
  34. > {
  35. >   {"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
  36. >    NO_ARG, 0, 0, 0, 0, 0, 0},
  37. >   {"datadir", 'd', "Data directory of the new database",
  38. >   &opt_datadir, &opt_datadir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  39. >   {"service", 's', "Name of the Windows service",
  40.  
  41. Change to use -S as we want to reserver -s for silent
  42.  
  43. >   &opt_service, &opt_service, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  44. >   {"password", 'p', "Root password",
  45. >   &opt_password, &opt_password, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  46. >   {"port", 'P', "mysql port",
  47. >   &opt_port, &opt_port, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  48. >   {"socket", 'S',
  49. >   "named pipe name (if missing, it will be set the same as service)",
  50.  
  51. Change to use 'W'
  52.  
  53. <cut>
  54.  
  55. Add verbose and silent to the above options.
  56. (You already have the variables, but users can't use them)
  57.  
  58.  
  59. > static void die(const char *fmt, ...)
  60. > {
  61. >   va_list args;
  62. >   DBUG_ENTER("die");
  63. >
  64. >   /* Print the error message */
  65. >   va_start(args, fmt);
  66. >   if (fmt)
  67.  
  68. - Remove test for 'fmt'
  69.  
  70. >   {
  71. >     fprintf(stderr, "FATAL ERROR: ");
  72. >     vfprintf(stderr, fmt, args);
  73. >     fprintf(stderr, "\n");
  74.  
  75. Use: fputc(stderr, '\n');
  76.  
  77. >     fflush(stderr);
  78. >   }
  79.  
  80. Add here (before fflush())
  81.  
  82.     if (!opt_verbose)
  83.       fprintf(stderr, "Use --verbose to get more information of where things geos wrong\n");
  84.     fprintf(
  85. "http://kb.askmonty.org/v/installation-issues-on-windows contains some help\n"
  86. "for solving the most common problems.  If this doesn't help you, please\n"
  87. "leave a comment in the knowledge base or file a bug report at\n"
  88. "https://bugs.launchpad.net/maria"
  89.  
  90. >   va_end(args);
  91. >   my_end(0);
  92. >   exit(1);
  93. > }
  94. >
  95. >
  96. > static void verbose(const char *fmt, ...)
  97. > {
  98. >   va_list args;
  99. >
  100. >   if (opt_silent)
  101. >     return;
  102.  
  103. >   /* Print the verbose message */
  104. >   va_start(args, fmt);
  105. >   if (fmt)
  106. >   {
  107.  
  108. Assume fmt is given.
  109.  
  110. >     vfprintf(stdout, fmt, args);
  111. >     fprintf(stdout, "\n");
  112. >     fflush(stdout);
  113. >   }
  114. >   va_end(args);
  115. > }
  116. >
  117. >
  118. > int main(int argc, char **argv)
  119. > {
  120. >   int error;
  121. >   MY_INIT(argv[0]);
  122. >   char self_name[FN_REFLEN];
  123. >   char *p;
  124. >
  125. >   GetModuleFileName(NULL, self_name, FN_REFLEN);
  126. >   strcpy(mysqld_path,self_name);
  127. >   p = strrchr(mysqld_path, FN_LIBCHAR);
  128. >   if(p)
  129. >   {
  130. >     strcpy(p, "\\mysqld.exe");
  131. >   }
  132. >
  133. >   if ((error= handle_options(&argc, &argv, my_long_options, get_one_option)))
  134. >     exit(error);
  135. >   if(!opt_datadir)
  136. >   {
  137. >     my_print_help(my_long_options);
  138. >     die("parameter datadir is mandatory");
  139.  
  140. Change to:
  141.  
  142.   die("--datadir=... parameter is mandatory");
  143.  
  144. <cut>
  145.  
  146. >
  147. > /**
  148. >   Convert slashes in paths into MySQL-compatible form
  149. > */
  150. > static void convert_slashes(char *s)
  151. > {
  152. >   for(size_t i=0; i< strlen(s); i++)
  153. >     if(s[i] == '\\')
  154. >       s[i] = '/';
  155.  
  156. Argh!  (You know better ;)
  157.  
  158. Change to:
  159.  
  160.    for (; *s ; s++)
  161.      if (*s == '\\')
  162.      *s= '/';
  163.  
  164. <cut>
  165.  
  166. > /**
  167. >   Calculate basedir from mysqld.exe path
  168.  
  169. Add to comment:
  170.  
  171. The assumption is that basedir is one level up from mysqld.exe.
  172. basedir for C:\my\bin\mysqld.exe would thus be C:\my
  173.  
  174. > */
  175.  
  176. Add empty line between function comment and function
  177.  
  178. > static void get_basedir(char *basedir, int size, const char *mysqld_path)
  179. > {
  180. >   strcpy_s(basedir, size,  mysqld_path);
  181. >   convert_slashes(basedir);
  182. >   char *p = strrchr(basedir,'/');
  183. >   if(p)
  184. >   {
  185. >     *p = 0;
  186. >     p=strrchr(basedir, '/');
  187. >     if(p)
  188. >       *p=0;
  189. >   }
  190. > }
  191. >
  192. >
  193. >
  194.  
  195. Remove one empty line here (2 empty lines to separate functions from
  196. next function comment)
  197.  
  198. > /**
  199. >   Allocate and initialize command line for mysqld --bootstrap.
  200. >  The resulting string is passed to popen, so it has a lot of quoting
  201. >  quoting around the full string plus quoting around parameters with spaces.
  202. > */
  203.  
  204. <cut>
  205.  
  206. >
  207. > static int create_myini()
  208. > {
  209. >   my_bool enable_named_pipe= FALSE;
  210. >   printf("Creating my.ini file\n");
  211. >
  212. >   char path_buf[MAX_PATH];
  213. >   GetCurrentDirectory(MAX_PATH, path_buf);
  214. >
  215. >   /* Create ini file. */
  216. >   FILE *myini = fopen("my.ini","wt");
  217. >   if(!myini)
  218. >   {
  219. >     die("Cannot create my.ini in data directory");
  220.  
  221. Change to:
  222.  
  223.       die(Cannot create my.ini in data directory '%s'", path_buf);
  224.  
  225. >   }
  226. >   /*
  227. >     Write out server settings. datadir and basedir are calculated,
  228. >     using path to mysqld.exe.
  229. >   */
  230.  
  231. remove comment about basedir as it's not stored.
  232.  
  233. >   fprintf(myini, "[mysqld]\n");
  234. >   convert_slashes(path_buf);
  235. >   fprintf(myini, "datadir=%s\n", path_buf);
  236. >   if (opt_skip_networking)
  237. >   {
  238. >     fprintf(myini,"skip-networking\n");
  239. >     if(!opt_socket)
  240. >       opt_socket= opt_service;
  241. >   }
  242. >   enable_named_pipe= (my_bool)
  243. >     ((opt_socket && opt_socket[0]) || opt_skip_networking);
  244. >
  245. >   if(enable_named_pipe)
  246. >   {
  247. >     fprintf(myini,"enable-named-pipe\n");
  248. >   }
  249. >
  250. >   if(opt_socket && opt_socket[0])
  251. >   {
  252. >     fprintf(myini, "socket=%s\n", opt_socket);
  253. >   }
  254.  
  255. Add a comment that the above is used for named pipes.
  256.  
  257. >   if (opt_port)
  258. >   {
  259. >     fprintf(myini,"port=%d\n", opt_port);
  260. >   }
  261.  
  262. Add if (!opt_skip_networking) to the above
  263.  
  264. >
  265. >   /* Write out client settings. */
  266. >   fprintf(myini, "[client]\n");
  267. >   if(opt_socket && opt_socket[0])
  268. >     fprintf(myini,"socket=%s\n",opt_socket);
  269. >   if(opt_skip_networking)
  270. >     fprintf(myini,"protocol=pipe\n");
  271. >   if(opt_port)
  272.  
  273. -> if(!opt_port && opt_skip_networking)
  274.  
  275. >     fprintf(myini,"port=%d\n",opt_port);
  276.  
  277. >   fclose(myini);
  278. >   return 0;
  279. > }
  280. >
  281. >
  282. > static const char update_root_passwd_part1[]=
  283. >   "UPDATE mysql.user SET Password = PASSWORD('";
  284. > static const char update_root_passwd_part2[]=
  285. >   "') where User='root';\n";
  286. > static const char remove_default_user_cmd[]=
  287. >   "DELETE FROM mysql.user where User='';\n";
  288. > static const char allow_remote_root_access_cmd[]=
  289. >   "CREATE TEMPORARY TABLE tmp_user LIKE user;\n"
  290.  
  291. You could add here:
  292. engine=memory
  293.  
  294. >   "INSERT INTO tmp_user SELECT * from user where user='root' "
  295. >     " AND host='localhost';\n"
  296. >   "UPDATE tmp_user SET host='%';\n"
  297. >   "INSERT INTO user SELECT * FROM tmp_user;\n"
  298. >   "DROP TABLE tmp_user;\n";
  299. > static const char end_of_script[]="-- end.";
  300.  
  301. <cut>
  302.  
  303. >   /* Get a handle to the SCM database. */
  304. >   sc_manager= OpenSCManager(
  305. >     NULL,
  306. >     NULL,
  307. >     SC_MANAGER_ALL_ACCESS);
  308.  
  309. Fix indentation
  310.  
  311. >
  312. >   if (!sc_manager)
  313. >   {
  314. >     die("OpenSCManager failed (%d)\n", GetLastError());
  315. >   }
  316.  
  317. Add cast to (int)
  318.  
  319. >
  320. >   /* Create the service. */
  321. >   sc_service = CreateServiceA(sc_manager, opt_service,  opt_service,
  322. >     SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
  323. >     SERVICE_ERROR_NORMAL, buf, NULL, NULL, NULL, opt_os_user, opt_os_password);
  324. >
  325. >   if (!sc_service)
  326. >   {
  327. >     CloseServiceHandle(sc_manager);
  328. >     die("CreateService failed (%d)", GetLastError());
  329. >   }
  330.  
  331. Add cast to (int)
  332.  
  333. >   SERVICE_DESCRIPTION sd = { "MariaDB database server" };
  334. >   ChangeServiceConfig2(sc_service, SERVICE_CONFIG_DESCRIPTION, &sd);
  335. >   CloseServiceHandle(sc_service);
  336. >   CloseServiceHandle(sc_manager);
  337. >   return 0;
  338. > }
  339. >
  340. >
  341. > static void clean_directory(const char *dir)
  342. > {
  343. >   char dir2[MAX_PATH+2];
  344. >   size_t len = strlen(dir);
  345. >
  346. >   strcpy_s(dir2, MAX_PATH+2, dir);
  347. >   dir2[len+1] = 0;
  348.  
  349. A better way would be:
  350.  
  351.     *(strmake(dir2, dir, MAX_PATH+1)+1)= 0;
  352.  
  353. <cut>
  354.  
  355. > /* Create database instance (including registering as service etc) .*/
  356. > static int create_db_instance()
  357. > {
  358. >   int ret=0;
  359. >   char cwd[MAX_PATH];
  360. >   DWORD cwd_len= MAX_PATH;
  361. >   char cmdline[3*MAX_PATH];
  362. >   FILE *in;
  363. >
  364. >   verbose("Running bootstrap");
  365. >
  366. >   GetCurrentDirectory(cwd_len, cwd);
  367. >   CreateDirectory(opt_datadir, NULL); /*ignore error, it might already exist */
  368. >
  369. >   if(!SetCurrentDirectory(opt_datadir))
  370. >   {
  371. >     die("Cannot set current directory to %s\n",opt_datadir);
  372.  
  373. Add quotes around '%s' so that we can detect empty names and space.
  374.  
  375. >     return -1;
  376. >   }
  377. >
  378. >   CreateDirectory("mysql",NULL);
  379. >   CreateDirectory("test", NULL);
  380. >
  381. >   set_directory_permissions(opt_datadir, NULL);
  382. >   set_directory_permissions(opt_datadir, default_os_user);
  383.  
  384. Please add a comment that the above gives permissions for both the current
  385. user and default_os_user.
  386.  
  387. >
  388. >   /* Create mysqld --bootstrap process */
  389. >   init_bootstrap_command_line(cmdline, sizeof(cmdline));
  390. >   /* verbose("Executing %s", cmdline); */
  391. >
  392. >   in= popen(cmdline, "wt");
  393. >   if(!in)
  394. >     goto end;
  395. >
  396. >   if (fwrite("use mysql;\n",11,1, in) != 1)
  397. >   {
  398. >     verbose("ERROR: Cannot write to mysqld's stdin");
  399. >     ret = 1;
  400. >     goto end;
  401. >   }
  402. >
  403. >   /* Write the bootstrap script to stdin. */
  404. >   if (fwrite(mysql_bootstrap_sql, strlen(mysql_bootstrap_sql), 1, in) != 1)
  405. >   {
  406. >     verbose("ERROR: Cannot write to mysqld's stdin");
  407. >     ret= 1;
  408. >     goto end;
  409. >   }
  410. >
  411. >  
  412. >   /* Remove default user, if requested. */
  413. >   if(!opt_default_user)
  414. >   {
  415. >     verbose("Removing default user",remove_default_user_cmd);
  416. >     fputs(remove_default_user_cmd, in);
  417. >     fflush(in);
  418. >   }
  419. >
  420. >   if(opt_allow_remote_root_access)
  421. >   {
  422. >      verbose("Allowing remote access for user root",remove_default_user_cmd);
  423. >      fputs(allow_remote_root_access_cmd,in);
  424. >      fflush(in);
  425. >   }
  426. >
  427. >   /* Change root password if requested. */
  428. >   if (opt_password)
  429. >   {
  430. >     verbose("Changing root password",remove_default_user_cmd);
  431. >     fputs(update_root_passwd_part1, in);
  432. >     fputs(opt_password, in);
  433. >     fputs(update_root_passwd_part2, in);
  434. >     fflush(in);
  435. >   }
  436.  
  437. Here you should end with 'flush privileges' to enable the above changes.
  438.  
  439. >   /*
  440. >     On some reason, bootstrap chokes if last command sent via stdin ends with
  441. >     newline, so we supply a dummy comment, that does not end with newline.
  442. >   */
  443. >   fputs(end_of_script, in);
  444. >   fflush(in);
  445. >
  446. >   /* Check if bootstrap has completed successfully. */
  447. >   ret= pclose(in);
  448. >   if (ret)
  449. >   {
  450. >     verbose("mysqld returned an error in pclose");
  451.  
  452. Please print error code
  453.  
  454. >     goto end;
  455. >   }
  456. >
  457. >   /* Create my.ini file in data directory.*/
  458. >   ret= create_myini();
  459. >   if(ret)
  460. >     goto end;
  461. >
  462. >   /* Register service if requested. */
  463. >   if(opt_service && opt_service[0])
  464. >   {
  465.  
  466. Add here
  467.  
  468.       verbose("Registering service '%s', opt_service);
  469.  
  470. <cut>