5 #include <gio/gfiledescriptorbased.h>
11 test_basic_for_file (GFile *file,
16 s = g_file_get_basename (file);
17 g_assert_cmpstr (s, ==, "testfile");
20 s = g_file_get_uri (file);
21 g_assert_true (g_str_has_prefix (s, "file://"));
22 g_assert_true (g_str_has_suffix (s, suffix));
25 g_assert_true (g_file_has_uri_scheme (file, "file"));
26 s = g_file_get_uri_scheme (file);
27 g_assert_cmpstr (s, ==, "file");
36 file = g_file_new_for_path ("./some/directory/testfile");
37 test_basic_for_file (file, "/some/directory/testfile");
38 g_object_unref (file);
42 test_build_filename (void)
46 file = g_file_new_build_filename (".", "some", "directory", "testfile", NULL);
47 test_basic_for_file (file, "/some/directory/testfile");
48 g_object_unref (file);
50 file = g_file_new_build_filename ("testfile", NULL);
51 test_basic_for_file (file, "/testfile");
52 g_object_unref (file);
63 file = g_file_new_for_path ("./some/directory/testfile");
64 file2 = g_file_new_for_path ("./some/directory");
65 root = g_file_new_for_path ("/");
67 g_assert_true (g_file_has_parent (file, file2));
69 parent = g_file_get_parent (file);
70 g_assert_true (g_file_equal (parent, file2));
71 g_object_unref (parent);
73 g_assert_null (g_file_get_parent (root));
75 g_object_unref (file);
76 g_object_unref (file2);
77 g_object_unref (root);
87 file = g_file_new_for_path ("./some/directory");
88 child = g_file_get_child (file, "child");
89 g_assert_true (g_file_has_parent (child, file));
91 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
92 g_assert_true (g_file_has_parent (child2, file));
94 g_object_unref (child);
95 g_object_unref (child2);
96 g_object_unref (file);
105 GError *error = NULL;
107 datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
109 file = g_file_get_child (datapath_f, "g-icon.c");
110 type = g_file_query_file_type (file, 0, NULL);
111 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
112 g_object_unref (file);
114 file = g_file_get_child (datapath_f, "cert-tests");
115 type = g_file_query_file_type (file, 0, NULL);
116 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
118 g_file_read (file, NULL, &error);
119 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
120 g_error_free (error);
121 g_object_unref (file);
123 g_object_unref (datapath_f);
127 test_parse_name (void)
132 file = g_file_new_for_uri ("file://somewhere");
133 name = g_file_get_parse_name (file);
134 g_assert_cmpstr (name, ==, "file://somewhere");
135 g_object_unref (file);
138 file = g_file_parse_name ("~foo");
139 name = g_file_get_parse_name (file);
140 g_assert_nonnull (name);
141 g_object_unref (file);
147 GMainContext *context;
149 GFileMonitor *monitor;
150 GOutputStream *ostream;
151 GInputStream *istream;
153 gint monitor_created;
154 gint monitor_deleted;
155 gint monitor_changed;
161 gboolean file_deleted;
166 monitor_changed (GFileMonitor *monitor,
169 GFileMonitorEvent event_type,
172 CreateDeleteData *data = user_data;
174 const gchar *peeked_path;
176 path = g_file_get_path (file);
177 peeked_path = g_file_peek_path (file);
178 g_assert_cmpstr (data->monitor_path, ==, path);
179 g_assert_cmpstr (path, ==, peeked_path);
182 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
183 data->monitor_created++;
184 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
185 data->monitor_deleted++;
186 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
187 data->monitor_changed++;
189 g_main_context_wakeup (data->context);
193 iclosed_cb (GObject *source,
197 CreateDeleteData *data = user_data;
202 ret = g_input_stream_close_finish (data->istream, res, &error);
203 g_assert_no_error (error);
206 g_assert_true (g_input_stream_is_closed (data->istream));
208 ret = g_file_delete (data->file, NULL, &error);
210 g_assert_no_error (error);
212 data->file_deleted = TRUE;
213 g_main_context_wakeup (data->context);
217 read_cb (GObject *source,
221 CreateDeleteData *data = user_data;
226 size = g_input_stream_read_finish (data->istream, res, &error);
227 g_assert_no_error (error);
230 if (data->pos < strlen (data->data))
232 g_input_stream_read_async (data->istream,
233 data->buffer + data->pos,
234 strlen (data->data) - data->pos,
242 g_assert_cmpstr (data->buffer, ==, data->data);
243 g_assert_false (g_input_stream_is_closed (data->istream));
244 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
249 ipending_cb (GObject *source,
253 CreateDeleteData *data = user_data;
257 g_input_stream_read_finish (data->istream, res, &error);
258 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
259 g_error_free (error);
263 skipped_cb (GObject *source,
267 CreateDeleteData *data = user_data;
272 size = g_input_stream_skip_finish (data->istream, res, &error);
273 g_assert_no_error (error);
274 g_assert_cmpint (size, ==, data->pos);
276 g_input_stream_read_async (data->istream,
277 data->buffer + data->pos,
278 strlen (data->data) - data->pos,
283 /* check that we get a pending error */
284 g_input_stream_read_async (data->istream,
285 data->buffer + data->pos,
286 strlen (data->data) - data->pos,
294 opened_cb (GObject *source,
298 GFileInputStream *base;
299 CreateDeleteData *data = user_data;
303 base = g_file_read_finish (data->file, res, &error);
304 g_assert_no_error (error);
306 if (data->buffersize == 0)
307 data->istream = G_INPUT_STREAM (g_object_ref (base));
309 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
310 g_object_unref (base);
312 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
314 /* copy initial segment directly, then skip */
315 memcpy (data->buffer, data->data, 10);
318 g_input_stream_skip_async (data->istream,
327 oclosed_cb (GObject *source,
331 CreateDeleteData *data = user_data;
336 ret = g_output_stream_close_finish (data->ostream, res, &error);
337 g_assert_no_error (error);
339 g_assert_true (g_output_stream_is_closed (data->ostream));
341 g_file_read_async (data->file, 0, NULL, opened_cb, data);
345 written_cb (GObject *source,
349 CreateDeleteData *data = user_data;
354 size = g_output_stream_write_finish (data->ostream, res, &error);
355 g_assert_no_error (error);
358 if (data->pos < strlen (data->data))
360 g_output_stream_write_async (data->ostream,
361 data->data + data->pos,
362 strlen (data->data) - data->pos,
370 g_assert_false (g_output_stream_is_closed (data->ostream));
371 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
376 opending_cb (GObject *source,
380 CreateDeleteData *data = user_data;
384 g_output_stream_write_finish (data->ostream, res, &error);
385 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
386 g_error_free (error);
390 created_cb (GObject *source,
394 GFileOutputStream *base;
395 CreateDeleteData *data = user_data;
399 base = g_file_create_finish (G_FILE (source), res, &error);
400 g_assert_no_error (error);
401 g_assert_true (g_file_query_exists (data->file, NULL));
403 if (data->buffersize == 0)
404 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
406 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
407 g_object_unref (base);
409 g_output_stream_write_async (data->ostream,
416 /* check that we get a pending error */
417 g_output_stream_write_async (data->ostream,
427 stop_timeout (gpointer user_data)
429 CreateDeleteData *data = user_data;
431 data->timed_out = TRUE;
432 g_main_context_wakeup (data->context);
434 return G_SOURCE_REMOVE;
438 * This test does a fully async create-write-read-delete.
442 test_create_delete (gconstpointer d)
445 CreateDeleteData *data;
446 GFileIOStream *iostream;
448 data = g_new0 (CreateDeleteData, 1);
450 data->buffersize = GPOINTER_TO_INT (d);
451 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
454 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
456 g_assert_nonnull (data->file);
457 g_object_unref (iostream);
459 data->monitor_path = g_file_get_path (data->file);
460 remove (data->monitor_path);
462 g_assert_false (g_file_query_exists (data->file, NULL));
465 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
466 g_assert_no_error (error);
468 /* This test doesn't work with GPollFileMonitor, because it assumes
469 * that the monitor will notice a create immediately followed by a
470 * delete, rather than coalescing them into nothing.
472 /* This test also doesn't work with GKqueueFileMonitor because of
473 * the same reason. Kqueue is able to return a kevent when a file is
474 * created or deleted in a directory. However, the kernel doesn't tell
475 * the program file names, so GKqueueFileMonitor has to calculate the
476 * difference itself. This is usually too slow for rapid file creation
477 * and deletion tests.
479 if (strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor") == 0 ||
480 strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GKqueueFileMonitor") == 0)
482 g_test_skip ("skipping test for this GFileMonitor implementation");
486 g_file_monitor_set_rate_limit (data->monitor, 100);
488 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
490 /* Use the global default main context */
491 data->context = NULL;
492 data->timeout = g_timeout_add_seconds (10, stop_timeout, data);
494 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
496 while (!data->timed_out &&
497 (data->monitor_created == 0 ||
498 data->monitor_deleted == 0 ||
499 data->monitor_changed == 0 ||
500 !data->file_deleted))
501 g_main_context_iteration (data->context, TRUE);
503 g_source_remove (data->timeout);
505 g_assert_false (data->timed_out);
506 g_assert_true (data->file_deleted);
507 g_assert_cmpint (data->monitor_created, ==, 1);
508 g_assert_cmpint (data->monitor_deleted, ==, 1);
509 g_assert_cmpint (data->monitor_changed, >, 0);
511 g_assert_false (g_file_monitor_is_cancelled (data->monitor));
512 g_file_monitor_cancel (data->monitor);
513 g_assert_true (g_file_monitor_is_cancelled (data->monitor));
515 g_clear_pointer (&data->context, g_main_context_unref);
516 g_object_unref (data->ostream);
517 g_object_unref (data->istream);
520 g_object_unref (data->monitor);
521 g_object_unref (data->file);
522 g_free (data->monitor_path);
523 g_free (data->buffer);
527 static const gchar *original_data =
529 " * g_file_replace_contents_async:\n"
532 static const gchar *replace_data =
534 " * g_file_replace_contents_async:\n"
535 " * @file: input #GFile.\n"
536 " * @contents: string of contents to replace the file with.\n"
537 " * @length: the length of @contents in bytes.\n"
538 " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
539 " * @make_backup: %TRUE if a backup should be created.\n"
540 " * @flags: a set of #GFileCreateFlags.\n"
541 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
542 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
543 " * @user_data: the data to pass to callback function\n"
545 " * Starts an asynchronous replacement of @file with the given \n"
546 " * @contents of @length bytes. @etag will replace the document's\n"
547 " * current entity tag.\n"
549 " * When this operation has completed, @callback will be called with\n"
550 " * @user_user data, and the operation can be finalized with \n"
551 " * g_file_replace_contents_finish().\n"
553 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
554 " * triggering the cancellable object from another thread. If the operation\n"
555 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
557 " * If @make_backup is %TRUE, this function will attempt to \n"
558 " * make a backup of @file.\n"
569 static void replaced_cb (GObject *source,
574 loaded_cb (GObject *source,
578 ReplaceLoadData *data = user_data;
585 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
587 g_assert_no_error (error);
588 g_assert_cmpint (length, ==, strlen (data->data));
589 g_assert_cmpstr (contents, ==, data->data);
596 data->data = "pi pa po";
598 g_file_replace_contents_async (data->file,
611 ret = g_file_delete (data->file, NULL, &error);
612 g_assert_no_error (error);
614 g_assert_false (g_file_query_exists (data->file, NULL));
616 g_main_loop_quit (data->loop);
621 replaced_cb (GObject *source,
625 ReplaceLoadData *data = user_data;
629 g_file_replace_contents_finish (data->file, res, NULL, &error);
630 g_assert_no_error (error);
632 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
636 test_replace_load (void)
638 ReplaceLoadData *data;
640 GFileIOStream *iostream;
642 data = g_new0 (ReplaceLoadData, 1);
644 data->data = replace_data;
646 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
648 g_assert_nonnull (data->file);
649 g_object_unref (iostream);
651 path = g_file_peek_path (data->file);
654 g_assert_false (g_file_query_exists (data->file, NULL));
656 data->loop = g_main_loop_new (NULL, FALSE);
658 g_file_replace_contents_async (data->file,
668 g_main_loop_run (data->loop);
670 g_main_loop_unref (data->loop);
671 g_object_unref (data->file);
676 test_replace_cancel (void)
678 GFile *tmpdir, *file;
679 GFileOutputStream *ostream;
680 GFileEnumerator *fenum;
682 GCancellable *cancellable;
685 gsize nwrote, length;
687 GError *error = NULL;
689 g_test_bug ("https://bugzilla.gnome.org/629301");
691 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
692 g_assert_no_error (error);
693 tmpdir = g_file_new_for_path (path);
696 file = g_file_get_child (tmpdir, "file");
697 g_file_replace_contents (file,
699 strlen (original_data),
700 NULL, FALSE, 0, NULL,
702 g_assert_no_error (error);
704 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
705 g_assert_no_error (error);
707 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
708 replace_data, strlen (replace_data),
709 &nwrote, NULL, &error);
710 g_assert_no_error (error);
711 g_assert_cmpint (nwrote, ==, strlen (replace_data));
713 /* At this point there should be two files; the original and the
716 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
717 g_assert_no_error (error);
719 info = g_file_enumerator_next_file (fenum, NULL, &error);
720 g_assert_no_error (error);
721 g_assert_nonnull (info);
722 g_object_unref (info);
723 info = g_file_enumerator_next_file (fenum, NULL, &error);
724 g_assert_no_error (error);
725 g_assert_nonnull (info);
726 g_object_unref (info);
728 g_file_enumerator_close (fenum, NULL, &error);
729 g_assert_no_error (error);
730 g_object_unref (fenum);
732 /* Also test the g_file_enumerator_iterate() API */
733 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
734 g_assert_no_error (error);
739 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
741 g_assert_no_error (error);
746 g_assert_cmpint (count, ==, 2);
748 g_file_enumerator_close (fenum, NULL, &error);
749 g_assert_no_error (error);
750 g_object_unref (fenum);
752 /* Now test just getting child from the g_file_enumerator_iterate() API */
753 fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
754 g_assert_no_error (error);
760 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
763 g_assert_no_error (error);
768 g_assert_true (G_IS_FILE (child));
771 g_assert_cmpint (count, ==, 2);
773 g_file_enumerator_close (fenum, NULL, &error);
774 g_assert_no_error (error);
775 g_object_unref (fenum);
777 /* Make sure the temporary gets deleted even if we cancel. */
778 cancellable = g_cancellable_new ();
779 g_cancellable_cancel (cancellable);
780 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
781 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
782 g_clear_error (&error);
784 g_object_unref (cancellable);
785 g_object_unref (ostream);
787 /* Make sure that file contents wasn't actually replaced. */
788 g_file_load_contents (file,
794 g_assert_no_error (error);
795 g_assert_cmpstr (contents, ==, original_data);
798 g_file_delete (file, NULL, &error);
799 g_assert_no_error (error);
800 g_object_unref (file);
802 /* This will only succeed if the temp file was deleted. */
803 g_file_delete (tmpdir, NULL, &error);
804 g_assert_no_error (error);
805 g_object_unref (tmpdir);
809 test_replace_symlink (void)
812 gchar *tmpdir_path = NULL;
813 GFile *tmpdir = NULL, *source_file = NULL, *target_file = NULL;
814 GFileOutputStream *stream = NULL;
815 const gchar *new_contents = "this is a test message which should be written to source and not target";
817 GFileEnumerator *enumerator = NULL;
818 GFileInfo *info = NULL;
819 gchar *contents = NULL;
821 GError *local_error = NULL;
823 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2325");
824 g_test_summary ("Test that G_FILE_CREATE_REPLACE_DESTINATION doesn’t follow symlinks");
826 /* Create a fresh, empty working directory. */
827 tmpdir_path = g_dir_make_tmp ("g_file_replace_symlink_XXXXXX", &local_error);
828 g_assert_no_error (local_error);
829 tmpdir = g_file_new_for_path (tmpdir_path);
831 g_test_message ("Using temporary directory %s", tmpdir_path);
832 g_free (tmpdir_path);
834 /* Create symlink `source` which points to `target`. */
835 source_file = g_file_get_child (tmpdir, "source");
836 target_file = g_file_get_child (tmpdir, "target");
837 g_file_make_symbolic_link (source_file, "target", NULL, &local_error);
838 g_assert_no_error (local_error);
840 /* Ensure that `target` doesn’t exist */
841 g_assert_false (g_file_query_exists (target_file, NULL));
843 /* Replace the `source` symlink with a regular file using
844 * %G_FILE_CREATE_REPLACE_DESTINATION, which should replace it *without*
845 * following the symlink */
846 stream = g_file_replace (source_file, NULL, FALSE /* no backup */,
847 G_FILE_CREATE_REPLACE_DESTINATION, NULL, &local_error);
848 g_assert_no_error (local_error);
850 g_output_stream_write_all (G_OUTPUT_STREAM (stream), new_contents, strlen (new_contents),
851 &n_written, NULL, &local_error);
852 g_assert_no_error (local_error);
853 g_assert_cmpint (n_written, ==, strlen (new_contents));
855 g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, &local_error);
856 g_assert_no_error (local_error);
858 g_clear_object (&stream);
860 /* At this point, there should still only be one file: `source`. It should
861 * now be a regular file. `target` should not exist. */
862 enumerator = g_file_enumerate_children (tmpdir,
863 G_FILE_ATTRIBUTE_STANDARD_NAME ","
864 G_FILE_ATTRIBUTE_STANDARD_TYPE,
865 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
866 g_assert_no_error (local_error);
868 info = g_file_enumerator_next_file (enumerator, NULL, &local_error);
869 g_assert_no_error (local_error);
870 g_assert_nonnull (info);
872 g_assert_cmpstr (g_file_info_get_name (info), ==, "source");
873 g_assert_cmpint (g_file_info_get_file_type (info), ==, G_FILE_TYPE_REGULAR);
875 g_clear_object (&info);
877 info = g_file_enumerator_next_file (enumerator, NULL, &local_error);
878 g_assert_no_error (local_error);
879 g_assert_null (info);
881 g_file_enumerator_close (enumerator, NULL, &local_error);
882 g_assert_no_error (local_error);
883 g_clear_object (&enumerator);
885 /* Double-check that `target` doesn’t exist */
886 g_assert_false (g_file_query_exists (target_file, NULL));
888 /* Check the content of `source`. */
889 g_file_load_contents (source_file,
895 g_assert_no_error (local_error);
896 g_assert_cmpstr (contents, ==, new_contents);
897 g_assert_cmpuint (length, ==, strlen (new_contents));
901 g_file_delete (source_file, NULL, &local_error);
902 g_assert_no_error (local_error);
904 g_file_delete (tmpdir, NULL, &local_error);
905 g_assert_no_error (local_error);
907 g_clear_object (&target_file);
908 g_clear_object (&source_file);
909 g_clear_object (&tmpdir);
910 #else /* if !G_OS_UNIX */
911 g_test_skip ("Symlink replacement tests can only be run on Unix")
916 on_file_deleted (GObject *object,
917 GAsyncResult *result,
920 GError *local_error = NULL;
921 GMainLoop *loop = user_data;
923 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
924 g_assert_no_error (local_error);
926 g_main_loop_quit (loop);
930 test_async_delete (void)
933 GFileIOStream *iostream;
934 GError *local_error = NULL;
935 GError **error = &local_error;
938 file = g_file_new_tmp ("g_file_delete_XXXXXX",
940 g_assert_no_error (local_error);
941 g_object_unref (iostream);
943 g_assert_true (g_file_query_exists (file, NULL));
945 loop = g_main_loop_new (NULL, TRUE);
947 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
949 g_main_loop_run (loop);
951 g_assert_false (g_file_query_exists (file, NULL));
953 g_main_loop_unref (loop);
954 g_object_unref (file);
958 test_copy_preserve_mode (void)
961 mode_t current_umask = umask (0);
965 guint32 expected_destination_mode;
966 gboolean create_destination_before_copy;
967 GFileCopyFlags copy_flags;
971 /* Overwriting the destination file should copy the permissions from the
972 * source file, even if %G_FILE_COPY_ALL_METADATA is set: */
973 { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
974 { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
975 /* The same behaviour should hold if the destination file is not being
976 * overwritten because it doesn’t already exist: */
977 { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
978 { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS },
979 /* Anything with %G_FILE_COPY_TARGET_DEFAULT_PERMS should use the current
980 * umask for the destination file: */
981 { 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
982 { 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
983 { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
984 { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS },
988 /* Reset the umask after querying it above. There’s no way to query it without
990 umask (current_umask);
991 g_test_message ("Current umask: %u", current_umask);
993 for (i = 0; i < G_N_ELEMENTS (vectors); i++)
997 GFileInfo *dest_info;
998 GFileIOStream *iostream;
999 GError *local_error = NULL;
1000 guint32 romode = vectors[i].source_mode;
1003 g_test_message ("Vector %" G_GSIZE_FORMAT, i);
1005 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
1006 &iostream, &local_error);
1007 g_assert_no_error (local_error);
1008 g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
1009 g_assert_no_error (local_error);
1010 g_clear_object (&iostream);
1012 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
1013 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1014 NULL, &local_error);
1015 g_assert_no_error (local_error);
1017 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
1018 &iostream, &local_error);
1019 g_assert_no_error (local_error);
1020 g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
1021 g_assert_no_error (local_error);
1022 g_clear_object (&iostream);
1024 if (!vectors[i].create_destination_before_copy)
1026 g_file_delete (dest_tmpfile, NULL, &local_error);
1027 g_assert_no_error (local_error);
1030 g_file_copy (tmpfile, dest_tmpfile, vectors[i].copy_flags,
1031 NULL, NULL, NULL, &local_error);
1032 g_assert_no_error (local_error);
1034 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1035 NULL, &local_error);
1036 g_assert_no_error (local_error);
1038 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
1040 g_assert_cmpint (dest_mode & ~S_IFMT, ==, vectors[i].expected_destination_mode);
1041 g_assert_cmpint (dest_mode & S_IFMT, ==, S_IFREG);
1043 (void) g_file_delete (tmpfile, NULL, NULL);
1044 (void) g_file_delete (dest_tmpfile, NULL, NULL);
1046 g_clear_object (&tmpfile);
1047 g_clear_object (&dest_tmpfile);
1048 g_clear_object (&dest_info);
1050 #else /* if !G_OS_UNIX */
1051 g_test_skip ("File permissions tests can only be run on Unix")
1056 splice_to_string (GInputStream *stream,
1059 GMemoryOutputStream *buffer = NULL;
1062 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
1063 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
1066 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
1069 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
1072 ret = g_memory_output_stream_steal_data (buffer);
1074 g_clear_object (&buffer);
1079 get_size_from_du (const gchar *path, guint64 *size)
1085 GError *error = NULL;
1086 gchar *du_path = NULL;
1088 /* If we can’t find du, don’t try and run the test. */
1089 du_path = g_find_program_in_path ("du");
1090 if (du_path == NULL)
1094 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
1096 "du", "--bytes", "-s", path, NULL);
1097 g_assert_no_error (error);
1099 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
1100 g_assert_no_error (error);
1102 *size = g_ascii_strtoll (result, &endptr, 10);
1104 g_subprocess_wait (du, NULL, &error);
1105 g_assert_no_error (error);
1107 ok = g_subprocess_get_successful (du);
1109 g_object_unref (du);
1123 GError *error = NULL;
1127 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1128 file = g_file_new_for_path (path);
1130 if (!get_size_from_du (path, &size))
1132 g_test_message ("du not found or fail to run, skipping byte measurement");
1136 ok = g_file_measure_disk_usage (file,
1137 G_FILE_MEASURE_APPARENT_SIZE,
1146 g_assert_no_error (error);
1149 g_assert_cmpuint (num_bytes, ==, size);
1150 g_assert_cmpuint (num_dirs, ==, 6);
1151 g_assert_cmpuint (num_files, ==, 32);
1153 g_object_unref (file);
1158 guint64 expected_bytes;
1159 guint64 expected_dirs;
1160 guint64 expected_files;
1161 gint progress_count;
1162 guint64 progress_bytes;
1163 guint64 progress_dirs;
1164 guint64 progress_files;
1168 measure_progress (gboolean reporting,
1169 guint64 current_size,
1174 MeasureData *data = user_data;
1176 data->progress_count += 1;
1178 g_assert_cmpuint (current_size, >=, data->progress_bytes);
1179 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1180 g_assert_cmpuint (num_files, >=, data->progress_files);
1182 data->progress_bytes = current_size;
1183 data->progress_dirs = num_dirs;
1184 data->progress_files = num_files;
1188 measure_done (GObject *source,
1192 MeasureData *data = user_data;
1193 guint64 num_bytes, num_dirs, num_files;
1194 GError *error = NULL;
1197 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
1199 g_assert_no_error (error);
1201 if (data->expected_bytes > 0)
1202 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
1203 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
1204 g_assert_cmpuint (data->expected_files, ==, num_files);
1206 g_assert_cmpuint (data->progress_count, >, 0);
1207 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
1208 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1209 g_assert_cmpuint (num_files, >=, data->progress_files);
1212 g_object_unref (source);
1216 test_measure_async (void)
1222 data = g_new (MeasureData, 1);
1224 data->progress_count = 0;
1225 data->progress_bytes = 0;
1226 data->progress_files = 0;
1227 data->progress_dirs = 0;
1229 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1230 file = g_file_new_for_path (path);
1232 if (!get_size_from_du (path, &data->expected_bytes))
1234 g_test_message ("du not found or fail to run, skipping byte measurement");
1235 data->expected_bytes = 0;
1240 data->expected_dirs = 6;
1241 data->expected_files = 32;
1243 g_file_measure_disk_usage_async (file,
1244 G_FILE_MEASURE_APPARENT_SIZE,
1246 measure_progress, data,
1247 measure_done, data);
1251 test_load_bytes (void)
1253 gchar filename[] = "g_file_load_bytes_XXXXXX";
1254 GError *error = NULL;
1261 fd = g_mkstemp (filename);
1262 g_assert_cmpint (fd, !=, -1);
1263 len = strlen ("test_load_bytes");
1264 ret = write (fd, "test_load_bytes", len);
1265 g_assert_cmpint (ret, ==, len);
1268 file = g_file_new_for_path (filename);
1269 bytes = g_file_load_bytes (file, NULL, NULL, &error);
1270 g_assert_no_error (error);
1271 g_assert_nonnull (bytes);
1272 g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
1273 g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
1275 g_file_delete (file, NULL, NULL);
1277 g_bytes_unref (bytes);
1278 g_object_unref (file);
1283 GMainLoop *main_loop;
1286 } LoadBytesAsyncData;
1289 test_load_bytes_cb (GObject *object,
1290 GAsyncResult *result,
1293 GFile *file = G_FILE (object);
1294 LoadBytesAsyncData *data = user_data;
1295 GError *error = NULL;
1297 data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
1298 g_assert_no_error (error);
1299 g_assert_nonnull (data->bytes);
1301 g_main_loop_quit (data->main_loop);
1305 test_load_bytes_async (void)
1307 LoadBytesAsyncData data = { 0 };
1308 gchar filename[] = "g_file_load_bytes_XXXXXX";
1313 fd = g_mkstemp (filename);
1314 g_assert_cmpint (fd, !=, -1);
1315 len = strlen ("test_load_bytes_async");
1316 ret = write (fd, "test_load_bytes_async", len);
1317 g_assert_cmpint (ret, ==, len);
1320 data.main_loop = g_main_loop_new (NULL, FALSE);
1321 data.file = g_file_new_for_path (filename);
1323 g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
1324 g_main_loop_run (data.main_loop);
1326 g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
1327 g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
1329 g_file_delete (data.file, NULL, NULL);
1330 g_object_unref (data.file);
1331 g_bytes_unref (data.bytes);
1332 g_main_loop_unref (data.main_loop);
1336 test_writev_helper (GOutputVector *vectors,
1338 gboolean use_bytes_written,
1339 const guint8 *expected_contents,
1340 gsize expected_length)
1343 GFileIOStream *iostream = NULL;
1344 GOutputStream *ostream;
1345 GError *error = NULL;
1346 gsize bytes_written = 0;
1351 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1353 g_assert_nonnull (file);
1354 g_assert_nonnull (iostream);
1356 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1358 res = g_output_stream_writev_all (ostream, vectors, n_vectors, use_bytes_written ? &bytes_written : NULL, NULL, &error);
1359 g_assert_no_error (error);
1360 g_assert_true (res);
1361 if (use_bytes_written)
1362 g_assert_cmpuint (bytes_written, ==, expected_length);
1364 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1365 g_assert_no_error (error);
1366 g_assert_true (res);
1367 g_object_unref (iostream);
1369 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1370 g_assert_no_error (error);
1371 g_assert_true (res);
1373 g_assert_cmpmem (contents, length, expected_contents, expected_length);
1377 g_file_delete (file, NULL, NULL);
1378 g_object_unref (file);
1381 /* Test that writev() on local file output streams works on a non-empty vector */
1385 GOutputVector vectors[3];
1386 const guint8 buffer[] = {1, 2, 3, 4, 5,
1387 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1390 vectors[0].buffer = buffer;
1391 vectors[0].size = 5;
1393 vectors[1].buffer = buffer + 5;
1394 vectors[1].size = 12;
1396 vectors[2].buffer = buffer + 5 + 12;
1397 vectors[2].size = 3;
1399 test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, buffer, sizeof buffer);
1402 /* Test that writev() on local file output streams works on a non-empty vector without returning bytes_written */
1404 test_writev_no_bytes_written (void)
1406 GOutputVector vectors[3];
1407 const guint8 buffer[] = {1, 2, 3, 4, 5,
1408 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1411 vectors[0].buffer = buffer;
1412 vectors[0].size = 5;
1414 vectors[1].buffer = buffer + 5;
1415 vectors[1].size = 12;
1417 vectors[2].buffer = buffer + 5 + 12;
1418 vectors[2].size = 3;
1420 test_writev_helper (vectors, G_N_ELEMENTS (vectors), FALSE, buffer, sizeof buffer);
1423 /* Test that writev() on local file output streams works on 0 vectors */
1425 test_writev_no_vectors (void)
1427 test_writev_helper (NULL, 0, TRUE, NULL, 0);
1430 /* Test that writev() on local file output streams works on empty vectors */
1432 test_writev_empty_vectors (void)
1434 GOutputVector vectors[3];
1436 vectors[0].buffer = NULL;
1437 vectors[0].size = 0;
1438 vectors[1].buffer = NULL;
1439 vectors[1].size = 0;
1440 vectors[2].buffer = NULL;
1441 vectors[2].size = 0;
1443 test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, NULL, 0);
1446 /* Test that writev() fails if the sum of sizes in the vector is too big */
1448 test_writev_too_big_vectors (void)
1451 GFileIOStream *iostream = NULL;
1452 GOutputStream *ostream;
1453 GError *error = NULL;
1454 gsize bytes_written = 0;
1458 GOutputVector vectors[3];
1460 vectors[0].buffer = (void*) 1;
1461 vectors[0].size = G_MAXSIZE / 2;
1463 vectors[1].buffer = (void*) 1;
1464 vectors[1].size = G_MAXSIZE / 2;
1466 vectors[2].buffer = (void*) 1;
1467 vectors[2].size = G_MAXSIZE / 2;
1469 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1471 g_assert_nonnull (file);
1472 g_assert_nonnull (iostream);
1474 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1476 res = g_output_stream_writev_all (ostream, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
1477 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1478 g_assert_cmpuint (bytes_written, ==, 0);
1479 g_assert_false (res);
1480 g_clear_error (&error);
1482 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1483 g_assert_no_error (error);
1484 g_assert_true (res);
1485 g_object_unref (iostream);
1487 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1488 g_assert_no_error (error);
1489 g_assert_true (res);
1491 g_assert_cmpmem (contents, length, NULL, 0);
1495 g_file_delete (file, NULL, NULL);
1496 g_object_unref (file);
1501 gsize bytes_written;
1502 GOutputVector *vectors;
1509 test_writev_async_cb (GObject *object,
1510 GAsyncResult *result,
1513 GOutputStream *ostream = G_OUTPUT_STREAM (object);
1514 WritevAsyncData *data = user_data;
1515 GError *error = NULL;
1516 gsize bytes_written;
1519 res = g_output_stream_writev_finish (ostream, result, &bytes_written, &error);
1520 g_assert_true (res);
1521 g_assert_no_error (error);
1522 data->bytes_written += bytes_written;
1524 /* skip vectors that have been written in full */
1525 while (data->n_vectors > 0 && bytes_written >= data->vectors[0].size)
1527 bytes_written -= data->vectors[0].size;
1531 /* skip partially written vector data */
1532 if (bytes_written > 0 && data->n_vectors > 0)
1534 data->vectors[0].size -= bytes_written;
1535 data->vectors[0].buffer = ((guint8 *) data->vectors[0].buffer) + bytes_written;
1538 if (data->n_vectors > 0)
1539 g_output_stream_writev_async (ostream, data->vectors, data->n_vectors, 0, NULL, test_writev_async_cb, &data);
1542 /* Test that writev_async() on local file output streams works on a non-empty vector */
1544 test_writev_async (void)
1546 WritevAsyncData data = { 0 };
1548 GFileIOStream *iostream = NULL;
1549 GOutputVector vectors[3];
1550 const guint8 buffer[] = {1, 2, 3, 4, 5,
1551 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1553 GOutputStream *ostream;
1554 GError *error = NULL;
1559 vectors[0].buffer = buffer;
1560 vectors[0].size = 5;
1562 vectors[1].buffer = buffer + 5;
1563 vectors[1].size = 12;
1565 vectors[2].buffer = buffer + 5 + 12;
1566 vectors[2].size = 3;
1568 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1570 g_assert_nonnull (file);
1571 g_assert_nonnull (iostream);
1573 data.vectors = vectors;
1574 data.n_vectors = G_N_ELEMENTS (vectors);
1576 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1578 g_output_stream_writev_async (ostream, data.vectors, data.n_vectors, 0, NULL, test_writev_async_cb, &data);
1580 while (data.n_vectors > 0)
1581 g_main_context_iteration (NULL, TRUE);
1583 g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
1585 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1586 g_assert_no_error (error);
1587 g_assert_true (res);
1588 g_object_unref (iostream);
1590 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1591 g_assert_no_error (error);
1592 g_assert_true (res);
1594 g_assert_cmpmem (contents, length, buffer, sizeof buffer);
1598 g_file_delete (file, NULL, NULL);
1599 g_object_unref (file);
1603 test_writev_all_cb (GObject *object,
1604 GAsyncResult *result,
1607 GOutputStream *ostream = G_OUTPUT_STREAM (object);
1608 WritevAsyncData *data = user_data;
1610 g_output_stream_writev_all_finish (ostream, result, &data->bytes_written, &data->error);
1614 /* Test that writev_async_all() on local file output streams works on a non-empty vector */
1616 test_writev_async_all (void)
1618 WritevAsyncData data = { 0 };
1620 GFileIOStream *iostream = NULL;
1621 GOutputStream *ostream;
1622 GOutputVector vectors[3];
1623 const guint8 buffer[] = {1, 2, 3, 4, 5,
1624 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1626 GError *error = NULL;
1631 vectors[0].buffer = buffer;
1632 vectors[0].size = 5;
1634 vectors[1].buffer = buffer + 5;
1635 vectors[1].size = 12;
1637 vectors[2].buffer = buffer + 5 + 12;
1638 vectors[2].size = 3;
1640 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1642 g_assert_nonnull (file);
1643 g_assert_nonnull (iostream);
1645 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1647 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
1650 g_main_context_iteration (NULL, TRUE);
1652 g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
1653 g_assert_no_error (data.error);
1655 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1656 g_assert_no_error (error);
1657 g_assert_true (res);
1658 g_object_unref (iostream);
1660 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1661 g_assert_no_error (error);
1662 g_assert_true (res);
1664 g_assert_cmpmem (contents, length, buffer, sizeof buffer);
1668 g_file_delete (file, NULL, NULL);
1669 g_object_unref (file);
1672 /* Test that writev_async_all() on local file output streams handles cancellation correctly */
1674 test_writev_async_all_cancellation (void)
1676 WritevAsyncData data = { 0 };
1678 GFileIOStream *iostream = NULL;
1679 GOutputVector vectors[3];
1680 const guint8 buffer[] = {1, 2, 3, 4, 5,
1681 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1683 GOutputStream *ostream;
1684 GError *error = NULL;
1688 GCancellable *cancellable;
1690 vectors[0].buffer = buffer;
1691 vectors[0].size = 5;
1693 vectors[1].buffer = buffer + 5;
1694 vectors[1].size = 12;
1696 vectors[2].buffer = buffer + 5 + 12;
1697 vectors[2].size = 3;
1699 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1701 g_assert_nonnull (file);
1702 g_assert_nonnull (iostream);
1704 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1706 cancellable = g_cancellable_new ();
1707 g_cancellable_cancel (cancellable);
1709 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, cancellable, test_writev_all_cb, &data);
1712 g_main_context_iteration (NULL, TRUE);
1714 g_assert_cmpuint (data.bytes_written, ==, 0);
1715 g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1716 g_clear_error (&data.error);
1718 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1719 g_assert_no_error (error);
1720 g_assert_true (res);
1721 g_object_unref (iostream);
1723 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1724 g_assert_no_error (error);
1725 g_assert_true (res);
1726 g_assert_cmpuint (length, ==, 0);
1730 g_file_delete (file, NULL, NULL);
1731 g_object_unref (file);
1732 g_object_unref (cancellable);
1735 /* Test that writev_async_all() with empty vectors is handled correctly */
1737 test_writev_async_all_empty_vectors (void)
1739 WritevAsyncData data = { 0 };
1741 GFileIOStream *iostream = NULL;
1742 GOutputVector vectors[3];
1743 GOutputStream *ostream;
1744 GError *error = NULL;
1749 vectors[0].buffer = NULL;
1750 vectors[0].size = 0;
1752 vectors[1].buffer = NULL;
1753 vectors[1].size = 0;
1755 vectors[2].buffer = NULL;
1756 vectors[2].size = 0;
1758 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1760 g_assert_nonnull (file);
1761 g_assert_nonnull (iostream);
1763 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1765 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
1768 g_main_context_iteration (NULL, TRUE);
1770 g_assert_cmpuint (data.bytes_written, ==, 0);
1771 g_assert_no_error (data.error);
1772 g_clear_error (&data.error);
1774 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1775 g_assert_no_error (error);
1776 g_assert_true (res);
1777 g_object_unref (iostream);
1779 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1780 g_assert_no_error (error);
1781 g_assert_true (res);
1782 g_assert_cmpuint (length, ==, 0);
1786 g_file_delete (file, NULL, NULL);
1787 g_object_unref (file);
1790 /* Test that writev_async_all() with no vectors is handled correctly */
1792 test_writev_async_all_no_vectors (void)
1794 WritevAsyncData data = { 0 };
1796 GFileIOStream *iostream = NULL;
1797 GOutputStream *ostream;
1798 GError *error = NULL;
1803 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1805 g_assert_nonnull (file);
1806 g_assert_nonnull (iostream);
1808 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1810 g_output_stream_writev_all_async (ostream, NULL, 0, 0, NULL, test_writev_all_cb, &data);
1813 g_main_context_iteration (NULL, TRUE);
1815 g_assert_cmpuint (data.bytes_written, ==, 0);
1816 g_assert_no_error (data.error);
1817 g_clear_error (&data.error);
1819 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1820 g_assert_no_error (error);
1821 g_assert_true (res);
1822 g_object_unref (iostream);
1824 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1825 g_assert_no_error (error);
1826 g_assert_true (res);
1827 g_assert_cmpuint (length, ==, 0);
1831 g_file_delete (file, NULL, NULL);
1832 g_object_unref (file);
1835 /* Test that writev_async_all() with too big vectors is handled correctly */
1837 test_writev_async_all_too_big_vectors (void)
1839 WritevAsyncData data = { 0 };
1841 GFileIOStream *iostream = NULL;
1842 GOutputVector vectors[3];
1843 GOutputStream *ostream;
1844 GError *error = NULL;
1849 vectors[0].buffer = (void*) 1;
1850 vectors[0].size = G_MAXSIZE / 2;
1852 vectors[1].buffer = (void*) 1;
1853 vectors[1].size = G_MAXSIZE / 2;
1855 vectors[2].buffer = (void*) 1;
1856 vectors[2].size = G_MAXSIZE / 2;
1858 file = g_file_new_tmp ("g_file_writev_XXXXXX",
1860 g_assert_nonnull (file);
1861 g_assert_nonnull (iostream);
1863 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1865 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
1868 g_main_context_iteration (NULL, TRUE);
1870 g_assert_cmpuint (data.bytes_written, ==, 0);
1871 g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1872 g_clear_error (&data.error);
1874 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1875 g_assert_no_error (error);
1876 g_assert_true (res);
1877 g_object_unref (iostream);
1879 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1880 g_assert_no_error (error);
1881 g_assert_true (res);
1882 g_assert_cmpuint (length, ==, 0);
1886 g_file_delete (file, NULL, NULL);
1887 g_object_unref (file);
1891 main (int argc, char *argv[])
1893 g_test_init (&argc, &argv, NULL);
1895 g_test_add_func ("/file/basic", test_basic);
1896 g_test_add_func ("/file/build-filename", test_build_filename);
1897 g_test_add_func ("/file/parent", test_parent);
1898 g_test_add_func ("/file/child", test_child);
1899 g_test_add_func ("/file/type", test_type);
1900 g_test_add_func ("/file/parse-name", test_parse_name);
1901 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1902 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1903 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1904 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1905 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1906 g_test_add_func ("/file/replace-load", test_replace_load);
1907 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1908 g_test_add_func ("/file/replace-symlink", test_replace_symlink);
1909 g_test_add_func ("/file/async-delete", test_async_delete);
1910 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1911 g_test_add_func ("/file/measure", test_measure);
1912 g_test_add_func ("/file/measure-async", test_measure_async);
1913 g_test_add_func ("/file/load-bytes", test_load_bytes);
1914 g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
1915 g_test_add_func ("/file/writev", test_writev);
1916 g_test_add_func ("/file/writev/no-bytes-written", test_writev_no_bytes_written);
1917 g_test_add_func ("/file/writev/no-vectors", test_writev_no_vectors);
1918 g_test_add_func ("/file/writev/empty-vectors", test_writev_empty_vectors);
1919 g_test_add_func ("/file/writev/too-big-vectors", test_writev_too_big_vectors);
1920 g_test_add_func ("/file/writev/async", test_writev_async);
1921 g_test_add_func ("/file/writev/async_all", test_writev_async_all);
1922 g_test_add_func ("/file/writev/async_all-empty-vectors", test_writev_async_all_empty_vectors);
1923 g_test_add_func ("/file/writev/async_all-no-vectors", test_writev_async_all_no_vectors);
1924 g_test_add_func ("/file/writev/async_all-to-big-vectors", test_writev_async_all_too_big_vectors);
1925 g_test_add_func ("/file/writev/async_all-cancellation", test_writev_async_all_cancellation);
1927 return g_test_run ();