static void write_dir_file (void) { GString *content, *filename; GError *error = NULL; filename = replace_variables ("$HOME/" EVOLUTION_DIR_FILE, TRUE); g_return_if_fail (filename != NULL); content = replace_variables ( "[" KEY_FILE_GROUP "]\n" "Version=" VERSION "\n" "UserDataDir=$STRIPDATADIR\n" "UserConfigDir=$STRIPCONFIGDIR\n" , TRUE); g_return_if_fail (content != NULL); g_file_set_contents (filename->str, content->str, content->len, &error); if (error != NULL) { g_warning ("Failed to write file '%s': %s\n", filename->str, error->message); g_error_free (error); } g_string_free (filename, TRUE); g_string_free (content, TRUE); }
static void write_dir_file (void) { GString *content, *filename; GError *error = NULL; filename = replace_variables ("$HOME/" EVOLUTION_DIR_FILE); g_return_if_fail (filename != NULL); content = replace_variables ( "[dirs]\n" "data=$STRIPDATADIR\n" "config=$STRIPCONFIGDIR\n"); g_return_if_fail (content != NULL); g_file_set_contents (filename->str, content->str, content->len, &error); if (error) { g_warning ("Failed to write file '%s': %s\n", filename->str, error->message); g_error_free (error); } g_string_free (filename, TRUE); g_string_free (content, TRUE); }
static void replace_in_file (const gchar *filename, const gchar *find, const gchar *replace) { gchar *content = NULL; GError *error = NULL; GString *filenamestr = NULL; g_return_if_fail (filename != NULL); g_return_if_fail (find != NULL); g_return_if_fail (*find); g_return_if_fail (replace != NULL); if (strstr (filename, "$")) { filenamestr = replace_variables (filename, TRUE); if (!filenamestr) { g_warning ( "%s: Replace variables in '%s' failed!", G_STRFUNC, filename); return; } filename = filenamestr->str; } if (g_file_get_contents (filename, &content, NULL, &error)) { GString *str = e_str_replace_string (content, find, replace); if (str) { if (!g_file_set_contents (filename, str->str, -1, &error) && error) { g_warning ( "%s: cannot write file content, " "error: %s", G_STRFUNC, error->message); g_error_free (error); } g_string_free (str, TRUE); } else { g_warning ( "%s: Replace of '%s' to '%s' failed!", G_STRFUNC, find, replace); } g_free (content); } else if (error != NULL) { g_warning ( "%s: Cannot read file content, error: %s", G_STRFUNC, error->message); g_error_free (error); } if (filenamestr) g_string_free (filenamestr, TRUE); }
static bool prepare_common(int lineno, struct connection *con, const char *name, const char *variable) { struct statement *stmt; struct prepared_statement *this; PGresult *query; /* allocate new statement */ this = (struct prepared_statement *) ecpg_alloc(sizeof(struct prepared_statement), lineno); if (!this) return false; stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno); if (!stmt) { ecpg_free(this); return false; } memset(stmt, 0, sizeof(struct statement)); /* create statement */ stmt->lineno = lineno; stmt->connection = con; stmt->command = ecpg_strdup(variable, lineno); stmt->inlist = stmt->outlist = NULL; /* if we have C variables in our statement replace them with '?' */ replace_variables(&(stmt->command), lineno); /* add prepared statement to our list */ this->name = ecpg_strdup(name, lineno); this->stmt = stmt; /* and finally really prepare the statement */ query = PQprepare(stmt->connection->connection, name, stmt->command, 0, NULL); if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat)) { ecpg_free(stmt->command); ecpg_free(this->name); ecpg_free(this); ecpg_free(stmt); return false; } ecpg_log("prepare_common on line %d: name %s; query: \"%s\"\n", stmt->lineno, name, stmt->command); PQclear(query); this->prepared = true; if (con->prep_stmts == NULL) this->next = NULL; else this->next = con->prep_stmts; con->prep_stmts = this; return true; }
/** * Puts random (compiled) phrase to destination string * * @param *destination Places random phrase here */ void get_random_phrase(char *destination) { /* Allocate dynamic memory, care stack */ char *pattern = (char *) kmalloc(sizeof(char) * \ MAX_PHRASE_LENGTH, GFP_USER); char *buffer = (char *) kmalloc(sizeof(char) * \ MAX_PHRASE_LENGTH, GFP_USER); get_random_from_dict(pattern, patterns); parse_syntax(pattern, buffer); replace_variables(buffer, destination); kfree(pattern); kfree(buffer); }
static void run_cmd (const gchar *cmd) { if (!cmd) return; if (strstr (cmd, "$") != NULL) { /* read the doc for g_get_home_dir to know why replacing it here */ GString *str = replace_variables (cmd, FALSE); if (str) { print_and_run (str->str); g_string_free (str, TRUE); } } else print_and_run (cmd); }
static gchar * get_source_manager_reload_command (void) { GString *tmp; gchar *command; tmp = replace_variables (DBUS_SOURCE_REGISTRY_SERVICE_FILE, TRUE); if (tmp) { GKeyFile *key_file; gchar *str = NULL; key_file = g_key_file_new (); if (g_key_file_load_from_file (key_file, tmp->str, G_KEY_FILE_NONE, NULL)) { str = g_key_file_get_string (key_file, "D-BUS Service", "Name", NULL); } g_key_file_free (key_file); if (str && *str) { g_string_assign (tmp, str); } else { g_string_free (tmp, TRUE); tmp = NULL; } g_free (str); } if (!tmp) tmp = g_string_new ("org.gnome.evolution.dataserver.Sources0"); command = g_strdup_printf ("gdbus call --session --dest %s " "--object-path /org/gnome/evolution/dataserver/SourceManager " "--method org.gnome.evolution.dataserver.SourceManager.Reload", tmp->str); g_string_free (tmp, TRUE); return command; }
int read_list_file(const string& filename, const map<string, string>& variables, vector<FileRecord>* files, vector<string>* excludes) { int err = 0; FILE* f = NULL; long size; char* buf = NULL; char *p, *q; int i, lineCount; f = fopen(filename.c_str(), "r"); if (f == NULL) { fprintf(stderr, "Could not open list file (%s): %s\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } err = fseek(f, 0, SEEK_END); if (err != 0) { fprintf(stderr, "Could not seek to the end of file %s. (%s)\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } size = ftell(f); err = fseek(f, 0, SEEK_SET); if (err != 0) { fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } buf = (char*)malloc(size+1); if (buf == NULL) { // (potentially large) fprintf(stderr, "out of memory (%ld)\n", size); err = ENOMEM; goto cleanup; } if (1 != fread(buf, size, 1, f)) { fprintf(stderr, "error reading file %s. (%s)\n", filename.c_str(), strerror(errno)); err = errno; goto cleanup; } // split on lines p = buf; q = buf+size; lineCount = 0; while (p<q) { if (*p == '\r' || *p == '\n') { *p = '\0'; lineCount++; } p++; } // read lines p = buf; for (i=0; i<lineCount; i++) { int len = strlen(p); q = p + len + 1; if (is_whitespace_line(p) || is_comment_line(p)) { ; } else if (is_exclude_line(p)) { while (*p != '-') p++; p++; excludes->push_back(string(p)); } else { vector<string> words; split_line(p, &words); #if 0 printf("[ "); for (size_t k=0; k<words.size(); k++) { printf("'%s' ", words[k].c_str()); } printf("]\n"); #endif if (words.size() == 1) { // pattern: DEST bool error = false; string w0 = replace_variables(words[0], variables, &error); if (error) { err = 1; goto cleanup; } add_file(files, filename, i+1, w0, w0); } else if (words.size() == 2) { // pattern: SRC DEST bool error = false; string w0, w1; w0 = replace_variables(words[0], variables, &error); if (!error) { w1 = replace_variables(words[1], variables, &error); } if (error) { err = 1; goto cleanup; } add_file(files, filename, i+1, w0, w1); } else { fprintf(stderr, "%s:%d: bad format: %s\n", filename.c_str(), i+1, p); err = 1; } } p = q; } cleanup: if (buf != NULL) { free(buf); } if (f != NULL) { fclose(f); } return err; }
/** * getconfs: get property string * * @param[in] name property name * @param[out] result string buffer (if not NULL) * @return 1: found, 0: not found */ int getconfs(const char *name, STRBUF *result) { STRBUF *sb = NULL; const char *p; char buf[MAXPROPLEN]; int all = 0; int exist = 0; int bufsize; if (!opened) die("configuration file not opened."); /* 'path' is reserved name for the current path of configuration file */ if (!strcmp(name, "path")) { if (config_path && result) strbuf_puts(result, config_path); return 1; } sb = strbuf_open(0); if (!strcmp(name, "skip") || !strcmp(name, "gtags_parser") || !strcmp(name, "langmap")) all = 1; snprintf(buf, sizeof(buf), ":%s=", name); bufsize = strlen(buf); p = confline; while ((p = locatestring(p, buf, MATCH_FIRST)) != NULL) { if (exist && sb) strbuf_putc(sb, ','); exist = 1; for (p += bufsize; *p; p++) { if (*p == ':') break; if (*p == '\\' && *(p + 1) == ':') /* quoted character */ p++; if (sb) strbuf_putc(sb, *p); } if (!all) break; } /* * If 'bindir' and 'datadir' are not defined then * return system configuration value. */ if (!exist) { if (!strcmp(name, "bindir")) { if (sb) strbuf_puts(sb, BINDIR); exist = 1; } else if (!strcmp(name, "datadir")) { #if defined(_WIN32) && !defined(__CYGWIN__) /* * Test if this directory exists, and if not, take the * directory relative to the binary. */ if (test("d", DATADIR)) { if (sb) strbuf_puts(sb, DATADIR); } else { char path[MAX_PATH], *name, *p; GetModuleFileName(NULL, path, MAX_PATH); for (p = name = path; *p; ++p) { if (*p == '\\') { *p = '/'; name = p+1; } } strcpy(name, "../share"); if (sb) strbuf_puts(sb, path); } #else if (sb) strbuf_puts(sb, DATADIR); #endif exist = 1; } else if (!strcmp(name, "libdir")) { if (sb) strbuf_puts(sb, LIBDIR); exist = 1; } else if (!strcmp(name, "localstatedir")) { if (sb) strbuf_puts(sb, LOCALSTATEDIR); exist = 1; } else if (!strcmp(name, "sysconfdir")) { if (sb) strbuf_puts(sb, SYSCONFDIR); exist = 1; } } replace_variables(sb); if (result) strbuf_puts(result, !strcmp(name, "langmap") ? trim_langmap(strbuf_value(sb)) : strbuf_value(sb)); strbuf_close(sb); return exist; }
static void restore (const gchar *filename, GCancellable *cancellable) { gchar *command; gchar *quotedfname; gboolean is_new_format = FALSE; g_return_if_fail (filename && *filename); if (!check (filename, &is_new_format)) { g_message ("Cannot restore from an incorrect archive '%s'.", filename); goto end; } quotedfname = g_shell_quote (filename); if (g_cancellable_is_cancelled (cancellable)) return; /* FIXME Will the versioned setting always work? */ txt = _("Shutting down Evolution"); run_cmd (EVOLUTION " --quit"); if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Back up current Evolution data"); run_cmd ("mv $DATADIR $DATADIR_old"); run_cmd ("mv $CONFIGDIR $CONFIGDIR_old"); if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Extracting files from back up"); if (is_new_format) { GString *dir_fn; gchar *data_dir = NULL; gchar *config_dir = NULL; gchar *restored_version = NULL; const gchar *tar_opts; if (get_filename_is_xz (filename)) tar_opts = "-xJf"; else tar_opts = "-xzf"; command = g_strdup_printf ( "cd $TMP && tar %s %s " EVOLUTION_DIR_FILE, tar_opts, quotedfname); run_cmd (command); g_free (command); dir_fn = replace_variables ("$TMP" G_DIR_SEPARATOR_S EVOLUTION_DIR_FILE, TRUE); if (!dir_fn) { g_warning ("Failed to create evolution's dir filename"); goto end; } /* data_dir and config_dir are quoted inside extract_backup_data */ extract_backup_data ( dir_fn->str, &restored_version, &data_dir, &config_dir); g_unlink (dir_fn->str); g_string_free (dir_fn, TRUE); if (!data_dir || !config_dir) { g_warning ( "Failed to get old data_dir (%p)/" "config_dir (%p)", data_dir, config_dir); g_free (data_dir); g_free (config_dir); goto end; } g_mkdir_with_parents (e_get_user_data_dir (), 0700); g_mkdir_with_parents (e_get_user_config_dir (), 0700); command = g_strdup_printf ( "cd $DATADIR && tar --strip-components %d %s %s %s", get_dir_level (data_dir), tar_opts, quotedfname, data_dir); run_cmd (command); g_free (command); command = g_strdup_printf ( "cd $CONFIGDIR && tar --strip-components %d %s %s %s", get_dir_level (config_dir), tar_opts, quotedfname, config_dir); run_cmd (command); g_free (command); /* If the back file had version information, set the last * used version in GSettings before restarting Evolution. */ if (restored_version != NULL && *restored_version != '\0') { GSettings *settings; settings = e_util_ref_settings ("org.gnome.evolution"); g_settings_set_string ( settings, "version", restored_version); g_object_unref (settings); } g_free (data_dir); g_free (config_dir); g_free (restored_version); } else { const gchar *decr_opts; if (get_filename_is_xz (filename)) decr_opts = "xz -cd"; else decr_opts = "gzip -cd"; run_cmd ("mv $HOME/.evolution $HOME/.evolution_old"); command = g_strdup_printf ( "cd $HOME && %s %s | tar xf -", decr_opts, quotedfname); run_cmd (command); g_free (command); } g_free (quotedfname); if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Loading Evolution settings"); if (is_new_format) { /* new format has it in DATADIR... */ GString *file = replace_variables (EVOLUTION_DIR ANCIENT_GCONF_DUMP_FILE, TRUE); if (file && g_file_test (file->str, G_FILE_TEST_EXISTS)) { unset_eds_migrated_flag (); /* ancient backup */ replace_in_file ( EVOLUTION_DIR ANCIENT_GCONF_DUMP_FILE, EVOUSERDATADIR_MAGIC, e_get_user_data_dir ()); run_cmd ("gconftool-2 --load " EVOLUTION_DIR ANCIENT_GCONF_DUMP_FILE); /* give a chance to GConf to save what was loaded into a disk */ g_usleep (G_USEC_PER_SEC * 5); /* do not forget to convert GConf keys into GSettings */ run_cmd ("gsettings-data-convert"); run_cmd ("rm " EVOLUTION_DIR ANCIENT_GCONF_DUMP_FILE); } else { replace_in_file ( EVOLUTION_DIR DCONF_DUMP_FILE_EDS, EVOUSERDATADIR_MAGIC, e_get_user_data_dir ()); run_cmd ("cat " EVOLUTION_DIR DCONF_DUMP_FILE_EDS " | dconf load " DCONF_PATH_EDS); run_cmd ("rm " EVOLUTION_DIR DCONF_DUMP_FILE_EDS); replace_in_file ( EVOLUTION_DIR DCONF_DUMP_FILE_EVO, EVOUSERDATADIR_MAGIC, e_get_user_data_dir ()); run_cmd ("cat " EVOLUTION_DIR DCONF_DUMP_FILE_EVO " | dconf load " DCONF_PATH_EVO); run_cmd ("rm " EVOLUTION_DIR DCONF_DUMP_FILE_EVO); } g_string_free (file, TRUE); } else { gchar *gconf_dump_file; unset_eds_migrated_flag (); /* ... old format in ~/.evolution */ gconf_dump_file = g_build_filename ( "$HOME", ".evolution", ANCIENT_GCONF_DUMP_FILE, NULL); replace_in_file ( gconf_dump_file, EVOUSERDATADIR_MAGIC, e_get_user_data_dir ()); command = g_strconcat ( "gconftool-2 --load ", gconf_dump_file, NULL); run_cmd (command); g_free (command); /* give a chance to GConf to save what was loaded into a disk */ g_usleep (G_USEC_PER_SEC * 5); /* do not forget to convert GConf keys into GSettings */ run_cmd ("gsettings-data-convert"); command = g_strconcat ("rm ", gconf_dump_file, NULL); run_cmd (command); g_free (command); g_free (gconf_dump_file); } if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Removing temporary back up files"); run_cmd ("rm -rf $DATADIR_old"); run_cmd ("rm -rf $CONFIGDIR_old"); run_cmd ("rm $DATADIR/.running"); if (!is_new_format) run_cmd ("rm -rf $HOME/.evolution_old"); if (g_cancellable_is_cancelled (cancellable)) return; /* Make full-restart background processes after restore */ run_cmd (EVOLUTION " --force-shutdown"); txt = _("Reloading registry service"); /* wait few seconds, till changes settle */ g_usleep (G_USEC_PER_SEC * 5); command = get_source_manager_reload_command (); /* This runs migration routines on the newly-restored data. */ run_cmd (command); g_free (command); end: if (restart_arg) { if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Restarting Evolution"); /* wait 5 seconds before restarting evolution, thus any * changes being done are updated in source registry too */ g_usleep (G_USEC_PER_SEC * 5); run_evolution_no_wait (); } }
static void restore (const gchar *filename, GCancellable *cancellable) { gchar *command; gchar *quotedfname; gboolean is_new_format = FALSE; g_return_if_fail (filename && *filename); if (!check (filename, &is_new_format)) { g_message ("Cannot restore from an incorrect archive '%s'.", filename); goto end; } quotedfname = g_shell_quote (filename); if (g_cancellable_is_cancelled (cancellable)) return; /* FIXME Will the versioned setting always work? */ txt = _("Shutting down Evolution"); run_cmd (EVOLUTION " --quit"); if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Back up current Evolution data"); run_cmd ("mv $DATADIR $DATADIR_old"); run_cmd ("mv $CONFIGDIR $CONFIGDIR_old"); run_cmd ("mv $HOME/.camel_certs $HOME/.camel_certs_old"); if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Extracting files from back up"); if (is_new_format) { GString *dir_fn; gchar *data_dir = NULL, *config_dir = NULL; command = g_strdup_printf ( "cd $TMP && tar xzf %s " EVOLUTION_DIR_FILE, quotedfname); run_cmd (command); g_free (command); dir_fn = replace_variables ("$TMP" G_DIR_SEPARATOR_S EVOLUTION_DIR_FILE); if (!dir_fn) { g_warning ("Failed to create evolution's dir filename"); goto end; } /* data_dir and config_dir are quoted inside extract_backup_dirs */ extract_backup_dirs (dir_fn->str, &data_dir, &config_dir); g_unlink (dir_fn->str); g_string_free (dir_fn, TRUE); if (!data_dir || !config_dir) { g_warning ("Failed to get old data_dir (%p)/config_dir (%p)", data_dir, config_dir); g_free (data_dir); g_free (config_dir); goto end; } g_mkdir_with_parents (e_get_user_data_dir (), 0700); g_mkdir_with_parents (e_get_user_config_dir (), 0700); command = g_strdup_printf ( "cd $DATADIR && tar xzf %s %s --strip-components=%d", quotedfname, data_dir, get_dir_level (data_dir)); run_cmd (command); g_free (command); command = g_strdup_printf ( "cd $CONFIGDIR && tar xzf %s %s --strip-components=%d", quotedfname, config_dir, get_dir_level (config_dir)); run_cmd (command); g_free (command); command = g_strdup_printf ( "cd $HOME && tar xzf %s .camel_certs", quotedfname); run_cmd (command); g_free (command); g_free (data_dir); g_free (config_dir); } else { run_cmd ("mv $HOME/.evolution $HOME/.evolution_old"); command = g_strdup_printf ( "cd $HOME && gzip -cd %s | tar xf -", quotedfname); run_cmd (command); g_free (command); } g_free (quotedfname); if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Loading Evolution settings"); if (is_new_format) { /* new format has it in DATADIR... */ replace_in_file ( EVOLUTION_DIR GCONF_DUMP_FILE, EVOUSERDATADIR_MAGIC, e_get_user_data_dir ()); run_cmd ("gconftool-2 --load " EVOLUTION_DIR GCONF_DUMP_FILE); run_cmd ("rm " EVOLUTION_DIR GCONF_DUMP_FILE); } else { gchar *gconf_dump_file; /* ... old format in ~/.evolution */ gconf_dump_file = g_build_filename ( "$HOME", ".evolution", GCONF_DUMP_FILE, NULL); replace_in_file ( gconf_dump_file, EVOUSERDATADIR_MAGIC, e_get_user_data_dir ()); command = g_strconcat ( "gconftool-2 --load ", gconf_dump_file, NULL); run_cmd (command); g_free (command); command = g_strconcat ("rm ", gconf_dump_file, NULL); run_cmd (command); g_free (command); g_free (gconf_dump_file); } if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Removing temporary back up files"); run_cmd ("rm -rf $DATADIR_old"); run_cmd ("rm -rf $CONFIGDIR_old"); run_cmd ("rm -rf $HOME/.camel_certs_old"); run_cmd ("rm $DATADIR/.running"); if (!is_new_format) run_cmd ("rm -rf $HOME/.evolution_old"); if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Ensuring local sources"); end: if (restart_arg) { if (g_cancellable_is_cancelled (cancellable)) return; txt = _("Restarting Evolution"); run_evolution_no_wait (); } }
int db__driver_open_database(dbHandle * handle) { char *name; dbConnection default_connection; MYSQL *res; db_get_connection(&default_connection); name = G_store(db_get_handle_dbname(handle)); /* if name is empty use default_connection.databaseName */ if (strlen(name) == 0) name = default_connection.databaseName; G_debug(3, "db_driver_open_database() mysql: database definition = '%s'", name); /* Embedded version */ { char *datadir, *database; char *server_args[4]; char *buf; if (!replace_variables(name, &datadir, &database)) { db_d_append_error(_("Unable parse MySQL embedded database name")); db_d_append_error(mysql_error(connection)); db_d_report_error(); return DB_FAILED; } server_args[0] = "mesql"; /* this string is not used */ G_asprintf(&buf, "--datadir=%s", datadir); server_args[1] = buf; /* With InnoDB it is very slow to close the database */ server_args[2] = "--skip-innodb"; /* OK? */ /* Without --bootstrap it complains about missing * mysql.time_zone_leap_second table */ server_args[3] = "--bootstrap"; /* OK? */ if (mysql_server_init(4, server_args, NULL)) { db_d_append_error(_("Cannot initialize MySQL embedded server")); db_d_append_error(mysql_error(connection)); db_d_report_error(); free(datadir); free(database); return DB_FAILED; } connection = mysql_init(NULL); mysql_options(connection, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL); res = mysql_real_connect(connection, NULL, NULL, NULL, database, 0, NULL, 0); free(datadir); free(database); if (res == NULL) { db_d_append_error(_("Unable to connect to MySQL embedded server: ")); db_d_append_error(mysql_error(connection)); db_d_report_error(); return DB_FAILED; } } return DB_OK; }