[glib] GFile: add g_file_new_temp



commit 721667399a2ebc198292ecb5bfedc8f296aef04d
Author: Thomas Hindoe Paaboel Andersen <phomes gmail com>
Date:   Thu Dec 8 23:17:07 2011 +0100

    GFile: add g_file_new_temp
    
    A convenience function that creates a temporary file and returns
    a GFile and GFileIOStream for it.
    
    The file is created using g_file_open_tmp.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=657085

 docs/reference/gio/gio-sections.txt |    1 +
 gio/gfile.c                         |   54 ++++++++++++++++++++++++++++++++++-
 gio/gfile.h                         |    3 ++
 gio/gio.symbols                     |    1 +
 gio/glocalfileoutputstream.c        |   10 ++++++
 gio/glocalfileoutputstream.h        |    1 +
 6 files changed, 69 insertions(+), 1 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index 58a1857..6e1c5d7 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -77,6 +77,7 @@ GFileReadMoreCallback
 g_file_new_for_path
 g_file_new_for_uri
 g_file_new_for_commandline_arg
+g_file_new_tmp
 g_file_parse_name
 g_file_dup
 g_file_hash
diff --git a/gio/gfile.c b/gio/gfile.c
index 86111d2..eac2eaf 100644
--- a/gio/gfile.c
+++ b/gio/gfile.c
@@ -44,6 +44,8 @@
 #include "gappinfo.h"
 #include "gfileinputstream.h"
 #include "gfileoutputstream.h"
+#include "glocalfileoutputstream.h"
+#include "glocalfileiostream.h"
 #include "gcancellable.h"
 #include "gasyncresult.h"
 #include "gioerror.h"
@@ -67,6 +69,7 @@
  * g_file_new_for_path() if you have a path.
  * g_file_new_for_uri() if you have a URI.
  * g_file_new_for_commandline_arg() for a command line argument.
+ * g_file_new_tmp() to create a temporary file from a template.
  * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
  * 
  * One way to think of a #GFile is as an abstraction of a pathname. For normal
@@ -5889,7 +5892,56 @@ g_file_new_for_uri (const char *uri)
 
   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
 }
-  
+
+/**
+ * g_file_new_tmp:
+ * @template: (type filename) (allow-none): Template for the file
+ *   name, as in g_file_open_tmp(), or %NULL for a default template.
+ * @iostream: (out): on return, a #GFileIOStream for the created file.
+ * @error: a #GError, or %NULL
+ *
+ * Opens a file in the preferred directory for temporary files (as
+ * returned by g_get_tmp_dir()) and returns a #GFile and
+ * #GFileIOStream pointing to it.
+ *
+ * @template should be a string in the GLib file name encoding
+ * containing a sequence of six 'X' characters, and containing no
+ * directory components. If it is %NULL, a default template is used.
+ *
+ * Unlike the other #GFile constructors, this will return %NULL if
+ * a temporary file could not be created.
+ *
+ * Returns: (transfer full): a new #GFile.
+ *   Free the returned object with g_object_unref().
+ **/
+GFile *
+g_file_new_tmp (const char     *template,
+                GFileIOStream **iostream,
+                GError        **error)
+{
+  gint fd;
+  gchar *path;
+  GFile *file;
+  GFileOutputStream *output;
+
+  g_return_val_if_fail (template != NULL, NULL);
+  g_return_val_if_fail (iostream != NULL, NULL);
+
+  fd = g_file_open_tmp (template, &path, error);
+  if (fd == -1)
+    return NULL;
+
+  file = g_file_new_for_path (path);
+
+  output = _g_local_file_output_stream_new (fd);
+  *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
+
+  g_object_unref (output);
+  g_free (path);
+
+  return file;
+}
+
 /**
  * g_file_parse_name:
  * @parse_name: a file name or path to be parsed.
diff --git a/gio/gfile.h b/gio/gfile.h
index 500d7de..0638f23 100644
--- a/gio/gfile.h
+++ b/gio/gfile.h
@@ -550,6 +550,9 @@ GType                   g_file_get_type                   (void) G_GNUC_CONST;
 GFile *                 g_file_new_for_path               (const char                 *path);
 GFile *                 g_file_new_for_uri                (const char                 *uri);
 GFile *                 g_file_new_for_commandline_arg    (const char                 *arg);
+GFile *                 g_file_new_tmp                    (const char                 *template,
+                                                           GFileIOStream             **iostream,
+                                                           GError                    **error);
 GFile *                 g_file_parse_name                 (const char                 *parse_name);
 GFile *                 g_file_dup                        (GFile                      *file);
 guint                   g_file_hash                       (gconstpointer               file);
diff --git a/gio/gio.symbols b/gio/gio.symbols
index f18e8f7..8fed849 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -251,6 +251,7 @@ g_file_get_type
 g_file_new_for_path
 g_file_new_for_uri
 g_file_new_for_commandline_arg
+g_file_new_tmp
 g_file_parse_name
 g_file_dup
 g_file_hash
diff --git a/gio/glocalfileoutputstream.c b/gio/glocalfileoutputstream.c
index aff270c..a310fcd 100644
--- a/gio/glocalfileoutputstream.c
+++ b/gio/glocalfileoutputstream.c
@@ -541,6 +541,16 @@ g_local_file_output_stream_query_info (GFileOutputStream  *stream,
 }
 
 GFileOutputStream *
+_g_local_file_output_stream_new (int fd)
+{
+  GLocalFileOutputStream *stream;
+
+  stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
+  stream->priv->fd = fd;
+  return G_FILE_OUTPUT_STREAM (stream);
+}
+
+GFileOutputStream *
 _g_local_file_output_stream_open  (const char        *filename,
 				   gboolean          readable,
 				   GCancellable      *cancellable,
diff --git a/gio/glocalfileoutputstream.h b/gio/glocalfileoutputstream.h
index 831c37f..844eacb 100644
--- a/gio/glocalfileoutputstream.h
+++ b/gio/glocalfileoutputstream.h
@@ -59,6 +59,7 @@ gboolean _g_local_file_output_stream_really_close (GLocalFileOutputStream *out,
 						   GCancellable   *cancellable,
 						   GError        **error);
 
+GFileOutputStream * _g_local_file_output_stream_new      (int               fd);
 GFileOutputStream * _g_local_file_output_stream_open     (const char       *filename,
 							  gboolean          readable,
                                                           GCancellable     *cancellable,



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