From: <dg...@su...> - 2009-01-10 22:29:26
|
Author: dgollub Date: Sat Jan 10 22:31:15 2009 New Revision: 5074 URL: http://www.opensync.org/changeset/5074 Log: Move file- and plain-format plugins to file-sync repo. Added: plugins/file-sync/src/file.c - copied unchanged from r5072, trunk/formats/file.c plugins/file-sync/src/file.h - copied unchanged from r5072, trunk/formats/file.h plugins/file-sync/src/plain.c - copied unchanged from r5072, trunk/formats/plain.c Copied: plugins/file-sync/src/file.c (from r5072, trunk/formats/file.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ plugins/file-sync/src/file.c Sat Jan 10 22:31:15 2009 (r5074, copy of r5072, trunk/formats/file.c) @@ -0,0 +1,260 @@ +/* + * opensync - A plugin for file objects for the opensync framework + * Copyright (C) 2004-2005 Armin Bauer <arm...@op...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "config.h" + +#include <string.h> +#include <glib.h> + +#include <opensync/opensync.h> +#include <opensync/opensync-serializer.h> +#include <opensync/opensync-format.h> + +#include "file.h" + +static OSyncConvCmpResult compare_file(const char *leftdata, unsigned int leftsize, const char *rightdata, unsigned int rightsize, void *user_data) +{ + OSyncFileFormat *leftfile = (OSyncFileFormat *)leftdata; + OSyncFileFormat *rightfile = (OSyncFileFormat *)rightdata; + + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p, %i)", __func__, leftdata, leftsize, rightdata, rightsize); + osync_assert(leftdata); + osync_assert(rightdata); + + osync_assert(rightfile->path); + osync_assert(leftfile->path); + + osync_trace(TRACE_INTERNAL, "Comparing %s and %s", leftfile->path, rightfile->path); + + + if (!strcmp(leftfile->path, rightfile->path)) { + if (leftfile->size == rightfile->size) { + if (leftfile->size == 0 || !memcmp(leftfile->data, rightfile->data, rightfile->size)) { + osync_trace(TRACE_EXIT, "%s: Same", __func__); + return OSYNC_CONV_DATA_SAME; + } + } + + osync_trace(TRACE_EXIT, "%s: Similar", __func__); + return OSYNC_CONV_DATA_SIMILAR; + } + + osync_trace(TRACE_EXIT, "%s: Mismatch", __func__); + return OSYNC_CONV_DATA_MISMATCH; +} + +static osync_bool conv_file_to_plain(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *user_data, OSyncError **error) +{ + OSyncFileFormat *file = (OSyncFileFormat *)input; + char *plaindata = NULL; + osync_trace(TRACE_INTERNAL, "Converting file to plain"); + + *free_input = TRUE; + + /* Add a \0 to make a usable plain (text) format. input gets freed by destroy_func() */ + plaindata = osync_try_malloc0(file->size + 1, error); + memcpy(plaindata, file->data, file->size); + + *output = plaindata; + *outpsize = file->size + 1; + + return TRUE; +} + +static osync_bool conv_plain_to_file(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error) +{ + OSyncFileFormat *file = NULL; + osync_trace(TRACE_INTERNAL, "Converting plain to file"); + + *free_input = FALSE; + file = osync_try_malloc0(sizeof(OSyncFileFormat), error); + if (!file) + return FALSE; + file->path = osync_rand_str(g_random_int_range(1, 100)); + + file->data = input; + file->size = inpsize - 1; + + *output = (char *)file; + *outpsize = sizeof(OSyncFileFormat); + return TRUE; +} + +static void destroy_file(char *input, unsigned int inpsize, void *user_data) +{ + OSyncFileFormat *file = (OSyncFileFormat *)input; + + if (file->data) + g_free(file->data); + + if (file->path) + g_free(file->path); + + g_free(file); +} + +static osync_bool duplicate_file(const char *uid, const char *input, unsigned int insize, char **newuid, char **output, unsigned int *outsize, osync_bool *dirty, void *user_data, OSyncError **error) +{ + OSyncFileFormat *file = (OSyncFileFormat *)input; + + char *newpath = g_strdup_printf ("%s-dupe", file->path); + g_free(file->path); + file->path = newpath; + *newuid = g_strdup(file->path); + *dirty = TRUE; + return TRUE; +} + +static osync_bool copy_file(const char *input, unsigned int inpsize, char **output, unsigned int *outpsize, void *user_data, OSyncError **error) +{ + OSyncFileFormat *inpfile = (OSyncFileFormat *)input; + + OSyncFileFormat *outfile = osync_try_malloc0(sizeof(OSyncFileFormat), error); + if (!outfile) + return FALSE; + + if (inpfile->data) { + outfile->data = g_malloc0(inpfile->size); + memcpy(outfile->data, inpfile->data, inpfile->size); + outfile->size = inpfile->size; + } + + outfile->path = g_strdup(inpfile->path); + + *output = (char *)outfile; + *outpsize = sizeof(OSyncFileFormat); + return TRUE; +} + +static time_t revision_file(const char *input, unsigned int inpsize, void *user_data, OSyncError **error) +{ + OSyncFileFormat *file = (OSyncFileFormat *)input; + time_t lastmod; + + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, input, inpsize, error); + + lastmod = file->last_mod; + + osync_trace(TRACE_EXIT, "%s: %i", __func__, lastmod); + return lastmod; +} + +static char *print_file(const char *data, unsigned int size, void *user_data) +{ + OSyncFileFormat *file = (OSyncFileFormat *)data; + + char *printable = g_strdup_printf ("File %s: size: %i", file->path, file->size); + return printable; +} + +static osync_bool marshal_file(const char *input, unsigned int inpsize, OSyncMarshal *marshal, void *user_data, OSyncError **error) +{ + OSyncFileFormat *file = (OSyncFileFormat *)input; + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p, %p)", __func__, input, inpsize, marshal, error); + + osync_marshal_write_string(marshal, file->path); + osync_marshal_write_buffer(marshal, file->data, file->size); + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; +} + +static osync_bool demarshal_file(OSyncMarshal *marshal, char **output, unsigned int *outpsize, void *user_data, OSyncError **error) +{ + OSyncFileFormat *file = NULL; + osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, marshal, output, outpsize, error); + + file = osync_try_malloc0(sizeof(OSyncFileFormat), error); + if (!file) + goto error; + + osync_marshal_read_string(marshal, &(file->path)); + osync_marshal_read_buffer(marshal, (void *)&(file->data), (int *)&(file->size)); + + *output = (char *)file; + *outpsize = sizeof(OSyncFileFormat); + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + + error: + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + +osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *format = osync_objformat_new("file", "data", error); + if (!format) + return FALSE; + + osync_objformat_set_compare_func(format, compare_file); + osync_objformat_set_destroy_func(format, destroy_file); + osync_objformat_set_duplicate_func(format, duplicate_file); + osync_objformat_set_print_func(format, print_file); + osync_objformat_set_revision_func(format, revision_file); + osync_objformat_set_copy_func(format, copy_file); + + osync_objformat_set_marshal_func(format, marshal_file); + osync_objformat_set_demarshal_func(format, demarshal_file); + + osync_format_env_register_objformat(env, format); + osync_objformat_unref(format); + return TRUE; +} + +osync_bool get_conversion_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *file = osync_format_env_find_objformat(env, "file"); + OSyncObjFormat *plain = NULL; + OSyncFormatConverter *conv = NULL; + + if (!file) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find file format"); + return FALSE; + } + + plain = osync_format_env_find_objformat(env, "plain"); + if (!plain) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find plain format"); + return FALSE; + } + + conv = osync_converter_new(OSYNC_CONVERTER_DECAP, file, plain, conv_file_to_plain, error); + if (!conv) + return FALSE; + + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); + + conv = osync_converter_new(OSYNC_CONVERTER_ENCAP, plain, file, conv_plain_to_file, error); + if (!conv) + return FALSE; + + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); + return TRUE; +} + +int get_version(void) +{ + return 1; +} Copied: plugins/file-sync/src/file.h (from r5072, trunk/formats/file.h) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ plugins/file-sync/src/file.h Sat Jan 10 22:31:15 2009 (r5074, copy of r5072, trunk/formats/file.h) @@ -0,0 +1,50 @@ +/* + * opensync - A file format for opensync + * Copyright (C) 2005 Armin Bauer <arm...@op...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + * + */ + +#ifndef _FILE_H +#define _FILE_H + +#ifndef _WIN32 +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#endif //_WIN32 + +#ifdef _WIN32 +#define mode_t int +#define uid_t int +#define gid_t int +#endif //_WIN32 + +typedef struct OSyncFileFormat { + /** The mode of this file. See man fstat for explanation */ + mode_t mode; + /** The id of the user (owner) of this file */ + uid_t userid; + /** The id of the owning group of this file */ + gid_t groupid; + /** Time of the last modification */ + time_t last_mod; + char *path; + char *data; + unsigned int size; +} OSyncFileFormat; + +#endif //_FILE_H Copied: plugins/file-sync/src/plain.c (from r5072, trunk/formats/plain.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ plugins/file-sync/src/plain.c Sat Jan 10 22:31:15 2009 (r5074, copy of r5072, trunk/formats/plain.c) @@ -0,0 +1,184 @@ +/* + * data - A plugin for data objects for the opensync framework + * Copyright (C) 2004-2005 Armin Bauer <arm...@op...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include <string.h> +#include <glib.h> + +#include <opensync/opensync.h> +#include <opensync/opensync-serializer.h> +#include <opensync/opensync-format.h> +#include <opensync/opensync-xmlformat.h> + +static OSyncConvCmpResult compare_plain(const char *leftdata, unsigned int leftsize, const char *rightdata, unsigned int rightsize, void *user_data) +{ + /* Consider empty block equal NULL pointers */ + if (!leftsize) leftdata = NULL; + if (!rightsize) rightdata = NULL; + + if (!leftdata && !rightdata) + return OSYNC_CONV_DATA_SAME; + + if (leftdata && rightdata && (leftsize == rightsize)) { + if (!memcmp(leftdata, rightdata, leftsize)) + return OSYNC_CONV_DATA_SAME; + else + return OSYNC_CONV_DATA_MISMATCH; + } + + return OSYNC_CONV_DATA_MISMATCH; +} + +static osync_bool copy_plain(const char *input, unsigned int inpsize, char **output, unsigned int *outpsize, void *user_data, OSyncError **error) +{ + char *r = g_malloc0(inpsize); + + memcpy(r, input, inpsize); + *output = r; + *outpsize = inpsize; + return TRUE; +} + +static osync_bool conv_xmlformatnote_to_memo(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error) +{ + const char *body = NULL; + OSyncXMLFormat *xmlformat = (OSyncXMLFormat *)input; + OSyncXMLFieldList *xmlfieldlist = osync_xmlformat_search_field(xmlformat, "Description", error, NULL); + OSyncXMLField *xmlfield = osync_xmlfieldlist_item(xmlfieldlist, 0); + + *free_input = TRUE; + if (xmlfield) { + body = osync_xmlfield_get_key_value(xmlfield, "Content"); + } + if (!body) { + body = ""; + } + *output = g_strdup(body); + *outpsize = strlen(body); + return TRUE; +} + +static osync_bool conv_memo_to_xmlformatnote(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error) +{ + GString *str; + const char *p; + OSyncXMLFormat *ret = NULL; + OSyncXMLField *field = NULL; + + *free_input = TRUE; + + ret = osync_xmlformat_new("note", error); + if (!ret) + return FALSE; + + if (input && *input) { + field = osync_xmlfield_new(ret, "Description", error); + + str = g_string_new(""); + for (p = input; p && *p; p++) { + switch (*p) { + case '\r': + if (*(p+1) == '\n') + p++; + osync_trace(TRACE_INTERNAL, "[%s] escape carriage returns!!", __func__); + str = g_string_append (str, "\n"); + break; + default: + str = g_string_append_c (str, *p); + break; + } + } + osync_trace(TRACE_SENSITIVE, "Input : %s", str->str); + osync_xmlfield_add_key_value(field, "Content", str->str); + } + + osync_xmlformat_sort(ret); + + *output = (char *)ret; + *outpsize = osync_xmlformat_size(); + return TRUE; +} + +static void destroy_plain(char *input, unsigned int inpsize, void *user_data) +{ + g_free(input); +} + +osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *format = osync_objformat_new("plain", "data", error); + if (!format) + return FALSE; + + osync_objformat_set_compare_func(format, compare_plain); + osync_objformat_set_copy_func(format, copy_plain); + osync_objformat_set_destroy_func(format, destroy_plain); + + osync_format_env_register_objformat(env, format); + osync_objformat_unref(format); + + /* "memo" is the same as "plain" expect the object type is fixed to "note" */ + format = osync_objformat_new("memo", "note", error); + if (!format) + return FALSE; + + osync_objformat_set_compare_func(format, compare_plain); + osync_objformat_set_copy_func(format, copy_plain); + osync_objformat_set_destroy_func(format, destroy_plain); + + osync_format_env_register_objformat(env, format); + osync_objformat_unref(format); + + return TRUE; +} + +osync_bool get_conversion_info(OSyncFormatEnv *env, OSyncError **error) +{ + OSyncObjFormat *memo = osync_format_env_find_objformat(env, "memo"); + OSyncObjFormat *xmlformatnote = osync_format_env_find_objformat(env, "xmlformat-note"); + OSyncFormatConverter *conv = NULL; + if (!memo) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find memo format"); + return FALSE; + } + if (!xmlformatnote) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find xmlformat-note format"); + return FALSE; + } + conv = osync_converter_new(OSYNC_CONVERTER_CONV, xmlformatnote, memo, conv_xmlformatnote_to_memo, error); + if (!conv) + return FALSE; + + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); + + conv = osync_converter_new(OSYNC_CONVERTER_CONV, memo, xmlformatnote, conv_memo_to_xmlformatnote, error); + if (!conv) + return FALSE; + + osync_format_env_register_converter(env, conv); + osync_converter_unref(conv); + return TRUE; +} + +int get_version(void) +{ + return 1; +} + |