Bug 594034 - Add g_mkstemp_full()

This function exposes more variables than g_mkstemp() and therefor
allows more flexibility when creating temporary files.
The intended use is gio's code for g_file_replace() (see next patch)
This commit is contained in:
Benjamin Otte 2009-09-03 15:36:37 +02:00
parent 16ddefea15
commit 24bec5c5bd
5 changed files with 50 additions and 10 deletions

View File

@ -1105,6 +1105,7 @@ g_file_get_contents
g_file_set_contents
g_file_test
g_mkstemp
g_mkstemp_full
g_file_open_tmp
g_file_read_link
g_mkdir_with_parents

View File

@ -202,6 +202,17 @@ A test to perform on a file using g_file_test().
@Returns:
<!-- ##### FUNCTION g_mkstemp_full ##### -->
<para>
</para>
@tmpl:
@flags:
@mode:
@Returns:
<!-- ##### FUNCTION g_file_open_tmp ##### -->
<para>

View File

@ -54,9 +54,6 @@
#include "galias.h"
static gint create_temp_file (gchar *tmpl,
int permissions);
/**
* g_mkdir_with_parents:
* @pathname: a pathname in the GLib file name encoding
@ -883,7 +880,7 @@ write_to_temp_file (const gchar *contents,
tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
errno = 0;
fd = create_temp_file (tmp_name, 0666);
fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666);
save_errno = errno;
display_name = g_filename_display_name (tmp_name);
@ -1144,13 +1141,37 @@ g_file_set_contents (const gchar *filename,
return retval;
}
/**
* g_mkstemp_full:
* @tmpl: template filename
* @flags: flags to pass to an open() call in addition to O_EXCL and
* O_CREAT, which are passed automatically
* @mode: permissios to create the temporary file with
*
* Opens a temporary file. See the mkstemp() documentation
* on most UNIX-like systems.
*
* The parameter is a string that should follow the rules for
* mkstemp() templates, i.e. contain the string "XXXXXX".
* g_mkstemp_full() is slightly more flexible than mkstemp()
* in that the sequence does not have to occur at the very end of the
* template and you can pass a @mode and additional @flags. The X
* string will be modified to form the name of a file that didn't exist.
* The string should be in the GLib file name encoding. Most importantly,
* on Windows it should be in UTF-8.
*
* Return value: A file handle (as from open()) to the file
* opened for reading and writing. The file handle should be
* closed with close(). In case of errors, -1 is returned.
*/
/*
* create_temp_file based on the mkstemp implementation from the GNU C library.
* g_mkstemp_full based on the mkstemp implementation from the GNU C library.
* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
*/
static gint
create_temp_file (gchar *tmpl,
int permissions)
gint
g_mkstemp_full (gchar *tmpl,
int flags,
int mode)
{
char *XXXXXX;
int count, fd;
@ -1161,6 +1182,9 @@ create_temp_file (gchar *tmpl,
GTimeVal tv;
static int counter = 0;
g_return_val_if_fail (tmpl != NULL, -1);
/* find the last occurrence of "XXXXXX" */
XXXXXX = g_strrstr (tmpl, "XXXXXX");
@ -1192,7 +1216,7 @@ create_temp_file (gchar *tmpl,
XXXXXX[5] = letters[v % NLETTERS];
/* tmpl is in UTF-8 on Windows, thus use g_open() */
fd = g_open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, permissions);
fd = g_open (tmpl, flags | O_CREAT | O_EXCL, mode);
if (fd >= 0)
return fd;
@ -1232,7 +1256,7 @@ create_temp_file (gchar *tmpl,
gint
g_mkstemp (gchar *tmpl)
{
return create_temp_file (tmpl, 0600);
return g_mkstemp_full (tmpl, O_RDWR | O_BINARY, 0600);
}
/**

View File

@ -99,6 +99,9 @@ gchar *g_file_read_link (const gchar *filename,
/* Wrapper / workalike for mkstemp() */
gint g_mkstemp (gchar *tmpl);
gint g_mkstemp_full (gchar *tmpl,
int flags,
int mode);
/* Wrapper for g_mkstemp */
gint g_file_open_tmp (const gchar *tmpl,

View File

@ -378,6 +378,7 @@ g_format_size_for_display
#ifndef _WIN64
g_mkstemp PRIVATE
#endif
g_mkstemp_full
g_mkdir_with_parents
#ifdef G_OS_WIN32
g_file_get_contents_utf8