2 #include <glib/gstdio.h>
7 static const char *original_data
= "This is some test data that we can put in a file...";
8 static const char *new_data
= "new data..";
11 verify_pos (GIOStream
*iostream
, goffset expected_pos
)
15 pos
= g_seekable_tell (G_SEEKABLE (iostream
));
16 g_assert_cmpint (pos
, ==, expected_pos
);
18 pos
= g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream
)));
19 g_assert_cmpint (pos
, ==, expected_pos
);
21 pos
= g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream
)));
22 g_assert_cmpint (pos
, ==, expected_pos
);
26 verify_iostream (GFileIOStream
*file_iostream
)
37 iostream
= G_IO_STREAM (file_iostream
);
39 verify_pos (iostream
, 0);
41 in
= g_io_stream_get_input_stream (iostream
);
42 out
= g_io_stream_get_output_stream (iostream
);
44 res
= g_input_stream_read_all (in
, buffer
, 20, &n_bytes
, NULL
, NULL
);
46 g_assert_cmpint ((int)n_bytes
, ==, 20);
48 g_assert (memcmp (buffer
, original_data
, 20) == 0);
50 verify_pos (iostream
, 20);
52 res
= g_seekable_seek (G_SEEKABLE (iostream
),
56 verify_pos (iostream
, strlen (original_data
) - 10);
58 res
= g_input_stream_read_all (in
, buffer
, 20, &n_bytes
, NULL
, NULL
);
60 g_assert_cmpint ((int)n_bytes
, ==, 10);
61 g_assert (memcmp (buffer
, original_data
+ strlen (original_data
) - 10, 10) == 0);
63 verify_pos (iostream
, strlen (original_data
));
65 res
= g_seekable_seek (G_SEEKABLE (iostream
),
69 verify_pos (iostream
, 10);
71 res
= g_output_stream_write_all (out
, new_data
, strlen (new_data
),
72 &n_bytes
, NULL
, NULL
);
74 g_assert_cmpint (n_bytes
, ==, strlen (new_data
));
76 verify_pos (iostream
, 10 + strlen (new_data
));
78 res
= g_seekable_seek (G_SEEKABLE (iostream
),
82 verify_pos (iostream
, 0);
84 res
= g_input_stream_read_all (in
, buffer
, strlen (original_data
), &n_bytes
, NULL
, NULL
);
86 g_assert_cmpint ((int)n_bytes
, ==, strlen (original_data
));
89 modified_data
= g_strdup (original_data
);
90 memcpy (modified_data
+ 10, new_data
, strlen (new_data
));
91 g_assert_cmpstr (buffer
, ==, modified_data
);
93 verify_pos (iostream
, strlen (original_data
));
95 res
= g_seekable_seek (G_SEEKABLE (iostream
),
99 verify_pos (iostream
, 0);
101 res
= g_output_stream_close (out
, NULL
, NULL
);
104 res
= g_input_stream_read_all (in
, buffer
, 15, &n_bytes
, NULL
, NULL
);
106 g_assert_cmpint ((int)n_bytes
, ==, 15);
107 g_assert (memcmp (buffer
, modified_data
, 15) == 0);
110 res
= g_output_stream_write_all (out
, new_data
, strlen (new_data
),
111 &n_bytes
, NULL
, &error
);
113 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_CLOSED
);
114 g_error_free (error
);
117 res
= g_io_stream_close (iostream
, NULL
, &error
);
119 g_assert_no_error (error
);
121 g_free (modified_data
);
125 test_g_file_open_readwrite (void)
130 GFileIOStream
*file_iostream
;
135 fd
= g_file_open_tmp ("readwrite_XXXXXX",
140 res
= g_file_set_contents (tmp_file
,
141 original_data
, -1, NULL
);
144 path
= g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL
);
145 file
= g_file_new_for_path (path
);
148 file_iostream
= g_file_open_readwrite (file
, NULL
, &error
);
149 g_assert (file_iostream
== NULL
);
150 g_assert (g_error_matches (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
));
151 g_error_free (error
);
152 g_object_unref (file
);
154 file
= g_file_new_for_path (tmp_file
);
156 file_iostream
= g_file_open_readwrite (file
, NULL
, &error
);
157 g_assert (file_iostream
!= NULL
);
158 g_object_unref (file
);
160 verify_iostream (file_iostream
);
162 g_object_unref (file_iostream
);
169 test_g_file_create_readwrite (void)
174 GFileIOStream
*file_iostream
;
180 fd
= g_file_open_tmp ("readwrite_XXXXXX",
185 file
= g_file_new_for_path (tmp_file
);
187 file_iostream
= g_file_create_readwrite (file
, 0, NULL
, &error
);
188 g_assert (file_iostream
== NULL
);
189 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
);
190 g_error_free (error
);
193 file_iostream
= g_file_create_readwrite (file
, 0, NULL
, &error
);
194 g_assert (file_iostream
!= NULL
);
196 out
= g_io_stream_get_output_stream (G_IO_STREAM (file_iostream
));
197 res
= g_output_stream_write_all (out
, original_data
, strlen (original_data
),
198 &n_bytes
, NULL
, NULL
);
200 g_assert_cmpint (n_bytes
, ==, strlen (original_data
));
202 res
= g_seekable_seek (G_SEEKABLE (file_iostream
),
207 verify_iostream (file_iostream
);
209 g_object_unref (file_iostream
);
210 g_object_unref (file
);
217 test_g_file_replace_readwrite (void)
219 char *tmp_file
, *backup
, *data
;
222 GFileIOStream
*file_iostream
;
230 fd
= g_file_open_tmp ("readwrite_XXXXXX",
235 res
= g_file_set_contents (tmp_file
,
239 file
= g_file_new_for_path (tmp_file
);
241 file_iostream
= g_file_replace_readwrite (file
, NULL
,
242 TRUE
, 0, NULL
, &error
);
243 g_assert (file_iostream
!= NULL
);
245 in
= g_io_stream_get_input_stream (G_IO_STREAM (file_iostream
));
247 /* Ensure its empty */
248 res
= g_input_stream_read_all (in
, buffer
, sizeof buffer
, &n_bytes
, NULL
, NULL
);
250 g_assert_cmpint ((int)n_bytes
, ==, 0);
252 out
= g_io_stream_get_output_stream (G_IO_STREAM (file_iostream
));
253 res
= g_output_stream_write_all (out
, original_data
, strlen (original_data
),
254 &n_bytes
, NULL
, NULL
);
256 g_assert_cmpint (n_bytes
, ==, strlen (original_data
));
258 res
= g_seekable_seek (G_SEEKABLE (file_iostream
),
263 verify_iostream (file_iostream
);
265 g_object_unref (file_iostream
);
266 g_object_unref (file
);
268 backup
= g_strconcat (tmp_file
, "~", NULL
);
269 res
= g_file_get_contents (backup
,
273 g_assert_cmpstr (data
, ==, new_data
);
288 g_test_init (&argc
, &argv
, NULL
);
290 g_test_add_func ("/readwrite/test_g_file_open_readwrite",
291 test_g_file_open_readwrite
);
292 g_test_add_func ("/readwrite/test_g_file_create_readwrite",
293 test_g_file_create_readwrite
);
294 g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
295 test_g_file_replace_readwrite
);