storage: Add services getter
authorJukka Rissanen <jukka.rissanen@linux.intel.com>
Tue, 13 Sep 2011 06:55:11 +0000 (09:55 +0300)
committerSamuel Ortiz <sameo@linux.intel.com>
Tue, 13 Sep 2011 08:49:08 +0000 (10:49 +0200)
Makefile.am
include/storage.h [new file with mode: 0644]
src/storage.c

index b384991..9b1b374 100644 (file)
@@ -6,7 +6,8 @@ includedir = @includedir@/connman
 include_HEADERS = include/types.h include/log.h include/plugin.h \
                        include/notifier.h include/service.h \
                        include/resolver.h include/ipconfig.h \
-                       include/device.h include/network.h include/inet.h
+                       include/device.h include/network.h include/inet.h \
+                       include/storage.h
 
 nodist_include_HEADERS = include/version.h
 
diff --git a/include/storage.h b/include/storage.h
new file mode 100644 (file)
index 0000000..cd05c60
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2011  Intel Corporation. All rights reserved.
+ *
+ *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __CONNMAN_STORAGE_H
+#define __CONNMAN_STORAGE_H
+
+#include <glib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+gchar **connman_storage_get_services();
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CONNMAN_STORAGE_H */
index 4e30a77..1755650 100644 (file)
@@ -26,6 +26,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <dirent.h>
+
+#include <connman/storage.h>
 
 #include "connman.h"
 
@@ -183,6 +186,57 @@ GKeyFile *__connman_storage_open_service(const char *service_id)
        return keyfile;
 }
 
+gchar **connman_storage_get_services()
+{
+       struct dirent *d;
+       gchar *str;
+       DIR *dir;
+       GString *result;
+       gchar **services = NULL;
+       struct stat buf;
+       int ret;
+
+       dir = opendir(STORAGEDIR);
+       if (dir == NULL)
+               return NULL;
+
+       result = g_string_new(NULL);
+
+       while ((d = readdir(dir))) {
+               if (strcmp(d->d_name, ".") == 0 ||
+                               strcmp(d->d_name, "..") == 0)
+                       continue;
+
+               switch (d->d_type) {
+               case DT_DIR:
+                       /*
+                        * If the settings file is not found, then
+                        * assume this directory is not a services dir.
+                        */
+                       str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
+                                                               d->d_name);
+                       ret = stat(str, &buf);
+                       g_free(str);
+                       if (ret < 0)
+                               continue;
+
+                       g_string_append_printf(result, "%s/", d->d_name);
+                       break;
+               }
+       }
+
+       closedir(dir);
+
+       str = g_string_free(result, FALSE);
+       if (str) {
+               str[strlen(str) - 1] = '\0';
+               services = g_strsplit(str, "/", -1);
+       }
+       g_free(str);
+
+       return services;
+}
+
 GKeyFile *__connman_storage_load_service(const char *service_id)
 {
        gchar *pathname;