mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-05 02:36:19 +01:00
GLocalFileOutputStream: Deduplicate stream creation code
Lots of copy/paste of the error handling path, let's deduplicate so I can sanely patch this code later. https://bugzilla.gnome.org/699959
This commit is contained in:
parent
49030c8797
commit
5e65cd4e51
@ -544,77 +544,15 @@ _g_local_file_output_stream_new (int fd)
|
|||||||
return G_FILE_OUTPUT_STREAM (stream);
|
return G_FILE_OUTPUT_STREAM (stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
GFileOutputStream *
|
static GFileOutputStream *
|
||||||
_g_local_file_output_stream_open (const char *filename,
|
output_stream_open (const char *filename,
|
||||||
gboolean readable,
|
gint open_flags,
|
||||||
|
guint mode,
|
||||||
GCancellable *cancellable,
|
GCancellable *cancellable,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GLocalFileOutputStream *stream;
|
GLocalFileOutputStream *stream;
|
||||||
int fd;
|
gint fd;
|
||||||
int open_flags;
|
|
||||||
|
|
||||||
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
open_flags = O_BINARY;
|
|
||||||
if (readable)
|
|
||||||
open_flags |= O_RDWR;
|
|
||||||
else
|
|
||||||
open_flags |= O_WRONLY;
|
|
||||||
|
|
||||||
fd = g_open (filename, open_flags, 0666);
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
int errsv = errno;
|
|
||||||
|
|
||||||
if (errsv == EINVAL)
|
|
||||||
/* This must be an invalid filename, on e.g. FAT */
|
|
||||||
g_set_error_literal (error, G_IO_ERROR,
|
|
||||||
G_IO_ERROR_INVALID_FILENAME,
|
|
||||||
_("Invalid filename"));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *display_name = g_filename_display_name (filename);
|
|
||||||
g_set_error (error, G_IO_ERROR,
|
|
||||||
g_io_error_from_errno (errsv),
|
|
||||||
_("Error opening file '%s': %s"),
|
|
||||||
display_name, g_strerror (errsv));
|
|
||||||
g_free (display_name);
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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_create (const char *filename,
|
|
||||||
gboolean readable,
|
|
||||||
GFileCreateFlags flags,
|
|
||||||
GCancellable *cancellable,
|
|
||||||
GError **error)
|
|
||||||
{
|
|
||||||
GLocalFileOutputStream *stream;
|
|
||||||
int mode;
|
|
||||||
int fd;
|
|
||||||
int open_flags;
|
|
||||||
|
|
||||||
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (flags & G_FILE_CREATE_PRIVATE)
|
|
||||||
mode = 0600;
|
|
||||||
else
|
|
||||||
mode = 0666;
|
|
||||||
|
|
||||||
open_flags = O_CREAT | O_EXCL | O_BINARY;
|
|
||||||
if (readable)
|
|
||||||
open_flags |= O_RDWR;
|
|
||||||
else
|
|
||||||
open_flags |= O_WRONLY;
|
|
||||||
|
|
||||||
fd = g_open (filename, open_flags, mode);
|
fd = g_open (filename, open_flags, mode);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
@ -644,14 +582,34 @@ _g_local_file_output_stream_create (const char *filename,
|
|||||||
}
|
}
|
||||||
|
|
||||||
GFileOutputStream *
|
GFileOutputStream *
|
||||||
_g_local_file_output_stream_append (const char *filename,
|
_g_local_file_output_stream_open (const char *filename,
|
||||||
|
gboolean readable,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GError **error)
|
||||||
|
{
|
||||||
|
int open_flags;
|
||||||
|
|
||||||
|
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
open_flags = O_BINARY;
|
||||||
|
if (readable)
|
||||||
|
open_flags |= O_RDWR;
|
||||||
|
else
|
||||||
|
open_flags |= O_WRONLY;
|
||||||
|
|
||||||
|
return output_stream_open (filename, open_flags, 0666, cancellable, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
GFileOutputStream *
|
||||||
|
_g_local_file_output_stream_create (const char *filename,
|
||||||
|
gboolean readable,
|
||||||
GFileCreateFlags flags,
|
GFileCreateFlags flags,
|
||||||
GCancellable *cancellable,
|
GCancellable *cancellable,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GLocalFileOutputStream *stream;
|
|
||||||
int mode;
|
int mode;
|
||||||
int fd;
|
int open_flags;
|
||||||
|
|
||||||
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -661,32 +619,33 @@ _g_local_file_output_stream_append (const char *filename,
|
|||||||
else
|
else
|
||||||
mode = 0666;
|
mode = 0666;
|
||||||
|
|
||||||
fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
|
open_flags = O_CREAT | O_EXCL | O_BINARY;
|
||||||
if (fd == -1)
|
if (readable)
|
||||||
{
|
open_flags |= O_RDWR;
|
||||||
int errsv = errno;
|
|
||||||
|
|
||||||
if (errsv == EINVAL)
|
|
||||||
/* This must be an invalid filename, on e.g. FAT */
|
|
||||||
g_set_error_literal (error, G_IO_ERROR,
|
|
||||||
G_IO_ERROR_INVALID_FILENAME,
|
|
||||||
_("Invalid filename"));
|
|
||||||
else
|
else
|
||||||
{
|
open_flags |= O_WRONLY;
|
||||||
char *display_name = g_filename_display_name (filename);
|
|
||||||
g_set_error (error, G_IO_ERROR,
|
return output_stream_open (filename, open_flags, mode, cancellable, error);
|
||||||
g_io_error_from_errno (errsv),
|
}
|
||||||
_("Error opening file '%s': %s"),
|
|
||||||
display_name, g_strerror (errsv));
|
GFileOutputStream *
|
||||||
g_free (display_name);
|
_g_local_file_output_stream_append (const char *filename,
|
||||||
}
|
GFileCreateFlags flags,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GError **error)
|
||||||
|
{
|
||||||
|
int mode;
|
||||||
|
|
||||||
|
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
|
if (flags & G_FILE_CREATE_PRIVATE)
|
||||||
stream->priv->fd = fd;
|
mode = 0600;
|
||||||
|
else
|
||||||
|
mode = 0666;
|
||||||
|
|
||||||
return G_FILE_OUTPUT_STREAM (stream);
|
return output_stream_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode,
|
||||||
|
cancellable, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
|
Loading…
Reference in New Issue
Block a user