summaryrefslogtreecommitdiff
path: root/src/mfs_cleanup_db.c
blob: 3ce2ff39f60bb7e0a3e98903568e33770d301fc7 (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
/*
 * Musicfs is a FUSE module implementing a media filesystem in userland.
 * Copyright (C) 2008  Ulf Lilleengen, Kjetil Ørbekk
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * A copy of the license can typically be found in COPYING
 */

#include <sqlite3.h>
#include <pthread.h>

#include <debug.h>
#include <musicfs.h>

extern char *db_path;

/*
 * Clean up the database:
 *
 * - Remove disabled paths
 * - Remove songs in disabled paths
 * - Remove unused artists and genres
 */
void
mfs_cleanup_db(sqlite3 *handle)
{
	DEBUG("cleaning up db\n");
	/* sqlite doesn't support deleting from multiple tables at the same time :-( */
	sqlite3_stmt *st, *st_;
	int res, res_;
	const char *path;

	
	/* Removed songs from inactive paths */
	res = sqlite3_prepare_v2(handle, "SELECT path FROM path WHERE "
	    "active = 0", -1, &st, NULL);
	if (res != SQLITE_OK) {
		DEBUG("Error preparing statement: %s\n",
		    sqlite3_errmsg(handle));
		return;
	}
	res = sqlite3_step(st);
	
	while (res == SQLITE_ROW) {
		path = (const char*)sqlite3_column_text(st, 0);
		
		res_ = sqlite3_prepare_v2(handle, "DELETE FROM song WHERE "
		    "filepath LIKE (?||'%')", -1, &st_, NULL);
		if (res_ != SQLITE_OK) {
			DEBUG("Error preparing statement: %s\n",
			    sqlite3_errmsg(handle));
			return;
		}

		DEBUG("removing songs from '%s'\n", path);
		sqlite3_bind_text(st_, 1, path, -1, SQLITE_TRANSIENT);
		res_ = sqlite3_step(st_);
		if (res_ != SQLITE_DONE) {
			DEBUG("Error deleting '%s' from database: %s\n",
			    path, sqlite3_errmsg(handle));
		}
		sqlite3_finalize(st_);
		
		res = sqlite3_step(st);
	}
	sqlite3_finalize(st);
	
	/* Remove the actual paths */
	res = sqlite3_prepare_v2(handle, "DELETE FROM path WHERE "
	    "active = 0", -1, &st, NULL);
	if (res != SQLITE_OK) {
		DEBUG("Error preparing statement: %s\n",
		    sqlite3_errmsg(handle));
		return;
	}
	res = sqlite3_step(st);
	if (res != SQLITE_DONE) {
		DEBUG("Error deleting inactive paths from db: %s\n",
		    sqlite3_errmsg(handle));
		return;
	}
	sqlite3_finalize(st);
}