2 #include <glib/gstdio.h>
10 static const char *original_data
= "This is some test data that we can put in a file...";
11 static const char *new_data
= "new data..";
14 verify_pos (GIOStream
*iostream
, goffset expected_pos
)
18 pos
= g_seekable_tell (G_SEEKABLE (iostream
));
19 g_assert_cmpint (pos
, ==, expected_pos
);
21 pos
= g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream
)));
22 g_assert_cmpint (pos
, ==, expected_pos
);
24 pos
= g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream
)));
25 g_assert_cmpint (pos
, ==, expected_pos
);
29 verify_iostream (GFileIOStream
*file_iostream
)
40 iostream
= G_IO_STREAM (file_iostream
);
42 verify_pos (iostream
, 0);
44 in
= g_io_stream_get_input_stream (iostream
);
45 out
= g_io_stream_get_output_stream (iostream
);
47 res
= g_input_stream_read_all (in
, buffer
, 20, &n_bytes
, NULL
, NULL
);
49 g_assert_cmpmem (buffer
, n_bytes
, original_data
, 20);
51 verify_pos (iostream
, 20);
53 res
= g_seekable_seek (G_SEEKABLE (iostream
),
57 verify_pos (iostream
, strlen (original_data
) - 10);
59 res
= g_input_stream_read_all (in
, buffer
, 20, &n_bytes
, NULL
, NULL
);
61 g_assert_cmpmem (buffer
, n_bytes
, original_data
+ strlen (original_data
) - 10, 10);
63 verify_pos (iostream
, strlen (original_data
));
65 res
= g_seekable_seek (G_SEEKABLE (iostream
),
69 res
= g_input_stream_skip (in
, 5, NULL
, NULL
);
71 verify_pos (iostream
, 15);
73 res
= g_input_stream_skip (in
, 10000, NULL
, NULL
);
74 g_assert (res
== strlen (original_data
) - 15);
75 verify_pos (iostream
, strlen (original_data
));
77 res
= g_seekable_seek (G_SEEKABLE (iostream
),
81 verify_pos (iostream
, 10);
83 res
= g_output_stream_write_all (out
, new_data
, strlen (new_data
),
84 &n_bytes
, NULL
, NULL
);
86 g_assert_cmpint (n_bytes
, ==, strlen (new_data
));
88 verify_pos (iostream
, 10 + strlen (new_data
));
90 res
= g_seekable_seek (G_SEEKABLE (iostream
),
94 verify_pos (iostream
, 0);
96 res
= g_input_stream_read_all (in
, buffer
, strlen (original_data
), &n_bytes
, NULL
, NULL
);
98 g_assert_cmpint ((int)n_bytes
, ==, strlen (original_data
));
101 modified_data
= g_strdup (original_data
);
102 memcpy (modified_data
+ 10, new_data
, strlen (new_data
));
103 g_assert_cmpstr (buffer
, ==, modified_data
);
105 verify_pos (iostream
, strlen (original_data
));
107 res
= g_seekable_seek (G_SEEKABLE (iostream
),
111 verify_pos (iostream
, 0);
113 res
= g_output_stream_close (out
, NULL
, NULL
);
116 res
= g_input_stream_read_all (in
, buffer
, 15, &n_bytes
, NULL
, NULL
);
118 g_assert_cmpmem (buffer
, n_bytes
, modified_data
, 15);
121 res
= g_output_stream_write_all (out
, new_data
, strlen (new_data
),
122 &n_bytes
, NULL
, &error
);
124 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_CLOSED
);
125 g_error_free (error
);
128 res
= g_io_stream_close (iostream
, NULL
, &error
);
130 g_assert_no_error (error
);
132 g_free (modified_data
);
136 test_g_file_open_readwrite (void)
141 GFileIOStream
*file_iostream
;
146 fd
= g_file_open_tmp ("readwrite_XXXXXX",
151 res
= g_file_set_contents (tmp_file
,
152 original_data
, -1, NULL
);
155 path
= g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL
);
156 file
= g_file_new_for_path (path
);
159 file_iostream
= g_file_open_readwrite (file
, NULL
, &error
);
160 g_assert (file_iostream
== NULL
);
161 g_assert (g_error_matches (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
));
162 g_error_free (error
);
163 g_object_unref (file
);
165 file
= g_file_new_for_path (tmp_file
);
167 file_iostream
= g_file_open_readwrite (file
, NULL
, &error
);
168 g_assert (file_iostream
!= NULL
);
169 g_object_unref (file
);
171 verify_iostream (file_iostream
);
173 g_object_unref (file_iostream
);
180 test_g_file_create_readwrite (void)
185 GFileIOStream
*file_iostream
;
191 fd
= g_file_open_tmp ("readwrite_XXXXXX",
196 file
= g_file_new_for_path (tmp_file
);
198 file_iostream
= g_file_create_readwrite (file
, 0, NULL
, &error
);
199 g_assert (file_iostream
== NULL
);
200 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
);
201 g_error_free (error
);
204 file_iostream
= g_file_create_readwrite (file
, 0, NULL
, &error
);
205 g_assert (file_iostream
!= NULL
);
207 out
= g_io_stream_get_output_stream (G_IO_STREAM (file_iostream
));
208 res
= g_output_stream_write_all (out
, original_data
, strlen (original_data
),
209 &n_bytes
, NULL
, NULL
);
211 g_assert_cmpint (n_bytes
, ==, strlen (original_data
));
213 res
= g_seekable_seek (G_SEEKABLE (file_iostream
),
218 verify_iostream (file_iostream
);
220 g_object_unref (file_iostream
);
221 g_object_unref (file
);
228 test_g_file_replace_readwrite (void)
230 char *tmp_file
, *backup
, *data
;
233 GFileIOStream
*file_iostream
;
241 fd
= g_file_open_tmp ("readwrite_XXXXXX",
246 res
= g_file_set_contents (tmp_file
,
250 file
= g_file_new_for_path (tmp_file
);
252 file_iostream
= g_file_replace_readwrite (file
, NULL
,
253 TRUE
, 0, NULL
, &error
);
254 g_assert (file_iostream
!= NULL
);
256 in
= g_io_stream_get_input_stream (G_IO_STREAM (file_iostream
));
258 /* Ensure its empty */
259 res
= g_input_stream_read_all (in
, buffer
, sizeof buffer
, &n_bytes
, NULL
, NULL
);
261 g_assert_cmpint ((int)n_bytes
, ==, 0);
263 out
= g_io_stream_get_output_stream (G_IO_STREAM (file_iostream
));
264 res
= g_output_stream_write_all (out
, original_data
, strlen (original_data
),
265 &n_bytes
, NULL
, NULL
);
267 g_assert_cmpint (n_bytes
, ==, strlen (original_data
));
269 res
= g_seekable_seek (G_SEEKABLE (file_iostream
),
274 verify_iostream (file_iostream
);
276 g_object_unref (file_iostream
);
277 g_object_unref (file
);
279 backup
= g_strconcat (tmp_file
, "~", NULL
);
280 res
= g_file_get_contents (backup
,
284 g_assert_cmpstr (data
, ==, new_data
);
298 g_test_init (&argc
, &argv
, NULL
);
300 g_test_add_func ("/readwrite/test_g_file_open_readwrite",
301 test_g_file_open_readwrite
);
302 g_test_add_func ("/readwrite/test_g_file_create_readwrite",
303 test_g_file_create_readwrite
);
304 g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
305 test_g_file_replace_readwrite
);