From 4132366bf60c142aa77dd89280bb9f8a7e3b27b6 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Sat, 26 Feb 2011 16:49:41 -0300 Subject: [PATCH] obexd: Move PC Suite drivers to its own plugin This makes it easier to enable/disable this functionality as a whole. --- obexd/plugins/ftp.c | 165 +------------- obexd/plugins/ftp.h | 30 +++ obexd/plugins/{nokia-backup.c => pcsuite.c} | 234 ++++++++++++++++++-- 3 files changed, 259 insertions(+), 170 deletions(-) create mode 100644 obexd/plugins/ftp.h rename obexd/plugins/{nokia-backup.c => pcsuite.c} (56%) diff --git a/obexd/plugins/ftp.c b/obexd/plugins/ftp.c index 633abf3f4..79223bf4c 100644 --- a/obexd/plugins/ftp.c +++ b/obexd/plugins/ftp.c @@ -50,6 +50,7 @@ #include "dbus.h" #include "mimetype.h" #include "service.h" +#include "ftp.h" #define LST_TYPE "x-obex/folder-listing" #define CAP_TYPE "x-obex/capability" @@ -189,7 +190,7 @@ static int get_by_type(struct ftp_session *ftp, const char *type) return err; } -static void *ftp_connect(struct obex_session *os, int *err) +void *ftp_connect(struct obex_session *os, int *err) { struct ftp_session *ftp; const char *root_folder; @@ -212,8 +213,8 @@ static void *ftp_connect(struct obex_session *os, int *err) return ftp; } -static int ftp_get(struct obex_session *os, obex_object_t *obj, - gboolean *stream, void *user_data) +int ftp_get(struct obex_session *os, obex_object_t *obj, gboolean *stream, + void *user_data) { struct ftp_session *ftp = user_data; const char *type = obex_get_type(os); @@ -254,7 +255,7 @@ static int ftp_delete(struct ftp_session *ftp, const char *name) return ret; } -static int ftp_chkput(struct obex_session *os, void *user_data) +int ftp_chkput(struct obex_session *os, void *user_data) { struct ftp_session *ftp = user_data; const char *name = obex_get_name(os); @@ -278,8 +279,7 @@ static int ftp_chkput(struct obex_session *os, void *user_data) return ret; } -static int ftp_put(struct obex_session *os, obex_object_t *obj, - void *user_data) +int ftp_put(struct obex_session *os, obex_object_t *obj, void *user_data) { struct ftp_session *ftp = user_data; const char *name = obex_get_name(os); @@ -299,8 +299,7 @@ static int ftp_put(struct obex_session *os, obex_object_t *obj, return 0; } -static int ftp_setpath(struct obex_session *os, obex_object_t *obj, - void *user_data) +int ftp_setpath(struct obex_session *os, obex_object_t *obj, void *user_data) { struct ftp_session *ftp = user_data; const char *root_folder, *name; @@ -404,7 +403,7 @@ done: return err; } -static void ftp_disconnect(struct obex_session *os, void *user_data) +void ftp_disconnect(struct obex_session *os, void *user_data) { struct ftp_session *ftp = user_data; @@ -416,145 +415,6 @@ static void ftp_disconnect(struct obex_session *os, void *user_data) g_free(ftp); } -static void *pcsuite_connect(struct obex_session *os, int *err) -{ - struct pcsuite_session *pcsuite; - struct ftp_session *ftp; - int fd; - char *filename; - - DBG(""); - - ftp = ftp_connect(os, err); - if (ftp == NULL) - return NULL; - - filename = g_build_filename(g_get_home_dir(), ".pcsuite", NULL); - - fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644); - if (fd < 0 && errno != EEXIST) { - error("open(%s): %s(%d)", filename, strerror(errno), errno); - goto fail; - } - - /* Try to remove the file before retrying since it could be - that some process left/crash without removing it */ - if (fd < 0) { - if (remove(filename) < 0) { - error("remove(%s): %s(%d)", filename, strerror(errno), - errno); - goto fail; - } - - fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644); - if (fd < 0) { - error("open(%s): %s(%d)", filename, strerror(errno), - errno); - goto fail; - } - } - - DBG("%s created", filename); - - pcsuite = g_new0(struct pcsuite_session, 1); - pcsuite->ftp = ftp; - pcsuite->lock_file = filename; - pcsuite->fd = fd; - - DBG("session %p created", pcsuite); - - if (err) - *err = 0; - - return pcsuite; - -fail: - if (ftp) - ftp_disconnect(os, ftp); - if (err) - *err = -errno; - - g_free(filename); - - return NULL; -} - -static int pcsuite_get(struct obex_session *os, obex_object_t *obj, - gboolean *stream, void *user_data) -{ - struct pcsuite_session *pcsuite = user_data; - - DBG("%p", pcsuite); - - return ftp_get(os, obj, stream, pcsuite->ftp); -} - -static int pcsuite_chkput(struct obex_session *os, void *user_data) -{ - struct pcsuite_session *pcsuite = user_data; - - DBG("%p", pcsuite); - - return ftp_chkput(os, pcsuite->ftp); -} - -static int pcsuite_put(struct obex_session *os, obex_object_t *obj, - void *user_data) -{ - struct pcsuite_session *pcsuite = user_data; - - DBG("%p", pcsuite); - - return ftp_put(os, obj, pcsuite->ftp); -} - -static int pcsuite_setpath(struct obex_session *os, obex_object_t *obj, - void *user_data) -{ - struct pcsuite_session *pcsuite = user_data; - - DBG("%p", pcsuite); - - return ftp_setpath(os, obj, pcsuite->ftp); -} - -static void pcsuite_disconnect(struct obex_session *os, void *user_data) -{ - struct pcsuite_session *pcsuite = user_data; - - DBG("%p", pcsuite); - - if (pcsuite->fd >= 0) - close(pcsuite->fd); - - if (pcsuite->lock_file) { - remove(pcsuite->lock_file); - g_free(pcsuite->lock_file); - } - - if (pcsuite->ftp) - ftp_disconnect(os, pcsuite->ftp); - - g_free(pcsuite); -} - -static struct obex_service_driver pcsuite = { - .name = "Nokia OBEX PC Suite Services", - .service = OBEX_PCSUITE, - .channel = PCSUITE_CHANNEL, - .record = PCSUITE_RECORD, - .target = FTP_TARGET, - .target_size = TARGET_SIZE, - .who = PCSUITE_WHO, - .who_size = PCSUITE_WHO_SIZE, - .connect = pcsuite_connect, - .get = pcsuite_get, - .put = pcsuite_put, - .chkput = pcsuite_chkput, - .setpath = pcsuite_setpath, - .disconnect = pcsuite_disconnect -}; - static struct obex_service_driver ftp = { .name = "File Transfer server", .service = OBEX_FTP, @@ -572,19 +432,12 @@ static struct obex_service_driver ftp = { static int ftp_init(void) { - int err; - - err = obex_service_driver_register(&ftp); - if (err < 0) - return err; - - return obex_service_driver_register(&pcsuite); + return obex_service_driver_register(&ftp); } static void ftp_exit(void) { obex_service_driver_unregister(&ftp); - obex_service_driver_unregister(&pcsuite); } OBEX_PLUGIN_DEFINE(ftp, ftp_init, ftp_exit) diff --git a/obexd/plugins/ftp.h b/obexd/plugins/ftp.h new file mode 100644 index 000000000..23741255a --- /dev/null +++ b/obexd/plugins/ftp.h @@ -0,0 +1,30 @@ +/* + * + * OBEX Server + * + * Copyright (C) 2007-2010 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +void *ftp_connect(struct obex_session *os, int *err); +int ftp_get(struct obex_session *os, obex_object_t *obj, gboolean *stream, + void *user_data); +int ftp_chkput(struct obex_session *os, void *user_data); +int ftp_put(struct obex_session *os, obex_object_t *obj, void *user_data); +int ftp_setpath(struct obex_session *os, obex_object_t *obj, void *user_data); +void ftp_disconnect(struct obex_session *os, void *user_data); diff --git a/obexd/plugins/nokia-backup.c b/obexd/plugins/pcsuite.c similarity index 56% rename from obexd/plugins/nokia-backup.c rename to obexd/plugins/pcsuite.c index cf17e1659..9cf65c99d 100644 --- a/obexd/plugins/nokia-backup.c +++ b/obexd/plugins/pcsuite.c @@ -2,8 +2,8 @@ * * OBEX Server * - * Copyright (C) 2010 Nokia Corporation - * Copyright (C) 2010 Marcel Holtmann + * Copyright (C) 2007-2010 Nokia Corporation + * Copyright (C) 2007-2010 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify @@ -26,31 +26,78 @@ #include #endif +#include #include #include #include #include -#include #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include "gdbus.h" - #include #include #include "plugin.h" #include "log.h" #include "obex.h" +#include "dbus.h" #include "mimetype.h" #include "service.h" +#include "ftp.h" + +#define PCSUITE_CHANNEL 24 +#define PCSUITE_WHO_SIZE 8 + +#define PCSUITE_RECORD " \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +" #define BACKUP_BUS_NAME "com.nokia.backup.plugin" #define BACKUP_PATH "/com/nokia/backup" @@ -58,8 +105,156 @@ #define BACKUP_DBUS_TIMEOUT (1000 * 60 * 15) static const uint8_t FTP_TARGET[TARGET_SIZE] = { - 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2, - 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 }; + 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2, + 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 }; + +static const uint8_t PCSUITE_WHO[PCSUITE_WHO_SIZE] = { + 'P','C',' ','S','u','i','t','e' }; + +struct pcsuite_session { + struct ftp_session *ftp; + char *lock_file; + int fd; +}; + +static void *pcsuite_connect(struct obex_session *os, int *err) +{ + struct pcsuite_session *pcsuite; + struct ftp_session *ftp; + int fd; + char *filename; + + DBG(""); + + ftp = ftp_connect(os, err); + if (ftp == NULL) + return NULL; + + filename = g_build_filename(g_get_home_dir(), ".pcsuite", NULL); + + fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644); + if (fd < 0 && errno != EEXIST) { + error("open(%s): %s(%d)", filename, strerror(errno), errno); + goto fail; + } + + /* Try to remove the file before retrying since it could be + that some process left/crash without removing it */ + if (fd < 0) { + if (remove(filename) < 0) { + error("remove(%s): %s(%d)", filename, strerror(errno), + errno); + goto fail; + } + + fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644); + if (fd < 0) { + error("open(%s): %s(%d)", filename, strerror(errno), + errno); + goto fail; + } + } + + DBG("%s created", filename); + + pcsuite = g_new0(struct pcsuite_session, 1); + pcsuite->ftp = ftp; + pcsuite->lock_file = filename; + pcsuite->fd = fd; + + DBG("session %p created", pcsuite); + + if (err) + *err = 0; + + return pcsuite; + +fail: + if (ftp) + ftp_disconnect(os, ftp); + if (err) + *err = -errno; + + g_free(filename); + + return NULL; +} + +static int pcsuite_get(struct obex_session *os, obex_object_t *obj, + gboolean *stream, void *user_data) +{ + struct pcsuite_session *pcsuite = user_data; + + DBG("%p", pcsuite); + + return ftp_get(os, obj, stream, pcsuite->ftp); +} + +static int pcsuite_chkput(struct obex_session *os, void *user_data) +{ + struct pcsuite_session *pcsuite = user_data; + + DBG("%p", pcsuite); + + return ftp_chkput(os, pcsuite->ftp); +} + +static int pcsuite_put(struct obex_session *os, obex_object_t *obj, + void *user_data) +{ + struct pcsuite_session *pcsuite = user_data; + + DBG("%p", pcsuite); + + return ftp_put(os, obj, pcsuite->ftp); +} + +static int pcsuite_setpath(struct obex_session *os, obex_object_t *obj, + void *user_data) +{ + struct pcsuite_session *pcsuite = user_data; + + DBG("%p", pcsuite); + + return ftp_setpath(os, obj, pcsuite->ftp); +} + +static void pcsuite_disconnect(struct obex_session *os, void *user_data) +{ + struct pcsuite_session *pcsuite = user_data; + + DBG("%p", pcsuite); + + if (pcsuite->fd >= 0) + close(pcsuite->fd); + + if (pcsuite->lock_file) { + remove(pcsuite->lock_file); + g_free(pcsuite->lock_file); + } + + if (pcsuite->ftp) + ftp_disconnect(os, pcsuite->ftp); + + g_free(pcsuite); +} + +static struct obex_service_driver pcsuite = { + .name = "Nokia OBEX PC Suite Services", + .service = OBEX_PCSUITE, + .channel = PCSUITE_CHANNEL, + .record = PCSUITE_RECORD, + .target = FTP_TARGET, + .target_size = TARGET_SIZE, + .who = PCSUITE_WHO, + .who_size = PCSUITE_WHO_SIZE, + .connect = pcsuite_connect, + .get = pcsuite_get, + .put = pcsuite_put, + .chkput = pcsuite_chkput, + .setpath = pcsuite_setpath, + .disconnect = pcsuite_disconnect +}; struct backup_object{ gchar *cmd; @@ -296,14 +491,25 @@ static struct obex_mime_type_driver backup = { .flush = backup_flush, }; -static int backup_init(void) +static int pcsuite_init(void) { - return obex_mime_type_driver_register(&backup); + int err; + + err = obex_service_driver_register(&pcsuite); + if (err < 0) + return err; + + err = obex_mime_type_driver_register(&backup); + if (err < 0) + obex_service_driver_unregister(&pcsuite); + + return err; } -static void backup_exit(void) +static void pcsuite_exit(void) { obex_mime_type_driver_unregister(&backup); + obex_service_driver_unregister(&pcsuite); } -OBEX_PLUGIN_DEFINE(backup, backup_init, backup_exit) +OBEX_PLUGIN_DEFINE(pcsuite, pcsuite_init, pcsuite_exit) -- 2.47.3