1 #include <opensync/opensync.h>
2 #include <opensync/opensync_internals.h>
3 #include <opensync/opensync-group.h>
4 #include <opensync/opensync-archive.h>
5 #include <opensync/opensync-mapping.h>
6 #include <opensync/opensync-helper.h>
11 #include <sys/types.h>
12 //#include <sys/wait.h>
15 static void usage (char *name
, int ecode
)
17 fprintf (stderr
, "Usage: %s <groupname>\n", name
);
18 fprintf (stderr
, "[--mappings <objtype>] \tDump all mappings. Default\n");
19 fprintf (stderr
, "[--hash <objtype> <memberid>] \tDump hash table for member id\n");
20 fprintf (stderr
, "[--configdir] \tSet a different configdir then ~./opensync\n");
21 fprintf (stderr
, "[--reset] \tReset the database for this group\n");
31 static void dump_map_objtype(OSyncGroupEnv
*env
, const char *objtype
, const char *groupname
)
33 OSyncError
*error
= NULL
;
34 OSyncGroup
*group
= osync_group_env_find_group(env
, groupname
);
36 char *path
= g_strdup_printf("%s/archive.db", osync_group_get_configdir(group
));
37 OSyncArchive
*archive
= osync_archive_new(path
, &error
);
38 OSyncList
*ids
= NULL
;
39 OSyncList
*uids
= NULL
;
40 OSyncList
*mappingids
= NULL
;
41 OSyncList
*memberids
= NULL
;
42 OSyncList
*d
, *u
, *m
, *i
;
48 if (!osync_archive_load_changes(archive
, objtype
, &ids
, &uids
, &mappingids
, &memberids
, &error
))
56 for (; u
; u
= u
->next
) {
57 long long int id
= (long long int)GPOINTER_TO_INT(d
->data
);
59 long long int memberid
= (long long int)GPOINTER_TO_INT(i
->data
);
60 long long int mappingid
= (long long int)GPOINTER_TO_INT(m
->data
);
62 printf("ID: %lli UID: %s MEMBER: %lli MAPPINGID: %lli\n", id
, uid
, memberid
, mappingid
);
70 osync_list_free(uids
);
71 osync_list_free(mappingids
);
72 osync_list_free(memberids
);
74 osync_archive_unref(archive
);
78 printf("ERROR: %s", osync_error_print(&error
));
79 osync_error_unref(&error
);
82 static void dump_map(OSyncGroupEnv
*env
, const char *groupname
)
85 OSyncGroup
*group
= osync_group_env_find_group(env
, groupname
);
89 printf("Unable to find group with name \"%s\"\n", groupname
);
93 num_objtypes
= osync_group_num_objtypes(group
);
94 if (num_objtypes
== 0) {
95 printf("Group has no objtypes. Have the objtypes already been discovered?\n");
99 for (i
= 0; i
< num_objtypes
; i
++) {
100 const char *objtype
= osync_group_nth_objtype(group
, i
);
101 printf("Mappings for objtype \"%s\":\n", objtype
);
102 dump_map_objtype(env
, objtype
, groupname
);
107 static void print_hashtable(const char *uid
, const char *hash
, void *user_data
)
109 printf("UID: %s\tHASH:%s\n", uid
, hash
);
112 static void dump_hash(OSyncGroupEnv
*env
, const char *objtype
, const char *groupname
, char *memberid
)
114 OSyncError
*error
= NULL
;
115 OSyncGroup
*group
= osync_group_env_find_group(env
, groupname
);
116 long long int id
= 0;
117 OSyncMember
*member
= NULL
;
119 OSyncHashTable
*table
= NULL
;
123 printf("Unable to find group with name \"%s\"\n", groupname
);
128 member
= osync_group_find_member(group
, id
);
130 printf("Unable to find member with id %s\n", memberid
);
134 path
= g_strdup_printf("%s/hashtable.db", osync_member_get_configdir(member
));
135 table
= osync_hashtable_new(path
, objtype
, &error
);
140 osync_hashtable_foreach(table
, print_hashtable
, NULL
);
142 osync_hashtable_unref(table
);
147 printf("ERROR: %s", osync_error_print(&error
));
148 osync_error_unref(&error
);
151 static void reset(OSyncGroupEnv
*osync
, char *groupname
)
153 OSyncGroup
*group
= osync_group_env_find_group(osync
, groupname
);
156 printf("Unable to find group with name \"%s\"\n", groupname
);
160 osync_group_reset(group
, NULL
);
163 int main (int argc
, char *argv
[])
166 char *groupname
= NULL
;
167 char *membername
= NULL
;
168 ToolAction action
= DUMPMAPS
;
169 char *configdir
= NULL
;
170 char *objtype
= NULL
;
171 OSyncError
*error
= NULL
;
172 OSyncGroupEnv
*env
= NULL
;
178 for (i
= 2; i
< argc
; i
++) {
180 if (!strcmp (arg
, "--mappings")) {
182 objtype
= argv
[i
+ 1];
186 } else if (!strcmp (arg
, "--hash")) {
188 objtype
= argv
[i
+ 1];
189 membername
= argv
[i
+ 2];
191 if (!objtype
|| !membername
)
193 } else if (!strcmp (arg
, "--reset")) {
195 } else if (!strcmp (arg
, "--help")) {
197 } else if (!strcmp (arg
, "--configdir")) {
198 configdir
= argv
[i
+ 1];
202 } else if (!strcmp (arg
, "--")) {
204 } else if (arg
[0] == '-') {
211 env
= osync_group_env_new(&error
);
215 if (!osync_group_env_load_groups(env
, configdir
, &error
))
221 dump_map_objtype(env
, objtype
, groupname
);
223 dump_map(env
, groupname
);
226 dump_hash(env
, objtype
, groupname
, membername
);
229 reset(env
, groupname
);
236 printf("ERROR: %s", osync_error_print(&error
));
237 osync_error_unref(&error
);