obexd: Split internal obc_transfer_register()

The creation process has been internally split into two steps: creation
and D-Bus registration. This is easier to understand and it also allows
to expose these two-steps in the transfer API.
This commit is contained in:
Mikel Astiz 2012-05-04 14:39:34 +02:00 committed by Marcel Holtmann
parent cc648c10fe
commit ec2a58fdae

View File

@ -243,29 +243,35 @@ static void obc_transfer_free(struct obc_transfer *transfer)
g_free(transfer); g_free(transfer);
} }
static struct obc_transfer *obc_transfer_register(DBusConnection *conn, static struct obc_transfer *obc_transfer_create(guint8 op,
const char *agent,
guint8 op,
const char *filename, const char *filename,
const char *name, const char *name,
const char *type, const char *type)
GError **err)
{ {
struct obc_transfer *transfer; struct obc_transfer *transfer;
transfer = g_new0(struct obc_transfer, 1); transfer = g_new0(struct obc_transfer, 1);
transfer->op = op; transfer->op = op;
transfer->agent = g_strdup(agent);
transfer->filename = g_strdup(filename); transfer->filename = g_strdup(filename);
transfer->name = g_strdup(name); transfer->name = g_strdup(name);
transfer->type = g_strdup(type); transfer->type = g_strdup(type);
return transfer;
}
static gboolean obc_transfer_register(struct obc_transfer *transfer,
DBusConnection *conn,
const char *agent,
GError **err)
{
/* for OBEX specific mime types we don't need to register a transfer */ /* for OBEX specific mime types we don't need to register a transfer */
if (type != NULL && if (transfer->type != NULL &&
(strncmp(type, "x-obex/", 7) == 0 || (strncmp(transfer->type, "x-obex/", 7) == 0 ||
strncmp(type, "x-bt/", 5) == 0)) strncmp(transfer->type, "x-bt/", 5) == 0))
goto done; goto done;
transfer->agent = g_strdup(agent);
transfer->path = g_strdup_printf("%s/transfer%ju", transfer->path = g_strdup_printf("%s/transfer%ju",
TRANSFER_BASEPATH, counter++); TRANSFER_BASEPATH, counter++);
@ -273,7 +279,7 @@ static struct obc_transfer *obc_transfer_register(DBusConnection *conn,
if (transfer->conn == NULL) { if (transfer->conn == NULL) {
g_set_error(err, OBC_TRANSFER_ERROR, -EFAULT, g_set_error(err, OBC_TRANSFER_ERROR, -EFAULT,
"Unable to connect to D-Bus"); "Unable to connect to D-Bus");
goto fail; return FALSE;
} }
if (g_dbus_register_interface(transfer->conn, transfer->path, if (g_dbus_register_interface(transfer->conn, transfer->path,
@ -282,18 +288,13 @@ static struct obc_transfer *obc_transfer_register(DBusConnection *conn,
transfer, NULL) == FALSE) { transfer, NULL) == FALSE) {
g_set_error(err, OBC_TRANSFER_ERROR, -EFAULT, g_set_error(err, OBC_TRANSFER_ERROR, -EFAULT,
"Unable to register to D-Bus"); "Unable to register to D-Bus");
goto fail; return FALSE;
} }
done: done:
DBG("%p registered %s", transfer, transfer->path); DBG("%p registered %s", transfer, transfer->path);
return transfer; return TRUE;
fail:
obc_transfer_free(transfer);
return NULL;
} }
static gboolean transfer_open(struct obc_transfer *transfer, int flags, static gboolean transfer_open(struct obc_transfer *transfer, int flags,
@ -336,10 +337,12 @@ struct obc_transfer *obc_transfer_get(DBusConnection *conn,
struct obc_transfer *transfer; struct obc_transfer *transfer;
int perr; int perr;
transfer = obc_transfer_register(conn, agent, G_OBEX_OP_GET, filename, transfer = obc_transfer_create(G_OBEX_OP_GET, filename, name, type);
name, type, err);
if (transfer == NULL) if (!obc_transfer_register(transfer, conn, agent, err)) {
obc_transfer_free(transfer);
return NULL; return NULL;
}
perr = transfer_open(transfer, O_WRONLY | O_CREAT | O_TRUNC, 0600, err); perr = transfer_open(transfer, O_WRONLY | O_CREAT | O_TRUNC, 0600, err);
if (perr < 0) { if (perr < 0) {
@ -366,10 +369,12 @@ struct obc_transfer *obc_transfer_put(DBusConnection *conn,
struct stat st; struct stat st;
int perr; int perr;
transfer = obc_transfer_register(conn, agent, G_OBEX_OP_PUT, filename, transfer = obc_transfer_create(G_OBEX_OP_PUT, filename, name, type);
name, type, err);
if (transfer == NULL) if (!obc_transfer_register(transfer, conn, agent, err)) {
obc_transfer_free(transfer);
return NULL; return NULL;
}
if (contents != NULL) { if (contents != NULL) {
ssize_t w; ssize_t w;