OpenSync  0.22
opensync_anchor.c
1 /*
2  * libopensync - A synchronization framework
3  * Copyright (C) 2004-2005 Armin Bauer <armin.bauer@opensync.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include <opensync.h>
22 #include "opensync_internals.h"
23 
24 osync_bool osync_anchor_compare(OSyncMember *member, const char *objtype, const char *new_anchor)
25 {
26  osync_trace(TRACE_ENTRY, "%s(%p, %s, %s)", __func__, member, objtype, new_anchor);
27  g_assert(member);
28 
29  OSyncError *error = NULL;
30  OSyncDB *db = NULL;
31  if (!(db = osync_db_open_anchor(member, &error)))
32  goto error;
33 
34  osync_bool retval = FALSE;
35 
36  char *old_anchor = NULL;
37  osync_db_get_anchor(db, objtype, &old_anchor);
38 
39  if (old_anchor) {
40  if (!strcmp(old_anchor, new_anchor)) {
41  retval = TRUE;
42  } else {
43  osync_trace(TRACE_INTERNAL, "Anchor mismatch");
44  retval = FALSE;
45  }
46  } else {
47  osync_trace(TRACE_INTERNAL, "No previous anchor");
48  retval = FALSE;
49  }
50 
51  osync_db_close_anchor(db);
52 
53  osync_trace(TRACE_EXIT, "%s: %i", __func__, retval);
54  return retval;
55 
56 error:
57  osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error));
58  osync_error_free(&error);
59  return FALSE;
60 }
61 
62 void osync_anchor_update(OSyncMember *member, const char *objtype, const char *new_anchor)
63 {
64  osync_trace(TRACE_ENTRY, "%s(%p, %s, %s)", __func__, member, objtype, new_anchor);
65  g_assert(member);
66 
67  OSyncError *error = NULL;
68  OSyncDB *db = NULL;
69  if (!(db = osync_db_open_anchor(member, &error)))
70  goto error;
71 
72  osync_db_put_anchor(db, objtype, new_anchor);
73  osync_db_close_anchor(db);
74 
75  osync_trace(TRACE_EXIT, "%s", __func__);
76  return;
77 
78 error:
79  osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error));
80  osync_error_free(&error);
81  return;
82 }
83 
84 char *osync_anchor_retrieve(OSyncMember *member, const char *objtype)
85 {
86  osync_trace(TRACE_ENTRY, "%s(%p, %s)", __func__, member, objtype);
87  g_assert(member);
88 
89  OSyncError *error = NULL;
90  OSyncDB *db = NULL;
91  if (!(db = osync_db_open_anchor(member, &error)))
92  goto error;
93 
94  char *anchor = NULL;
95  osync_db_get_anchor(db, objtype, &anchor);
96  osync_db_close_anchor(db);
97 
98  osync_trace(TRACE_EXIT, "%s: %s", __func__, anchor);
99  return anchor;
100 
101 error:
102  osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error));
103  osync_error_free(&error);
104  return NULL;
105 }