client: Remove buffer based transfer
Luiz Augusto von Dentz
maintainerPATCH v3 1/1 client: Remove buffer based transfer
From: Luiz Augusto von Dentz <[email protected]>
Simplify the code by using temporary files and eliminates reallocations.
---
v3: - Uses g_file_open_tmp which properly checks for tmp location
- Reuse code for both PUT and GET
- Add parameters contents and size to obc_transfer_set_file
- Remove unrelated/inconsistent changes
- Replace g_malloc0 with g_malloc
client/ftp.c | 8 +- client/manager.c | 18 +++- client/map.c | 18 +++- client/pbap.c | 39 +++++-- client/session.c | 44 +++++---- client/session.h | 7 +- client/sync.c | 36 +++++-- client/transfer.c | 304 ++++++++++++++++++++++------------------------------- client/transfer.h | 8 +- 9 files changed, 241 insertions(+), 241 deletions(-)
client/ftp.c +4 -4
@@ -196,13 +196,12 @@ static void list_folder_callback(struct obc_session *session,GMarkupParseContext *ctxt;DBusMessage *reply;DBusMessageIter iter, array; -const char *buf; +char *contents;size_t size; reply =dbus_message_new_method_return (msg); - buf =obc_session_get_buffer (session, &size); -if (size ==0 ) +if (obc_session_get_contents (session, &contents, &size) <0 ) goto done;dbus_message_iter_init_append (reply, &iter);
@@ -212,9 +211,10 @@ static void list_folder_callback(struct obc_session *session, DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array); ctxt =g_markup_parse_context_new (&parser,0 , &array,NULL ); -g_markup_parse_context_parse (ctxt, buf,strlen (buf) -1 ,NULL ); +g_markup_parse_context_parse (ctxt, contents, size,NULL );g_markup_parse_context_free (ctxt);dbus_message_iter_close_container (&iter, &array); +g_free (contents); done:g_dbus_send_message (conn, reply);
client/manager.c +13 -5
@@ -442,8 +442,9 @@ static void capabilities_complete_callback(struct obc_session *session, GError *err, void *user_data) {struct send_data *data = user_data; -const char *capabilities; +char *contents;size_t size; +int perr;if (err !=NULL ) { DBusMessage *error = g_dbus_create_error(data->message,
@@ -453,13 +454,20 @@ static void capabilities_complete_callback(struct obc_session *session, goto done; } - capabilities =obc_session_get_buffer (session, &size); -if (size ==0 ) - capabilities ="" ; + perr =obc_session_get_contents (session, &contents, &size); +if (perr <0 ) { +DBusMessage *error =g_dbus_create_error (data->message, +"org.openobex.Error.Failed" , +"Error reading contents: %s" , +strerror (-perr)); +g_dbus_send_message (data->connection, error); + goto done; + }g_dbus_send_reply (data->connection, data->message, -DBUS_TYPE_STRING , &capabilities, +DBUS_TYPE_STRING , &contents,DBUS_TYPE_INVALID ); +g_free (contents); done:
client/map.c +13 -5
@@ -25,6 +25,7 @@#endif #include <errno.h> +#include <string.h> #include <glib.h> #include <gdbus.h>
@@ -98,8 +99,9 @@ static void buffer_cb(struct obc_session *session, GError *err, {struct map_data *map = user_data;DBusMessage *reply; -const char *buf; +char *contents;size_t size; +int perr;if (err !=NULL ) { reply = g_dbus_create_error(map->msg,
@@ -108,13 +110,19 @@ static void buffer_cb(struct obc_session *session, GError *err, goto done; } - buf =obc_session_get_buffer (session, &size); -if (size ==0 ) - buf ="" ; + perr =obc_session_get_contents (session, &contents, &size); +if (perr <0 ) { + reply =g_dbus_create_error (map->msg, +"org.openobex.Error.Failed" , +"Error reading contents: %s" , +strerror (-perr)); + goto done; + } - reply =g_dbus_create_reply (map->msg,DBUS_TYPE_STRING , &buf, + reply =g_dbus_create_reply (map->msg,DBUS_TYPE_STRING , &contents,DBUS_TYPE_INVALID ); +g_free (contents); done:g_dbus_send_message (conn, reply);dbus_message_unref (map->msg);
client/pbap.c +27 -12
@@ -343,8 +343,9 @@ static void pull_phonebook_callback(struct obc_session *session, {struct pending_request *request = user_data;DBusMessage *reply; -const char *buf; +char *contents;size_t size; +int perr;if (err) { reply = g_dbus_create_error(request->msg,
@@ -353,16 +354,23 @@ static void pull_phonebook_callback(struct obc_session *session, goto send; } - reply =dbus_message_new_method_return (request->msg); + perr =obc_session_get_contents (session, &contents, &size); +if (perr <0 ) { + reply =g_dbus_create_error (request->msg, +"org.openobex.Error.Failed" , +"Error reading contents: %s" , +strerror (-perr)); + goto send; + } - buf =obc_session_get_buffer (session, &size); -if (size ==0 ) - buf ="" ; + reply =dbus_message_new_method_return (request->msg);dbus_message_append_args (reply, -DBUS_TYPE_STRING , &buf, +DBUS_TYPE_STRING , &contents,DBUS_TYPE_INVALID ); +g_free (contents); + send:g_dbus_send_message (conn, reply);pending_request_free (request);
@@ -403,8 +411,9 @@ static void pull_vcard_listing_callback(struct obc_session *session,GMarkupParseContext *ctxt;DBusMessage *reply;DBusMessageIter iter, array; -const char *buf; +char *contents;size_t size; +int perr;if (err) { reply = g_dbus_create_error(request->msg,
@@ -413,11 +422,16 @@ static void pull_vcard_listing_callback(struct obc_session *session, goto send; } - reply =dbus_message_new_method_return (request->msg); + perr =obc_session_get_contents (session, &contents, &size); +if (perr <0 ) { + reply =g_dbus_create_error (request->msg, +"org.openobex.Error.Failed" , +"Error reading contents: %s" , +strerror (-perr)); + goto send; + } - buf =obc_session_get_buffer (session, &size); -if (size ==0 ) - buf ="" ; + reply =dbus_message_new_method_return (request->msg);dbus_message_iter_init_append (reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
@@ -425,9 +439,10 @@ static void pull_vcard_listing_callback(struct obc_session *session, DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_STRUCT_END_CHAR_AS_STRING, &array); ctxt =g_markup_parse_context_new (&listing_parser,0 , &array,NULL ); -g_markup_parse_context_parse (ctxt, buf,strlen (buf) -1 ,NULL ); +g_markup_parse_context_parse (ctxt, contents, size,NULL );g_markup_parse_context_free (ctxt);dbus_message_iter_close_container (&iter, &array); +g_free (contents); send:g_dbus_send_message (conn, reply);
client/session.c +25 -19
92 lines not on this page · raw message
client/session.h +4 -3
@@ -52,7 +52,8 @@ const char *obc_session_get_agent(struct obc_session *session);const char *obc_session_get_path (struct obc_session *session);const char *obc_session_get_target (struct obc_session *session); -const char *obc_session_get_buffer (struct obc_session *session,size_t *size); +int obc_session_get_contents (struct obc_session *session,char **contents, +size_t *size);void *obc_session_get_params (struct obc_session *session,size_t *size); int obc_session_send(struct obc_session *session, const char *filename,
@@ -66,8 +67,8 @@ int obc_session_pull(struct obc_session *session, session_callback_t function, void *user_data);const char *obc_session_register (struct obc_session *session,GDBusDestroyFunction destroy); -int obc_session_put (struct obc_session *session,char *buf, -const char *name); +int obc_session_put (struct obc_session *session,const char *contents, +size_t size,const char *name); guint obc_session_setpath(struct obc_session *session, const char *path, session_callback_t func, void *user_data,
client/sync.c +25 -11
@@ -27,6 +27,7 @@#endif #include <errno.h> +#include <string.h> #include <glib.h> #include <gdbus.h>
@@ -87,19 +88,34 @@ static void sync_getphonebook_callback(struct obc_session *session, {struct sync_data *sync = user_data;DBusMessage *reply; -const char *buf; +char *contents;size_t size; +int perr; + +if (err) { + reply =g_dbus_create_error (sync->msg, +"org.openobex.Error.Failed" , +"%s" , err->message); + goto send; + } + + perr =obc_session_get_contents (session, &contents, &size); +if (perr <0 ) { + reply =g_dbus_create_error (sync->msg, +"org.openobex.Error.Failed" , +"Error reading contents: %s" , +strerror (-perr)); + goto send; + } reply =dbus_message_new_method_return (sync->msg); - buf =obc_session_get_buffer (session, &size); -if (buf ==NULL ) - buf ="" ; +dbus_message_append_args (reply,DBUS_TYPE_STRING , &contents, +DBUS_TYPE_INVALID ); -dbus_message_append_args (reply, -DBUS_TYPE_STRING , &buf, -DBUS_TYPE_INVALID ); +g_free (contents); +send:g_dbus_send_message (conn, reply);dbus_message_unref (sync->msg); sync->msg =NULL ;
@@ -133,7 +149,6 @@ static DBusMessage *sync_putphonebook(DBusConnection *connection, {struct sync_data *sync = user_data;const char *buf; -char *buffer; if (dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &buf,
@@ -145,9 +160,8 @@ static DBusMessage *sync_putphonebook(DBusConnection *connection,if (!sync->phonebook_path) sync->phonebook_path =g_strdup ("telecom/pb.vcf" ); - buffer =g_strdup (buf); - -if (obc_session_put (sync->session, buffer, sync->phonebook_path) <0 ) +if (obc_session_put (sync->session, buf,strlen (buf), + sync->phonebook_path) <0 )return g_dbus_create_error (message,ERROR_INF ".Failed" ,"Failed" );
client/transfer.c +126 -178
460 lines not on this page · raw message
client/transfer.h +4 -4
@@ -51,13 +51,13 @@ int obc_transfer_put(struct obc_transfer *transfer);int obc_transfer_get_params (struct obc_transfer *transfer,struct obc_transfer_params *params); -const char *obc_transfer_get_buffer (struct obc_transfer *transfer,size_t *size); -void obc_transfer_set_buffer (struct obc_transfer *transfer,char *buffer); -void obc_transfer_clear_buffer (struct obc_transfer *transfer); +int obc_transfer_get_contents (struct obc_transfer *transfer,char **contents, +size_t *size);void obc_transfer_set_name (struct obc_transfer *transfer,const char *name);void obc_transfer_set_filename (struct obc_transfer *transfer,const char *filename);const char *obc_transfer_get_path (struct obc_transfer *transfer);gint64 obc_transfer_get_size (struct obc_transfer *transfer); -int obc_transfer_set_file (struct obc_transfer *transfer); +int obc_transfer_set_file (struct obc_transfer *transfer,const char *contents, +size_t size);
Signature · 1 line
1.7.7.6
Hi Luiz,
<snip>
Review
Quoted 2 lines
(quoted diff) +1
-1- g_markup_parse_context_parse(ctxt, buf, strlen(buf) - 1, NULL); + g_markup_parse_context_parse(ctxt, contents, size, NULL);
I still think the removal of "-1" should be done is a separate patch, but ok it might not very important.
<snip>
Quoted 11 lines
client/session.c +1
-4@@ -1075,15 +1077,23 @@ int obc_session_put(struct obc_session *session, char *buf, const char *name) agent,NULL , name,NULL , NULL); -if (transfer ==NULL ) { -g_free (buf); +if (transfer ==NULL )return -EIO ; - } -obc_transfer_set_buffer (transfer, buf);
client/session.c int obc_session_put(struct obc_session *session, char *buf, const char *name) · line 1090
Quoted 10 lines
client/session.c +7
-2+ err =obc_transfer_set_file (transfer, contents, size); +if (err <0 ) + goto fail; -return session_request (session, transfer, session_prepare_put, -NULL ,NULL ); + err =session_request (session, transfer, session_prepare_put,NULL , +NULL ); +if (err <0 ) + goto fail;
The error-handling here is wrong because session_request() already calls obc_transfer_unregister() in case of error.
<snip>
client/transfer.c struct obc_transfer { · line 64
Quoted 5 lines
client/transfer.c +1
-0@@ -61,13 +61,11 @@ struct obc_transfer {char *agent;/* Transfer agent */ char *path;/* Transfer path */ gchar *filename;/* Transfer file location */ +char *tmpname;/* Transfer temporary location */
I think we should avoid having one more name here. If we need to know if the file needs to be removed, a bool would be enough. And we can probably make it even without that flag, with an early call to remove() while the file is still open.
<snip>
client/transfer.c gboolean obc_transfer_set_callback(struct obc_transfer *transfer, · line 431
Quoted 2 lines
client/transfer.c +1
-1-int obc_transfer_get (struct obc_transfer *transfer) +static int transfer_open (struct obc_transfer *transfer,int flags,mode_t mode)
Can we perhaps rename it to transfer_open_file() instead?
Quoted 18 lines
client/transfer.c +1
-14{GError *err =NULL ; -GObexPacket *req; -GObexDataConsumer data_cb; -GObexFunc complete_cb; -GObexResponseFunc rsp_cb =NULL ; - -if (transfer->xfer !=0 ) -return -EALREADY ; +int fd; -if (transfer->type !=NULL && - (strncmp (transfer->type,"x-obex/" ,7 ) ==0 || -strncmp (transfer->type,"x-bt/" ,5 ) ==0 )) { - rsp_cb = get_buf_xfer_progress; - }else { -int fd =open (transfer->filename ? : transfer->name, -O_WRONLY |O_CREAT ,0600 );
client/transfer.c gboolean obc_transfer_set_callback(struct obc_transfer *transfer, · line 442
Quoted 10 lines
client/transfer.c +3
-3+if (transfer->filename !=NULL ) { + fd =open (transfer->filename, flags, mode);if (fd <0 ) {error ("open(): %s(%d)" ,strerror (errno), errno);return -errno; } - transfer->fd = fd; - data_cb = get_xfer_progress; - complete_cb = xfer_complete; + goto done;
An empty line is missing before the goto, right?
Quoted 13 lines
client/transfer.c +11
-0+ } + + fd =g_file_open_tmp ("obex-clientXXXXXX" , &transfer->tmpname, &err); +if (fd <0 ) { +error ("g_file_open_tmp(): %s" , err->message); +return -EFAULT ; } +done: + transfer->fd = fd; +return fd; +} +
client/transfer.c gboolean obc_transfer_set_callback(struct obc_transfer *transfer, · line 465
Quoted 10 lines
client/transfer.c +10
-0+int obc_transfer_get (struct obc_transfer *transfer) +{ +GError *err =NULL ; +GObexPacket *req; +int perr; + +if (transfer->xfer !=0 ) +return -EALREADY ; + + perr =transfer_open (transfer,O_WRONLY |O_CREAT ,0600 );
I think we need O_TRUNC here, but that would also require a separate patch to fix it in the original version.
<snip>
client/transfer.c gint64 obc_transfer_get_size(struct obc_transfer *transfer) · line 597
Quoted 3 lines
client/transfer.c +2
-1-int obc_transfer_set_file (struct obc_transfer *transfer) +int obc_transfer_set_file (struct obc_transfer *transfer,const char *contents, +size_t size)
Minor nitpicking: shouldn't we use the <boolean, GError**> style for this kind of functions?
I think we said we would start using it for the new APIs, so maybe it's time to do so progressively.
Quoted 5 lines
{
client/transfer.c +1
-1-int fd; +int err;struct stat st;
client/transfer.c gint64 obc_transfer_get_size(struct obc_transfer *transfer) · line 607
Quoted 10 lines
client/transfer.c +6
-4- fd =open (transfer->filename,O_RDONLY ); -if (fd <0 ) { -error ("open(): %s(%d)" ,strerror (errno), errno); -return -errno; + err =transfer_open (transfer,O_RDONLY ,0 ); +if (err <0 ) +return err; + +if (contents !=NULL ) { +ssize_t w =write (transfer->fd, contents, size);
This is not going to work because of O_RDONLY. And you can't use WRONLY either because this function is used for put operations.
In general, I think is not a good idea to get obc_transfer_set_file() involved in this patch. This function should disappear once we open the files during creation.
Using obc_transfer_set_contents() would be better, as you initially suggested. But first it would be convenient to have the "v1 08/11:
client: open transfer file during creation" patch, which as was posted in the mailing list depends on several other patches in that series, but if you agree with this idea I can send the relevant ones again, specially to avoid patch 07/11 which now wouldn't make sense.
Cheers,
Mikel
Luiz Augusto von Dentz
maintainerHi Mikel,
Quoted 9 lines · Mikel Astiz
Quoted 6 lines
+ err =
transfer_open(transfer, O_RDONLY, 0);
+ if (err < 0)
+ return err;
+
+ if (contents != NULL) {
+ssize_tw =write(transfer->fd, contents, size);This is not going to work because of
O_RDONLY. And you can't use WRONLY either because this function is used for put operations.
While this is not logically correct, it works because it normally will call g_file_open_tmp which does open the file with connect mode, so the mode is ignored.
Quoted 10 lines
In general, I think is not a good idea to get
obc_transfer_set_file()involved in this patch. This function should disappear once we open the files during creation.Using
obc_transfer_set_contents()would be better, as you initially suggested. But first it would be convenient to have the "v1 08/11:
client: open transfer file during creation" patch, which as was posted in the mailing list depends on several other patches in that series, but if you agree with this idea I can send the relevant ones again, specially to avoid patch 07/11 which now wouldn't make sense.
Im working on integrate your patches on top of this one.
Signature · 1 line
Luiz Augusto von Dentz