summaryrefslogtreecommitdiff
path: root/src/mp3_subr.c
blob: 546bbdd0d80e1edab627679ddb298e8953709589 (plain)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* Miscellaneous subroutines for mp3fs. */
#define FUSE_USE_VERSION 26
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>

#include <sys/types.h>
#include <sys/param.h>
#include <dirent.h>
#include <fuse.h>

#include <tag_c.h>
#include <mp3fs.h>
#include <sqlite3.h>
#include <debug.h>

struct lookuphandle {
	sqlite3 *handle;
	sqlite3_stmt *st;
	const char *query;
	int field;
	int count;
	void *priv;
	lookup_fn_t *lookup;
};

sqlite3 *handle;

int
mp3_initscan(char *musicpath)
{
	int error;

	/* Open database. */
	error = sqlite3_open(DBNAME, &handle);
	if (error) {
		warnx("Can't open database: %s\n", sqlite3_errmsg(handle));
		sqlite3_close(handle);
		return (-1);
	}
	traverse_hierarchy(musicpath, mp3_scan);
	sqlite3_close(handle);
	return (0);
}

/*
 * Traverse a hierarchy given a directory path. Perform fileoperations on the
 * files in the directory giving data as arguments, as well as recursing on
 * sub-directories.
 */
void
traverse_hierarchy(char *dirpath, traverse_fn_t fileop)
{
	DIR *dirp;
	struct dirent *dp;
	char filepath[MAXPATHLEN];
	struct stat st;

	dirp = opendir(dirpath);
	if (dirp == NULL)
		return;
	
	/* Loop through all files. */
	while ((dp = readdir(dirp)) != NULL) {
		if (!strcmp(dp->d_name, ".") ||
		    !strcmp(dp->d_name, ".."))
			continue;
		
		snprintf(filepath, sizeof(filepath), "%s/%s",
		    dirpath, dp->d_name);
		if (stat(filepath, &st) < 0)
			err(1, "error doing stat on %s", filepath);
		/* Recurse if it's a directory. */
		if (st.st_mode & S_IFDIR)  {
			traverse_hierarchy(filepath, fileop);
			continue;
		}
		/* If it's a regular file, perform the operation. */
		if (st.st_mode & S_IFREG) {
			fileop(filepath);
		}
	}
	closedir(dirp);
}

/* Scan the music initially. */
void
mp3_scan(char *filepath)
{
	TagLib_File *file;
	TagLib_Tag *tag;
	char *artist, *album, *genre, *title;
	const char *extension;
	int ret;
	unsigned int year;
	sqlite3_stmt *st;
	struct stat fstat;
	
	file = taglib_file_new(filepath);
	/* XXX: errmsg. */
	if (file == NULL)
		return;
	tag = taglib_file_tag(file);
	if (tag == NULL) {
		printf("error!\n");
		return;
	}

	if (stat(filepath, &fstat) < 0) {
		perror("stat");
		return;
	}

	/* XXX: The main query code should perhaps be a bit generalized. */

	/* First insert artist if we have it. */
	do {
		artist = taglib_tag_artist(tag);
		if (artist == NULL)
			break;
		/* First find out if it exists. */
		ret = sqlite3_prepare_v2(handle, "SELECT * FROM artist WHERE "
		    "name=?", -1, &st, NULL);
		if (ret != SQLITE_OK) {
			warnx("Error preparing statement: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
		sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC);
		ret = sqlite3_step(st);
		sqlite3_finalize(st);
		/* Already exists or generic error. */
		if (ret != SQLITE_DONE)
			break;
		/* Doesn't exist, so we can insert it. */
		ret = sqlite3_prepare_v2(handle, "INSERT INTO artist(name) "
		    "VALUES(?)", -1, &st, NULL);
		if (ret != SQLITE_OK) {
			warnx("Error preparing statement: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
		sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC);
		ret = sqlite3_step(st);
		sqlite3_finalize(st);
		if (ret != SQLITE_DONE) {
			warnx("Error inserting into database: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
	} while (0);

	/* Insert genre if it doesn't exist. */
	do {
		genre = taglib_tag_genre(tag);
		if (genre == NULL)
			break;
		/* First find out if it exists. */
		ret = sqlite3_prepare_v2(handle, "SELECT * FROM genre WHERE "
		    "name=?", -1, &st, NULL);
		if (ret != SQLITE_OK) {
			warnx("Error preparing statement: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
		sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC);
		ret = sqlite3_step(st);
		sqlite3_finalize(st);
		/* Already exists or generic error. */
		if (ret != SQLITE_DONE)
			break;
		/* Doesn't exist, so we can insert it. */
		ret = sqlite3_prepare_v2(handle, "INSERT INTO genre(name) "
		    "VALUES(?)", -1, &st, NULL);
		if (ret != SQLITE_OK) {
			warnx("Error preparing statement: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
		sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC);
		ret = sqlite3_step(st);
		sqlite3_finalize(st);
		if (ret != SQLITE_DONE) {
			warnx("Error inserting into database: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
	} while (0);

	/* Finally, insert song. */
	do {
		title = taglib_tag_title(tag);
		album = taglib_tag_album(tag);
		year = taglib_tag_year(tag);
		extension = strrchr(filepath, (int)'.');
		if (extension == NULL)
			extension = "";
		else
			/* Drop the '.' */
			extension++;
		
		if (title == NULL || genre == NULL || artist == NULL ||
		    album == NULL)
			break;

		/* First find out if it exists. */
		ret = sqlite3_prepare_v2(handle, "SELECT * FROM song, artist "
		    " WHERE artist.name=song.artistname AND title=? AND year=?",
		    -1, &st, NULL);
		if (ret != SQLITE_OK) {
			warnx("Error preparing statement: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
		sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC);
		sqlite3_bind_int(st, 2, year);
		ret = sqlite3_step(st);
		sqlite3_finalize(st);
		if (ret == SQLITE_ROW)
			/* Already exists. XXX: Update it if mtime mismatches */
			break;
		if (ret != SQLITE_DONE)
			/* SQL Error. */
			break;
		/* Now, finally insert it. */
		ret = sqlite3_prepare_v2(handle, "INSERT INTO song(title, "
		    "artistname, album, genrename, year, filepath, mtime, "
		    "extension) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
		    -1, &st, NULL);
		if (ret != SQLITE_OK) {
			warnx("Error preparing insert statement: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
		sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC);
		sqlite3_bind_text(st, 2, artist, -1, SQLITE_STATIC);
		sqlite3_bind_text(st, 3, album, -1, SQLITE_STATIC);
		sqlite3_bind_text(st, 4, genre, -1, SQLITE_STATIC);
		sqlite3_bind_int(st, 5, year);
		sqlite3_bind_text(st, 6, filepath, -1, SQLITE_STATIC);
		sqlite3_bind_int(st, 7, fstat.st_mtime);
		sqlite3_bind_text(st, 8, extension, -1, SQLITE_STATIC);
		ret = sqlite3_step(st);
		sqlite3_finalize(st);
		if (ret != SQLITE_DONE) {
			warnx("Error inserting into database: %s\n",
			    sqlite3_errmsg(handle));
			break;
		}
	} while (0);
	taglib_tag_free_strings();
	taglib_file_free(file);
}

/*
 * Create a handle for listing music with a certain query. Allocate the
 * resources and return the handle.
 */
struct lookuphandle *
mp3_lookup_start(int field, void *data, lookup_fn_t *fn, const char *query)
{
	struct lookuphandle *lh;
	int ret, error;

	lh = malloc(sizeof(*lh));
	if (lh == NULL)
		return (NULL);
	lh->field = field;
	lh->lookup = fn;
	lh->priv = data;
	/* Open database. */
	error = sqlite3_open(DBNAME, &lh->handle);
	if (error) {
		warnx("Can't open database: %s\n", sqlite3_errmsg(lh->handle));
		sqlite3_close(lh->handle);
		free(lh);
		return (NULL);
	}
	ret = sqlite3_prepare_v2(lh->handle, query, -1, &lh->st, NULL);
	if (ret != SQLITE_OK) {
		free(lh);
		return (NULL);
	}
	lh->query = query;
	lh->count = 1;
	return (lh);
}

/*
 * Insert data that should be searched for in the list. The data is assumed to
 * be dynamically allocated, and will be free'd when mp3_lookup_finish is called!
 */
void
mp3_lookup_insert(struct lookuphandle *lh, void *data, int type)
{
	char *str;
	int val;

	switch (type) {
	case LIST_DATATYPE_STRING:
		str = (char *)data;
		sqlite3_bind_text(lh->st, lh->count++, str, -1, free);
		break;
	case LIST_DATATYPE_INT:
		val = *((int *)data);
		sqlite3_bind_int(lh->st, lh->count++, val);
		break;
	}
}

/*
 * Finish a statement buildup and use the lookup function to operate on the
 * returned data. Free the handle when done.
 */
void
mp3_lookup_finish(struct lookuphandle *lh)
{
	char buf[1024];
	const unsigned char *value;
	int ret, type, val, finished;

	if (lh == NULL)
		return;
	ret = sqlite3_step(lh->st);
	while (ret == SQLITE_ROW) {
		type = sqlite3_column_type(lh->st, lh->field);
		switch (type) {
		case SQLITE_INTEGER:
			val = sqlite3_column_int(lh->st, lh->field);
			snprintf(buf, sizeof(buf), "%d", val);
			finished = lh->lookup(lh->priv, (const char *)buf);
			break;
		case SQLITE_TEXT:
			value = sqlite3_column_text(lh->st, lh->field);
			finished = lh->lookup(lh->priv, (const char *)value);
			break;
		default:
			finished = 0;
			break;
//		default:
//			lh->filler(lh->buf, "UNKNOWN TYPE", NULL, 0);
		}
		if (finished)
			break;
		ret = sqlite3_step(lh->st);
	}
	// XXX: Check for errors too.
	sqlite3_finalize(lh->st);
	sqlite3_close(lh->handle);
	free(lh);
}

/*
 * Build a file_data struct for a path.
 *
 * data should be a pointer to a file handle
 * returns 0 on success
 */
int
mp3_file_data_for_path(const char *path, void *data) {
	struct file_data *fd;
	fd = (struct file_data *)data;
	struct lookuphandle *lh;
	char *artist, *album, *title;

	lh = NULL;
	fd->fd = -1;
	fd->found = 0;

	/* Open a specific track. */
	if (strncmp(path, "/Tracks", 7) == 0) {
		switch (mp3_numtoken(path)) {
		case 2:
			title = mp3_gettoken(path, 2);
			if (title == NULL)
				break;
			lh = mp3_lookup_start(0, fd, mp3_lookup_open,
			    "SELECT filepath FROM song WHERE title LIKE ?");
			if (lh == NULL)
				return (-EIO);
			mp3_lookup_insert(lh, title, LIST_DATATYPE_STRING);
			break;
		default:
			return (-ENOENT);
		}
	} else if (strncmp(path, "/Albums", 7) == 0) {
		switch (mp3_numtoken(path)) {
		case 3:
			album = mp3_gettoken(path, 2);
			if (album == NULL)
				break;
			title = mp3_gettoken(path, 3);
			if (title == NULL)
				break;
			lh = mp3_lookup_start(0, fd, mp3_lookup_open,
			    "SELECT filepath FROM song WHERE title LIKE ? AND "
			    "album LIKE ?");
			if (lh == NULL)
				return (-EIO);
			mp3_lookup_insert(lh, title, LIST_DATATYPE_STRING);
			mp3_lookup_insert(lh, album, LIST_DATATYPE_STRING);
			break;
		default:
			return (-ENOENT);
		}
	} else if (strncmp(path, "/Artists", 8) == 0) {
		switch (mp3_numtoken(path)) {
		case 4:
			artist = mp3_gettoken(path, 2);
			album  = mp3_gettoken(path, 3);
			title  = mp3_gettoken(path, 4);
			DEBUG("artist(%s) album(%s) title(%s)\n", artist, album, title);
			if (!(artist && album && title)) {
				break;
			}
			lh = mp3_lookup_start(0, fd, mp3_lookup_open,
			    "SELECT filepath FROM song WHERE artistname LIKE ? AND "
			    "album LIKE ? AND title LIKE ?");
			if (lh == NULL)
			    return (-EIO);
			mp3_lookup_insert(lh, artist, LIST_DATATYPE_STRING);
			mp3_lookup_insert(lh, album, LIST_DATATYPE_STRING);
			mp3_lookup_insert(lh, title, LIST_DATATYPE_STRING);
			break;
		default:
			return (-ENOENT);
		}
	}

	if (lh) {
		mp3_lookup_finish(lh);
	}

	return 0;
}

/*
 * Count number of tokens in pathname.
 * XXX: should we strip the first and last?
 */
int
mp3_numtoken(const char *str)
{
	const char *ptr;
	int num;

	num = 0;
	ptr = str;
	if (*ptr == '/')
		ptr++;
	while ((*ptr != '\0')) {
		if (*ptr++ == '/')
			num++;
	}
	if (*(ptr - 1) == '/')
		num--;
	return (num + 1);
}

/*
 * Extract token number toknum and return it in escaped form.
 */
char *
mp3_gettoken(const char *str, int toknum)
{
	const char *ptr, *start, *end;
	char *ret, *dest;
	int numescape;
	int i;
	size_t size;

	start = str;
	/* Find the token start. */
	for (i = 0; i < toknum; i++) {
		start = strchr(start, '/');
		if (start == NULL)
			return (NULL);
		start++;
	}
	/* Find the end pointer. */
	end = strchr(start, '/');
	if (end == NULL)
		end = strchr(start, '\0');
	/* 
	 * Count how many bytes extra to take into account because of escaping.
	 */
	numescape = 0;
	ptr = start;
	while ((ptr = strchr(ptr, '\'')) != NULL && ptr < end) {
		ptr++;
		numescape++;
	}
	size = (end - start) + numescape;
	ret = malloc(size + 1);
	if (ret == NULL)
		return (ret);
	ptr = start;
	dest = ret;
	while (ptr < end) {
		if (*ptr == '\'')
			*dest++ = '\\';
		*dest++ = *ptr++;
	}
	ret[size] = '\0';
	return (ret);
}

/*
 * List album given a path.
 */
void
mp3_lookup_album(const char *path, struct filler_data *fd)
{
	struct lookuphandle *lh;
	char *album;

	switch (mp3_numtoken(path)) {
	case 1:
		lh = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT DISTINCT album FROM song");
		break;
	case 2:
		/* So, now we got to find out the artist and list its albums. */
		album = mp3_gettoken(path, 2);
		if (album == NULL)
			break;
		lh  = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT DISTINCT title FROM song WHERE album LIKE ?");
		mp3_lookup_insert(lh, album, LIST_DATATYPE_STRING);
		break;
	}
	mp3_lookup_finish(lh);
}

/*
 * List artist given a path.
 */
void
mp3_lookup_artist(const char *path, struct filler_data *fd)
{
	struct lookuphandle *lh;
	char *name, *album;

	switch (mp3_numtoken(path)) {
	case 1:
		lh = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT name FROM artist");
		break;
	case 2:
		/* So, now we got to find out the artist and list its albums. */
		name = mp3_gettoken(path, 2);
		if (name == NULL)
			break;
		lh  = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT DISTINCT album FROM song, "
		    "artist WHERE song.artistname = artist.name AND artist.name"
		    " LIKE ?");
		mp3_lookup_insert(lh, name, LIST_DATATYPE_STRING);
		break;
	case 3:
		/* List songs in an album. */
		name = mp3_gettoken(path, 2);
		if (name == NULL)
			break;
		album = mp3_gettoken(path, 3);
		if (album == NULL)
			break;
		lh = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT title FROM song, artist "
		    "WHERE song.artistname = artist.name AND artist.name "
		    "LIKE ? AND song.album LIKE ?");
		mp3_lookup_insert(lh, name, LIST_DATATYPE_STRING);
		mp3_lookup_insert(lh, album, LIST_DATATYPE_STRING);
		break;
	}
	mp3_lookup_finish(lh);
}

/*
 * Looks up tracks given a genre, or all genres.
 */
void
mp3_lookup_genre(const char *path, struct filler_data *fd)
{
	struct lookuphandle *lh;
	char *genre, *album;

	switch (mp3_numtoken(path)) {
	case 1:
		lh = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT name FROM genre");
		break;
	case 2:
		genre = mp3_gettoken(path, 2);
		if (genre == NULL)
			break;
		lh = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT DISTINCT album FROM song, "
		    "genre WHERE song.genrename = genre.name AND genre.name "
		    "LIKE ?");
		mp3_lookup_insert(lh, genre, LIST_DATATYPE_STRING);
		break;
	case 3:
		genre = mp3_gettoken(path, 2);
		if (genre == NULL)
			break;
		album = mp3_gettoken(path, 3);
		if (album == NULL)
			break;
		lh = mp3_lookup_start(0, fd, mp3_lookup_list,
		    "SELECT title FROM song, genre WHERE"
		    " song.genrename = genre.name AND genre.name LIKE ? "
		    " AND song.album LIKE ?");
		mp3_lookup_insert(lh, genre, LIST_DATATYPE_STRING);
		mp3_lookup_insert(lh, album, LIST_DATATYPE_STRING);
		break;
	}
	mp3_lookup_finish(lh);
}

/*
 * Lookup function for filling given data into a filler.
 */
int
mp3_lookup_list(void *data, const char *str)
{
	struct filler_data *fd;

	fd = (struct filler_data *)data;
	fd->filler(fd->buf, str, NULL, 0);
	return (0);
}

/*
 * Lookup a file and open it if found.
 */
int
mp3_lookup_open(void *data, const char *str)
{
	struct file_data *fd;

	fd = (struct file_data *)data;
	fd->fd = open(str, O_RDONLY);
	fd->found = 1;
	return (1);
}

/*
 * Stat a file.
 * XXX: watch out for duplicates, we might stat more than once.
 */
int
mp3_lookup_stat(void *data, const char *str)
{
	struct stat *st;

	st = (struct stat *)data;
	if (stat(str, st) < 0)
		return (0);
	return (1);
}

/*
 * Guess on a filetype for a path.
 *
 * Examples:
 *
 * - /Artists/Whoever should be a directory, since it's an
 *   Album.
 *
 * - /Tracks/Whatever should be a file, since Whatever is a song
 *
 * Note: This should only be used for paths inside the mp3fs
 * directories (Artists, Tracks, ...)
 */
enum mp3_filetype
mp3_get_filetype(const char *path) {
	int tokens = mp3_numtoken(path);

	if (strncmp(path, "/Genres", 7) == 0 ||
	    strncmp(path, "/Artists", 8) == 0) {
		switch (tokens) {
		case 1:
		case 2:
		case 3:
			return (MP3_DIRECTORY);
		case 4:
			return (MP3_FILE);
		default:
			return (MP3_NOTFOUND);
		}
	} else if (strncmp(path, "/Albums", 7) == 0) {
		switch (tokens) {
		case 1:
		case 2:
			return (MP3_DIRECTORY);
		case 3:
			return (MP3_FILE);
		default:
			return (MP3_NOTFOUND);
		}
	} else if (strncmp(path, "/Tracks", 7) == 0) {
		switch (tokens) {
		case 1:
			return (MP3_DIRECTORY);
		case 2:
			return (MP3_FILE);
		default:
			return (MP3_NOTFOUND);
		}
	} else {
		return (MP3_NOTFOUND);
	}
}