Add utilities for settings management
authorDenis Kenzior <denkenz@gmail.com>
Fri, 30 Oct 2009 20:22:06 +0000 (15:22 -0500)
committerDenis Kenzior <denkenz@gmail.com>
Fri, 30 Oct 2009 20:22:06 +0000 (15:22 -0500)
src/storage.c
src/storage.h

index 8587424..5fb6b8c 100644 (file)
@@ -124,3 +124,65 @@ ssize_t write_file(const unsigned char *buffer, size_t len, mode_t mode,
        g_free(path);
        return r;
 }
+
+GKeyFile *storage_open(const char *imsi, const char *store)
+{
+       GKeyFile *keyfile;
+       char *path;
+
+       if (store == NULL)
+               return NULL;
+
+       if (imsi)
+               path = g_strdup_printf(STORAGEDIR "/%s/%s", imsi, store);
+       else
+               path = g_strdup_printf(STORAGEDIR "/%s", store);
+
+       keyfile = g_key_file_new();
+
+       if (path) {
+               g_key_file_load_from_file(keyfile, path, 0, NULL);
+               g_free(path);
+       }
+
+       return keyfile;
+}
+
+void storage_sync(const char *imsi, const char *store, GKeyFile *keyfile)
+{
+       char *path;
+       char *data;
+       gsize length = 0;
+
+       if (imsi)
+               path = g_strdup_printf(STORAGEDIR "/%s/%s", imsi, store);
+       else
+               path = g_strdup_printf(STORAGEDIR "/%s", store);
+
+       if (path == NULL)
+               return;
+
+       if (create_dirs(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
+               g_free(path);
+               return;
+       }
+
+       data = g_key_file_to_data(keyfile, &length, NULL);
+
+       g_file_set_contents(path, data, length, NULL);
+
+       g_free(data);
+       g_free(path);
+}
+
+void storage_close(const char *imsi, const char *store, GKeyFile *keyfile,
+                       gboolean save)
+{
+       gchar *pathname, *data = NULL;
+       gsize length = 0;
+
+       if (save == TRUE)
+               storage_sync(imsi, store, keyfile);
+
+       g_key_file_free(keyfile);
+}
index 305c4a5..42707f6 100644 (file)
@@ -34,3 +34,8 @@ ssize_t read_file(unsigned char *buffer, size_t len,
 
 ssize_t write_file(const unsigned char *buffer, size_t len, mode_t mode,
                        const char *path_fmt, ...);
+
+GKeyFile *storage_open(const char *imsi, const char *store);
+void storage_sync(const char *imsi, const char *store, GKeyFile *keyfile);
+void storage_close(const char *imsi, const char *store, GKeyFile *keyfile,
+                       gboolean save);