5 #include <gio/gfiledescriptorbased.h>
10 static const gchar *datapath;
18 file = g_file_new_for_path ("./some/directory/testfile");
20 s = g_file_get_basename (file);
21 g_assert_cmpstr (s, ==, "testfile");
24 s = g_file_get_uri (file);
25 g_assert (g_str_has_prefix (s, "file://"));
26 g_assert (g_str_has_suffix (s, "/some/directory/testfile"));
29 g_assert (g_file_has_uri_scheme (file, "file"));
30 s = g_file_get_uri_scheme (file);
31 g_assert_cmpstr (s, ==, "file");
34 g_object_unref (file);
45 file = g_file_new_for_path ("./some/directory/testfile");
46 file2 = g_file_new_for_path ("./some/directory");
47 root = g_file_new_for_path ("/");
49 g_assert (g_file_has_parent (file, file2));
51 parent = g_file_get_parent (file);
52 g_assert (g_file_equal (parent, file2));
53 g_object_unref (parent);
55 g_assert (g_file_get_parent (root) == NULL);
57 g_object_unref (file);
58 g_object_unref (file2);
59 g_object_unref (root);
69 file = g_file_new_for_path ("./some/directory");
70 child = g_file_get_child (file, "child");
71 g_assert (g_file_has_parent (child, file));
73 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
74 g_assert (g_file_has_parent (child2, file));
76 g_object_unref (child);
77 g_object_unref (child2);
78 g_object_unref (file);
89 datapath_f = g_file_new_for_path (datapath);
91 file = g_file_get_child (datapath_f, "g-icon.c");
92 type = g_file_query_file_type (file, 0, NULL);
93 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
94 g_object_unref (file);
96 file = g_file_get_child (datapath_f, "schema-tests");
97 type = g_file_query_file_type (file, 0, NULL);
98 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
100 g_file_read (file, NULL, &error);
101 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
102 g_error_free (error);
103 g_object_unref (file);
105 g_object_unref (datapath_f);
112 GFileMonitor *monitor;
113 GOutputStream *ostream;
114 GInputStream *istream;
117 gint monitor_created;
118 gint monitor_deleted;
119 gint monitor_changed;
128 monitor_changed (GFileMonitor *monitor,
131 GFileMonitorEvent event_type,
134 CreateDeleteData *data = user_data;
137 path = g_file_get_path (file);
138 g_assert_cmpstr (data->monitor_path, ==, path);
141 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
142 data->monitor_created++;
143 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
144 data->monitor_deleted++;
145 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
146 data->monitor_changed++;
151 quit_idle (gpointer user_data)
153 CreateDeleteData *data = user_data;
155 g_source_remove (data->timeout);
156 g_main_loop_quit (data->loop);
162 iclosed_cb (GObject *source,
166 CreateDeleteData *data = user_data;
171 ret = g_input_stream_close_finish (data->istream, res, &error);
172 g_assert_no_error (error);
175 g_assert (g_input_stream_is_closed (data->istream));
177 ret = g_file_delete (data->file, NULL, &error);
179 g_assert_no_error (error);
181 /* work around file monitor bug:
182 * inotify events are only processed every 1000 ms, regardless
183 * of the rate limit set on the file monitor
185 g_timeout_add (2000, quit_idle, data);
189 read_cb (GObject *source,
193 CreateDeleteData *data = user_data;
198 size = g_input_stream_read_finish (data->istream, res, &error);
199 g_assert_no_error (error);
202 if (data->pos < strlen (data->data))
204 g_input_stream_read_async (data->istream,
205 data->buffer + data->pos,
206 strlen (data->data) - data->pos,
214 g_assert_cmpstr (data->buffer, ==, data->data);
215 g_assert (!g_input_stream_is_closed (data->istream));
216 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
221 ipending_cb (GObject *source,
225 CreateDeleteData *data = user_data;
229 g_input_stream_read_finish (data->istream, res, &error);
230 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
231 g_error_free (error);
235 skipped_cb (GObject *source,
239 CreateDeleteData *data = user_data;
244 size = g_input_stream_skip_finish (data->istream, res, &error);
245 g_assert_no_error (error);
246 g_assert_cmpint (size, ==, data->pos);
248 g_input_stream_read_async (data->istream,
249 data->buffer + data->pos,
250 strlen (data->data) - data->pos,
255 /* check that we get a pending error */
256 g_input_stream_read_async (data->istream,
257 data->buffer + data->pos,
258 strlen (data->data) - data->pos,
266 opened_cb (GObject *source,
270 GFileInputStream *base;
271 CreateDeleteData *data = user_data;
275 base = g_file_read_finish (data->file, res, &error);
276 g_assert_no_error (error);
278 if (data->buffersize == 0)
279 data->istream = G_INPUT_STREAM (g_object_ref (base));
281 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
282 g_object_unref (base);
284 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
286 /* copy initial segment directly, then skip */
287 memcpy (data->buffer, data->data, 10);
290 g_input_stream_skip_async (data->istream,
299 oclosed_cb (GObject *source,
303 CreateDeleteData *data = user_data;
308 ret = g_output_stream_close_finish (data->ostream, res, &error);
309 g_assert_no_error (error);
311 g_assert (g_output_stream_is_closed (data->ostream));
313 g_file_read_async (data->file, 0, NULL, opened_cb, data);
317 written_cb (GObject *source,
321 CreateDeleteData *data = user_data;
326 size = g_output_stream_write_finish (data->ostream, res, &error);
327 g_assert_no_error (error);
330 if (data->pos < strlen (data->data))
332 g_output_stream_write_async (data->ostream,
333 data->data + data->pos,
334 strlen (data->data) - data->pos,
342 g_assert (!g_output_stream_is_closed (data->ostream));
343 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
348 opending_cb (GObject *source,
352 CreateDeleteData *data = user_data;
356 g_output_stream_write_finish (data->ostream, res, &error);
357 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
358 g_error_free (error);
362 created_cb (GObject *source,
366 GFileOutputStream *base;
367 CreateDeleteData *data = user_data;
371 base = g_file_create_finish (G_FILE (source), res, &error);
372 g_assert_no_error (error);
373 g_assert (g_file_query_exists (data->file, NULL));
375 if (data->buffersize == 0)
376 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
378 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
379 g_object_unref (base);
381 g_output_stream_write_async (data->ostream,
388 /* check that we get a pending error */
389 g_output_stream_write_async (data->ostream,
399 stop_timeout (gpointer data)
401 g_assert_not_reached ();
407 * This test does a fully async create-write-read-delete.
411 test_create_delete (gconstpointer d)
414 CreateDeleteData *data;
415 GFileIOStream *iostream;
417 data = g_new0 (CreateDeleteData, 1);
419 data->buffersize = GPOINTER_TO_INT (d);
420 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
423 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
425 g_assert (data->file != NULL);
426 g_object_unref (iostream);
428 data->monitor_path = g_file_get_path (data->file);
429 remove (data->monitor_path);
431 g_assert (!g_file_query_exists (data->file, NULL));
434 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
435 g_assert_no_error (error);
437 /* This test doesn't work with GPollFileMonitor, because it assumes
438 * that the monitor will notice a create immediately followed by a
439 * delete, rather than coalescing them into nothing.
441 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
443 g_print ("skipping test for this GFileMonitor implementation");
447 g_file_monitor_set_rate_limit (data->monitor, 100);
449 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
451 data->loop = g_main_loop_new (NULL, FALSE);
453 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
455 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
457 g_main_loop_run (data->loop);
459 g_assert_cmpint (data->monitor_created, ==, 1);
460 g_assert_cmpint (data->monitor_deleted, ==, 1);
461 g_assert_cmpint (data->monitor_changed, >, 0);
463 g_assert (!g_file_monitor_is_cancelled (data->monitor));
464 g_file_monitor_cancel (data->monitor);
465 g_assert (g_file_monitor_is_cancelled (data->monitor));
467 g_main_loop_unref (data->loop);
468 g_object_unref (data->ostream);
469 g_object_unref (data->istream);
472 g_object_unref (data->monitor);
473 g_object_unref (data->file);
474 free (data->monitor_path);
475 g_free (data->buffer);
479 static const gchar *replace_data =
481 " * g_file_replace_contents_async:\n"
482 " * @file: input #GFile.\n"
483 " * @contents: string of contents to replace the file with.\n"
484 " * @length: the length of @contents in bytes.\n"
485 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
486 " * @make_backup: %TRUE if a backup should be created.\n"
487 " * @flags: a set of #GFileCreateFlags.\n"
488 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
489 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
490 " * @user_data: the data to pass to callback function\n"
492 " * Starts an asynchronous replacement of @file with the given \n"
493 " * @contents of @length bytes. @etag will replace the document's\n"
494 " * current entity tag.\n"
496 " * When this operation has completed, @callback will be called with\n"
497 " * @user_user data, and the operation can be finalized with \n"
498 " * g_file_replace_contents_finish().\n"
500 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
501 " * triggering the cancellable object from another thread. If the operation\n"
502 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
504 " * If @make_backup is %TRUE, this function will attempt to \n"
505 " * make a backup of @file.\n"
516 static void replaced_cb (GObject *source,
521 loaded_cb (GObject *source,
525 ReplaceLoadData *data = user_data;
532 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
534 g_assert_no_error (error);
535 g_assert_cmpint (length, ==, strlen (data->data));
536 g_assert_cmpstr (contents, ==, data->data);
543 data->data = "pi pa po";
545 g_file_replace_contents_async (data->file,
558 ret = g_file_delete (data->file, NULL, &error);
559 g_assert_no_error (error);
561 g_assert (!g_file_query_exists (data->file, NULL));
563 g_main_loop_quit (data->loop);
568 replaced_cb (GObject *source,
572 ReplaceLoadData *data = user_data;
576 g_file_replace_contents_finish (data->file, res, NULL, &error);
577 g_assert_no_error (error);
579 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
583 test_replace_load (void)
585 ReplaceLoadData *data;
587 GFileIOStream *iostream;
589 data = g_new0 (ReplaceLoadData, 1);
591 data->data = replace_data;
593 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
595 g_assert (data->file != NULL);
596 g_object_unref (iostream);
598 path = g_file_get_path (data->file);
601 g_assert (!g_file_query_exists (data->file, NULL));
603 data->loop = g_main_loop_new (NULL, FALSE);
605 g_file_replace_contents_async (data->file,
615 g_main_loop_run (data->loop);
617 g_main_loop_unref (data->loop);
618 g_object_unref (data->file);
624 test_replace_cancel (void)
626 GFile *tmpdir, *file;
627 GFileOutputStream *ostream;
628 GFileEnumerator *fenum;
630 GCancellable *cancellable;
633 GError *error = NULL;
635 g_test_bug ("629301");
637 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
638 g_assert_no_error (error);
639 tmpdir = g_file_new_for_path (path);
642 file = g_file_get_child (tmpdir, "file");
643 g_file_replace_contents (file,
645 strlen (replace_data),
646 NULL, FALSE, 0, NULL,
648 g_assert_no_error (error);
650 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
651 g_assert_no_error (error);
653 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
654 replace_data, strlen (replace_data),
655 &nwrote, NULL, &error);
656 g_assert_no_error (error);
657 g_assert_cmpint (nwrote, ==, strlen (replace_data));
659 /* At this point there should be two files; the original and the
662 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
663 g_assert_no_error (error);
665 info = g_file_enumerator_next_file (fenum, NULL, &error);
666 g_assert_no_error (error);
667 g_assert (info != NULL);
668 g_object_unref (info);
669 info = g_file_enumerator_next_file (fenum, NULL, &error);
670 g_assert_no_error (error);
671 g_assert (info != NULL);
672 g_object_unref (info);
674 g_file_enumerator_close (fenum, NULL, &error);
675 g_assert_no_error (error);
676 g_object_unref (fenum);
678 /* Make sure the temporary gets deleted even if we cancel. */
679 cancellable = g_cancellable_new ();
680 g_cancellable_cancel (cancellable);
681 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
682 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
683 g_clear_error (&error);
685 g_object_unref (cancellable);
686 g_object_unref (ostream);
688 g_file_delete (file, NULL, &error);
689 g_assert_no_error (error);
690 g_object_unref (file);
692 /* This will only succeed if the temp file was deleted. */
693 g_file_delete (tmpdir, NULL, &error);
694 g_assert_no_error (error);
695 g_object_unref (tmpdir);
699 on_file_deleted (GObject *object,
700 GAsyncResult *result,
703 GError *local_error = NULL;
704 GMainLoop *loop = user_data;
706 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
707 g_assert_no_error (local_error);
709 g_main_loop_quit (loop);
713 test_async_delete (void)
716 GFileIOStream *iostream;
717 GError *local_error = NULL;
718 GError **error = &local_error;
721 file = g_file_new_tmp ("g_file_delete_XXXXXX",
723 g_assert_no_error (local_error);
724 g_object_unref (iostream);
726 g_assert (g_file_query_exists (file, NULL));
728 loop = g_main_loop_new (NULL, TRUE);
730 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
732 g_main_loop_run (loop);
734 g_assert (!g_file_query_exists (file, NULL));
736 g_main_loop_unref (loop);
737 g_object_unref (file);
742 test_copy_preserve_mode (void)
746 GFileInfo *dest_info;
747 GFileIOStream *iostream;
748 GError *local_error = NULL;
749 GError **error = &local_error;
750 guint32 romode = S_IFREG | 0600;
753 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
755 g_assert_no_error (local_error);
756 g_io_stream_close ((GIOStream*)iostream, NULL, error);
757 g_assert_no_error (local_error);
758 g_clear_object (&iostream);
760 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
761 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
763 g_assert_no_error (local_error);
765 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
767 g_assert_no_error (local_error);
768 g_io_stream_close ((GIOStream*)iostream, NULL, error);
769 g_assert_no_error (local_error);
770 g_clear_object (&iostream);
772 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
773 NULL, NULL, NULL, error);
774 g_assert_no_error (local_error);
776 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
778 g_assert_no_error (local_error);
780 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
782 g_assert_cmpint (dest_mode, ==, romode);
784 (void) g_file_delete (tmpfile, NULL, NULL);
785 (void) g_file_delete (dest_tmpfile, NULL, NULL);
787 g_clear_object (&tmpfile);
788 g_clear_object (&dest_tmpfile);
789 g_clear_object (&dest_info);
794 main (int argc, char *argv[])
796 if (g_getenv ("G_TEST_DATA"))
797 datapath = g_getenv ("G_TEST_DATA");
801 g_test_init (&argc, &argv, NULL);
803 g_test_bug_base ("http://bugzilla.gnome.org/");
805 g_test_add_func ("/file/basic", test_basic);
806 g_test_add_func ("/file/parent", test_parent);
807 g_test_add_func ("/file/child", test_child);
808 g_test_add_func ("/file/type", test_type);
809 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
810 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
811 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
812 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
813 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
814 g_test_add_func ("/file/replace-load", test_replace_load);
815 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
816 g_test_add_func ("/file/async-delete", test_async_delete);
818 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
821 return g_test_run ();