mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-12 15:36:17 +01:00
Change semantics of seek on memory output stream
It is our intention that memory output streams should operate in two distinct modes, depending on if a realloc function was provided or not. In the case that we have a realloc function (resizable mode), we want the stream to behave as if it were a file that started out empty. In the case that we don't have a realloc function (fixed-sized mode), we want the stream to behave as a block device would. To this end, we introduce two changes in functionality: - seeking to SEEK_END on a resizable stream will now seek to the end of the valid data region, not to the end of the allocated memory (which is really just an implementation detail) - seeks past the end of the allocated memory size are now permitted, but only on resizable streams. The next write will grow the buffer (inserting zeros between). Some tweaks to testcases were required in order not to break the build, which indicates that this is an API break, but it seems unlikely that anyone will be effected by these changes 'in the real world'. Updates to documentation and further testcases are in following commits. Based on a patch from Maciej Piechotka <uzytkownik2@gmail.com>. https://bugzilla.gnome.org/show_bug.cgi?id=684842
This commit is contained in:
parent
38ef509cf3
commit
fdc5cd8d9f
@ -726,7 +726,14 @@ g_memory_output_stream_seek (GSeekable *seekable,
|
||||
break;
|
||||
|
||||
case G_SEEK_END:
|
||||
absolute = priv->len + offset;
|
||||
/* For resizable streams, we consider the end to be the data
|
||||
* length. For fixed-sized streams, we consider the end to be the
|
||||
* size of the buffer.
|
||||
*/
|
||||
if (priv->realloc_fn)
|
||||
absolute = priv->valid_len + offset;
|
||||
else
|
||||
absolute = priv->len + offset;
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -747,7 +754,13 @@ g_memory_output_stream_seek (GSeekable *seekable,
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (absolute > priv->len)
|
||||
/* Can't seek past the end of a fixed-size stream.
|
||||
*
|
||||
* Note: seeking to the non-existent byte at the end of a fixed-sized
|
||||
* stream is valid (eg: a 1-byte fixed sized stream can have position
|
||||
* 0 or 1). Therefore '>' is what we want.
|
||||
* */
|
||||
if (priv->realloc_fn == NULL && absolute > priv->len)
|
||||
{
|
||||
g_set_error_literal (error,
|
||||
G_IO_ERROR,
|
||||
|
@ -8,7 +8,7 @@ test_write (void)
|
||||
GError *error;
|
||||
const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
base = g_memory_output_stream_new (g_malloc0 (20), 20, g_realloc, g_free);
|
||||
base = g_memory_output_stream_new (g_malloc0 (20), 20, NULL, g_free);
|
||||
out = g_buffered_output_stream_new (base);
|
||||
|
||||
g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 4096);
|
||||
@ -128,7 +128,7 @@ test_seek (void)
|
||||
gboolean ret;
|
||||
const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
base = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free));
|
||||
base = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (g_malloc0 (30), 30, NULL, g_free));
|
||||
out = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), 8);
|
||||
seekable = G_SEEKABLE (out);
|
||||
error = NULL;
|
||||
|
@ -60,7 +60,7 @@ test_seek (void)
|
||||
GOutputStream *mo;
|
||||
GError *error;
|
||||
|
||||
mo = g_memory_output_stream_new (g_new (gchar, 100), 100, g_realloc, g_free);
|
||||
mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
|
||||
|
||||
g_assert (G_IS_SEEKABLE (mo));
|
||||
g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
|
||||
|
Loading…
Reference in New Issue
Block a user