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);
83 file = g_file_new_for_path (SRCDIR "/file.c");
84 type = g_file_query_file_type (file, 0, NULL);
85 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
86 g_object_unref (file);
88 file = g_file_new_for_path (SRCDIR "/schema-tests");
89 type = g_file_query_file_type (file, 0, NULL);
90 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
92 g_file_read (file, NULL, &error);
93 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
95 g_object_unref (file);
102 GFileMonitor *monitor;
103 GOutputStream *ostream;
104 GInputStream *istream;
107 gint monitor_created;
108 gint monitor_deleted;
109 gint monitor_changed;
118 monitor_changed (GFileMonitor *monitor,
121 GFileMonitorEvent event_type,
124 CreateDeleteData *data = user_data;
127 path = g_file_get_path (file);
128 g_assert_cmpstr (data->monitor_path, ==, path);
131 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
132 data->monitor_created++;
133 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
134 data->monitor_deleted++;
135 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
136 data->monitor_changed++;
141 quit_idle (gpointer user_data)
143 CreateDeleteData *data = user_data;
145 g_source_remove (data->timeout);
146 g_main_loop_quit (data->loop);
152 iclosed_cb (GObject *source,
156 CreateDeleteData *data = user_data;
161 ret = g_input_stream_close_finish (data->istream, res, &error);
162 g_assert_no_error (error);
165 g_assert (g_input_stream_is_closed (data->istream));
167 ret = g_file_delete (data->file, NULL, &error);
169 g_assert_no_error (error);
171 /* work around file monitor bug:
172 * inotify events are only processed every 1000 ms, regardless
173 * of the rate limit set on the file monitor
175 g_timeout_add (2000, quit_idle, data);
179 read_cb (GObject *source,
183 CreateDeleteData *data = user_data;
188 size = g_input_stream_read_finish (data->istream, res, &error);
189 g_assert_no_error (error);
192 if (data->pos < strlen (data->data))
194 g_input_stream_read_async (data->istream,
195 data->buffer + data->pos,
196 strlen (data->data) - data->pos,
204 g_assert_cmpstr (data->buffer, ==, data->data);
205 g_assert (!g_input_stream_is_closed (data->istream));
206 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
211 ipending_cb (GObject *source,
215 CreateDeleteData *data = user_data;
219 g_input_stream_read_finish (data->istream, res, &error);
220 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
221 g_error_free (error);
225 skipped_cb (GObject *source,
229 CreateDeleteData *data = user_data;
234 size = g_input_stream_skip_finish (data->istream, res, &error);
235 g_assert_no_error (error);
236 g_assert_cmpint (size, ==, data->pos);
238 g_input_stream_read_async (data->istream,
239 data->buffer + data->pos,
240 strlen (data->data) - data->pos,
245 /* check that we get a pending error */
246 g_input_stream_read_async (data->istream,
247 data->buffer + data->pos,
248 strlen (data->data) - data->pos,
256 opened_cb (GObject *source,
260 GFileInputStream *base;
261 CreateDeleteData *data = user_data;
265 base = g_file_read_finish (data->file, res, &error);
266 g_assert_no_error (error);
268 if (data->buffersize == 0)
269 data->istream = G_INPUT_STREAM (g_object_ref (base));
271 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
272 g_object_unref (base);
274 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
276 /* copy initial segment directly, then skip */
277 memcpy (data->buffer, data->data, 10);
280 g_input_stream_skip_async (data->istream,
289 oclosed_cb (GObject *source,
293 CreateDeleteData *data = user_data;
298 ret = g_output_stream_close_finish (data->ostream, res, &error);
299 g_assert_no_error (error);
301 g_assert (g_output_stream_is_closed (data->ostream));
303 g_file_read_async (data->file, 0, NULL, opened_cb, data);
307 written_cb (GObject *source,
311 CreateDeleteData *data = user_data;
316 size = g_output_stream_write_finish (data->ostream, res, &error);
317 g_assert_no_error (error);
320 if (data->pos < strlen (data->data))
322 g_output_stream_write_async (data->ostream,
323 data->data + data->pos,
324 strlen (data->data) - data->pos,
332 g_assert (!g_output_stream_is_closed (data->ostream));
333 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
338 opending_cb (GObject *source,
342 CreateDeleteData *data = user_data;
346 g_output_stream_write_finish (data->ostream, res, &error);
347 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
348 g_error_free (error);
352 created_cb (GObject *source,
356 GFileOutputStream *base;
357 CreateDeleteData *data = user_data;
361 base = g_file_create_finish (G_FILE (source), res, &error);
362 g_assert_no_error (error);
363 g_assert (g_file_query_exists (data->file, NULL));
365 if (data->buffersize == 0)
366 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
368 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
369 g_object_unref (base);
371 g_output_stream_write_async (data->ostream,
378 /* check that we get a pending error */
379 g_output_stream_write_async (data->ostream,
389 stop_timeout (gpointer data)
391 g_assert_not_reached ();
397 * This test does a fully async create-write-read-delete.
401 test_create_delete (gconstpointer d)
404 CreateDeleteData *data;
405 GFileIOStream *iostream;
407 data = g_new0 (CreateDeleteData, 1);
409 data->buffersize = GPOINTER_TO_INT (d);
410 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
413 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
415 g_assert (data->file != NULL);
416 g_object_unref (iostream);
418 data->monitor_path = g_file_get_path (data->file);
419 remove (data->monitor_path);
421 g_assert (!g_file_query_exists (data->file, NULL));
424 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
425 g_assert_no_error (error);
427 /* This test doesn't work with GPollFileMonitor, because it assumes
428 * that the monitor will notice a create immediately followed by a
429 * delete, rather than coalescing them into nothing.
431 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
433 g_print ("skipping test for this GFileMonitor implementation");
437 g_file_monitor_set_rate_limit (data->monitor, 100);
439 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
441 data->loop = g_main_loop_new (NULL, FALSE);
443 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
445 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
447 g_main_loop_run (data->loop);
449 g_assert_cmpint (data->monitor_created, ==, 1);
450 g_assert_cmpint (data->monitor_deleted, ==, 1);
451 g_assert_cmpint (data->monitor_changed, >, 0);
453 g_assert (!g_file_monitor_is_cancelled (data->monitor));
454 g_file_monitor_cancel (data->monitor);
455 g_assert (g_file_monitor_is_cancelled (data->monitor));
457 g_main_loop_unref (data->loop);
458 g_object_unref (data->ostream);
459 g_object_unref (data->istream);
462 g_object_unref (data->monitor);
463 g_object_unref (data->file);
464 free (data->monitor_path);
465 g_free (data->buffer);
477 static void replaced_cb (GObject *source,
482 loaded_cb (GObject *source,
486 ReplaceLoadData *data = user_data;
493 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
495 g_assert_no_error (error);
496 g_assert_cmpint (length, ==, strlen (data->data));
497 g_assert_cmpstr (contents, ==, data->data);
504 data->data = "pi pa po";
506 g_file_replace_contents_async (data->file,
519 ret = g_file_delete (data->file, NULL, &error);
520 g_assert_no_error (error);
522 g_assert (!g_file_query_exists (data->file, NULL));
524 g_main_loop_quit (data->loop);
529 replaced_cb (GObject *source,
533 ReplaceLoadData *data = user_data;
537 g_file_replace_contents_finish (data->file, res, NULL, &error);
538 g_assert_no_error (error);
540 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
544 test_replace_load (void)
546 ReplaceLoadData *data;
548 GFileIOStream *iostream;
550 data = g_new0 (ReplaceLoadData, 1);
554 " * g_file_replace_contents_async:\n"
555 " * @file: input #GFile.\n"
556 " * @contents: string of contents to replace the file with.\n"
557 " * @length: the length of @contents in bytes.\n"
558 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
559 " * @make_backup: %TRUE if a backup should be created.\n"
560 " * @flags: a set of #GFileCreateFlags.\n"
561 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
562 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
563 " * @user_data: the data to pass to callback function\n"
565 " * Starts an asynchronous replacement of @file with the given \n"
566 " * @contents of @length bytes. @etag will replace the document's\n"
567 " * current entity tag.\n"
569 " * When this operation has completed, @callback will be called with\n"
570 " * @user_user data, and the operation can be finalized with \n"
571 " * g_file_replace_contents_finish().\n"
573 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
574 " * triggering the cancellable object from another thread. If the operation\n"
575 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
577 " * If @make_backup is %TRUE, this function will attempt to \n"
578 " * make a backup of @file.\n"
581 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
583 g_assert (data->file != NULL);
584 g_object_unref (iostream);
586 path = g_file_get_path (data->file);
589 g_assert (!g_file_query_exists (data->file, NULL));
591 data->loop = g_main_loop_new (NULL, FALSE);
593 g_file_replace_contents_async (data->file,
603 g_main_loop_run (data->loop);
605 g_main_loop_unref (data->loop);
606 g_object_unref (data->file);
612 on_file_deleted (GObject *object,
613 GAsyncResult *result,
616 GError *local_error = NULL;
617 GMainLoop *loop = user_data;
619 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
620 g_assert_no_error (local_error);
622 g_main_loop_quit (loop);
626 test_async_delete (void)
629 GFileIOStream *iostream;
630 GError *local_error = NULL;
631 GError **error = &local_error;
634 file = g_file_new_tmp ("g_file_delete_XXXXXX",
636 g_assert_no_error (local_error);
637 g_object_unref (iostream);
639 g_assert (g_file_query_exists (file, NULL));
641 loop = g_main_loop_new (NULL, TRUE);
643 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
645 g_main_loop_run (loop);
647 g_assert (!g_file_query_exists (file, NULL));
649 g_main_loop_unref (loop);
650 g_object_unref (file);
654 main (int argc, char *argv[])
658 g_test_init (&argc, &argv, NULL);
660 g_test_add_func ("/file/basic", test_basic);
661 g_test_add_func ("/file/parent", test_parent);
662 g_test_add_func ("/file/child", test_child);
663 g_test_add_func ("/file/type", test_type);
664 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
665 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
666 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
667 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
668 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
669 g_test_add_func ("/file/replace-load", test_replace_load);
670 g_test_add_func ("/file/async-delete", test_async_delete);
672 return g_test_run ();