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);
86 file = g_file_new_for_path (SRCDIR "/file.c");
87 type = g_file_query_file_type (file, 0, NULL);
88 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
89 g_object_unref (file);
91 file = g_file_new_for_path (SRCDIR "/schema-tests");
92 type = g_file_query_file_type (file, 0, NULL);
93 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
95 g_file_read (file, NULL, &error);
96 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
98 g_object_unref (file);
105 GFileMonitor *monitor;
106 GOutputStream *ostream;
107 GInputStream *istream;
110 gint monitor_created;
111 gint monitor_deleted;
112 gint monitor_changed;
121 monitor_changed (GFileMonitor *monitor,
124 GFileMonitorEvent event_type,
127 CreateDeleteData *data = user_data;
130 path = g_file_get_path (file);
131 g_assert_cmpstr (data->monitor_path, ==, path);
134 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
135 data->monitor_created++;
136 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
137 data->monitor_deleted++;
138 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
139 data->monitor_changed++;
144 quit_idle (gpointer user_data)
146 CreateDeleteData *data = user_data;
148 g_source_remove (data->timeout);
149 g_main_loop_quit (data->loop);
155 iclosed_cb (GObject *source,
159 CreateDeleteData *data = user_data;
164 ret = g_input_stream_close_finish (data->istream, res, &error);
165 g_assert_no_error (error);
168 g_assert (g_input_stream_is_closed (data->istream));
170 ret = g_file_delete (data->file, NULL, &error);
172 g_assert_no_error (error);
174 /* work around file monitor bug:
175 * inotify events are only processed every 1000 ms, regardless
176 * of the rate limit set on the file monitor
178 g_timeout_add (2000, quit_idle, data);
182 read_cb (GObject *source,
186 CreateDeleteData *data = user_data;
191 size = g_input_stream_read_finish (data->istream, res, &error);
192 g_assert_no_error (error);
195 if (data->pos < strlen (data->data))
197 g_input_stream_read_async (data->istream,
198 data->buffer + data->pos,
199 strlen (data->data) - data->pos,
207 g_assert_cmpstr (data->buffer, ==, data->data);
208 g_assert (!g_input_stream_is_closed (data->istream));
209 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
214 ipending_cb (GObject *source,
218 CreateDeleteData *data = user_data;
222 g_input_stream_read_finish (data->istream, res, &error);
223 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
224 g_error_free (error);
228 skipped_cb (GObject *source,
232 CreateDeleteData *data = user_data;
237 size = g_input_stream_skip_finish (data->istream, res, &error);
238 g_assert_no_error (error);
239 g_assert_cmpint (size, ==, data->pos);
241 g_input_stream_read_async (data->istream,
242 data->buffer + data->pos,
243 strlen (data->data) - data->pos,
248 /* check that we get a pending error */
249 g_input_stream_read_async (data->istream,
250 data->buffer + data->pos,
251 strlen (data->data) - data->pos,
259 opened_cb (GObject *source,
263 GFileInputStream *base;
264 CreateDeleteData *data = user_data;
268 base = g_file_read_finish (data->file, res, &error);
269 g_assert_no_error (error);
271 if (data->buffersize == 0)
272 data->istream = G_INPUT_STREAM (g_object_ref (base));
274 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
275 g_object_unref (base);
277 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
279 /* copy initial segment directly, then skip */
280 memcpy (data->buffer, data->data, 10);
283 g_input_stream_skip_async (data->istream,
292 oclosed_cb (GObject *source,
296 CreateDeleteData *data = user_data;
301 ret = g_output_stream_close_finish (data->ostream, res, &error);
302 g_assert_no_error (error);
304 g_assert (g_output_stream_is_closed (data->ostream));
306 g_file_read_async (data->file, 0, NULL, opened_cb, data);
310 written_cb (GObject *source,
314 CreateDeleteData *data = user_data;
319 size = g_output_stream_write_finish (data->ostream, res, &error);
320 g_assert_no_error (error);
323 if (data->pos < strlen (data->data))
325 g_output_stream_write_async (data->ostream,
326 data->data + data->pos,
327 strlen (data->data) - data->pos,
335 g_assert (!g_output_stream_is_closed (data->ostream));
336 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
341 opending_cb (GObject *source,
345 CreateDeleteData *data = user_data;
349 g_output_stream_write_finish (data->ostream, res, &error);
350 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
351 g_error_free (error);
355 created_cb (GObject *source,
359 GFileOutputStream *base;
360 CreateDeleteData *data = user_data;
364 base = g_file_create_finish (G_FILE (source), res, &error);
365 g_assert_no_error (error);
366 g_assert (g_file_query_exists (data->file, NULL));
368 if (data->buffersize == 0)
369 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
371 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
372 g_object_unref (base);
374 g_output_stream_write_async (data->ostream,
381 /* check that we get a pending error */
382 g_output_stream_write_async (data->ostream,
392 stop_timeout (gpointer data)
394 g_assert_not_reached ();
400 * This test does a fully async create-write-read-delete.
404 test_create_delete (gconstpointer d)
407 CreateDeleteData *data;
408 GFileIOStream *iostream;
410 data = g_new0 (CreateDeleteData, 1);
412 data->buffersize = GPOINTER_TO_INT (d);
413 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
416 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
418 g_assert (data->file != NULL);
419 g_object_unref (iostream);
421 data->monitor_path = g_file_get_path (data->file);
422 remove (data->monitor_path);
424 g_assert (!g_file_query_exists (data->file, NULL));
427 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
428 g_assert_no_error (error);
430 /* This test doesn't work with GPollFileMonitor, because it assumes
431 * that the monitor will notice a create immediately followed by a
432 * delete, rather than coalescing them into nothing.
434 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
436 g_print ("skipping test for this GFileMonitor implementation");
440 g_file_monitor_set_rate_limit (data->monitor, 100);
442 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
444 data->loop = g_main_loop_new (NULL, FALSE);
446 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
448 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
450 g_main_loop_run (data->loop);
452 g_assert_cmpint (data->monitor_created, ==, 1);
453 g_assert_cmpint (data->monitor_deleted, ==, 1);
454 g_assert_cmpint (data->monitor_changed, >, 0);
456 g_assert (!g_file_monitor_is_cancelled (data->monitor));
457 g_file_monitor_cancel (data->monitor);
458 g_assert (g_file_monitor_is_cancelled (data->monitor));
460 g_main_loop_unref (data->loop);
461 g_object_unref (data->ostream);
462 g_object_unref (data->istream);
465 g_object_unref (data->monitor);
466 g_object_unref (data->file);
467 free (data->monitor_path);
468 g_free (data->buffer);
472 static const gchar *replace_data =
474 " * g_file_replace_contents_async:\n"
475 " * @file: input #GFile.\n"
476 " * @contents: string of contents to replace the file with.\n"
477 " * @length: the length of @contents in bytes.\n"
478 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
479 " * @make_backup: %TRUE if a backup should be created.\n"
480 " * @flags: a set of #GFileCreateFlags.\n"
481 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
482 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
483 " * @user_data: the data to pass to callback function\n"
485 " * Starts an asynchronous replacement of @file with the given \n"
486 " * @contents of @length bytes. @etag will replace the document's\n"
487 " * current entity tag.\n"
489 " * When this operation has completed, @callback will be called with\n"
490 " * @user_user data, and the operation can be finalized with \n"
491 " * g_file_replace_contents_finish().\n"
493 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
494 " * triggering the cancellable object from another thread. If the operation\n"
495 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
497 " * If @make_backup is %TRUE, this function will attempt to \n"
498 " * make a backup of @file.\n"
509 static void replaced_cb (GObject *source,
514 loaded_cb (GObject *source,
518 ReplaceLoadData *data = user_data;
525 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
527 g_assert_no_error (error);
528 g_assert_cmpint (length, ==, strlen (data->data));
529 g_assert_cmpstr (contents, ==, data->data);
536 data->data = "pi pa po";
538 g_file_replace_contents_async (data->file,
551 ret = g_file_delete (data->file, NULL, &error);
552 g_assert_no_error (error);
554 g_assert (!g_file_query_exists (data->file, NULL));
556 g_main_loop_quit (data->loop);
561 replaced_cb (GObject *source,
565 ReplaceLoadData *data = user_data;
569 g_file_replace_contents_finish (data->file, res, NULL, &error);
570 g_assert_no_error (error);
572 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
576 test_replace_load (void)
578 ReplaceLoadData *data;
580 GFileIOStream *iostream;
582 data = g_new0 (ReplaceLoadData, 1);
584 data->data = replace_data;
586 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
588 g_assert (data->file != NULL);
589 g_object_unref (iostream);
591 path = g_file_get_path (data->file);
594 g_assert (!g_file_query_exists (data->file, NULL));
596 data->loop = g_main_loop_new (NULL, FALSE);
598 g_file_replace_contents_async (data->file,
608 g_main_loop_run (data->loop);
610 g_main_loop_unref (data->loop);
611 g_object_unref (data->file);
617 test_replace_cancel (void)
619 GFile *tmpdir, *file;
620 GFileOutputStream *ostream;
621 GFileEnumerator *fenum;
623 GCancellable *cancellable;
626 GError *error = NULL;
628 g_test_bug ("629301");
630 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
631 g_assert_no_error (error);
632 tmpdir = g_file_new_for_path (path);
635 file = g_file_get_child (tmpdir, "file");
636 g_file_replace_contents (file,
638 strlen (replace_data),
639 NULL, FALSE, 0, NULL,
641 g_assert_no_error (error);
643 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
644 g_assert_no_error (error);
646 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
647 replace_data, strlen (replace_data),
648 &nwrote, NULL, &error);
649 g_assert_no_error (error);
650 g_assert_cmpint (nwrote, ==, strlen (replace_data));
652 /* At this point there should be two files; the original and the
655 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
656 g_assert_no_error (error);
658 info = g_file_enumerator_next_file (fenum, NULL, &error);
659 g_assert_no_error (error);
660 g_assert (info != NULL);
661 g_object_unref (info);
662 info = g_file_enumerator_next_file (fenum, NULL, &error);
663 g_assert_no_error (error);
664 g_assert (info != NULL);
665 g_object_unref (info);
667 g_file_enumerator_close (fenum, NULL, &error);
668 g_assert_no_error (error);
669 g_object_unref (fenum);
671 /* Make sure the temporary gets deleted even if we cancel. */
672 cancellable = g_cancellable_new ();
673 g_cancellable_cancel (cancellable);
674 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
675 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
676 g_clear_error (&error);
678 g_object_unref (cancellable);
679 g_object_unref (ostream);
681 g_file_delete (file, NULL, &error);
682 g_assert_no_error (error);
683 g_object_unref (file);
685 /* This will only succeed if the temp file was deleted. */
686 g_file_delete (tmpdir, NULL, &error);
687 g_assert_no_error (error);
688 g_object_unref (tmpdir);
692 on_file_deleted (GObject *object,
693 GAsyncResult *result,
696 GError *local_error = NULL;
697 GMainLoop *loop = user_data;
699 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
700 g_assert_no_error (local_error);
702 g_main_loop_quit (loop);
706 test_async_delete (void)
709 GFileIOStream *iostream;
710 GError *local_error = NULL;
711 GError **error = &local_error;
714 file = g_file_new_tmp ("g_file_delete_XXXXXX",
716 g_assert_no_error (local_error);
717 g_object_unref (iostream);
719 g_assert (g_file_query_exists (file, NULL));
721 loop = g_main_loop_new (NULL, TRUE);
723 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
725 g_main_loop_run (loop);
727 g_assert (!g_file_query_exists (file, NULL));
729 g_main_loop_unref (loop);
730 g_object_unref (file);
735 test_copy_preserve_mode (void)
739 GFileInfo *dest_info;
740 GFileIOStream *iostream;
741 GError *local_error = NULL;
742 GError **error = &local_error;
743 guint32 romode = S_IFREG | 0600;
746 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
748 g_assert_no_error (local_error);
749 g_io_stream_close ((GIOStream*)iostream, NULL, error);
750 g_assert_no_error (local_error);
751 g_clear_object (&iostream);
753 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
754 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
756 g_assert_no_error (local_error);
758 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
760 g_assert_no_error (local_error);
761 g_io_stream_close ((GIOStream*)iostream, NULL, error);
762 g_assert_no_error (local_error);
763 g_clear_object (&iostream);
765 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
766 NULL, NULL, NULL, error);
767 g_assert_no_error (local_error);
769 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
771 g_assert_no_error (local_error);
773 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
775 g_assert_cmpint (dest_mode, ==, romode);
777 (void) g_file_delete (tmpfile, NULL, NULL);
778 (void) g_file_delete (dest_tmpfile, NULL, NULL);
780 g_clear_object (&tmpfile);
781 g_clear_object (&dest_tmpfile);
782 g_clear_object (&dest_info);
787 main (int argc, char *argv[])
789 g_test_init (&argc, &argv, NULL);
791 g_test_bug_base ("http://bugzilla.gnome.org/");
793 g_test_add_func ("/file/basic", test_basic);
794 g_test_add_func ("/file/parent", test_parent);
795 g_test_add_func ("/file/child", test_child);
796 g_test_add_func ("/file/type", test_type);
797 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
798 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
799 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
800 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
801 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
802 g_test_add_func ("/file/replace-load", test_replace_load);
803 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
804 g_test_add_func ("/file/async-delete", test_async_delete);
806 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
809 return g_test_run ();