storage: Add function to remove a service directory
authorJukka Rissanen <jukka.rissanen@linux.intel.com>
Tue, 5 Jun 2012 08:24:03 +0000 (11:24 +0300)
committerPatrik Flykt <patrik.flykt@linux.intel.com>
Mon, 11 Jun 2012 10:01:42 +0000 (13:01 +0300)
All known files from service directory are removed and
if successfull then the service directory is also removed.

src/connman.h
src/storage.c

index bda4f7f..388e6fb 100644 (file)
@@ -214,6 +214,7 @@ int __connman_storage_save_service(GKeyFile *keyfile, const char *ident);
 GKeyFile *__connman_storage_load_provider(const char *identifier);
 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier);
 char **__connman_storage_get_providers(void);
+gboolean __connman_storage_remove_service(const char *service_id);
 
 int __connman_detect_init(void);
 void __connman_detect_cleanup(void);
index b93554d..47bd0cb 100644 (file)
@@ -266,6 +266,69 @@ int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
        return ret;
 }
 
+static gboolean remove_file(const char *service_id, const char *file)
+{
+       gchar *pathname;
+       gboolean ret = FALSE;
+
+       pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
+       if(pathname == NULL)
+               return FALSE;
+
+       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
+               ret = TRUE;
+       } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
+               unlink(pathname);
+               ret = TRUE;
+       }
+
+       g_free(pathname);
+       return ret;
+}
+
+static gboolean remove_dir(const char *service_id)
+{
+       gchar *pathname;
+       gboolean ret = FALSE;
+
+       pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
+       if(pathname == NULL)
+               return FALSE;
+
+       if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
+               ret = TRUE;
+       } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
+               rmdir(pathname);
+               ret = TRUE;
+       }
+
+       g_free(pathname);
+       return ret;
+}
+
+gboolean __connman_storage_remove_service(const char *service_id)
+{
+       gboolean removed;
+
+       /* Remove service configuration file */
+       removed = remove_file(service_id, SETTINGS);
+       if (removed == FALSE)
+               return FALSE;
+
+       /* Remove the statistics file also */
+       removed = remove_file(service_id, "data");
+       if (removed == FALSE)
+               return FALSE;
+
+       removed = remove_dir(service_id);
+       if (removed == FALSE)
+               return FALSE;
+
+       DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
+
+       return TRUE;
+}
+
 GKeyFile *__connman_storage_load_provider(const char *identifier)
 {
        gchar *pathname;