From 129ca8dfffde4244c3d5c16d4f789ff3b69f6fb9 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Thu, 11 Jun 2009 16:12:17 +0200 Subject: [PATCH] Add watch deletion by path/match/cookie routines --- src/db.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++------- src/db.h | 9 +++++ 2 files changed, 107 insertions(+), 13 deletions(-) diff --git a/src/db.c b/src/db.c index ade4acf8..0c5c42dc 100644 --- a/src/db.c +++ b/src/db.c @@ -2207,22 +2207,12 @@ db_watch_add(struct watch_info *wi) #undef Q_TMPL } -int -db_watch_delete_bywd(struct watch_info *wi) +static int +db_watch_delete_byquery(char *query) { -#define Q_TMPL "DELETE FROM inotify WHERE wd = %d;" - char *query; char *errmsg; int ret; - query = sqlite3_mprintf(Q_TMPL, wi->wd); - if (!query) - { - DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); - - return -1; - } - DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query); errmsg = NULL; @@ -2236,9 +2226,104 @@ db_watch_delete_bywd(struct watch_info *wi) return -1; } + return 0; +} + +int +db_watch_delete_bywd(struct watch_info *wi) +{ +#define Q_TMPL "DELETE FROM inotify WHERE wd = %d;" + char *query; + int ret; + + query = sqlite3_mprintf(Q_TMPL, wi->wd); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); + + return -1; + } + + ret = db_watch_delete_byquery(query); + sqlite3_free(query); - return 0; + return ret; + +#undef Q_TMPL +} + +int +db_watch_delete_bypath(char *path) +{ +#define Q_TMPL "DELETE FROM inotify WHERE path = '%q';" + char *query; + int ret; + + query = sqlite3_mprintf(Q_TMPL, path); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); + + return -1; + } + + ret = db_watch_delete_byquery(query); + + sqlite3_free(query); + + return ret; + +#undef Q_TMPL +} + +int +db_watch_delete_bymatch(char *path) +{ +#define Q_TMPL "DELETE FROM inotify WHERE path LIKE '%q/%%';" + char *query; + int ret; + + query = sqlite3_mprintf(Q_TMPL, path); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); + + return -1; + } + + ret = db_watch_delete_byquery(query); + + sqlite3_free(query); + + return ret; + +#undef Q_TMPL +} + +int +db_watch_delete_bycookie(uint32_t cookie) +{ +#define Q_TMPL "DELETE FROM inotify WHERE cookie = %" PRIi64 ";" + char *query; + int ret; + + if (cookie == 0) + return -1; + + query = sqlite3_mprintf(Q_TMPL, (int64_t)cookie); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); + + return -1; + } + + ret = db_watch_delete_byquery(query); + + sqlite3_free(query); + + return ret; #undef Q_TMPL } diff --git a/src/db.h b/src/db.h index 40c54b41..ddfd1c4a 100644 --- a/src/db.h +++ b/src/db.h @@ -296,6 +296,15 @@ db_watch_add(struct watch_info *wi); int db_watch_delete_bywd(struct watch_info *wi); +int +db_watch_delete_bypath(char *path); + +int +db_watch_delete_bymatch(char *path); + +int +db_watch_delete_bycookie(uint32_t cookie); + int db_watch_get_bywd(struct watch_info *wi);