-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.c
449 lines (363 loc) · 12.2 KB
/
db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "db.h"
static int callback (void *data, int argc, char **argv, char **azColName)
{
// callback function fpr sqlite3 calls
int *int_data = (int*) data;
*int_data = atoi(argv[0]); // storing the id of the entry in data
return 0;
}
sqlite3 *db_init (void)
{
sqlite3 *db = NULL;
if (sqlite3_open(DB_NAME, &db)) // open the database
logcmd(LOG_ERROR_MALLOC, "db: db_init: could not malloc db");
// create tables if they do not exist
sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS \
artist(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \
name TEXT NOT NULL);", NULL, 0, NULL);
sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS \
album(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \
name TEXT NOT NULL, \
artist_id INTEGER NOT NULL);", NULL, 0, NULL);
sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS \
track(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \
title TEXT NOT NULL, \
number INTEGER, \
total_number INTEGER, \
artist_id INTEGER NOT NULL, \
album_id INTEGER NOT NULL, \
path TEXT NOT NULL);", NULL, 0, NULL);
// create 'Playlists' artist if it does not exist
struct entry e;
e.artist = "Playlists";
db_add_artist(db, &e);
logcmd(LOG_DMSG, "db: db_init: db initialized");
return db;
}
int is_supported_audio_file (const char *file)
{
// check if the given file is a supported audio file
char *end = strrchr(file, '.');
if (!end)
{
logcmd(LOG_DMSG, "db: is_supported_audio_file: could not malloc end");
return 0;
}
const char *supported[] = {
".ogg",
".mp3"
};
int size_supported = 2;
for (int i = 0; i < size_supported; i++)
{
if (strcmp(end, supported[i]) == 0)
return 1;
}
return 0;
}
int db_add_dir (sqlite3 *db, const char *path)
{
if (*path == '~')
path += 2;
// check if path is a file or directory
struct stat sb;
stat(path, &sb);
if (S_ISREG(sb.st_mode))
{
// path is a file -> call db_add_file
db_add_file (db, path);
return 1;
}
DIR *dir = NULL;
struct dirent *ent = NULL;
if ((dir = opendir(path))) // open the directory
{
if (ent)
{
free(ent);
ent = NULL;
}
while ((ent = readdir(dir))) // read files in directory
{
if (ent->d_type == DT_DIR) // an other directory
{
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) // do not search recursiv in '.' and '..'
continue;
int size = strlen(path) + strlen(ent->d_name) + 2;
char *subdir = malloc(sizeof(char) * size);
if (!subdir)
exit(1);
snprintf(subdir, size, "%s%s/", path, ent->d_name);
db_add_dir(db, subdir);
free(subdir);
}
else if (ent->d_type == DT_REG)
{
if (is_supported_audio_file(ent->d_name))
{
int size = strlen(path) + strlen(ent->d_name) + 1;
char *file = malloc(sizeof(char) * size);
if (!file)
exit(1);
snprintf(file, size, "%s%s", path, ent->d_name);
int result = db_add_file(db, file);
free (file);
}
}
}
}
else
logcmd(LOG_MSG, "could not open directory: %s", path);
closedir(dir);
return 1;
}
int db_check_artist (sqlite3 *db, const struct entry *e)
{
/*
* check if the artist already exists in the database
* yes -> return the id
* no -> return 0
*/
logcmd(LOG_DMSG, "checking artist: %s", e->artist);
char *err = NULL;
int id = -1;
int size = snprintf(NULL, 0, CHECK_ARTIST, e->artist);
char *sql = malloc(sizeof(char) * size);
if (! sql)
logcmd(LOG_ERROR_MALLOC, "db: db_check_artist: could not allocate memory for sql");
snprintf(sql, size, CHECK_ARTIST, e->artist);
if (sqlite3_exec(db, sql, callback, &id, &err) != SQLITE_OK)
logcmd(LOG_ERROR, "SQL ERROR: %i", err);
free(sql);
free(err);
if (id >= 0)
return id;
return 0;
}
int db_check_album (sqlite3 *db, const struct entry *e)
{
/*
* check if the album already exists in the database
* yes -> return id
* no -> return 0
*/
logcmd(LOG_DMSG, "checking album: %s", e->album);
char *err = NULL;
int id = -1;
int size = snprintf(NULL, 0, CHECK_ALBUM, e->album, e->artist_id);
char *sql = malloc(sizeof(char) * size);
if (! sql)
logcmd(LOG_ERROR_MALLOC, "db: db_check_album: could not allocate memory for sql");
snprintf(sql, size, CHECK_ALBUM, e->album, e->artist_id);
if (sqlite3_exec(db, sql, callback, &id, &err) != SQLITE_OK)
logcmd(LOG_ERROR, "SQL ERROR: %i", err);
free(sql);
free(err);
if (id >= 0)
return id;
return 0;
}
int db_check_track (sqlite3 *db, const struct entry *e)
{
/*
* check if the title already exists in the database
* yes -> return id
* no -> return 0
*/
logcmd(LOG_DMSG, "checking track: %s", e->title);
char *err = NULL;
int id = -1;
int size = snprintf(NULL, 0, CHECK_TRACK, e->title, e->number, e->totalnumber, e->artist_id, e->album_id);
char *sql = malloc(sizeof(char) * size);
if (! sql)
logcmd(LOG_ERROR_MALLOC, "db: db_check_track: unable to allocate memory for sql");
snprintf(sql, size, CHECK_TRACK, e->title, e->number, e->totalnumber, e->artist_id, e->album_id);
if (sqlite3_exec(db, sql, callback, &id, &err) != SQLITE_OK)
{
logcmd(LOG_ERROR, "db: db_check_track: SQL ERROR: %i", err);
}
free(sql);
free(err);
if (id >= 0)
return -2; // exists already in the database
return 0;
}
int db_add_artist (sqlite3 *db, struct entry *e)
{
// add the artist to the database
logcmd(LOG_DMSG, "adding artist: %s", e->artist);
int artist_id;
if ((artist_id = db_check_artist(db, e)))
{
logcmd(LOG_DMSG, "artist exists");
e->artist_id = artist_id;
return 0;
}
else
{
logcmd(LOG_DMSG, "artist does noe exist yet: %s", e->artist);
char *err = NULL;
int size = snprintf(NULL, 0, INSERT_ARTIST, e->artist);
char *sql = malloc(sizeof(char) * size);
if (! sql)
logcmd(LOG_ERROR_MALLOC, "db: db_add_artist: unable to allocate memory for sql");
snprintf(sql, size, INSERT_ARTIST, e->artist);
if (sqlite3_exec(db, sql, NULL, 0, &err) != SQLITE_OK)
logcmd(LOG_ERROR, "SQL ERROR: %i", err);
free(sql);
free(err);
e->artist_id = sqlite3_last_insert_rowid(db);
return 1;
}
return 0;
}
int db_add_album (sqlite3 *db, struct entry *e)
{
// add the album to the database
logcmd(LOG_DMSG, "adding album: %s", e->album);
int album_id;
if ((album_id = db_check_album(db, e)))
{
logcmd(LOG_DMSG, "album exists");
e->album_id = album_id;
return 0;
}
else
{
logcmd(LOG_DMSG, "album does not exist yet: %s", e->album);
char *err = NULL;
int size = snprintf(NULL, 0, INSERT_ALBUM, e->album, e->artist_id);
char *sql = malloc(sizeof(char) * size);
if (! sql)
logcmd(LOG_ERROR_MALLOC, "db: db_add_album: unable to allocate memory for sql");
snprintf(sql, size, INSERT_ALBUM, e->album, e->artist_id);
if (sqlite3_exec(db, sql, NULL, 0, &err) != SQLITE_OK)
logcmd(LOG_ERROR, "SQL ERROR: %i", err);
free(sql);
free(err);
e->album_id = sqlite3_last_insert_rowid(db);
return 1;
}
return 0;
}
int db_add_track (sqlite3 *db, struct entry *e)
{
// add the track to the database
logcmd(LOG_DMSG, "adding track: %s", e->title);
int id;
if (db_check_track(db, e) == -2)
{
logcmd(LOG_MSG, "title does already exist: %s", e->title);
//return -2;
return 0;
}
else
{
logcmd(LOG_DMSG, "title does not exist yet: %s", e->title);
char *err = NULL;
int size = snprintf(NULL, 0, INSERT_TRACK, e->title, e->number, e->totalnumber, e->artist_id, e->album_id, e->file);
char *sql = malloc(sizeof(char) * size);
if (! sql)
logcmd(LOG_ERROR_MALLOC, "db: db_add_track: unable to allocate memory for sql");
snprintf(sql, size, INSERT_TRACK, e->title, e->number, e->totalnumber, e->artist_id, e->album_id, e->file);
if (sqlite3_exec(db, sql, NULL, 0, &err) != SQLITE_OK)
logcmd(LOG_ERROR, "SQL ERROR: %i", err);
free(sql);
free(err);
//return sqlite3_last_insert_rowid(db);
return 1;
}
return 0;
}
int db_add_file (sqlite3 *db, const char *file)
{
// add a file to the database
logcmd(LOG_DMSG, "db: db_add_file: adding a file into the database");
struct entry *e = db_load_entry_from_file(file);
if (! e)
return 0; // file was not loaded
db_add_artist(db, e);
db_add_album(db, e);
db_add_track(db, e);
if (e->artist)
free(e->artist);
if (e->album)
free(e->album);
if (e->title)
free(e->title);
if (e->file)
free(e->file);
free(e);
return 1; // file was added to the database
}
char *load_entry_string (const char *value)
{
//logcmd(LOG_DMSG, "tag->value: %s", value);
// copys the content of "value" into a new string
char *ptr = malloc(sizeof(char) * (strlen(value) + 1));
strcpy(ptr, value);
return ptr;
}
struct entry *db_load_entry_from_file (const char *file)
{
// loads the metadata from the file into the struct
logcmd(LOG_DMSG, "loading file: %s", file);
struct entry *e = malloc(sizeof(struct entry));
if (! e)
logcmd(LOG_ERROR_MALLOC, "db: db_load_entry_from_file: unable to allocate memory for e");
// open the file
AVFormatContext *fctx = NULL;
fctx = avformat_alloc_context();
if (! fctx)
logcmd(LOG_ERROR_MALLOC, "db: db_load_entry_from_file: could not allocate memory for fctx");
if (avformat_open_input(&fctx, file, NULL, NULL) < 0)
{
logcmd(LOG_MSG, "db: db_load_entry_from_file: could not open file (file not added): %s", file);
return NULL;
}
if (avformat_find_stream_info(fctx, NULL) < 0)
{
logcmd(LOG_MSG, "db: db_load_entry_from_file: could not find file info (file not added): %s", file);
return NULL;
}
// open the metadata
AVDictionary *m;
if (! (m = fctx->metadata))
m = fctx->streams[0]->metadata;
if (! m)
{
logcmd(LOG_MSG, "db: db_load_entry_from_file: no metadata found (file not added");
return NULL;
}
// initialize the entry struct
e->artist = NULL;
e->album = NULL;
e->title = NULL;
e->totalnumber = 0;
e->totalnumber = 0;
e->file = NULL;
// loading the metadata into the struct entry
AVDictionaryEntry *tag;
if (tag = av_dict_get(m, "ARTIST", NULL, 0))
e->artist = load_entry_string(tag->value);
else if (tag = av_dict_get(m, "album_artist", NULL, 0))
e->artist = load_entry_string(tag->value);
//sometimes the tag 'artist' does not exist use 'album_artist' instead
if (tag = av_dict_get(m, "ALBUM", NULL, 0)) // load artist
e->album = load_entry_string(tag->value);
if (tag = av_dict_get(m, "TITLE", NULL, 0)) // load album
e->title = load_entry_string(tag->value);
if (tag = av_dict_get(m, "track", NULL, 0)) // load track number
e->number = atoi(tag->value);
if (tag = av_dict_get(m, "TRACKTOTAL", NULL, 0)) // load total number of tracks
e->totalnumber = atoi(tag->value);
e->file = load_entry_string(file); // store path to the file
// debugging output
logcmd(LOG_DMSG, "\nArtist: %s\nAlbum: %s\nTitle: %s\nPath: %s", e->artist, e->album, e->title, e->file);
avformat_close_input(&fctx);
return e;
}
void db_close (sqlite3* db)
{
sqlite3_close(db);
}