2009-05-13 13:06:58 +02:00
|
|
|
#include <glib/glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <gio/gio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
static const char *original_data = "This is some test data that we can put in a file...";
|
|
|
|
static const char *new_data = "new data..";
|
2009-05-13 13:06:58 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
verify_pos (GIOStream *iostream, goffset expected_pos)
|
|
|
|
{
|
|
|
|
goffset pos;
|
|
|
|
|
|
|
|
pos = g_seekable_tell (G_SEEKABLE (iostream));
|
|
|
|
g_assert_cmpint (pos, ==, expected_pos);
|
|
|
|
|
|
|
|
pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
|
|
|
|
g_assert_cmpint (pos, ==, expected_pos);
|
|
|
|
|
|
|
|
pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
|
|
|
|
g_assert_cmpint (pos, ==, expected_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
verify_iostream (GFileIOStream *file_iostream)
|
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
GIOStream *iostream;
|
|
|
|
GError *error;
|
|
|
|
GInputStream *in;
|
|
|
|
GOutputStream *out;
|
|
|
|
char buffer[1024];
|
|
|
|
gsize n_bytes;
|
|
|
|
char *modified_data;
|
|
|
|
|
|
|
|
iostream = G_IO_STREAM (file_iostream);
|
|
|
|
|
|
|
|
verify_pos (iostream, 0);
|
|
|
|
|
|
|
|
in = g_io_stream_get_input_stream (iostream);
|
|
|
|
out = g_io_stream_get_output_stream (iostream);
|
|
|
|
|
|
|
|
res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint ((int)n_bytes, ==, 20);
|
|
|
|
|
|
|
|
g_assert (memcmp (buffer, original_data, 20) == 0);
|
|
|
|
|
|
|
|
verify_pos (iostream, 20);
|
|
|
|
|
|
|
|
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
|
|
-10, G_SEEK_END,
|
|
|
|
NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
verify_pos (iostream, strlen (original_data) - 10);
|
|
|
|
|
|
|
|
res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint ((int)n_bytes, ==, 10);
|
|
|
|
g_assert (memcmp (buffer, original_data + strlen (original_data) - 10, 10) == 0);
|
|
|
|
|
|
|
|
verify_pos (iostream, strlen (original_data));
|
|
|
|
|
|
|
|
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
|
|
10, G_SEEK_SET,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
verify_pos (iostream, 10);
|
|
|
|
|
|
|
|
res = g_output_stream_write_all (out, new_data, strlen (new_data),
|
|
|
|
&n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint (n_bytes, ==, strlen (new_data));
|
|
|
|
|
|
|
|
verify_pos (iostream, 10 + strlen (new_data));
|
|
|
|
|
|
|
|
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
|
|
0, G_SEEK_SET,
|
|
|
|
NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
verify_pos (iostream, 0);
|
|
|
|
|
|
|
|
res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
|
|
|
|
buffer[n_bytes] = 0;
|
|
|
|
|
|
|
|
modified_data = g_strdup (original_data);
|
|
|
|
memcpy (modified_data + 10, new_data, strlen (new_data));
|
|
|
|
g_assert_cmpstr (buffer, ==, modified_data);
|
|
|
|
|
|
|
|
verify_pos (iostream, strlen (original_data));
|
|
|
|
|
|
|
|
res = g_seekable_seek (G_SEEKABLE (iostream),
|
|
|
|
0, G_SEEK_SET,
|
|
|
|
NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
verify_pos (iostream, 0);
|
|
|
|
|
|
|
|
res = g_output_stream_close (out, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
|
|
|
|
res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint ((int)n_bytes, ==, 15);
|
|
|
|
g_assert (memcmp (buffer, modified_data, 15) == 0);
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
res = g_output_stream_write_all (out, new_data, strlen (new_data),
|
|
|
|
&n_bytes, NULL, &error);
|
|
|
|
g_assert (!res);
|
2010-09-03 21:35:44 +02:00
|
|
|
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
|
|
|
|
g_error_free (error);
|
2009-05-13 13:06:58 +02:00
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
res = g_io_stream_close (iostream, NULL, &error);
|
2010-09-03 21:35:44 +02:00
|
|
|
g_assert (res);
|
|
|
|
g_assert_no_error (error);
|
2009-05-13 13:06:58 +02:00
|
|
|
|
|
|
|
g_free (modified_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_g_file_open_readwrite (void)
|
|
|
|
{
|
2009-06-29 18:24:08 +02:00
|
|
|
char *tmp_file;
|
2009-05-13 13:06:58 +02:00
|
|
|
int fd;
|
|
|
|
gboolean res;
|
|
|
|
GFileIOStream *file_iostream;
|
|
|
|
char *path;
|
|
|
|
GFile *file;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
fd = g_file_open_tmp ("readwrite_XXXXXX",
|
2009-06-29 18:24:08 +02:00
|
|
|
&tmp_file, NULL);
|
2009-05-13 13:06:58 +02:00
|
|
|
g_assert (fd != -1);
|
|
|
|
close (fd);
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
res = g_file_set_contents (tmp_file,
|
2009-05-13 13:06:58 +02:00
|
|
|
original_data, -1, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
|
2010-02-23 18:19:16 +01:00
|
|
|
path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
|
2009-05-13 13:06:58 +02:00
|
|
|
file = g_file_new_for_path (path);
|
|
|
|
g_free (path);
|
|
|
|
error = NULL;
|
|
|
|
file_iostream = g_file_open_readwrite (file, NULL, &error);
|
|
|
|
g_assert (file_iostream == NULL);
|
|
|
|
g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
|
|
|
|
g_error_free (error);
|
|
|
|
g_object_unref (file);
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
file = g_file_new_for_path (tmp_file);
|
2009-05-13 13:06:58 +02:00
|
|
|
error = NULL;
|
|
|
|
file_iostream = g_file_open_readwrite (file, NULL, &error);
|
|
|
|
g_assert (file_iostream != NULL);
|
2010-09-03 21:32:32 +02:00
|
|
|
g_object_unref (file);
|
2009-05-13 13:06:58 +02:00
|
|
|
|
|
|
|
verify_iostream (file_iostream);
|
|
|
|
|
|
|
|
g_object_unref (file_iostream);
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
g_unlink (tmp_file);
|
2009-06-30 19:08:46 +02:00
|
|
|
g_free (tmp_file);
|
2009-05-13 13:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_g_file_create_readwrite (void)
|
|
|
|
{
|
2009-06-29 18:24:08 +02:00
|
|
|
char *tmp_file;
|
2009-05-13 13:06:58 +02:00
|
|
|
int fd;
|
|
|
|
gboolean res;
|
|
|
|
GFileIOStream *file_iostream;
|
|
|
|
GOutputStream *out;
|
|
|
|
GFile *file;
|
|
|
|
GError *error;
|
|
|
|
gsize n_bytes;
|
|
|
|
|
|
|
|
fd = g_file_open_tmp ("readwrite_XXXXXX",
|
2009-06-29 18:24:08 +02:00
|
|
|
&tmp_file, NULL);
|
2009-05-13 13:06:58 +02:00
|
|
|
g_assert (fd != -1);
|
|
|
|
close (fd);
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
file = g_file_new_for_path (tmp_file);
|
2009-05-13 13:06:58 +02:00
|
|
|
error = NULL;
|
|
|
|
file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
|
|
|
|
g_assert (file_iostream == NULL);
|
2010-09-03 21:44:28 +02:00
|
|
|
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
|
|
|
|
g_error_free (error);
|
2009-05-13 13:06:58 +02:00
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
g_unlink (tmp_file);
|
2009-05-13 13:06:58 +02:00
|
|
|
file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
|
|
|
|
g_assert (file_iostream != NULL);
|
|
|
|
|
|
|
|
out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
|
|
|
|
res = g_output_stream_write_all (out, original_data, strlen (original_data),
|
|
|
|
&n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint (n_bytes, ==, strlen (original_data));
|
|
|
|
|
|
|
|
res = g_seekable_seek (G_SEEKABLE (file_iostream),
|
|
|
|
0, G_SEEK_SET,
|
|
|
|
NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
|
|
|
|
verify_iostream (file_iostream);
|
|
|
|
|
|
|
|
g_object_unref (file_iostream);
|
2010-09-03 21:33:28 +02:00
|
|
|
g_object_unref (file);
|
2009-05-13 13:06:58 +02:00
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
g_unlink (tmp_file);
|
|
|
|
g_free (tmp_file);
|
2009-05-13 13:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_g_file_replace_readwrite (void)
|
|
|
|
{
|
2009-06-29 18:24:08 +02:00
|
|
|
char *tmp_file, *backup, *data;
|
2009-05-13 13:06:58 +02:00
|
|
|
int fd;
|
|
|
|
gboolean res;
|
|
|
|
GFileIOStream *file_iostream;
|
|
|
|
GInputStream *in;
|
|
|
|
GOutputStream *out;
|
|
|
|
GFile *file;
|
|
|
|
GError *error;
|
|
|
|
char buffer[1024];
|
|
|
|
gsize n_bytes;
|
|
|
|
|
|
|
|
fd = g_file_open_tmp ("readwrite_XXXXXX",
|
2009-06-29 18:24:08 +02:00
|
|
|
&tmp_file, NULL);
|
2009-05-13 13:06:58 +02:00
|
|
|
g_assert (fd != -1);
|
|
|
|
close (fd);
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
res = g_file_set_contents (tmp_file,
|
2009-05-13 13:06:58 +02:00
|
|
|
new_data, -1, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
file = g_file_new_for_path (tmp_file);
|
2009-05-13 13:06:58 +02:00
|
|
|
error = NULL;
|
|
|
|
file_iostream = g_file_replace_readwrite (file, NULL,
|
|
|
|
TRUE, 0, NULL, &error);
|
|
|
|
g_assert (file_iostream != NULL);
|
|
|
|
|
|
|
|
in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
|
|
|
|
|
|
|
|
/* Ensure its empty */
|
|
|
|
res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint ((int)n_bytes, ==, 0);
|
|
|
|
|
|
|
|
out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
|
|
|
|
res = g_output_stream_write_all (out, original_data, strlen (original_data),
|
|
|
|
&n_bytes, NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpint (n_bytes, ==, strlen (original_data));
|
|
|
|
|
|
|
|
res = g_seekable_seek (G_SEEKABLE (file_iostream),
|
|
|
|
0, G_SEEK_SET,
|
|
|
|
NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
|
|
|
|
verify_iostream (file_iostream);
|
|
|
|
|
|
|
|
g_object_unref (file_iostream);
|
2010-09-03 21:34:12 +02:00
|
|
|
g_object_unref (file);
|
2009-05-13 13:06:58 +02:00
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
backup = g_strconcat (tmp_file, "~", NULL);
|
2009-05-13 13:06:58 +02:00
|
|
|
res = g_file_get_contents (backup,
|
|
|
|
&data,
|
|
|
|
NULL, NULL);
|
|
|
|
g_assert (res);
|
|
|
|
g_assert_cmpstr (data, ==, new_data);
|
|
|
|
g_free (data);
|
|
|
|
g_unlink (backup);
|
|
|
|
g_free (backup);
|
|
|
|
|
2009-06-29 18:24:08 +02:00
|
|
|
g_unlink (tmp_file);
|
|
|
|
g_free (tmp_file);
|
2009-05-13 13:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
|
|
|
g_type_init ();
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
|
|
|
g_test_add_func ("/readwrite/test_g_file_open_readwrite",
|
|
|
|
test_g_file_open_readwrite);
|
|
|
|
g_test_add_func ("/readwrite/test_g_file_create_readwrite",
|
|
|
|
test_g_file_create_readwrite);
|
|
|
|
g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
|
|
|
|
test_g_file_replace_readwrite);
|
|
|
|
|
|
|
|
return g_test_run();
|
|
|
|
}
|