aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClaudio Takahasi <claudio.takahasi@openbossa.org>2011-11-07 11:00:05 -0300
committerJohan Hedberg <johan.hedberg@intel.com>2011-11-14 20:48:41 +0200
commit13fc666eaa7ce57902405d4cc61c7b1ef58e90bf (patch)
treee87ded9f5b03c5f5746bee80e725401f2a765f49
parenta9e207e5a160e3d0fbfe9fa42beaba8527338031 (diff)
downloadbluez-13fc666eaa7ce57902405d4cc61c7b1ef58e90bf.tar.gz
bluez-13fc666eaa7ce57902405d4cc61c7b1ef58e90bf.tar.xz
bluez-13fc666eaa7ce57902405d4cc61c7b1ef58e90bf.zip
Add generic function to delete by pattern
Remove entries of textfile if the key has the prefix defined by the pattern string.
-rw-r--r--src/storage.c66
1 files changed, 24 insertions, 42 deletions
diff --git a/src/storage.c b/src/storage.c
index e001987a..7471b0f2 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1140,76 +1140,58 @@ int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
static void filter_keys(char *key, char *value, void *data)
{
struct match *match = data;
- const char *address = match->pattern;
- /* Each key contains: MAC#handle*/
- if (strncasecmp(key, address, 17) == 0)
+ if (strncasecmp(key, match->pattern, strlen(match->pattern)) == 0)
match->keys = g_slist_append(match->keys, g_strdup(key));
}
-int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba)
+static void delete_by_pattern(const char *filename, char *pattern)
{
- GSList *l;
struct match match;
- char filename[PATH_MAX + 1], address[18];
+ GSList *l;
int err;
- create_filename(filename, PATH_MAX, sba, "primary");
-
- memset(address, 0, sizeof(address));
- ba2str(dba, address);
-
- err = textfile_del(filename, address);
- if (err < 0)
- return err;
-
- /* Deleting all characteristics of a given address */
memset(&match, 0, sizeof(match));
- match.pattern = address;
+ match.pattern = pattern;
- create_filename(filename, PATH_MAX, sba, "characteristic");
err = textfile_foreach(filename, filter_keys, &match);
if (err < 0)
- return err;
+ goto done;
for (l = match.keys; l; l = l->next) {
const char *key = l->data;
textfile_del(filename, key);
}
+done:
g_slist_free_full(match.keys, g_free);
+}
- /* Deleting all attributes values of a given address */
- memset(&match, 0, sizeof(match));
- match.pattern = address;
+int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba)
+{
+ char filename[PATH_MAX + 1], address[18];
+ int err;
- create_filename(filename, PATH_MAX, sba, "attributes");
- err = textfile_foreach(filename, filter_keys, &match);
+ create_filename(filename, PATH_MAX, sba, "primary");
+
+ memset(address, 0, sizeof(address));
+ ba2str(dba, address);
+
+ err = textfile_del(filename, address);
if (err < 0)
return err;
- for (l = match.keys; l; l = l->next) {
- const char *key = l->data;
- textfile_del(filename, key);
- }
+ /* Deleting all characteristics of a given address */
+ create_filename(filename, PATH_MAX, sba, "characteristic");
+ delete_by_pattern(filename, address);
- g_slist_free_full(match.keys, g_free);
+ /* Deleting all attributes values of a given address */
+ create_filename(filename, PATH_MAX, sba, "attributes");
+ delete_by_pattern(filename, address);
/* Deleting all CCC values of a given address */
- memset(&match, 0, sizeof(match));
- match.pattern = address;
-
create_filename(filename, PATH_MAX, sba, "ccc");
- err = textfile_foreach(filename, filter_keys, &match);
- if (err < 0)
- return err;
-
- for (l = match.keys; l; l = l->next) {
- const char *key = l->data;
- textfile_del(filename, key);
- }
-
- g_slist_free_full(match.keys, g_free);
+ delete_by_pattern(filename, address);
return 0;
}