|
Packit |
ae235b |
#include <glib/glib.h>
|
|
Packit |
ae235b |
#include <glib/gstdio.h>
|
|
Packit |
ae235b |
#include <gio/gio.h>
|
|
Packit |
ae235b |
#include <string.h>
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
#ifdef G_OS_UNIX
|
|
Packit |
ae235b |
#include <unistd.h>
|
|
Packit |
ae235b |
#endif
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static const char *original_data = "This is some test data that we can put in a file...";
|
|
Packit |
ae235b |
static const char *new_data = "new data..";
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
verify_pos (GIOStream *iostream, goffset expected_pos)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
goffset pos;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
pos = g_seekable_tell (G_SEEKABLE (iostream));
|
|
Packit |
ae235b |
g_assert_cmpint (pos, ==, expected_pos);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
|
|
Packit |
ae235b |
g_assert_cmpint (pos, ==, expected_pos);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
|
|
Packit |
ae235b |
g_assert_cmpint (pos, ==, expected_pos);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
verify_iostream (GFileIOStream *file_iostream)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
gboolean res;
|
|
Packit |
ae235b |
GIOStream *iostream;
|
|
Packit |
ae235b |
GError *error;
|
|
Packit |
ae235b |
GInputStream *in;
|
|
Packit |
ae235b |
GOutputStream *out;
|
|
Packit |
ae235b |
char buffer[1024];
|
|
Packit |
ae235b |
gsize n_bytes;
|
|
Packit |
ae235b |
char *modified_data;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
iostream = G_IO_STREAM (file_iostream);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_pos (iostream, 0);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
in = g_io_stream_get_input_stream (iostream);
|
|
Packit |
ae235b |
out = g_io_stream_get_output_stream (iostream);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpmem (buffer, n_bytes, original_data, 20);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_pos (iostream, 20);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
Packit |
ae235b |
-10, G_SEEK_END,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
verify_pos (iostream, strlen (original_data) - 10);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpmem (buffer, n_bytes, original_data + strlen (original_data) - 10, 10);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_pos (iostream, strlen (original_data));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
Packit |
ae235b |
10, G_SEEK_SET,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_input_stream_skip (in, 5, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res == 5);
|
|
Packit |
ae235b |
verify_pos (iostream, 15);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_input_stream_skip (in, 10000, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res == strlen (original_data) - 15);
|
|
Packit |
ae235b |
verify_pos (iostream, strlen (original_data));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
Packit |
ae235b |
10, G_SEEK_SET,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_pos (iostream, 10);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_output_stream_write_all (out, new_data, strlen (new_data),
|
|
Packit |
ae235b |
&n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpint (n_bytes, ==, strlen (new_data));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_pos (iostream, 10 + strlen (new_data));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
Packit |
ae235b |
0, G_SEEK_SET,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
verify_pos (iostream, 0);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
|
|
Packit |
ae235b |
buffer[n_bytes] = 0;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
modified_data = g_strdup (original_data);
|
|
Packit |
ae235b |
memcpy (modified_data + 10, new_data, strlen (new_data));
|
|
Packit |
ae235b |
g_assert_cmpstr (buffer, ==, modified_data);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_pos (iostream, strlen (original_data));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
Packit |
ae235b |
0, G_SEEK_SET,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
verify_pos (iostream, 0);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_output_stream_close (out, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpmem (buffer, n_bytes, modified_data, 15);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
error = NULL;
|
|
Packit |
ae235b |
res = g_output_stream_write_all (out, new_data, strlen (new_data),
|
|
Packit |
ae235b |
&n_bytes, NULL, &error);
|
|
Packit |
ae235b |
g_assert (!res);
|
|
Packit |
ae235b |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
|
|
Packit |
ae235b |
g_error_free (error);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
error = NULL;
|
|
Packit |
ae235b |
res = g_io_stream_close (iostream, NULL, &error);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_no_error (error);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_free (modified_data);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
test_g_file_open_readwrite (void)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
char *tmp_file;
|
|
Packit |
ae235b |
int fd;
|
|
Packit |
ae235b |
gboolean res;
|
|
Packit |
ae235b |
GFileIOStream *file_iostream;
|
|
Packit |
ae235b |
char *path;
|
|
Packit |
ae235b |
GFile *file;
|
|
Packit |
ae235b |
GError *error;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
fd = g_file_open_tmp ("readwrite_XXXXXX",
|
|
Packit |
ae235b |
&tmp_file, NULL);
|
|
Packit |
ae235b |
g_assert (fd != -1);
|
|
Packit |
ae235b |
close (fd);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_file_set_contents (tmp_file,
|
|
Packit |
ae235b |
original_data, -1, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
|
|
Packit |
ae235b |
file = g_file_new_for_path (path);
|
|
Packit |
ae235b |
g_free (path);
|
|
Packit |
ae235b |
error = NULL;
|
|
Packit |
ae235b |
file_iostream = g_file_open_readwrite (file, NULL, &error);
|
|
Packit |
ae235b |
g_assert (file_iostream == NULL);
|
|
Packit |
ae235b |
g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
|
|
Packit |
ae235b |
g_error_free (error);
|
|
Packit |
ae235b |
g_object_unref (file);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
file = g_file_new_for_path (tmp_file);
|
|
Packit |
ae235b |
error = NULL;
|
|
Packit |
ae235b |
file_iostream = g_file_open_readwrite (file, NULL, &error);
|
|
Packit |
ae235b |
g_assert (file_iostream != NULL);
|
|
Packit |
ae235b |
g_object_unref (file);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_iostream (file_iostream);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_object_unref (file_iostream);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_unlink (tmp_file);
|
|
Packit |
ae235b |
g_free (tmp_file);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
test_g_file_create_readwrite (void)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
char *tmp_file;
|
|
Packit |
ae235b |
int fd;
|
|
Packit |
ae235b |
gboolean res;
|
|
Packit |
ae235b |
GFileIOStream *file_iostream;
|
|
Packit |
ae235b |
GOutputStream *out;
|
|
Packit |
ae235b |
GFile *file;
|
|
Packit |
ae235b |
GError *error;
|
|
Packit |
ae235b |
gsize n_bytes;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
fd = g_file_open_tmp ("readwrite_XXXXXX",
|
|
Packit |
ae235b |
&tmp_file, NULL);
|
|
Packit |
ae235b |
g_assert (fd != -1);
|
|
Packit |
ae235b |
close (fd);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
file = g_file_new_for_path (tmp_file);
|
|
Packit |
ae235b |
error = NULL;
|
|
Packit |
ae235b |
file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
|
|
Packit |
ae235b |
g_assert (file_iostream == NULL);
|
|
Packit |
ae235b |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
|
|
Packit |
ae235b |
g_error_free (error);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_unlink (tmp_file);
|
|
Packit |
ae235b |
file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
|
|
Packit |
ae235b |
g_assert (file_iostream != NULL);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
|
|
Packit |
ae235b |
res = g_output_stream_write_all (out, original_data, strlen (original_data),
|
|
Packit |
ae235b |
&n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpint (n_bytes, ==, strlen (original_data));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_seekable_seek (G_SEEKABLE (file_iostream),
|
|
Packit |
ae235b |
0, G_SEEK_SET,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_iostream (file_iostream);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_object_unref (file_iostream);
|
|
Packit |
ae235b |
g_object_unref (file);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_unlink (tmp_file);
|
|
Packit |
ae235b |
g_free (tmp_file);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
test_g_file_replace_readwrite (void)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
char *tmp_file, *backup, *data;
|
|
Packit |
ae235b |
int fd;
|
|
Packit |
ae235b |
gboolean res;
|
|
Packit |
ae235b |
GFileIOStream *file_iostream;
|
|
Packit |
ae235b |
GInputStream *in;
|
|
Packit |
ae235b |
GOutputStream *out;
|
|
Packit |
ae235b |
GFile *file;
|
|
Packit |
ae235b |
GError *error;
|
|
Packit |
ae235b |
char buffer[1024];
|
|
Packit |
ae235b |
gsize n_bytes;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
fd = g_file_open_tmp ("readwrite_XXXXXX",
|
|
Packit |
ae235b |
&tmp_file, NULL);
|
|
Packit |
ae235b |
g_assert (fd != -1);
|
|
Packit |
ae235b |
close (fd);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_file_set_contents (tmp_file,
|
|
Packit |
ae235b |
new_data, -1, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
file = g_file_new_for_path (tmp_file);
|
|
Packit |
ae235b |
error = NULL;
|
|
Packit |
ae235b |
file_iostream = g_file_replace_readwrite (file, NULL,
|
|
Packit |
ae235b |
TRUE, 0, NULL, &error);
|
|
Packit |
ae235b |
g_assert (file_iostream != NULL);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* Ensure its empty */
|
|
Packit |
ae235b |
res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpint ((int)n_bytes, ==, 0);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
|
|
Packit |
ae235b |
res = g_output_stream_write_all (out, original_data, strlen (original_data),
|
|
Packit |
ae235b |
&n_bytes, NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpint (n_bytes, ==, strlen (original_data));
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
res = g_seekable_seek (G_SEEKABLE (file_iostream),
|
|
Packit |
ae235b |
0, G_SEEK_SET,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
verify_iostream (file_iostream);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_object_unref (file_iostream);
|
|
Packit |
ae235b |
g_object_unref (file);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
backup = g_strconcat (tmp_file, "~", NULL);
|
|
Packit |
ae235b |
res = g_file_get_contents (backup,
|
|
Packit |
ae235b |
&data,
|
|
Packit |
ae235b |
NULL, NULL);
|
|
Packit |
ae235b |
g_assert (res);
|
|
Packit |
ae235b |
g_assert_cmpstr (data, ==, new_data);
|
|
Packit |
ae235b |
g_free (data);
|
|
Packit |
ae235b |
g_unlink (backup);
|
|
Packit |
ae235b |
g_free (backup);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_unlink (tmp_file);
|
|
Packit |
ae235b |
g_free (tmp_file);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
int
|
|
Packit |
ae235b |
main (int argc,
|
|
Packit |
ae235b |
char *argv[])
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
g_test_init (&argc, &argv, NULL);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
g_test_add_func ("/readwrite/test_g_file_open_readwrite",
|
|
Packit |
ae235b |
test_g_file_open_readwrite);
|
|
Packit |
ae235b |
g_test_add_func ("/readwrite/test_g_file_create_readwrite",
|
|
Packit |
ae235b |
test_g_file_create_readwrite);
|
|
Packit |
ae235b |
g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
|
|
Packit |
ae235b |
test_g_file_replace_readwrite);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
return g_test_run();
|
|
Packit |
ae235b |
}
|