[gedit/dbus] Remove fifo when done with the write end



commit 65d175d312c3c8a2e64017a1b78d4acfd6646c22
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Thu May 6 13:01:42 2010 +0200

    Remove fifo when done with the write end

 gedit/gedit-fifo.c |   59 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 49 insertions(+), 10 deletions(-)
---
diff --git a/gedit/gedit-fifo.c b/gedit/gedit-fifo.c
index 79afaf5..fdc1010 100644
--- a/gedit/gedit-fifo.c
+++ b/gedit/gedit-fifo.c
@@ -22,10 +22,16 @@ enum
 	PROP_FILE
 };
 
+typedef enum
+{
+	GEDIT_FIFO_OPEN_MODE_READ,
+	GEDIT_FIFO_OPEN_MODE_WRITE
+} GeditFifoOpenMode;
+
 struct _GeditFifoPrivate
 {
 	GFile *file;
-	gint open_mode;
+	GeditFifoOpenMode open_mode;
 };
 
 static void gedit_fifo_initable_iface_init (gpointer giface, gpointer iface_data);
@@ -65,6 +71,13 @@ gedit_fifo_finalize (GObject *object)
 
 	if (self->priv->file)
 	{
+		if (self->priv->open_mode == GEDIT_FIFO_OPEN_MODE_WRITE)
+		{
+			gchar *path = g_file_get_path (self->priv->file);
+			g_unlink (path);
+			g_free (path);
+		}
+
 		g_object_unref (self->priv->file);
 	}
 
@@ -106,15 +119,19 @@ gedit_fifo_get_property (GObject *object, guint prop_id, GValue *value, GParamSp
 static void
 init_fifo (GeditFifo *fifo)
 {
-	gchar tmp[] = "gedit-fifo.XXXXXX";
+	gchar tmpl[] = "gedit-fifo.XXXXXX";
+	gchar *tmp;
 	gint fd;
+	GError *error = NULL;
 
-	fd = g_mkstemp (tmp);
+	fd = g_file_open_tmp (tmpl, &tmp, &error);
 
 	if (fd == -1)
 	{
 		g_warning ("Could not generate temporary name for fifo: %s",
-		           strerror (errno));
+		           error->message);
+		g_error_free (error);
+
 		return;
 	}
 
@@ -197,6 +214,7 @@ fifo_open_in_thread (GSimpleAsyncResult *res,
 	gint fd;
 	GeditFifo *fifo;
 	gpointer stream;
+	gint flags = 0;
 
 	if (cancellable && g_cancellable_set_error_if_cancelled (cancellable, &error))
 	{
@@ -206,8 +224,19 @@ fifo_open_in_thread (GSimpleAsyncResult *res,
 	}
 
 	fifo = GEDIT_FIFO (object);
+	
+	switch (fifo->priv->open_mode)
+	{
+		case GEDIT_FIFO_OPEN_MODE_READ:
+			flags = O_RDONLY;
+		break;
+		case GEDIT_FIFO_OPEN_MODE_WRITE:
+			flags = O_WRONLY;
+		break;
+	}
+	
 	path = g_file_get_path (fifo->priv->file);
-	fd = g_open (path, fifo->priv->open_mode, 0);
+	fd = g_open (path, flags, 0);
 	g_free (path);
 
 	if (cancellable && g_cancellable_set_error_if_cancelled (cancellable, &error))
@@ -232,7 +261,7 @@ fifo_open_in_thread (GSimpleAsyncResult *res,
 		return;
 	}
 
-	if (fifo->priv->open_mode & O_WRONLY)
+	if (fifo->priv->open_mode == GEDIT_FIFO_OPEN_MODE_WRITE)
 	{
 		stream = g_unix_output_stream_new (fd, TRUE);
 	}
@@ -248,7 +277,7 @@ fifo_open_in_thread (GSimpleAsyncResult *res,
 
 static void
 async_open (GeditFifo           *fifo,
-            gint                 mode,
+            GeditFifoOpenMode    open_mode,
             gint                 io_priority,
             GCancellable        *cancellable,
             GAsyncReadyCallback  callback,
@@ -256,7 +285,7 @@ async_open (GeditFifo           *fifo,
 {
 	GSimpleAsyncResult *ret;
 
-	fifo->priv->open_mode = mode;
+	fifo->priv->open_mode = open_mode;
 
 	ret = g_simple_async_result_new (G_OBJECT (fifo),
 	                                 callback,
@@ -324,7 +353,12 @@ gedit_fifo_open_read_async (GeditFifo           *fifo,
 {
 	g_return_if_fail (GEDIT_IS_FIFO (fifo));
 
-	async_open (fifo, O_RDONLY, io_priority, cancellable, callback, user_data);
+	async_open (fifo,
+	            GEDIT_FIFO_OPEN_MODE_READ,
+	            io_priority,
+	            cancellable,
+	            callback,
+	            user_data);
 }
 
 void
@@ -336,7 +370,12 @@ gedit_fifo_open_write_async (GeditFifo           *fifo,
 {
 	g_return_if_fail (GEDIT_IS_FIFO (fifo));
 
-	async_open (fifo, O_WRONLY, io_priority, cancellable, callback, user_data);
+	async_open (fifo,
+	            GEDIT_FIFO_OPEN_MODE_WRITE,
+	            io_priority,
+	            cancellable,
+	            callback,
+	            user_data);
 }
 
 GFile *



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]