5 #include <gio/gfiledescriptorbased.h>
16 file = g_file_new_for_path ("./some/directory/testfile");
18 s = g_file_get_basename (file);
19 g_assert_cmpstr (s, ==, "testfile");
22 s = g_file_get_uri (file);
23 g_assert (g_str_has_prefix (s, "file://"));
24 g_assert (g_str_has_suffix (s, "/some/directory/testfile"));
27 g_assert (g_file_has_uri_scheme (file, "file"));
28 s = g_file_get_uri_scheme (file);
29 g_assert_cmpstr (s, ==, "file");
32 g_object_unref (file);
43 file = g_file_new_for_path ("./some/directory/testfile");
44 file2 = g_file_new_for_path ("./some/directory");
45 root = g_file_new_for_path ("/");
47 g_assert (g_file_has_parent (file, file2));
49 parent = g_file_get_parent (file);
50 g_assert (g_file_equal (parent, file2));
51 g_object_unref (parent);
53 g_assert (g_file_get_parent (root) == NULL);
55 g_object_unref (file);
56 g_object_unref (file2);
57 g_object_unref (root);
67 file = g_file_new_for_path ("./some/directory");
68 child = g_file_get_child (file, "child");
69 g_assert (g_file_has_parent (child, file));
71 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
72 g_assert (g_file_has_parent (child2, file));
74 g_object_unref (child);
75 g_object_unref (child2);
76 g_object_unref (file);
87 datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
89 file = g_file_get_child (datapath_f, "g-icon.c");
90 type = g_file_query_file_type (file, 0, NULL);
91 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
92 g_object_unref (file);
94 file = g_file_get_child (datapath_f, "cert-tests");
95 type = g_file_query_file_type (file, 0, NULL);
96 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
98 g_file_read (file, NULL, &error);
99 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
100 g_error_free (error);
101 g_object_unref (file);
103 g_object_unref (datapath_f);
110 GFileMonitor *monitor;
111 GOutputStream *ostream;
112 GInputStream *istream;
115 gint monitor_created;
116 gint monitor_deleted;
117 gint monitor_changed;
126 monitor_changed (GFileMonitor *monitor,
129 GFileMonitorEvent event_type,
132 CreateDeleteData *data = user_data;
135 path = g_file_get_path (file);
136 g_assert_cmpstr (data->monitor_path, ==, path);
139 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
140 data->monitor_created++;
141 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
142 data->monitor_deleted++;
143 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
144 data->monitor_changed++;
149 quit_idle (gpointer user_data)
151 CreateDeleteData *data = user_data;
153 g_source_remove (data->timeout);
154 g_main_loop_quit (data->loop);
160 iclosed_cb (GObject *source,
164 CreateDeleteData *data = user_data;
169 ret = g_input_stream_close_finish (data->istream, res, &error);
170 g_assert_no_error (error);
173 g_assert (g_input_stream_is_closed (data->istream));
175 ret = g_file_delete (data->file, NULL, &error);
177 g_assert_no_error (error);
179 /* work around file monitor bug:
180 * inotify events are only processed every 1000 ms, regardless
181 * of the rate limit set on the file monitor
183 g_timeout_add (2000, quit_idle, data);
187 read_cb (GObject *source,
191 CreateDeleteData *data = user_data;
196 size = g_input_stream_read_finish (data->istream, res, &error);
197 g_assert_no_error (error);
200 if (data->pos < strlen (data->data))
202 g_input_stream_read_async (data->istream,
203 data->buffer + data->pos,
204 strlen (data->data) - data->pos,
212 g_assert_cmpstr (data->buffer, ==, data->data);
213 g_assert (!g_input_stream_is_closed (data->istream));
214 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
219 ipending_cb (GObject *source,
223 CreateDeleteData *data = user_data;
227 g_input_stream_read_finish (data->istream, res, &error);
228 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
229 g_error_free (error);
233 skipped_cb (GObject *source,
237 CreateDeleteData *data = user_data;
242 size = g_input_stream_skip_finish (data->istream, res, &error);
243 g_assert_no_error (error);
244 g_assert_cmpint (size, ==, data->pos);
246 g_input_stream_read_async (data->istream,
247 data->buffer + data->pos,
248 strlen (data->data) - data->pos,
253 /* check that we get a pending error */
254 g_input_stream_read_async (data->istream,
255 data->buffer + data->pos,
256 strlen (data->data) - data->pos,
264 opened_cb (GObject *source,
268 GFileInputStream *base;
269 CreateDeleteData *data = user_data;
273 base = g_file_read_finish (data->file, res, &error);
274 g_assert_no_error (error);
276 if (data->buffersize == 0)
277 data->istream = G_INPUT_STREAM (g_object_ref (base));
279 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
280 g_object_unref (base);
282 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
284 /* copy initial segment directly, then skip */
285 memcpy (data->buffer, data->data, 10);
288 g_input_stream_skip_async (data->istream,
297 oclosed_cb (GObject *source,
301 CreateDeleteData *data = user_data;
306 ret = g_output_stream_close_finish (data->ostream, res, &error);
307 g_assert_no_error (error);
309 g_assert (g_output_stream_is_closed (data->ostream));
311 g_file_read_async (data->file, 0, NULL, opened_cb, data);
315 written_cb (GObject *source,
319 CreateDeleteData *data = user_data;
324 size = g_output_stream_write_finish (data->ostream, res, &error);
325 g_assert_no_error (error);
328 if (data->pos < strlen (data->data))
330 g_output_stream_write_async (data->ostream,
331 data->data + data->pos,
332 strlen (data->data) - data->pos,
340 g_assert (!g_output_stream_is_closed (data->ostream));
341 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
346 opending_cb (GObject *source,
350 CreateDeleteData *data = user_data;
354 g_output_stream_write_finish (data->ostream, res, &error);
355 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
356 g_error_free (error);
360 created_cb (GObject *source,
364 GFileOutputStream *base;
365 CreateDeleteData *data = user_data;
369 base = g_file_create_finish (G_FILE (source), res, &error);
370 g_assert_no_error (error);
371 g_assert (g_file_query_exists (data->file, NULL));
373 if (data->buffersize == 0)
374 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
376 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
377 g_object_unref (base);
379 g_output_stream_write_async (data->ostream,
386 /* check that we get a pending error */
387 g_output_stream_write_async (data->ostream,
397 stop_timeout (gpointer data)
399 g_assert_not_reached ();
405 * This test does a fully async create-write-read-delete.
409 test_create_delete (gconstpointer d)
412 CreateDeleteData *data;
413 GFileIOStream *iostream;
415 data = g_new0 (CreateDeleteData, 1);
417 data->buffersize = GPOINTER_TO_INT (d);
418 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
421 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
423 g_assert (data->file != NULL);
424 g_object_unref (iostream);
426 data->monitor_path = g_file_get_path (data->file);
427 remove (data->monitor_path);
429 g_assert (!g_file_query_exists (data->file, NULL));
432 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
433 g_assert_no_error (error);
435 /* This test doesn't work with GPollFileMonitor, because it assumes
436 * that the monitor will notice a create immediately followed by a
437 * delete, rather than coalescing them into nothing.
439 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
441 g_print ("skipping test for this GFileMonitor implementation");
445 g_file_monitor_set_rate_limit (data->monitor, 100);
447 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
449 data->loop = g_main_loop_new (NULL, FALSE);
451 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
453 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
455 g_main_loop_run (data->loop);
457 g_assert_cmpint (data->monitor_created, ==, 1);
458 g_assert_cmpint (data->monitor_deleted, ==, 1);
459 g_assert_cmpint (data->monitor_changed, >, 0);
461 g_assert (!g_file_monitor_is_cancelled (data->monitor));
462 g_file_monitor_cancel (data->monitor);
463 g_assert (g_file_monitor_is_cancelled (data->monitor));
465 g_main_loop_unref (data->loop);
466 g_object_unref (data->ostream);
467 g_object_unref (data->istream);
470 g_object_unref (data->monitor);
471 g_object_unref (data->file);
472 free (data->monitor_path);
473 g_free (data->buffer);
477 static const gchar *replace_data =
479 " * g_file_replace_contents_async:\n"
480 " * @file: input #GFile.\n"
481 " * @contents: string of contents to replace the file with.\n"
482 " * @length: the length of @contents in bytes.\n"
483 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
484 " * @make_backup: %TRUE if a backup should be created.\n"
485 " * @flags: a set of #GFileCreateFlags.\n"
486 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
487 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
488 " * @user_data: the data to pass to callback function\n"
490 " * Starts an asynchronous replacement of @file with the given \n"
491 " * @contents of @length bytes. @etag will replace the document's\n"
492 " * current entity tag.\n"
494 " * When this operation has completed, @callback will be called with\n"
495 " * @user_user data, and the operation can be finalized with \n"
496 " * g_file_replace_contents_finish().\n"
498 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
499 " * triggering the cancellable object from another thread. If the operation\n"
500 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
502 " * If @make_backup is %TRUE, this function will attempt to \n"
503 " * make a backup of @file.\n"
514 static void replaced_cb (GObject *source,
519 loaded_cb (GObject *source,
523 ReplaceLoadData *data = user_data;
530 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
532 g_assert_no_error (error);
533 g_assert_cmpint (length, ==, strlen (data->data));
534 g_assert_cmpstr (contents, ==, data->data);
541 data->data = "pi pa po";
543 g_file_replace_contents_async (data->file,
556 ret = g_file_delete (data->file, NULL, &error);
557 g_assert_no_error (error);
559 g_assert (!g_file_query_exists (data->file, NULL));
561 g_main_loop_quit (data->loop);
566 replaced_cb (GObject *source,
570 ReplaceLoadData *data = user_data;
574 g_file_replace_contents_finish (data->file, res, NULL, &error);
575 g_assert_no_error (error);
577 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
581 test_replace_load (void)
583 ReplaceLoadData *data;
585 GFileIOStream *iostream;
587 data = g_new0 (ReplaceLoadData, 1);
589 data->data = replace_data;
591 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
593 g_assert (data->file != NULL);
594 g_object_unref (iostream);
596 path = g_file_get_path (data->file);
599 g_assert (!g_file_query_exists (data->file, NULL));
601 data->loop = g_main_loop_new (NULL, FALSE);
603 g_file_replace_contents_async (data->file,
613 g_main_loop_run (data->loop);
615 g_main_loop_unref (data->loop);
616 g_object_unref (data->file);
622 test_replace_cancel (void)
624 GFile *tmpdir, *file;
625 GFileOutputStream *ostream;
626 GFileEnumerator *fenum;
628 GCancellable *cancellable;
631 GError *error = NULL;
633 g_test_bug ("629301");
635 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
636 g_assert_no_error (error);
637 tmpdir = g_file_new_for_path (path);
640 file = g_file_get_child (tmpdir, "file");
641 g_file_replace_contents (file,
643 strlen (replace_data),
644 NULL, FALSE, 0, NULL,
646 g_assert_no_error (error);
648 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
649 g_assert_no_error (error);
651 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
652 replace_data, strlen (replace_data),
653 &nwrote, NULL, &error);
654 g_assert_no_error (error);
655 g_assert_cmpint (nwrote, ==, strlen (replace_data));
657 /* At this point there should be two files; the original and the
660 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
661 g_assert_no_error (error);
663 info = g_file_enumerator_next_file (fenum, NULL, &error);
664 g_assert_no_error (error);
665 g_assert (info != NULL);
666 g_object_unref (info);
667 info = g_file_enumerator_next_file (fenum, NULL, &error);
668 g_assert_no_error (error);
669 g_assert (info != NULL);
670 g_object_unref (info);
672 g_file_enumerator_close (fenum, NULL, &error);
673 g_assert_no_error (error);
674 g_object_unref (fenum);
676 /* Make sure the temporary gets deleted even if we cancel. */
677 cancellable = g_cancellable_new ();
678 g_cancellable_cancel (cancellable);
679 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
680 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
681 g_clear_error (&error);
683 g_object_unref (cancellable);
684 g_object_unref (ostream);
686 g_file_delete (file, NULL, &error);
687 g_assert_no_error (error);
688 g_object_unref (file);
690 /* This will only succeed if the temp file was deleted. */
691 g_file_delete (tmpdir, NULL, &error);
692 g_assert_no_error (error);
693 g_object_unref (tmpdir);
697 on_file_deleted (GObject *object,
698 GAsyncResult *result,
701 GError *local_error = NULL;
702 GMainLoop *loop = user_data;
704 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
705 g_assert_no_error (local_error);
707 g_main_loop_quit (loop);
711 test_async_delete (void)
714 GFileIOStream *iostream;
715 GError *local_error = NULL;
716 GError **error = &local_error;
719 file = g_file_new_tmp ("g_file_delete_XXXXXX",
721 g_assert_no_error (local_error);
722 g_object_unref (iostream);
724 g_assert (g_file_query_exists (file, NULL));
726 loop = g_main_loop_new (NULL, TRUE);
728 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
730 g_main_loop_run (loop);
732 g_assert (!g_file_query_exists (file, NULL));
734 g_main_loop_unref (loop);
735 g_object_unref (file);
740 test_copy_preserve_mode (void)
744 GFileInfo *dest_info;
745 GFileIOStream *iostream;
746 GError *local_error = NULL;
747 GError **error = &local_error;
748 guint32 romode = S_IFREG | 0600;
751 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
753 g_assert_no_error (local_error);
754 g_io_stream_close ((GIOStream*)iostream, NULL, error);
755 g_assert_no_error (local_error);
756 g_clear_object (&iostream);
758 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
759 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
761 g_assert_no_error (local_error);
763 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
765 g_assert_no_error (local_error);
766 g_io_stream_close ((GIOStream*)iostream, NULL, error);
767 g_assert_no_error (local_error);
768 g_clear_object (&iostream);
770 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
771 NULL, NULL, NULL, error);
772 g_assert_no_error (local_error);
774 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
776 g_assert_no_error (local_error);
778 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
780 g_assert_cmpint (dest_mode, ==, romode);
782 (void) g_file_delete (tmpfile, NULL, NULL);
783 (void) g_file_delete (dest_tmpfile, NULL, NULL);
785 g_clear_object (&tmpfile);
786 g_clear_object (&dest_tmpfile);
787 g_clear_object (&dest_info);
792 main (int argc, char *argv[])
794 g_test_init (&argc, &argv, NULL);
796 g_test_bug_base ("http://bugzilla.gnome.org/");
798 g_test_add_func ("/file/basic", test_basic);
799 g_test_add_func ("/file/parent", test_parent);
800 g_test_add_func ("/file/child", test_child);
801 g_test_add_func ("/file/type", test_type);
802 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
803 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
804 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
805 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
806 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
807 g_test_add_func ("/file/replace-load", test_replace_load);
808 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
809 g_test_add_func ("/file/async-delete", test_async_delete);
811 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
814 return g_test_run ();