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
This commit is contained in:
Thomas Hindoe Paaboel Andersen 2011-12-08 23:17:07 +01:00 committed by Dan Winship
parent e50d8a11b2
commit 721667399a
6 changed files with 69 additions and 1 deletions

View 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

View 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.

View 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);

View 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

View 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,

View 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,