1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #include "s4.h" |
16 | #include <xmmspriv/xmms_collection.h> |
17 | #include <xmmspriv/xmms_utils.h> |
18 | #include <stdlib.h> |
19 | #include <string.h> |
20 | #include <stdio.h> |
21 | #include <sqlite3.h> |
22 | #include <glib.h> |
23 | #include <ctype.h> |
24 | |
25 | extern gboolean try_upgrade (sqlite3 *sql); |
26 | extern void collection_restore (sqlite3 *db, GHashTable **ht); |
27 | extern void collection_dag_save (GHashTable **ht, const char *bdir); |
28 | |
29 | static s4_t *s4; |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | static gboolean |
39 | xmms_is_int (const gchar *str, int *val) |
40 | { |
41 | gboolean ret = FALSE(0); |
42 | gchar *end; |
43 | |
44 | if (str != NULL((void*)0) && !isspace (*str)((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short int ) _ISspace)) { |
45 | *val = strtol (str, &end, 10); |
46 | if (*end == '\0') |
47 | ret = TRUE(!(0)); |
48 | } |
49 | |
50 | return ret; |
51 | } |
52 | |
53 | static int tree_cmp (gconstpointer a, gconstpointer b) |
54 | { |
55 | int pa, pb; |
56 | pa = *(int*)a; |
57 | pb = *(int*)b; |
58 | |
59 | if (pa < pb) |
60 | return -1; |
61 | if (pa > pb) |
62 | return 1; |
63 | return 0; |
64 | } |
65 | |
66 | static int source_callback (void *u, int argc, char *argv[], char *col[]) |
67 | { |
68 | GTree *sources = u; |
69 | int i; |
70 | char *src; |
| 1 | 'src' declared without an initial value | |
|
71 | int *id = malloc (sizeof (int)); |
72 | |
73 | for (i = 0; i < argc; i++) { |
| 2 | | Assuming 'i' is >= 'argc' | |
|
| 3 | | Loop condition is false. Execution continues on line 80 | |
|
74 | if (!strcmp ("source", col[i])) |
75 | src = strdup (argv[i]); |
76 | else if (!strcmp ("id", col[i])) |
77 | *id = atoi (argv[i]); |
78 | } |
79 | |
80 | g_tree_insert (sources, id, src); |
| 4 | | Function call argument is an uninitialized value |
|
81 | |
82 | return 0; |
83 | } |
84 | |
85 | static int media_callback (void *u, int argc, char *argv[], char *col[]) |
86 | { |
87 | GTree *sources = u; |
88 | int id, src_id, i, intval; |
89 | char *key, *val, *intrepr, *src; |
90 | s4_val_t *id_val, *val_val; |
91 | s4_transaction_t *trans; |
92 | |
93 | intrepr = val = NULL((void*)0); |
94 | |
95 | for (i = 0; i < argc; i++) { |
96 | if (!strcmp ("id", col[i])) { |
97 | id = atoi (argv[i]); |
98 | } else if (!strcmp ("key", col[i])) { |
99 | key = argv[i]; |
100 | } else if (!strcmp ("value", col[i])) { |
101 | val = argv[i]; |
102 | } else if (!strcmp ("intval", col[i])) { |
103 | intrepr = argv[i]; |
104 | } else if (!strcmp ("source", col[i])) { |
105 | src_id = atoi (argv[i]); |
106 | } |
107 | } |
108 | |
109 | src = g_tree_lookup (sources, &src_id); |
110 | |
111 | id_val = s4_val_new_int (id); |
112 | |
113 | if (xmms_is_int (intrepr, &intval)) { |
114 | val_val = s4_val_new_int (intval); |
115 | } else { |
116 | val_val = s4_val_new_string (val); |
117 | } |
118 | |
119 | do { |
120 | trans = s4_begin (s4, 0); |
121 | s4_add (trans, "song_id", id_val, key, val_val, src); |
122 | } while (!s4_commit (trans)); |
123 | |
124 | s4_val_free (val_val); |
125 | s4_val_free (id_val); |
126 | |
127 | return 0; |
128 | } |
129 | |
130 | int main (int argc, char *argv[]) |
131 | { |
132 | sqlite3 *db; |
133 | char *coll_path, *foo, *bar, *uuid, *errmsg = NULL((void*)0); |
134 | int ret, i, uuid_len; |
135 | GTree *sources = g_tree_new (tree_cmp); |
136 | GHashTable **ht; |
137 | |
138 | if (argc != 4) { |
139 | fprintf (stderrstderr, "Usage: %s infile outfile\n" |
140 | "\tinfile - the sql file to import\n" |
141 | "\toutfile - the s4 file to write to\n" |
142 | "\tcolldir - the directory to place the collections\n", |
143 | argv[0]); |
144 | exit (1); |
145 | } |
146 | |
147 | ret = sqlite3_open (argv[1], &db); |
148 | if (ret) { |
149 | fprintf (stderrstderr, "Can't open database: %s\n", sqlite3_errmsg(db)); |
150 | sqlite3_close (db); |
151 | exit (1); |
152 | } |
153 | |
154 | if (!try_upgrade (db)) { |
155 | fprintf (stderrstderr, "Could not upgrade sqlite database to latest version\n"); |
156 | sqlite3_close (db); |
157 | exit (1); |
158 | } |
159 | |
160 | s4 = s4_open (argv[2], NULL((void*)0), S4_NEW); |
161 | if (s4 == NULL((void*)0)) { |
162 | fprintf (stderrstderr, "Can't open s4 file\n"); |
163 | exit (1); |
164 | } |
165 | |
166 | ret = sqlite3_exec (db, "select id,source from Sources;", |
167 | source_callback, sources, &errmsg); |
168 | |
169 | ret = sqlite3_exec (db, "select id,key,value,intval,source from Media;", |
170 | media_callback, sources, &errmsg); |
171 | |
172 | |
173 | ht = malloc (sizeof (GHashTable*) * XMMS_COLLECTION_NUM_NAMESPACES2); |
174 | for (i = 0; i < XMMS_COLLECTION_NUM_NAMESPACES2; i++) { |
175 | ht[i] = g_hash_table_new (g_str_hash, g_str_equal); |
176 | } |
177 | |
178 | |
179 | uuid = s4_get_uuid_string (s4); |
180 | uuid_len = strlen (uuid); |
181 | coll_path = strdup (argv[3]); |
182 | |
183 | while ((foo = strstr (coll_path, "${uuid}")) != NULL((void*)0)) { |
184 | int uuid_pos = foo - coll_path; |
185 | |
186 | bar = malloc (strlen (coll_path) - strlen ("${uuid}") + uuid_len); |
187 | memcpy (bar, coll_path, uuid_pos); |
188 | strcpy (bar + uuid_pos, uuid); |
189 | strcpy (bar + uuid_pos + uuid_len, foo + strlen ("${uuid}")); |
190 | free (coll_path); |
191 | coll_path = bar; |
192 | } |
193 | |
194 | collection_restore (db, ht); |
195 | collection_dag_save (ht, coll_path); |
196 | free (coll_path); |
197 | |
198 | sqlite3_close (db); |
199 | s4_close (s4); |
200 | |
201 | return 0; |
202 | } |