static int mysql_db_start (SeafileSession *session) { char *host, *port, *user, *passwd, *db, *unix_socket; gboolean use_ssl = FALSE; GError *error = NULL; host = g_key_file_get_string (session->config, "database", "host", &error); if (!host) { g_warning ("DB host not set in config.\n"); return -1; } port = g_key_file_get_string (session->config, "database", "port", &error); if (!port) { port = g_strdup(MYSQL_DEFAULT_PORT); } user = g_key_file_get_string (session->config, "database", "user", &error); if (!user) { g_warning ("DB user not set in config.\n"); return -1; } passwd = g_key_file_get_string (session->config, "database", "password", &error); if (!passwd) { g_warning ("DB passwd not set in config.\n"); return -1; } db = g_key_file_get_string (session->config, "database", "db_name", &error); if (!db) { g_warning ("DB name not set in config.\n"); return -1; } unix_socket = g_key_file_get_string (session->config, "database", "unix_socket", NULL); use_ssl = g_key_file_get_boolean (session->config, "database", "use_ssl", NULL); session->db = seaf_db_new_mysql (host, port, user, passwd, db, unix_socket, use_ssl); if (!session->db) { g_warning ("Failed to start mysql db.\n"); return -1; } g_free (host); g_free (port); g_free (user); g_free (passwd); g_free (db); g_free (unix_socket); return 0; }
int main (int argc, char **argv) { char sql[256]; gboolean verbose = FALSE; gboolean use_mysql = FALSE; int ret; if (argc == 1) usage(1); int c; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != EOF) { switch (c) { case 'h': usage(0); break; case 'v': verbose = TRUE; break; case 'm': use_mysql = TRUE; break; case 's': config.mysql_host = strdup(optarg); break; case 't': config.mysql_socket = strdup(optarg); break; case 'r': config.mysql_root_passwd = strdup(optarg); break; case 'd': config.seafile_dir = strdup(optarg); break; case 'p': config.port = strdup(optarg); break; case 'P': config.httpserver_port = strdup(optarg); break; default: usage(1); } } if (use_mysql && !config.mysql_root_passwd) { fprintf (stderr, "You choose to use mysql database. " "Mysql Root Password must be specified.\n"); exit(1); } if (!config.seafile_dir) { fprintf (stderr, "You must specify seafile data dir\n"); usage(1); } /* Create database for mysql */ if (use_mysql) { SeafDB *db_root = seaf_db_new_mysql (config.mysql_host, "root", config.mysql_root_passwd, NULL, config.mysql_socket); if (!db_root) { fprintf (stderr, "Out of memory!\n"); return 1; } /* Create database for Seafile server. */ snprintf (sql, sizeof(sql), "CREATE DATABASE IF NOT EXISTS `%s`", config.mysql_db_name); ret = seaf_db_query (db_root, sql); if (ret < 0) { fprintf (stderr, "Failed to create database %s.\n", config.mysql_db_name); return 1; } if (verbose) printf ("Successfully created database: %s\n", config.mysql_db_name); /* Create database for Seahub. */ snprintf (sql, sizeof(sql), "CREATE DATABASE IF NOT EXISTS `%s` character set utf8", config.mysql_seahub_db_name); ret = seaf_db_query (db_root, sql); if (ret < 0) { fprintf (stderr, "Failed to create database %s.\n", config.mysql_seahub_db_name); return 1; } if (verbose) printf ("Successfully created database: %s\n", config.mysql_seahub_db_name); } /* Generate config file. */ GKeyFile *key_file = g_key_file_new (); g_key_file_set_string (key_file, "database", "type", use_mysql ? "mysql" : "sqlite"); if (use_mysql) { g_key_file_set_string (key_file, "database", "host", config.mysql_host); g_key_file_set_string (key_file, "database", "user", "root"); g_key_file_set_string (key_file, "database", "password", config.mysql_root_passwd); g_key_file_set_string (key_file, "database", "db_name", config.mysql_db_name); if (config.mysql_socket) g_key_file_set_string (key_file, "database", "unix_socket", config.mysql_socket); } g_key_file_set_string (key_file, "network", "port", config.port); if (config.httpserver_port) { g_key_file_set_string (key_file, "httpserver", "port", config.httpserver_port); } else { /* httpserver port defaults to 8082 */ g_key_file_set_string (key_file, "httpserver", "port", "8082"); } struct stat st; if (lstat (config.seafile_dir, &st) < 0) { if (mkdir (config.seafile_dir, 0777) < 0) { fprintf (stderr, "Directory %s cannot be created.\n", config.seafile_dir); return 1; } } char *seafile_conf = g_build_filename (config.seafile_dir, "seafile.conf", NULL); if (verbose) printf ("Generating config files: %s\n", seafile_conf); if (save_config_file (key_file, seafile_conf) < 0) return 1; printf ("Done.\n"); return 0; }