GFile: add g_file_new_temp
authorThomas Hindoe Paaboel Andersen <phomes@gmail.com>
Thu, 8 Dec 2011 22:17:07 +0000 (23:17 +0100)
committerDan Winship <danw@gnome.org>
Fri, 9 Dec 2011 13:58:05 +0000 (08:58 -0500)
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
gio/gfile.c
gio/gfile.h
gio/gio.symbols
gio/glocalfileoutputstream.c
gio/glocalfileoutputstream.h

index 58a1857b4b23af42dff5c41fb810ff8e8324ff4b..6e1c5d76e567bee85cdb9b45d769fd0f0f1ad06d 100644 (file)
@@ -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
index 86111d2f214522d14e79b091a75a255eb7f16c34..eac2eaf85c1ac838712055d70395cd777720c45a 100644 (file)
@@ -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.
index 500d7de5c772bb7db926ba5c363965c042bdab33..0638f239f6d38e397e54cf748ffe7ad0e4d3046a 100644 (file)
@@ -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);
index f18e8f7091fb28ba74942ae7f2b4061ca23aa10b..8fed849e74aca9d86f1c77060a35d13c777d05d8 100644 (file)
@@ -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
index aff270c0cdd15ba30cd952a0393a956871e6a0b9..a310fcd7d48e8d2b659e82ac318420ade63fe24b 100644 (file)
@@ -540,6 +540,16 @@ g_local_file_output_stream_query_info (GFileOutputStream  *stream,
                                         error);
 }
 
+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,
index 831c37f1275b259802b3b54b1801d23fdbdc7f34..844eacb8f6783c1a4f14eea109ae5090bbaa924e 100644 (file)
@@ -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,