Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH 1/6] Add support for pcsuite lock file
@ 2011-02-23  9:54 Luiz Augusto von Dentz
  2011-02-23  9:54 ` [PATCH 2/6] Add more debug logs for ftp driver Luiz Augusto von Dentz
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Luiz Augusto von Dentz @ 2011-02-23  9:54 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.dentz-von@nokia.com>

This should make others applications able to detect when pcsuite is
connected.
---
 plugins/ftp.c |  140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 134 insertions(+), 6 deletions(-)

diff --git a/plugins/ftp.c b/plugins/ftp.c
index a952f64..546b58c 100644
--- a/plugins/ftp.c
+++ b/plugins/ftp.c
@@ -150,6 +150,12 @@ struct ftp_session {
 	char *folder;
 };
 
+struct pcsuite_session {
+	struct ftp_session *ftp;
+	char *lock_file;
+	int fd;
+};
+
 static void set_folder(struct ftp_session *ftp, const char *new_folder)
 {
 	g_free(ftp->folder);
@@ -390,6 +396,128 @@ 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,
@@ -399,12 +527,12 @@ static struct obex_service_driver pcsuite = {
 	.target_size = TARGET_SIZE,
 	.who = PCSUITE_WHO,
 	.who_size = PCSUITE_WHO_SIZE,
-	.connect = ftp_connect,
-	.get = ftp_get,
-	.put = ftp_put,
-	.chkput = ftp_chkput,
-	.setpath = ftp_setpath,
-	.disconnect = ftp_disconnect
+	.connect = pcsuite_connect,
+	.get = pcsuite_get,
+	.put = pcsuite_put,
+	.chkput = pcsuite_chkput,
+	.setpath = pcsuite_setpath,
+	.disconnect = pcsuite_disconnect
 };
 
 static struct obex_service_driver ftp = {
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-02-26 18:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-23  9:54 [PATCH 1/6] Add support for pcsuite lock file Luiz Augusto von Dentz
2011-02-23  9:54 ` [PATCH 2/6] Add more debug logs for ftp driver Luiz Augusto von Dentz
2011-02-23  9:54 ` [PATCH 3/6] Add flush callback to mimetype driver Luiz Augusto von Dentz
2011-02-23  9:54 ` [PATCH 4/6] Add .flush callback to backup plugin Luiz Augusto von Dentz
2011-02-23  9:54 ` [PATCH 5/6] Move pcsuite drivers to its own plugin Luiz Augusto von Dentz
2011-02-23 14:44   ` Johan Hedberg
2011-02-26 18:52     ` Luiz Augusto von Dentz
2011-02-23  9:54 ` [PATCH 6/6] Remove redundante check for server when checking for usb Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox