X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=backend%2Fgio%2Fgio.c;h=33a8ff91713ea226753fd720181d85dbe1d938f1;hb=5e0b46512db1307562af23db7ea1a1c5f6655472;hp=5ac4550a8077ee45d2adf136df12fce644fc1c9a;hpb=76db3f66324dd66faae215944f1296db7cb9de59;p=cascardo%2Fatompub.git diff --git a/backend/gio/gio.c b/backend/gio/gio.c index 5ac4550..33a8ff9 100644 --- a/backend/gio/gio.c +++ b/backend/gio/gio.c @@ -20,11 +20,14 @@ #include #include #include +#include +#include -GFile *gio_iri_to_file (AtomCtx *ctx, IRI *iri) +static GFile * +gio_id_to_file (AtomCtx *ctx, AtomID *id) { gchar *root = atom_config_get_str (ctx, "gio", "root"); - gchar *path = iri_get_path (iri); + gchar *path = atom_id_string (id); gchar *filename = g_build_filename (root, path, NULL); GFile *file = g_file_new_for_path (filename); g_free (root); @@ -32,23 +35,104 @@ GFile *gio_iri_to_file (AtomCtx *ctx, IRI *iri) return file; } -Atom * gio_atom_retrieve_resource (AtomCtx *ctx, IRI *iri) +static AtomEntry * +gio_file_to_atom (AtomCtx *ctx, GFile *file) { - GFile *file; GError *error = NULL; gchar *data; gsize len; - Atom *atom; - file = gio_iri_to_file (ctx, iri); + AtomEntry *atom; error = NULL; if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error)) { - g_object_unref (file); - atom_error_set (ctx, error); + AtomError *aerr = atom_error_new_from_gerror (error); + atom_error_set (ctx, aerr); + g_error_free (error); return NULL; } - g_object_unref (channel); - atom = atom_new_data_len (data, len); + atom = atom_entry_new_data_len (data, len); g_free (data); return atom; } + +static AtomEntry * +gio_atom_retrieve_entry (AtomCtx *ctx, AtomID *id) +{ + GFile *file; + AtomEntry *atom; + file = gio_id_to_file (ctx, id); + atom = gio_file_to_atom (ctx, file); + g_object_unref (file); + return atom; +} + +static void +gio_enumerate_entries (AtomCtx *ctx, char ***reqs, AtomEntry ***entries, + size_t *len) +{ + GFile *dir; + GFileEnumerator *enumerator; + GFileInfo *info; + GFile *file; + AtomEntry *entry; + GError *error; + gchar *root; + gchar *name; + gchar *filename; + GPtrArray *array; + GPtrArray *filenames; + root = atom_config_get_str (ctx, "gio", "root"); + dir = g_file_new_for_path (root); + error = NULL; + enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, + G_FILE_QUERY_INFO_NONE, NULL, &error); + if (enumerator == NULL) + { + AtomError *aerr = atom_error_new_from_gerror (error); + atom_error_set (ctx, aerr); + g_error_free (error); + return; + } + array = g_ptr_array_new (); + filenames = g_ptr_array_new (); + while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) + { + name = g_file_info_get_name (info); + filename = g_build_filename (root, name, NULL); + file = g_file_new_for_path (filename); + entry = gio_file_to_atom (ctx, file); + if (entry) + { + g_ptr_array_add (array, entry); + g_ptr_array_add (filenames, g_strdup (name)); + } + else + { + atom_error_set (ctx, NULL); + } + g_object_unref (file); + g_free (filename); + g_object_unref (info); + } + g_object_unref (enumerator); + g_object_unref (dir); + g_free (root); + if (reqs) + *reqs = filenames->pdata; + if (entries) + *entries = array->pdata; + if (len) + *len = array->len; + g_ptr_array_free (array, FALSE); + g_ptr_array_free (filenames, FALSE); +} + +AtomBackend * +gio_backend (void) +{ + AtomBackend *backend; + backend = atom_backend_new (); + atom_backend_retrieve_entry_set (backend, gio_atom_retrieve_entry); + atom_backend_enumerate_entries_set (backend, gio_enumerate_entries); + return backend; +}