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);
88 datapath = g_test_build_filename (G_TEST_DISTED, ".", NULL);
89 datapath_f = g_file_new_for_path (datapath);
92 file = g_file_get_child (datapath_f, "g-icon.c");
93 type = g_file_query_file_type (file, 0, NULL);
94 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
95 g_object_unref (file);
97 file = g_file_get_child (datapath_f, "schema-tests");
98 type = g_file_query_file_type (file, 0, NULL);
99 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
101 g_file_read (file, NULL, &error);
102 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
103 g_error_free (error);
104 g_object_unref (file);
106 g_object_unref (datapath_f);
113 GFileMonitor *monitor;
114 GOutputStream *ostream;
115 GInputStream *istream;
118 gint monitor_created;
119 gint monitor_deleted;
120 gint monitor_changed;
129 monitor_changed (GFileMonitor *monitor,
132 GFileMonitorEvent event_type,
135 CreateDeleteData *data = user_data;
138 path = g_file_get_path (file);
139 g_assert_cmpstr (data->monitor_path, ==, path);
142 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
143 data->monitor_created++;
144 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
145 data->monitor_deleted++;
146 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
147 data->monitor_changed++;
152 quit_idle (gpointer user_data)
154 CreateDeleteData *data = user_data;
156 g_source_remove (data->timeout);
157 g_main_loop_quit (data->loop);
163 iclosed_cb (GObject *source,
167 CreateDeleteData *data = user_data;
172 ret = g_input_stream_close_finish (data->istream, res, &error);
173 g_assert_no_error (error);
176 g_assert (g_input_stream_is_closed (data->istream));
178 ret = g_file_delete (data->file, NULL, &error);
180 g_assert_no_error (error);
182 /* work around file monitor bug:
183 * inotify events are only processed every 1000 ms, regardless
184 * of the rate limit set on the file monitor
186 g_timeout_add (2000, quit_idle, data);
190 read_cb (GObject *source,
194 CreateDeleteData *data = user_data;
199 size = g_input_stream_read_finish (data->istream, res, &error);
200 g_assert_no_error (error);
203 if (data->pos < strlen (data->data))
205 g_input_stream_read_async (data->istream,
206 data->buffer + data->pos,
207 strlen (data->data) - data->pos,
215 g_assert_cmpstr (data->buffer, ==, data->data);
216 g_assert (!g_input_stream_is_closed (data->istream));
217 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
222 ipending_cb (GObject *source,
226 CreateDeleteData *data = user_data;
230 g_input_stream_read_finish (data->istream, res, &error);
231 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
232 g_error_free (error);
236 skipped_cb (GObject *source,
240 CreateDeleteData *data = user_data;
245 size = g_input_stream_skip_finish (data->istream, res, &error);
246 g_assert_no_error (error);
247 g_assert_cmpint (size, ==, data->pos);
249 g_input_stream_read_async (data->istream,
250 data->buffer + data->pos,
251 strlen (data->data) - data->pos,
256 /* check that we get a pending error */
257 g_input_stream_read_async (data->istream,
258 data->buffer + data->pos,
259 strlen (data->data) - data->pos,
267 opened_cb (GObject *source,
271 GFileInputStream *base;
272 CreateDeleteData *data = user_data;
276 base = g_file_read_finish (data->file, res, &error);
277 g_assert_no_error (error);
279 if (data->buffersize == 0)
280 data->istream = G_INPUT_STREAM (g_object_ref (base));
282 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
283 g_object_unref (base);
285 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
287 /* copy initial segment directly, then skip */
288 memcpy (data->buffer, data->data, 10);
291 g_input_stream_skip_async (data->istream,
300 oclosed_cb (GObject *source,
304 CreateDeleteData *data = user_data;
309 ret = g_output_stream_close_finish (data->ostream, res, &error);
310 g_assert_no_error (error);
312 g_assert (g_output_stream_is_closed (data->ostream));
314 g_file_read_async (data->file, 0, NULL, opened_cb, data);
318 written_cb (GObject *source,
322 CreateDeleteData *data = user_data;
327 size = g_output_stream_write_finish (data->ostream, res, &error);
328 g_assert_no_error (error);
331 if (data->pos < strlen (data->data))
333 g_output_stream_write_async (data->ostream,
334 data->data + data->pos,
335 strlen (data->data) - data->pos,
343 g_assert (!g_output_stream_is_closed (data->ostream));
344 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
349 opending_cb (GObject *source,
353 CreateDeleteData *data = user_data;
357 g_output_stream_write_finish (data->ostream, res, &error);
358 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
359 g_error_free (error);
363 created_cb (GObject *source,
367 GFileOutputStream *base;
368 CreateDeleteData *data = user_data;
372 base = g_file_create_finish (G_FILE (source), res, &error);
373 g_assert_no_error (error);
374 g_assert (g_file_query_exists (data->file, NULL));
376 if (data->buffersize == 0)
377 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
379 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
380 g_object_unref (base);
382 g_output_stream_write_async (data->ostream,
389 /* check that we get a pending error */
390 g_output_stream_write_async (data->ostream,
400 stop_timeout (gpointer data)
402 g_assert_not_reached ();
408 * This test does a fully async create-write-read-delete.
412 test_create_delete (gconstpointer d)
415 CreateDeleteData *data;
416 GFileIOStream *iostream;
418 data = g_new0 (CreateDeleteData, 1);
420 data->buffersize = GPOINTER_TO_INT (d);
421 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
424 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
426 g_assert (data->file != NULL);
427 g_object_unref (iostream);
429 data->monitor_path = g_file_get_path (data->file);
430 remove (data->monitor_path);
432 g_assert (!g_file_query_exists (data->file, NULL));
435 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
436 g_assert_no_error (error);
438 /* This test doesn't work with GPollFileMonitor, because it assumes
439 * that the monitor will notice a create immediately followed by a
440 * delete, rather than coalescing them into nothing.
442 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
444 g_print ("skipping test for this GFileMonitor implementation");
448 g_file_monitor_set_rate_limit (data->monitor, 100);
450 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
452 data->loop = g_main_loop_new (NULL, FALSE);
454 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
456 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
458 g_main_loop_run (data->loop);
460 g_assert_cmpint (data->monitor_created, ==, 1);
461 g_assert_cmpint (data->monitor_deleted, ==, 1);
462 g_assert_cmpint (data->monitor_changed, >, 0);
464 g_assert (!g_file_monitor_is_cancelled (data->monitor));
465 g_file_monitor_cancel (data->monitor);
466 g_assert (g_file_monitor_is_cancelled (data->monitor));
468 g_main_loop_unref (data->loop);
469 g_object_unref (data->ostream);
470 g_object_unref (data->istream);
473 g_object_unref (data->monitor);
474 g_object_unref (data->file);
475 free (data->monitor_path);
476 g_free (data->buffer);
480 static const gchar *replace_data =
482 " * g_file_replace_contents_async:\n"
483 " * @file: input #GFile.\n"
484 " * @contents: string of contents to replace the file with.\n"
485 " * @length: the length of @contents in bytes.\n"
486 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
487 " * @make_backup: %TRUE if a backup should be created.\n"
488 " * @flags: a set of #GFileCreateFlags.\n"
489 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
490 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
491 " * @user_data: the data to pass to callback function\n"
493 " * Starts an asynchronous replacement of @file with the given \n"
494 " * @contents of @length bytes. @etag will replace the document's\n"
495 " * current entity tag.\n"
497 " * When this operation has completed, @callback will be called with\n"
498 " * @user_user data, and the operation can be finalized with \n"
499 " * g_file_replace_contents_finish().\n"
501 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
502 " * triggering the cancellable object from another thread. If the operation\n"
503 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
505 " * If @make_backup is %TRUE, this function will attempt to \n"
506 " * make a backup of @file.\n"
517 static void replaced_cb (GObject *source,
522 loaded_cb (GObject *source,
526 ReplaceLoadData *data = user_data;
533 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
535 g_assert_no_error (error);
536 g_assert_cmpint (length, ==, strlen (data->data));
537 g_assert_cmpstr (contents, ==, data->data);
544 data->data = "pi pa po";
546 g_file_replace_contents_async (data->file,
559 ret = g_file_delete (data->file, NULL, &error);
560 g_assert_no_error (error);
562 g_assert (!g_file_query_exists (data->file, NULL));
564 g_main_loop_quit (data->loop);
569 replaced_cb (GObject *source,
573 ReplaceLoadData *data = user_data;
577 g_file_replace_contents_finish (data->file, res, NULL, &error);
578 g_assert_no_error (error);
580 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
584 test_replace_load (void)
586 ReplaceLoadData *data;
588 GFileIOStream *iostream;
590 data = g_new0 (ReplaceLoadData, 1);
592 data->data = replace_data;
594 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
596 g_assert (data->file != NULL);
597 g_object_unref (iostream);
599 path = g_file_get_path (data->file);
602 g_assert (!g_file_query_exists (data->file, NULL));
604 data->loop = g_main_loop_new (NULL, FALSE);
606 g_file_replace_contents_async (data->file,
616 g_main_loop_run (data->loop);
618 g_main_loop_unref (data->loop);
619 g_object_unref (data->file);
625 test_replace_cancel (void)
627 GFile *tmpdir, *file;
628 GFileOutputStream *ostream;
629 GFileEnumerator *fenum;
631 GCancellable *cancellable;
634 GError *error = NULL;
636 g_test_bug ("629301");
638 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
639 g_assert_no_error (error);
640 tmpdir = g_file_new_for_path (path);
643 file = g_file_get_child (tmpdir, "file");
644 g_file_replace_contents (file,
646 strlen (replace_data),
647 NULL, FALSE, 0, NULL,
649 g_assert_no_error (error);
651 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
652 g_assert_no_error (error);
654 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
655 replace_data, strlen (replace_data),
656 &nwrote, NULL, &error);
657 g_assert_no_error (error);
658 g_assert_cmpint (nwrote, ==, strlen (replace_data));
660 /* At this point there should be two files; the original and the
663 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
664 g_assert_no_error (error);
666 info = g_file_enumerator_next_file (fenum, NULL, &error);
667 g_assert_no_error (error);
668 g_assert (info != NULL);
669 g_object_unref (info);
670 info = g_file_enumerator_next_file (fenum, NULL, &error);
671 g_assert_no_error (error);
672 g_assert (info != NULL);
673 g_object_unref (info);
675 g_file_enumerator_close (fenum, NULL, &error);
676 g_assert_no_error (error);
677 g_object_unref (fenum);
679 /* Make sure the temporary gets deleted even if we cancel. */
680 cancellable = g_cancellable_new ();
681 g_cancellable_cancel (cancellable);
682 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
683 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
684 g_clear_error (&error);
686 g_object_unref (cancellable);
687 g_object_unref (ostream);
689 g_file_delete (file, NULL, &error);
690 g_assert_no_error (error);
691 g_object_unref (file);
693 /* This will only succeed if the temp file was deleted. */
694 g_file_delete (tmpdir, NULL, &error);
695 g_assert_no_error (error);
696 g_object_unref (tmpdir);
700 on_file_deleted (GObject *object,
701 GAsyncResult *result,
704 GError *local_error = NULL;
705 GMainLoop *loop = user_data;
707 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
708 g_assert_no_error (local_error);
710 g_main_loop_quit (loop);
714 test_async_delete (void)
717 GFileIOStream *iostream;
718 GError *local_error = NULL;
719 GError **error = &local_error;
722 file = g_file_new_tmp ("g_file_delete_XXXXXX",
724 g_assert_no_error (local_error);
725 g_object_unref (iostream);
727 g_assert (g_file_query_exists (file, NULL));
729 loop = g_main_loop_new (NULL, TRUE);
731 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
733 g_main_loop_run (loop);
735 g_assert (!g_file_query_exists (file, NULL));
737 g_main_loop_unref (loop);
738 g_object_unref (file);
743 test_copy_preserve_mode (void)
747 GFileInfo *dest_info;
748 GFileIOStream *iostream;
749 GError *local_error = NULL;
750 GError **error = &local_error;
751 guint32 romode = S_IFREG | 0600;
754 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
756 g_assert_no_error (local_error);
757 g_io_stream_close ((GIOStream*)iostream, NULL, error);
758 g_assert_no_error (local_error);
759 g_clear_object (&iostream);
761 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
762 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
764 g_assert_no_error (local_error);
766 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
768 g_assert_no_error (local_error);
769 g_io_stream_close ((GIOStream*)iostream, NULL, error);
770 g_assert_no_error (local_error);
771 g_clear_object (&iostream);
773 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
774 NULL, NULL, NULL, error);
775 g_assert_no_error (local_error);
777 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
779 g_assert_no_error (local_error);
781 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
783 g_assert_cmpint (dest_mode, ==, romode);
785 (void) g_file_delete (tmpfile, NULL, NULL);
786 (void) g_file_delete (dest_tmpfile, NULL, NULL);
788 g_clear_object (&tmpfile);
789 g_clear_object (&dest_tmpfile);
790 g_clear_object (&dest_info);
795 main (int argc, char *argv[])
797 g_test_init (&argc, &argv, NULL);
799 g_test_bug_base ("http://bugzilla.gnome.org/");
801 g_test_add_func ("/file/basic", test_basic);
802 g_test_add_func ("/file/parent", test_parent);
803 g_test_add_func ("/file/child", test_child);
804 g_test_add_func ("/file/type", test_type);
805 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
806 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
807 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
808 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
809 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
810 g_test_add_func ("/file/replace-load", test_replace_load);
811 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
812 g_test_add_func ("/file/async-delete", test_async_delete);
814 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
817 return g_test_run ();