📄 mysql.c
字号:
return count;}/** * Update the priority for a particular key * in the datastore. */static intupdate (unsigned long long vkey, int delta, GNUNET_CronTime expire){ GNUNET_CronTime start; MYSQL_BIND qbind[4]; GNUNET_mutex_lock (lock); mysql_thread_init (); if (GNUNET_OK != CHECK_DBH) { mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } memset (qbind, 0, sizeof (qbind)); qbind[0].buffer_type = MYSQL_TYPE_LONG; qbind[0].buffer = δ qbind[1].buffer_type = MYSQL_TYPE_LONGLONG; qbind[1].buffer = &expire; qbind[1].is_unsigned = GNUNET_YES; qbind[2].buffer_type = MYSQL_TYPE_LONGLONG; qbind[2].is_unsigned = GNUNET_YES; qbind[2].buffer = &expire; qbind[3].buffer_type = MYSQL_TYPE_LONGLONG; qbind[3].is_unsigned = GNUNET_YES; qbind[3].buffer = &vkey; GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (dbh->update_entry) == 4); if (mysql_stmt_bind_param (dbh->update_entry, qbind)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_bind_param", __FILE__, __LINE__, mysql_stmt_error (dbh->update_entry)); iclose (); mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } /* NOTE: as the table entry for 'priority' is defined as unsigned, * mysql will zero the value if its about to go negative. (This * will generate a warning though, but its probably not seen * at all in this context.) */ start = GNUNET_get_time (); if (mysql_stmt_execute (dbh->update_entry)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error `%s' after %llums\n"), "mysql_stmt_execute", __FILE__, __LINE__, mysql_stmt_error (dbh->update_entry), GNUNET_get_time () - start); iclose (); mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } mysql_stmt_reset (dbh->update_entry); mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_OK;}/** * Get the current on-disk size of the SQ store. * Estimates are fine, if that's the only thing * available. * @return number of bytes used on disk */static unsigned long longgetSize (){ unsigned long long ret; GNUNET_mutex_lock (lock); ret = content_size; if (stats) stats->set (stat_size, ret); GNUNET_mutex_unlock (lock); return ret * 1.2;}/** * Delete the database. The next operation is * guaranteed to be unloading of the module. */static voiddrop (){ int ok; ok = GNUNET_YES; GNUNET_mutex_lock (lock); mysql_thread_init (); if (GNUNET_OK != CHECK_DBH) { mysql_thread_end (); GNUNET_mutex_unlock (lock); return; } mysql_query (dbh->dbf, "DROP TABLE gn080"); if (mysql_error (dbh->dbf)[0]) { LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK, "mysql_query", dbh); ok = GNUNET_NO; } mysql_query (dbh->dbf, "DROP TABLE gn072"); if (mysql_error (dbh->dbf)[0]) { LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK, "mysql_query", dbh); ok = GNUNET_NO; } if (ok == GNUNET_YES) content_size = 0; iclose (); mysql_thread_end (); GNUNET_mutex_unlock (lock);}GNUNET_SQstore_ServiceAPI *provide_module_sqstore_mysql (GNUNET_CoreAPIForPlugins * capi){ static GNUNET_SQstore_ServiceAPI api; GNUNET_State_ServiceAPI *state; char *cnffile; FILE *fp; struct passwd *pw; size_t nX;#ifndef WINDOWS char *home_dir;#endif unsigned long long *sb; MYSQL_RES *sql_res; MYSQL_ROW sql_row; ectx = capi->ectx; coreAPI = capi; stats = coreAPI->service_request ("stats"); if (stats) stat_size = stats->create (gettext_noop ("# bytes in datastore")); /* verify that .my.cnf can be found */#ifndef WINDOWS pw = getpwuid (getuid ()); if (!pw) GNUNET_GE_DIE_STRERROR (ectx, GNUNET_GE_FATAL | GNUNET_GE_ADMIN | GNUNET_GE_IMMEDIATE, "getpwuid"); home_dir = GNUNET_strdup (pw->pw_dir);#else home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1); plibc_conv_to_win_path ("~/", home_dir);#endif nX = strlen (home_dir) + 10; cnffile = GNUNET_malloc (nX); GNUNET_snprintf (cnffile, nX, "%s/.my.cnf", home_dir); GNUNET_free (home_dir); GNUNET_GC_get_configuration_value_filename (capi->cfg, "MYSQL", "CONFIG", cnffile, &home_dir); GNUNET_free (cnffile); cnffile = home_dir; GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER, _("Trying to use file `%s' for MySQL configuration.\n"), cnffile); fp = FOPEN (cnffile, "r"); if (!fp) { GNUNET_GE_LOG_STRERROR_FILE (ectx, GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK, "fopen", cnffile); if (stats != NULL) coreAPI->service_release (stats); GNUNET_free (cnffile); return NULL; } else { fclose (fp); } dbh = GNUNET_malloc (sizeof (mysqlHandle)); memset (dbh, 0, sizeof (mysqlHandle)); dbh->cnffile = cnffile; if (GNUNET_OK != iopen ()) { GNUNET_free (cnffile); GNUNET_free (dbh); GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _ ("Failed to load MySQL database module. Check that MySQL is running and configured properly!\n")); dbh = NULL; if (stats != NULL) coreAPI->service_release (stats); return NULL; } lock = GNUNET_mutex_create (GNUNET_NO); state = coreAPI->service_request ("state"); sb = NULL; if (sizeof (unsigned long long) != state->read (ectx, "mysql-size", (void *) &sb)) { /* need to recompute! */ sql_res = NULL; mysql_query (dbh->dbf, SELECT_SIZE); if ((mysql_error (dbh->dbf)[0]) || (!(sql_res = mysql_use_result (dbh->dbf))) || (!(sql_row = mysql_fetch_row (sql_res)))) { LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK, "mysql_query", dbh); content_size = 0; iclose (dbh); } else { if ((mysql_num_fields (sql_res) != 1) || (sql_row[0] == NULL)) { GNUNET_GE_BREAK (ectx, mysql_num_fields (sql_res) == 1); content_size = 0; } else { if (1 != SSCANF (sql_row[0], "%llu", &content_size)) { GNUNET_GE_BREAK (ectx, 0); content_size = 0; } } } if (sql_res != NULL) mysql_free_result (sql_res); } else { content_size = *sb; GNUNET_free (sb); /* no longer valid! remember it by deleting the outdated state file! */ state->unlink (ectx, "mysql-size"); } coreAPI->service_release (state); api.getSize = &getSize; api.put = &put; api.get = &get; api.iterateLowPriority = &iterateLowPriority; api.iterateNonAnonymous = &iterateNonAnonymous; api.iterateExpirationTime = &iterateExpirationTime; api.iterateMigrationOrder = &iterateMigrationOrder; api.iterateAllNow = &iterateAllNow; api.drop = &drop; api.update = &update; return &api;}/** * Shutdown the module. */voidrelease_module_sqstore_mysql (){ GNUNET_State_ServiceAPI *state; iclose (dbh); GNUNET_free (dbh->cnffile); GNUNET_free (dbh); dbh = NULL; if (stats != NULL) coreAPI->service_release (stats); GNUNET_mutex_destroy (lock); state = coreAPI->service_request ("state"); state->write (ectx, "mysql-size", sizeof (unsigned long long), &content_size); coreAPI->service_release (state); mysql_library_end (); ectx = NULL; coreAPI = NULL;}/** * Update mysql database module. */voidupdate_module_sqstore_mysql (GNUNET_UpdateAPI * uapi){ char *cnffile; FILE *fp; struct passwd *pw; size_t nX; char *home_dir; ectx = uapi->ectx;#ifndef WINDOWS pw = getpwuid (getuid ()); if (!pw) GNUNET_GE_DIE_STRERROR (ectx, GNUNET_GE_FATAL | GNUNET_GE_ADMIN | GNUNET_GE_IMMEDIATE, "getpwuid"); home_dir = GNUNET_strdup (pw->pw_dir);#else home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1); plibc_conv_to_win_path ("~/", home_dir);#endif nX = strlen (home_dir) + 10; cnffile = GNUNET_malloc (nX); GNUNET_snprintf (cnffile, nX, "%s/.my.cnf", home_dir); GNUNET_free (home_dir); GNUNET_GC_get_configuration_value_filename (uapi->cfg, "MYSQL", "CONFIG", cnffile, &home_dir); GNUNET_free (cnffile); cnffile = home_dir; GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER, _("Trying to use file `%s' for MySQL configuration.\n"), cnffile); fp = FOPEN (cnffile, "r"); if (!fp) { GNUNET_GE_LOG_STRERROR_FILE (ectx, GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK, "fopen", cnffile); GNUNET_free (cnffile); return; } else { fclose (fp); } dbh = GNUNET_malloc (sizeof (mysqlHandle)); memset (dbh, 0, sizeof (mysqlHandle)); dbh->cnffile = cnffile; if (GNUNET_OK != iopen ()) { GNUNET_free (cnffile); GNUNET_free (dbh); GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _ ("Failed to load MySQL database module. Check that MySQL is running and configured properly!\n")); dbh = NULL; return; } /* run update queries here */ iclose (dbh); GNUNET_free (dbh->cnffile); GNUNET_free (dbh); dbh = NULL; mysql_library_end (); ectx = NULL;}/* end of mysql.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -