5 #include <gio/gfiledescriptorbased.h>
13 file = g_file_new_for_path ("./some/directory/testfile");
15 s = g_file_get_basename (file);
16 g_assert_cmpstr (s, ==, "testfile");
19 s = g_file_get_uri (file);
20 g_assert (g_str_has_prefix (s, "file://"));
21 g_assert (g_str_has_suffix (s, "/some/directory/testfile"));
24 g_assert (g_file_has_uri_scheme (file, "file"));
25 s = g_file_get_uri_scheme (file);
26 g_assert_cmpstr (s, ==, "file");
29 g_object_unref (file);
40 file = g_file_new_for_path ("./some/directory/testfile");
41 file2 = g_file_new_for_path ("./some/directory");
42 root = g_file_new_for_path ("/");
44 g_assert (g_file_has_parent (file, file2));
46 parent = g_file_get_parent (file);
47 g_assert (g_file_equal (parent, file2));
48 g_object_unref (parent);
50 g_assert (g_file_get_parent (root) == NULL);
52 g_object_unref (file);
53 g_object_unref (file2);
54 g_object_unref (root);
64 file = g_file_new_for_path ("./some/directory");
65 child = g_file_get_child (file, "child");
66 g_assert (g_file_has_parent (child, file));
68 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
69 g_assert (g_file_has_parent (child2, file));
71 g_object_unref (child);
72 g_object_unref (child2);
73 g_object_unref (file);
82 file = g_file_new_for_path (SRCDIR "/file.c");
83 type = g_file_query_file_type (file, 0, NULL);
84 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
85 g_object_unref (file);
87 file = g_file_new_for_path (SRCDIR "/schema-tests");
88 type = g_file_query_file_type (file, 0, NULL);
89 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
90 g_object_unref (file);
97 GFileMonitor *monitor;
98 GOutputStream *ostream;
99 GInputStream *istream;
102 gint monitor_created;
103 gint monitor_deleted;
104 gint monitor_changed;
113 monitor_changed (GFileMonitor *monitor,
116 GFileMonitorEvent event_type,
119 CreateDeleteData *data = user_data;
121 g_assert_cmpstr (data->monitor_path, ==, g_file_get_path (file));
123 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
124 data->monitor_created++;
125 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
126 data->monitor_deleted++;
127 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
128 data->monitor_changed++;
133 quit_idle (gpointer user_data)
135 CreateDeleteData *data = user_data;
137 g_source_remove (data->timeout);
138 g_main_loop_quit (data->loop);
144 iclosed_cb (GObject *source,
148 CreateDeleteData *data = user_data;
153 ret = g_input_stream_close_finish (data->istream, res, &error);
154 g_assert_no_error (error);
157 g_assert (g_input_stream_is_closed (data->istream));
159 ret = g_file_delete (data->file, NULL, &error);
161 g_assert_no_error (error);
163 /* work around file monitor bug:
164 * inotify events are only processed every 1000 ms, regardless
165 * of the rate limit set on the file monitor
167 g_timeout_add (2000, quit_idle, data);
171 read_cb (GObject *source,
175 CreateDeleteData *data = user_data;
180 size = g_input_stream_read_finish (data->istream, res, &error);
181 g_assert_no_error (error);
184 if (data->pos < strlen (data->data))
186 g_input_stream_read_async (data->istream,
187 data->buffer + data->pos,
188 strlen (data->data) - data->pos,
196 g_assert_cmpstr (data->buffer, ==, data->data);
197 g_assert (!g_input_stream_is_closed (data->istream));
198 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
203 ipending_cb (GObject *source,
207 CreateDeleteData *data = user_data;
211 g_input_stream_read_finish (data->istream, res, &error);
212 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
213 g_error_free (error);
217 skipped_cb (GObject *source,
221 CreateDeleteData *data = user_data;
226 size = g_input_stream_skip_finish (data->istream, res, &error);
227 g_assert_no_error (error);
228 g_assert_cmpint (size, ==, data->pos);
230 g_input_stream_read_async (data->istream,
231 data->buffer + data->pos,
232 strlen (data->data) - data->pos,
237 /* check that we get a pending error */
238 g_input_stream_read_async (data->istream,
239 data->buffer + data->pos,
240 strlen (data->data) - data->pos,
248 opened_cb (GObject *source,
252 GFileInputStream *base;
253 CreateDeleteData *data = user_data;
257 base = g_file_read_finish (data->file, res, &error);
258 g_assert_no_error (error);
260 if (data->buffersize == 0)
261 data->istream = G_INPUT_STREAM (g_object_ref (base));
263 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
264 g_object_unref (base);
266 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
268 /* copy initial segment directly, then skip */
269 memcpy (data->buffer, data->data, 10);
272 g_input_stream_skip_async (data->istream,
281 oclosed_cb (GObject *source,
285 CreateDeleteData *data = user_data;
290 ret = g_output_stream_close_finish (data->ostream, res, &error);
291 g_assert_no_error (error);
293 g_assert (g_output_stream_is_closed (data->ostream));
295 g_file_read_async (data->file, 0, NULL, opened_cb, data);
299 written_cb (GObject *source,
303 CreateDeleteData *data = user_data;
308 size = g_output_stream_write_finish (data->ostream, res, &error);
309 g_assert_no_error (error);
312 if (data->pos < strlen (data->data))
314 g_output_stream_write_async (data->ostream,
315 data->data + data->pos,
316 strlen (data->data) - data->pos,
324 g_assert (!g_output_stream_is_closed (data->ostream));
325 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
330 opending_cb (GObject *source,
334 CreateDeleteData *data = user_data;
338 g_output_stream_write_finish (data->ostream, res, &error);
339 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
340 g_error_free (error);
344 created_cb (GObject *source,
348 GFileOutputStream *base;
349 CreateDeleteData *data = user_data;
353 base = g_file_create_finish (G_FILE (source), res, &error);
354 g_assert_no_error (error);
355 g_assert (g_file_query_exists (data->file, NULL));
357 if (data->buffersize == 0)
358 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
360 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
361 g_object_unref (base);
363 g_output_stream_write_async (data->ostream,
370 /* check that we get a pending error */
371 g_output_stream_write_async (data->ostream,
381 stop_timeout (gpointer data)
383 g_assert_not_reached ();
389 * This test does a fully async create-write-read-delete.
393 test_create_delete (gconstpointer d)
396 CreateDeleteData *data;
399 data = g_new0 (CreateDeleteData, 1);
401 data->buffersize = GPOINTER_TO_INT (d);
402 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
405 /* Using tempnam() would be easier here, but causes a compile warning */
406 tmpfd = g_file_open_tmp ("g_file_create_delete_XXXXXX",
407 &data->monitor_path, NULL);
408 g_assert_cmpint (tmpfd, !=, -1);
410 remove (data->monitor_path);
412 data->file = g_file_new_for_path (data->monitor_path);
413 g_assert (!g_file_query_exists (data->file, NULL));
416 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
417 g_assert_no_error (error);
418 g_file_monitor_set_rate_limit (data->monitor, 100);
420 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
422 data->loop = g_main_loop_new (NULL, FALSE);
424 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
426 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
428 g_main_loop_run (data->loop);
430 g_assert_cmpint (data->monitor_created, ==, 1);
431 g_assert_cmpint (data->monitor_deleted, ==, 1);
432 g_assert_cmpint (data->monitor_changed, >, 0);
434 g_assert (!g_file_monitor_is_cancelled (data->monitor));
435 g_file_monitor_cancel (data->monitor);
436 g_assert (g_file_monitor_is_cancelled (data->monitor));
438 g_main_loop_unref (data->loop);
439 g_object_unref (data->monitor);
440 g_object_unref (data->ostream);
441 g_object_unref (data->istream);
442 g_object_unref (data->file);
443 free (data->monitor_path);
444 g_free (data->buffer);
456 static void replaced_cb (GObject *source,
461 loaded_cb (GObject *source,
465 ReplaceLoadData *data = user_data;
472 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
474 g_assert_no_error (error);
475 g_assert_cmpint (length, ==, strlen (data->data));
476 g_assert_cmpstr (contents, ==, data->data);
483 data->data = "pi pa po";
485 g_file_replace_contents_async (data->file,
498 ret = g_file_delete (data->file, NULL, &error);
499 g_assert_no_error (error);
501 g_assert (!g_file_query_exists (data->file, NULL));
503 g_main_loop_quit (data->loop);
508 replaced_cb (GObject *source,
512 ReplaceLoadData *data = user_data;
516 g_file_replace_contents_finish (data->file, res, NULL, &error);
517 g_assert_no_error (error);
519 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
523 test_replace_load (void)
525 ReplaceLoadData *data;
529 data = g_new0 (ReplaceLoadData, 1);
533 " * g_file_replace_contents_async:\n"
534 " * @file: input #GFile.\n"
535 " * @contents: string of contents to replace the file with.\n"
536 " * @length: the length of @contents in bytes.\n"
537 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
538 " * @make_backup: %TRUE if a backup should be created.\n"
539 " * @flags: a set of #GFileCreateFlags.\n"
540 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
541 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
542 " * @user_data: the data to pass to callback function\n"
544 " * Starts an asynchronous replacement of @file with the given \n"
545 " * @contents of @length bytes. @etag will replace the document's\n"
546 " * current entity tag.\n"
548 " * When this operation has completed, @callback will be called with\n"
549 " * @user_user data, and the operation can be finalized with \n"
550 " * g_file_replace_contents_finish().\n"
552 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
553 " * triggering the cancellable object from another thread. If the operation\n"
554 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
556 " * If @make_backup is %TRUE, this function will attempt to \n"
557 " * make a backup of @file.\n"
560 /* Using tempnam() would be easier here, but causes a compile warning */
561 tmpfd = g_file_open_tmp ("g_file_replace_load_XXXXXX",
563 g_assert_cmpint (tmpfd, !=, -1);
567 data->file = g_file_new_for_path (path);
568 g_assert (!g_file_query_exists (data->file, NULL));
570 data->loop = g_main_loop_new (NULL, FALSE);
572 g_file_replace_contents_async (data->file,
582 g_main_loop_run (data->loop);
584 g_main_loop_unref (data->loop);
585 g_object_unref (data->file);
591 main (int argc, char *argv[])
595 g_test_init (&argc, &argv, NULL);
597 g_test_add_func ("/file/basic", test_basic);
598 g_test_add_func ("/file/parent", test_parent);
599 g_test_add_func ("/file/child", test_child);
600 g_test_add_func ("/file/type", test_type);
601 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
602 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
603 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
604 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
605 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
606 g_test_add_func ("/file/replace-load", test_replace_load);
608 return g_test_run ();