* [PATCH 1/2] Add messages backend initialization and finalization @ 2011-05-23 14:01 Slawomir Bochenski 2011-05-23 14:01 ` [PATCH 2/2] Add MAP session management and SetFolder function Slawomir Bochenski 2011-05-25 20:40 ` [PATCH 1/2] Add messages backend initialization and finalization Johan Hedberg 0 siblings, 2 replies; 4+ messages in thread From: Slawomir Bochenski @ 2011-05-23 14:01 UTC (permalink / raw) To: linux-bluetooth; +Cc: Slawomir Bochenski This adds functions for initializing and freeing resources used by message storage access backend and example implementation in the dummy (or rather - filesystem) backend. Dummy backend uses $MAP_ROOT (if set) and falls back to $HOME/map-messages for its message storage. This directory should at least contain basic folders required by the MAP specification. It represents the root as seen from the perspective of MAP client. You can prepare it as follows: $ mkdir -p "$MAP_ROOT/telecom/msg/inbox" $ mkdir "$MAP_ROOT/telecom/msg/sent" $ mkdir "$MAP_ROOT/telecom/msg/deleted" $ mkdir "$MAP_ROOT/telecom/msg/outbox" --- plugins/mas.c | 8 ++++++++ plugins/messages-dummy.c | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 0 deletions(-) diff --git a/plugins/mas.c b/plugins/mas.c index a84b8fd..243d2f6 100644 --- a/plugins/mas.c +++ b/plugins/mas.c @@ -261,6 +261,10 @@ static int mas_init(void) { int err; + err = messages_init(); + if (err < 0) + goto failed_messages_init; + err = obex_mime_type_driver_register(&mime_map); if (err < 0) goto failed_mime; @@ -275,6 +279,9 @@ failed_mas_reg: obex_mime_type_driver_unregister(&mime_map); failed_mime: + messages_exit(); + +failed_messages_init: return err; } @@ -282,6 +289,7 @@ static void mas_exit(void) { obex_service_driver_unregister(&mas); obex_mime_type_driver_unregister(&mime_map); + messages_exit(); } OBEX_PLUGIN_DEFINE(mas, mas_init, mas_exit) diff --git a/plugins/messages-dummy.c b/plugins/messages-dummy.c index a536d81..7b2e1a1 100644 --- a/plugins/messages-dummy.c +++ b/plugins/messages-dummy.c @@ -26,16 +26,38 @@ #endif #include <errno.h> +#include <stdlib.h> #include "messages.h" +static char *root_folder = NULL; + int messages_init(void) { + char *tmp; + + if (root_folder) + return 0; + + tmp = getenv("MAP_ROOT"); + if (tmp) { + root_folder = g_strdup(tmp); + return 0; + } + + tmp = getenv("HOME"); + if (!tmp) + return -1; + + root_folder = g_build_filename(tmp, "map-messages", NULL); + return 0; } void messages_exit(void) { + g_free(root_folder); + root_folder = NULL; } int messages_connect(void **session) -- 1.7.4.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] Add MAP session management and SetFolder function 2011-05-23 14:01 [PATCH 1/2] Add messages backend initialization and finalization Slawomir Bochenski @ 2011-05-23 14:01 ` Slawomir Bochenski 2011-05-25 20:40 ` [PATCH 1/2] Add messages backend initialization and finalization Johan Hedberg 1 sibling, 0 replies; 4+ messages in thread From: Slawomir Bochenski @ 2011-05-23 14:01 UTC (permalink / raw) To: linux-bluetooth; +Cc: Slawomir Bochenski --- plugins/mas.c | 16 ++++++++-- plugins/messages-dummy.c | 69 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/plugins/mas.c b/plugins/mas.c index 243d2f6..8775934 100644 --- a/plugins/mas.c +++ b/plugins/mas.c @@ -88,6 +88,7 @@ struct mas_session { struct mas_request *request; + void *backend_data; }; static const uint8_t MAS_TARGET[TARGET_SIZE] = { @@ -105,13 +106,20 @@ static void *mas_connect(struct obex_session *os, int *err) DBG(""); - *err = 0; - mas = g_new0(struct mas_session, 1); + *err = messages_connect(&mas->backend_data); + if (*err < 0) + goto failed; + manager_register_session(os); return mas; + +failed: + g_free(mas); + + return NULL; } static void mas_disconnect(struct obex_session *os, void *user_data) @@ -121,6 +129,7 @@ static void mas_disconnect(struct obex_session *os, void *user_data) DBG(""); manager_unregister_session(os); + messages_disconnect(mas->backend_data); mas_clean(mas); } @@ -178,6 +187,7 @@ static int mas_setpath(struct obex_session *os, obex_object_t *obj, { const char *name; uint8_t *nonhdr; + struct mas_session *mas = user_data; if (OBEX_ObjectGetNonHdrData(obj, &nonhdr) != 2) { error("Set path failed: flag and constants not found!"); @@ -193,7 +203,7 @@ static int mas_setpath(struct obex_session *os, obex_object_t *obj, return -EBADR; } - return 0; + return messages_set_folder(mas->backend_data, name, nonhdr[0] & 0x01); } static void *any_open(const char *name, int oflag, mode_t mode, diff --git a/plugins/messages-dummy.c b/plugins/messages-dummy.c index 7b2e1a1..3b5c98e 100644 --- a/plugins/messages-dummy.c +++ b/plugins/messages-dummy.c @@ -27,11 +27,17 @@ #include <errno.h> #include <stdlib.h> +#include <string.h> #include "messages.h" static char *root_folder = NULL; +struct session { + char *cwd; + char *cwd_absolute; +}; + int messages_init(void) { char *tmp; @@ -60,14 +66,26 @@ void messages_exit(void) root_folder = NULL; } -int messages_connect(void **session) +int messages_connect(void **s) { - *session = 0; + struct session *session; + + session = g_new0(struct session, 1); + session->cwd = g_strdup(""); + session->cwd_absolute = g_strdup(root_folder); + + *s = session; + return 0; } -void messages_disconnect(void *session) +void messages_disconnect(void *s) { + struct session *session = s; + + g_free(session->cwd); + g_free(session->cwd_absolute); + g_free(session); } int messages_set_notification_registration(void *session, @@ -78,9 +96,50 @@ int messages_set_notification_registration(void *session, return -EINVAL; } -int messages_set_folder(void *session, const char *name, gboolean cdup) +int messages_set_folder(void *s, const char *name, gboolean cdup) { - return -EINVAL; + struct session *session = s; + char *newrel = NULL; + char *newabs; + char *tmp; + + if (name && (strchr(name, '/') || strcmp(name, "..") == 0)) + return -EBADR; + + if (cdup) { + if (session->cwd[0] == 0) + return -ENOENT; + + newrel = g_path_get_dirname(session->cwd); + + /* We use empty string for indication of the root directory */ + if (newrel[0] == '.' && newrel[1] == 0) + newrel[0] = 0; + } + + tmp = newrel; + if (!cdup && (!name || name[0] == 0)) + newrel = g_strdup(""); + else + newrel = g_build_filename(newrel ? newrel : session->cwd, name, + NULL); + g_free(tmp); + + newabs = g_build_filename(root_folder, newrel, NULL); + + if (!g_file_test(newabs, G_FILE_TEST_IS_DIR)) { + g_free(newrel); + g_free(newabs); + return -ENOENT; + } + + g_free(session->cwd); + session->cwd = newrel; + + g_free(session->cwd_absolute); + session->cwd_absolute = newabs; + + return 0; } int messages_get_folder_listing(void *session, -- 1.7.4.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] Add messages backend initialization and finalization 2011-05-23 14:01 [PATCH 1/2] Add messages backend initialization and finalization Slawomir Bochenski 2011-05-23 14:01 ` [PATCH 2/2] Add MAP session management and SetFolder function Slawomir Bochenski @ 2011-05-25 20:40 ` Johan Hedberg 1 sibling, 0 replies; 4+ messages in thread From: Johan Hedberg @ 2011-05-25 20:40 UTC (permalink / raw) To: Slawomir Bochenski; +Cc: linux-bluetooth Hi Slawek, On Mon, May 23, 2011, Slawomir Bochenski wrote: > + err = messages_init(); > + if (err < 0) > + goto failed_messages_init; > + > err = obex_mime_type_driver_register(&mime_map); > if (err < 0) > goto failed_mime; > @@ -275,6 +279,9 @@ failed_mas_reg: > obex_mime_type_driver_unregister(&mime_map); > > failed_mime: > + messages_exit(); > + > +failed_messages_init: > return err; > } In this case it'd be simpler to do the following instead of adding the new label: if (err < 0) return err; If there was more complex cleanup involved then the new label would make sense, but not really with a simple return statement. Johan ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 0/2] Message Access Profile, cont. @ 2011-03-23 13:56 Slawomir Bochenski 2011-03-23 13:56 ` [PATCH 1/2] Add messages backend initialization and finalization Slawomir Bochenski 0 siblings, 1 reply; 4+ messages in thread From: Slawomir Bochenski @ 2011-03-23 13:56 UTC (permalink / raw) To: linux-bluetooth; +Cc: Slawomir Bochenski As Johan insisted, here is another approach. Functions used for accessing message storage are going to be a bit more independent from MAP architecture and so more reusable in other places. There may be some initialization needed before storage is usable. This is introduced in first patch. I was thinking about other ways to do it: 1) Add OBEX_PLUGIN_DEFINE to messages-*.c and so make it a plugin (this is related to what Luiz suggested before). This would however prevent MAP plugin from being able to refuse to load if message storage is not functional. Also it breaks the idea of message storage API being independent from obexd architecture. 2) Add reference counter of plugin users, so only last messages_exit would free common resources (like it is the case with root_folder in messages_dummy.c). This would get harder is someone would wish to use threads at some point. 3) Get rid of static data and return pointer from messages_init() that would be passed to all message storage functions calls. This option still may need some reference counting and data shared between API clients at some point (I'm thinking about implementation of undelete behaviour, required by MAP, when actual storage does not have that possibility). What do you think? ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] Add messages backend initialization and finalization 2011-03-23 13:56 [PATCH 0/2] Message Access Profile, cont Slawomir Bochenski @ 2011-03-23 13:56 ` Slawomir Bochenski 0 siblings, 0 replies; 4+ messages in thread From: Slawomir Bochenski @ 2011-03-23 13:56 UTC (permalink / raw) To: linux-bluetooth; +Cc: Slawomir Bochenski This adds functions for initializing and freeing resources used by message storage access backend and example implementation in the dummy (or rather - filesystem) backend. Dummy backend uses $HOME/map-messages for its message storage. This directory should at least contain basic folders required by the MAP specification. It represents the root as seen from the perspective of MAP client. You can prepare it as follows: $ MAPROOT=$HOME/map-messages $ mkdir -p "$MAPROOT/telecom/msg/inbox" $ mkdir "$MAPROOT/telecom/msg/sent" $ mkdir "$MAPROOT/telecom/msg/deleted" $ mkdir "$MAPROOT/telecom/msg/outbox" --- plugins/mas.c | 8 ++++++++ plugins/messages-dummy.c | 31 +++++++++++++++++++++++++++++++ plugins/messages.h | 4 ++++ 3 files changed, 43 insertions(+), 0 deletions(-) diff --git a/plugins/mas.c b/plugins/mas.c index a84b8fd..243d2f6 100644 --- a/plugins/mas.c +++ b/plugins/mas.c @@ -261,6 +261,10 @@ static int mas_init(void) { int err; + err = messages_init(); + if (err < 0) + goto failed_messages_init; + err = obex_mime_type_driver_register(&mime_map); if (err < 0) goto failed_mime; @@ -275,6 +279,9 @@ failed_mas_reg: obex_mime_type_driver_unregister(&mime_map); failed_mime: + messages_exit(); + +failed_messages_init: return err; } @@ -282,6 +289,7 @@ static void mas_exit(void) { obex_service_driver_unregister(&mas); obex_mime_type_driver_unregister(&mime_map); + messages_exit(); } OBEX_PLUGIN_DEFINE(mas, mas_init, mas_exit) diff --git a/plugins/messages-dummy.c b/plugins/messages-dummy.c index fd9679d..9bb1ba1 100644 --- a/plugins/messages-dummy.c +++ b/plugins/messages-dummy.c @@ -25,4 +25,35 @@ #include <config.h> #endif +#include <glib.h> +#include <stdlib.h> + #include "messages.h" + +static char *root_folder = NULL; + +int messages_init(void) +{ + char *home; + + if (root_folder) + return 0; + + home = getenv("HOME"); + if (!home) + return -1; + + /* XXX: It would be great to have per plugin parameters support, for + * example $ obexd --mas=param1=value1:param2:param3, so user would be + * able to select different root directory. + */ + root_folder = g_build_filename(home, "map-messages", NULL); + + return 0; +} + +void messages_exit(void) +{ + g_free(root_folder); + root_folder = NULL; +} diff --git a/plugins/messages.h b/plugins/messages.h index c510244..65e2274 100644 --- a/plugins/messages.h +++ b/plugins/messages.h @@ -20,3 +20,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */ + +int messages_init(void); + +void messages_exit(void); -- 1.7.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-05-25 20:40 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2011-05-23 14:01 [PATCH 1/2] Add messages backend initialization and finalization Slawomir Bochenski 2011-05-23 14:01 ` [PATCH 2/2] Add MAP session management and SetFolder function Slawomir Bochenski 2011-05-25 20:40 ` [PATCH 1/2] Add messages backend initialization and finalization Johan Hedberg -- strict thread matches above, loose matches on Subject: below -- 2011-03-23 13:56 [PATCH 0/2] Message Access Profile, cont Slawomir Bochenski 2011-03-23 13:56 ` [PATCH 1/2] Add messages backend initialization and finalization Slawomir Bochenski
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox