From: <svn...@op...> - 2009-07-13 10:23:43
|
Author: bellmich Date: Mon Jul 13 12:23:31 2009 New Revision: 1166 URL: http://libsyncml.opensync.org/changeset/1166 Log: migrated syncml-ds-tool from SmlError to GError Modified: trunk/tools/syncml-ds-tool.c Modified: trunk/tools/syncml-ds-tool.c ============================================================================== --- trunk/tools/syncml-ds-tool.c Thu Jul 9 13:57:42 2009 (r1165) +++ trunk/tools/syncml-ds-tool.c Mon Jul 13 12:23:31 2009 (r1166) @@ -230,9 +230,11 @@ /* *********** CONFIG ***************** */ /* ************************************ */ +#define TOOL_ERROR g_quark_from_static_string("syncml-ds-tool") + typedef struct SmlDsToolLocationType { char *source; - SmlBool slow; + gboolean slow; char *contentType; char *directory; GHashTable *remoteHash; @@ -253,22 +255,22 @@ char *maxObjSize = NULL; GMutex *runMutex = NULL; -SmlBool dumpinfo = FALSE; -SmlBool useNumberOfChanges = TRUE; -SmlBool useStringTable = FALSE; +gboolean dumpinfo = FALSE; +gboolean useNumberOfChanges = TRUE; +gboolean useStringTable = FALSE; SmlDevInf *remoteDevinf = NULL; -SmlBool localWinsConflicts = TRUE; +gboolean localWinsConflicts = TRUE; time_t checkpoint; -SmlBool readOnly = FALSE; +gboolean readOnly = FALSE; /* ************************************ */ /* *********** CALLBACKS ************** */ /* ************************************ */ -static void writeSyncStatus(SmlBool success) +static void writeSyncStatus(gboolean success) { SmlDsToolLocationType *datastore = NULL; GList *o = datastores; @@ -277,11 +279,11 @@ if (datastore->directory) { char *absolute = g_strdup_printf("%s/%s", datastore->directory, STATUS_FILENAME); if (success) { - GError *gerror = NULL; - if (!g_file_set_contents(absolute, "DONE", 4, &gerror)) { + GError *error = NULL; + if (!g_file_set_contents(absolute, "DONE", 4, &error)) { g_warning("Cannot write data to %s. %s", - absolute, gerror->message); - g_error_free(gerror); + absolute, error->message); + g_error_free(error); } } else { g_unlink(STATUS_FILENAME); @@ -294,7 +296,7 @@ SmlChangeType getChangeType( SmlDsToolLocationType *datastore, const char *filename, - SmlError **error) + GError **error) { smlTrace(TRACE_ENTRY, "%s(%s, %s, %p)", __func__, VA_STRING(datastore->source), VA_STRING(filename), error); struct stat fdata; @@ -303,7 +305,7 @@ /* does the file exist */ if (g_stat(filename, &fdata) != 0) { - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "Cannot stat file %s (%d).", filename, errno); goto error; @@ -312,7 +314,7 @@ /* was the file touched after the checkpoint */ if (fdata.st_mtime >= checkpoint) { if (sessionType == SML_SESSION_TYPE_CLIENT) { - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "Someone accessed the file absolute after the synchronization started.", filename); goto error; @@ -328,7 +330,7 @@ absolute_status = g_strdup_printf("%s/%s", datastore->directory, STATUS_FILENAME); if (!g_file_test(absolute_status, G_FILE_TEST_EXISTS)) { if (!datastore->slow) { - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "Status file %s is missing but it is no SLOW-SYNC.", absolute_status); goto error; @@ -342,7 +344,7 @@ /* read the stat from the status file */ if (g_stat(absolute_status, &fstatus) != 0) { /* this is a general error */ - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "Cannot stat file %s (%d).", absolute_status, errno); goto error; @@ -384,26 +386,25 @@ error: if (absolute_status) smlSafeCFree(&absolute_status); - smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error)); + smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, (*error)->message); return SML_CHANGE_UNKNOWN; } -SmlBool sendAllChanges( +gboolean sendAllChanges( SmlDataSyncObject *dsObject, - SmlError **error) + GError **error) { smlTrace(TRACE_ENTRY, "%s", __func__); GList *o = datastores; - GError *gerror = NULL; /* If readOnly then data MUST NOT be send. */ for (;!readOnly && o;o=o->next) { SmlDsToolLocationType *datastore = o->data; if (!datastore->directory) continue; smlTrace(TRACE_INTERNAL, "%s: checking %s", __func__, VA_STRING(datastore->source)); - GDir *dir = g_dir_open(datastore->directory, 0, &gerror); + GDir *dir = g_dir_open(datastore->directory, 0, error); if (!dir) - goto gerror; + goto error; const gchar *filename = g_dir_read_name(dir); for (;filename; filename = g_dir_read_name(dir)) { smlTrace(TRACE_INTERNAL, "%s: checking %s", __func__, VA_STRING(filename)); @@ -424,9 +425,9 @@ smlTrace(TRACE_INTERNAL, "%s: sending %s", __func__, VA_STRING(absolute)); gsize length; char *data = NULL; - if (!g_file_get_contents(absolute, &data, &length, &gerror)) { + if (!g_file_get_contents(absolute, &data, &length, error)) { smlSafeCFree(&absolute); - goto gerror; + goto error; } smlSafeCFree(&absolute); if (!smlDataSyncAddChange(dsObject, datastore->source, changeType, @@ -439,11 +440,8 @@ } smlTrace(TRACE_EXIT, "%s", __func__); return smlDataSyncSendChanges(dsObject, error); -gerror: - smlErrorSet(error, SML_ERROR_GENERIC, gerror->message); - g_error_free(gerror); error: - smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error)); + smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, (*error)->message); return FALSE; } @@ -451,16 +449,16 @@ SmlDataSyncObject *dsObject, SmlDataSyncEventType type, void *userdata, - SmlError *error) + GError *error) { smlTrace(TRACE_ENTRY, "%s(%p, %i, %p, %p)", __func__, dsObject, type, userdata, error); - SmlError *locerror = NULL; + GError *locerror = NULL; switch (type) { case SML_DATA_SYNC_EVENT_ERROR: writeSyncStatus(FALSE); - g_message("ERROR: %s\n", smlErrorPrint(&error)); - smlErrorDeref(&error); + g_message("ERROR: %s\n", error->message); + g_error_free(error); exit(2); break; case SML_DATA_SYNC_EVENT_CONNECT: @@ -508,15 +506,15 @@ return; error: - fprintf(stderr, "An error occured while handling events: %s\n", smlErrorPrint(&locerror)); - smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(&locerror)); - smlErrorDeref(&locerror); + fprintf(stderr, "An error occured while handling events: %s\n", locerror->message); + smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, locerror->message); + g_error_free(locerror); exit(3); } static char * getSafeFilename(const char *filename) { - SmlBool clean = TRUE; + gboolean clean = TRUE; size_t i; for (i=0; i < strlen(filename); i++) { @@ -532,7 +530,7 @@ } } -static SmlBool recvChangeCallback( +static gboolean recvChangeCallback( SmlDataSyncObject *dsObject, const char *source, SmlChangeType type, @@ -540,7 +538,7 @@ char *data, unsigned int size, void *userdata, - SmlError **error) + GError **error) { smlTrace(TRACE_ENTRY, "%s(%p, %s, %d, %s, %p, %d, %p, %p)", __func__, dsObject, VA_STRING(source), type, VA_STRING(uid), data, size, userdata, error); char *absolute_uid = NULL; @@ -572,7 +570,7 @@ /* sanity check for uid */ if (!strcmp(safeUID, STATUS_FILENAME)) { - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "The filename %s cannot be used as UID. This is a potential attack.", STATUS_FILENAME); printf("\tIllegal filename %s detected.\n", STATUS_FILENAME); @@ -587,7 +585,7 @@ g_hash_table_insert(datastore->remoteHash, g_strdup(absolute_uid), (char *)"1"); } /* the directory is checked by scanArguments */ - SmlBool doWrite = FALSE; + gboolean doWrite = FALSE; if (!doWrite && !g_file_test(absolute_uid, G_FILE_TEST_EXISTS)) doWrite = TRUE; if (!doWrite && sessionType == SML_SESSION_TYPE_CLIENT) @@ -617,13 +615,13 @@ struct stat fdata; struct stat status; if (g_stat(absolute_uid, &fdata) != 0) { - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "Cannot stat uid file %s (%d).", absolute_uid, errno); goto error; } if (g_stat(absolute_status, &status) != 0) { - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "Cannot stat status file %s (%d).", absolute_status, errno); goto error; @@ -640,14 +638,8 @@ g_unlink(absolute_uid); printf("\tThe item was successfully deleted.\n"); } else { - GError *gerror = NULL; - if (!g_file_set_contents(absolute_uid, data, size, &gerror)) { - smlErrorSet(error, SML_ERROR_GENERIC, - "Cannot write data to %s. %s", - absolute_uid, gerror->message); - g_error_free(gerror); + if (!g_file_set_contents(absolute_uid, data, size, error)) goto error; - } printf("\tThe item was successfully written.\n"); } } else { @@ -674,15 +666,15 @@ smlSafeCFree(&absolute_uid); if (absolute_status) smlSafeCFree(&absolute_status); - smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error)); + smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, (*error)->message); return FALSE; } -static SmlBool recvDevInfCallback( +static gboolean recvDevInfCallback( SmlDataSyncObject *dsObject, SmlDevInf *devinf, void *userdata, - SmlError **error) + GError **error) { smlTrace(TRACE_INTERNAL, "%s(%p, %p, %p, %p)", dsObject, devinf, userdata, error); printf("Received device information.\n"); @@ -696,7 +688,7 @@ const char *source, SmlAlertType type, void *userdata, - SmlError **error) + GError **error) { smlTrace(TRACE_ENTRY, "%s(%p, %s, %d, %p, %p)", __func__, dsObject, VA_STRING(source), type, userdata, error); /* find the appropriate datasoure */ @@ -713,7 +705,7 @@ if (o) o = o->next; } if (!datastore) { - smlErrorSet(error, SML_ERROR_GENERIC, + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "Cannot found datastore %s.", source); goto error; @@ -729,7 +721,7 @@ smlTrace(TRACE_EXIT, "%s - slow == %d", __func__, datastore->slow); return type; error: - smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error)); + smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, (*error)->message); return SML_ALERT_UNKNOWN; } @@ -907,10 +899,10 @@ return type; } -SmlBool scanArguments( +gboolean scanArguments( SmlDataSyncObject *dsObject, int argc, char *argv[], - SmlError **error) + GError **error) { smlTrace(TRACE_ENTRY, "%s", __func__); if (argc == 1) @@ -967,7 +959,7 @@ /* check the directory */ if (g_mkdir_with_parents(datastore->directory, 0700) != 0) { - smlErrorSet(error, SML_ERROR_INTERNAL_MISCONFIGURATION, + g_set_error(error, TOOL_ERROR, SML_ERROR_INTERNAL_MISCONFIGURATION, "There is a problem with the directory %s (%d).", datastore->directory, errno); goto error; @@ -1353,7 +1345,7 @@ if (g_list_length(datastores) == 0) { if (syncmlVersion == NULL || strcmp(syncmlVersion, "1.2")) { - smlErrorSet(error, SML_ERROR_GENERIC, "You have to configure at least one database"); + g_set_error(error, TOOL_ERROR, SML_ERROR_GENERIC, "You have to configure at least one database"); goto error; } else { printf("All datastores will be requested because no datastore is configured.\n"); @@ -1366,7 +1358,7 @@ error: smlTrace(TRACE_EXIT_ERROR, "Failed to start the client: %s\n", - smlErrorPrint(error)); + (*error)->message); return FALSE; } @@ -1376,7 +1368,7 @@ int main(int argc, char *argv[]) { - SmlError *error = NULL; + GError *error = NULL; if (!g_thread_supported ()) g_thread_init (NULL); /* init DS object */ @@ -1475,8 +1467,8 @@ return 0; error: if (error != NULL) { - fprintf(stderr, "ERROR: %s\n", smlErrorPrint(&error)); - smlErrorDeref(&error); + fprintf(stderr, "ERROR: %s\n", error->message); + g_error_free(error); } return 1; } |