Re: g_file_write()



Thus spake Soeren Sandmann:
> I have put up a new patch at
>  
>        http://www.daimi.au.dk/~sandmann/filewrite2.patch 

> +
> +static gchar *
> +write_to_temp_file (const gchar *contents,
> +		    gsize length,

<nitpick>
gssize to be consistent with g_file_replace?
</nitpick>

> +  
> +  tmp_name = g_strdup_printf (".%s.XXXXXX", template);
> +

Again - if the user specifies e.g. the absolute pathname of the file
(/foo/bar), you'll try to create a temp file with a name like
"./foo/bar", which will probably fail.

If you want the temp file to be a dot-file, you'll have to play with
g_path_get_basename and g_path_get_dirname .

> +/**
> + * g_file_replace:
> + * @filename: name of a file to write @contents to, in the GLib file name
> + *   encoding
> + * @contents: string to write to the file
> + * @length: length of @contents, or -1 if @contents is a nul-terminated string
> + * @error: return location for a #GError, or %NULL
> + *
> + * Writes all of @contents to a file named @filename, with good error checking.
> + * If a file called @filename already exists it will be overwritten.
> + *
> + * This write is atomic in the sense that it is first written to a temporary
> + * file which is then renamed to the final name. Notes:
> + * <itemizedlist>
> + * <listitem>
> + *    On Unix, if @filename already exists hard links to @filename will break.
> + *    Also since the file is recreated existing permissions, access control
> + *    lists and metadata may be lost.
 
Maybe also mention that if the file was a symlink, the symlink itself
will be replaced?

And I'm guessing the permissions issue may become a common gotcha with
this interface.  Testing it here (debian unstable) the new file was
created with permissions 600, which I guess is the lesser evil from a
security standpoint, but may cause problems if the file was supposed to
be readable by more than one user.

> +gboolean
> +g_file_replace (const gchar *filename,
> +		const gchar *contents,
> +		gssize	     length,
> +		GError	   **error)


> +
> +  display_tmpname = g_filename_display_name (tmp_filename);

this is not used anymore

> +  display_filename = g_filename_display_name (filename);

this is used only in one place in the error path for G_OS_WIN32, so it
may make sense to move it into the "if (!rename_file())"


Alexis



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