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);
100 test_empty_path (void)
104 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2328");
105 g_test_summary ("Check that creating a file with an empty path results in errors");
107 /* Creating the file must always succeed. */
108 file = g_file_new_for_path ("");
109 g_assert_nonnull (file);
111 /* But then querying its path should indicate it’s invalid. */
112 g_assert_null (g_file_get_path (file));
113 g_assert_null (g_file_get_basename (file));
114 g_assert_null (g_file_get_parent (file));
116 g_object_unref (file);
125 GError *error = NULL;
127 datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
129 file = g_file_get_child (datapath_f, "g-icon.c");
130 type = g_file_query_file_type (file, 0, NULL);
131 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
132 g_object_unref (file);
134 file = g_file_get_child (datapath_f, "cert-tests");
135 type = g_file_query_file_type (file, 0, NULL);
136 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
138 g_file_read (file, NULL, &error);
139 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
140 g_error_free (error);
141 g_object_unref (file);
143 g_object_unref (datapath_f);
147 test_parse_name (void)
152 file = g_file_new_for_uri ("file://somewhere");
153 name = g_file_get_parse_name (file);
154 g_assert_cmpstr (name, ==, "file://somewhere");
155 g_object_unref (file);
158 file = g_file_parse_name ("~foo");
159 name = g_file_get_parse_name (file);
160 g_assert_nonnull (name);
161 g_object_unref (file);
167 GMainContext *context;
169 GFileMonitor *monitor;
170 GOutputStream *ostream;
171 GInputStream *istream;
173 gint monitor_created;
174 gint monitor_deleted;
175 gint monitor_changed;
181 gboolean file_deleted;
186 monitor_changed (GFileMonitor *monitor,
189 GFileMonitorEvent event_type,
192 CreateDeleteData *data = user_data;
194 const gchar *peeked_path;
196 path = g_file_get_path (file);
197 peeked_path = g_file_peek_path (file);
198 g_assert_cmpstr (data->monitor_path, ==, path);
199 g_assert_cmpstr (path, ==, peeked_path);
202 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
203 data->monitor_created++;
204 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
205 data->monitor_deleted++;
206 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
207 data->monitor_changed++;
209 g_main_context_wakeup (data->context);
213 iclosed_cb (GObject *source,
217 CreateDeleteData *data = user_data;
222 ret = g_input_stream_close_finish (data->istream, res, &error);
223 g_assert_no_error (error);
226 g_assert_true (g_input_stream_is_closed (data->istream));
228 ret = g_file_delete (data->file, NULL, &error);
230 g_assert_no_error (error);
232 data->file_deleted = TRUE;
233 g_main_context_wakeup (data->context);
237 read_cb (GObject *source,
241 CreateDeleteData *data = user_data;
246 size = g_input_stream_read_finish (data->istream, res, &error);
247 g_assert_no_error (error);
250 if (data->pos < strlen (data->data))
252 g_input_stream_read_async (data->istream,
253 data->buffer + data->pos,
254 strlen (data->data) - data->pos,
262 g_assert_cmpstr (data->buffer, ==, data->data);
263 g_assert_false (g_input_stream_is_closed (data->istream));
264 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
269 ipending_cb (GObject *source,
273 CreateDeleteData *data = user_data;
277 g_input_stream_read_finish (data->istream, res, &error);
278 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
279 g_error_free (error);
283 skipped_cb (GObject *source,
287 CreateDeleteData *data = user_data;
292 size = g_input_stream_skip_finish (data->istream, res, &error);
293 g_assert_no_error (error);
294 g_assert_cmpint (size, ==, data->pos);
296 g_input_stream_read_async (data->istream,
297 data->buffer + data->pos,
298 strlen (data->data) - data->pos,
303 /* check that we get a pending error */
304 g_input_stream_read_async (data->istream,
305 data->buffer + data->pos,
306 strlen (data->data) - data->pos,
314 opened_cb (GObject *source,
318 GFileInputStream *base;
319 CreateDeleteData *data = user_data;
323 base = g_file_read_finish (data->file, res, &error);
324 g_assert_no_error (error);
326 if (data->buffersize == 0)
327 data->istream = G_INPUT_STREAM (g_object_ref (base));
329 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
330 g_object_unref (base);
332 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
334 /* copy initial segment directly, then skip */
335 memcpy (data->buffer, data->data, 10);
338 g_input_stream_skip_async (data->istream,
347 oclosed_cb (GObject *source,
351 CreateDeleteData *data = user_data;
356 ret = g_output_stream_close_finish (data->ostream, res, &error);
357 g_assert_no_error (error);
359 g_assert_true (g_output_stream_is_closed (data->ostream));
361 g_file_read_async (data->file, 0, NULL, opened_cb, data);
365 written_cb (GObject *source,
369 CreateDeleteData *data = user_data;
374 size = g_output_stream_write_finish (data->ostream, res, &error);
375 g_assert_no_error (error);
378 if (data->pos < strlen (data->data))
380 g_output_stream_write_async (data->ostream,
381 data->data + data->pos,
382 strlen (data->data) - data->pos,
390 g_assert_false (g_output_stream_is_closed (data->ostream));
391 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
396 opending_cb (GObject *source,
400 CreateDeleteData *data = user_data;
404 g_output_stream_write_finish (data->ostream, res, &error);
405 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
406 g_error_free (error);
410 created_cb (GObject *source,
414 GFileOutputStream *base;
415 CreateDeleteData *data = user_data;
419 base = g_file_create_finish (G_FILE (source), res, &error);
420 g_assert_no_error (error);
421 g_assert_true (g_file_query_exists (data->file, NULL));
423 if (data->buffersize == 0)
424 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
426 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
427 g_object_unref (base);
429 g_output_stream_write_async (data->ostream,
436 /* check that we get a pending error */
437 g_output_stream_write_async (data->ostream,
447 stop_timeout (gpointer user_data)
449 CreateDeleteData *data = user_data;
451 data->timed_out = TRUE;
452 g_main_context_wakeup (data->context);
454 return G_SOURCE_REMOVE;
458 * This test does a fully async create-write-read-delete.
462 test_create_delete (gconstpointer d)
465 CreateDeleteData *data;
466 GFileIOStream *iostream;
468 data = g_new0 (CreateDeleteData, 1);
470 data->buffersize = GPOINTER_TO_INT (d);
471 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
474 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
476 g_assert_nonnull (data->file);
477 g_object_unref (iostream);
479 data->monitor_path = g_file_get_path (data->file);
480 remove (data->monitor_path);
482 g_assert_false (g_file_query_exists (data->file, NULL));
485 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
486 g_assert_no_error (error);
488 /* This test doesn't work with GPollFileMonitor, because it assumes
489 * that the monitor will notice a create immediately followed by a
490 * delete, rather than coalescing them into nothing.
492 /* This test also doesn't work with GKqueueFileMonitor because of
493 * the same reason. Kqueue is able to return a kevent when a file is
494 * created or deleted in a directory. However, the kernel doesn't tell
495 * the program file names, so GKqueueFileMonitor has to calculate the
496 * difference itself. This is usually too slow for rapid file creation
497 * and deletion tests.
499 if (strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor") == 0 ||
500 strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GKqueueFileMonitor") == 0)
502 g_test_skip ("skipping test for this GFileMonitor implementation");
506 g_file_monitor_set_rate_limit (data->monitor, 100);
508 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
510 /* Use the global default main context */
511 data->context = NULL;
512 data->timeout = g_timeout_add_seconds (10, stop_timeout, data);
514 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
516 while (!data->timed_out &&
517 (data->monitor_created == 0 ||
518 data->monitor_deleted == 0 ||
519 data->monitor_changed == 0 ||
520 !data->file_deleted))
521 g_main_context_iteration (data->context, TRUE);
523 g_source_remove (data->timeout);
525 g_assert_false (data->timed_out);
526 g_assert_true (data->file_deleted);
527 g_assert_cmpint (data->monitor_created, ==, 1);
528 g_assert_cmpint (data->monitor_deleted, ==, 1);
529 g_assert_cmpint (data->monitor_changed, >, 0);
531 g_assert_false (g_file_monitor_is_cancelled (data->monitor));
532 g_file_monitor_cancel (data->monitor);
533 g_assert_true (g_file_monitor_is_cancelled (data->monitor));
535 g_clear_pointer (&data->context, g_main_context_unref);
536 g_object_unref (data->ostream);
537 g_object_unref (data->istream);
540 g_object_unref (data->monitor);
541 g_object_unref (data->file);
542 g_free (data->monitor_path);
543 g_free (data->buffer);
547 static const gchar *original_data =
549 " * g_file_replace_contents_async:\n"
552 static const gchar *replace_data =
554 " * g_file_replace_contents_async:\n"
555 " * @file: input #GFile.\n"
556 " * @contents: string of contents to replace the file with.\n"
557 " * @length: the length of @contents in bytes.\n"
558 " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
559 " * @make_backup: %TRUE if a backup should be created.\n"
560 " * @flags: a set of #GFileCreateFlags.\n"
561 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
562 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
563 " * @user_data: the data to pass to callback function\n"
565 " * Starts an asynchronous replacement of @file with the given \n"
566 " * @contents of @length bytes. @etag will replace the document's\n"
567 " * current entity tag.\n"
569 " * When this operation has completed, @callback will be called with\n"
570 " * @user_user data, and the operation can be finalized with \n"
571 " * g_file_replace_contents_finish().\n"
573 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
574 " * triggering the cancellable object from another thread. If the operation\n"
575 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
577 " * If @make_backup is %TRUE, this function will attempt to \n"
578 " * make a backup of @file.\n"
589 static void replaced_cb (GObject *source,
594 loaded_cb (GObject *source,
598 ReplaceLoadData *data = user_data;
605 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
607 g_assert_no_error (error);
608 g_assert_cmpint (length, ==, strlen (data->data));
609 g_assert_cmpstr (contents, ==, data->data);
616 data->data = "pi pa po";
618 g_file_replace_contents_async (data->file,
631 ret = g_file_delete (data->file, NULL, &error);
632 g_assert_no_error (error);
634 g_assert_false (g_file_query_exists (data->file, NULL));
636 g_main_loop_quit (data->loop);
641 replaced_cb (GObject *source,
645 ReplaceLoadData *data = user_data;
649 g_file_replace_contents_finish (data->file, res, NULL, &error);
650 g_assert_no_error (error);
652 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
656 test_replace_load (void)
658 ReplaceLoadData *data;
660 GFileIOStream *iostream;
662 data = g_new0 (ReplaceLoadData, 1);
664 data->data = replace_data;
666 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
668 g_assert_nonnull (data->file);
669 g_object_unref (iostream);
671 path = g_file_peek_path (data->file);
674 g_assert_false (g_file_query_exists (data->file, NULL));
676 data->loop = g_main_loop_new (NULL, FALSE);
678 g_file_replace_contents_async (data->file,
688 g_main_loop_run (data->loop);
690 g_main_loop_unref (data->loop);
691 g_object_unref (data->file);
696 test_replace_cancel (void)
698 GFile *tmpdir, *file;
699 GFileOutputStream *ostream;
700 GFileEnumerator *fenum;
702 GCancellable *cancellable;
705 gsize nwrote, length;
707 GError *error = NULL;
709 g_test_bug ("https://bugzilla.gnome.org/629301");
711 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
712 g_assert_no_error (error);
713 tmpdir = g_file_new_for_path (path);
716 file = g_file_get_child (tmpdir, "file");
717 g_file_replace_contents (file,
719 strlen (original_data),
720 NULL, FALSE, 0, NULL,
722 g_assert_no_error (error);
724 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
725 g_assert_no_error (error);
727 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
728 replace_data, strlen (replace_data),
729 &nwrote, NULL, &error);
730 g_assert_no_error (error);
731 g_assert_cmpint (nwrote, ==, strlen (replace_data));
733 /* At this point there should be two files; the original and the
736 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
737 g_assert_no_error (error);
739 info = g_file_enumerator_next_file (fenum, NULL, &error);
740 g_assert_no_error (error);
741 g_assert_nonnull (info);
742 g_object_unref (info);
743 info = g_file_enumerator_next_file (fenum, NULL, &error);
744 g_assert_no_error (error);
745 g_assert_nonnull (info);
746 g_object_unref (info);
748 g_file_enumerator_close (fenum, NULL, &error);
749 g_assert_no_error (error);
750 g_object_unref (fenum);
752 /* Also test the g_file_enumerator_iterate() API */
753 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
754 g_assert_no_error (error);
759 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
761 g_assert_no_error (error);
766 g_assert_cmpint (count, ==, 2);
768 g_file_enumerator_close (fenum, NULL, &error);
769 g_assert_no_error (error);
770 g_object_unref (fenum);
772 /* Now test just getting child from the g_file_enumerator_iterate() API */
773 fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
774 g_assert_no_error (error);
780 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
783 g_assert_no_error (error);
788 g_assert_true (G_IS_FILE (child));
791 g_assert_cmpint (count, ==, 2);
793 g_file_enumerator_close (fenum, NULL, &error);
794 g_assert_no_error (error);
795 g_object_unref (fenum);
797 /* Make sure the temporary gets deleted even if we cancel. */
798 cancellable = g_cancellable_new ();
799 g_cancellable_cancel (cancellable);
800 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
801 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
802 g_clear_error (&error);
804 g_object_unref (cancellable);
805 g_object_unref (ostream);
807 /* Make sure that file contents wasn't actually replaced. */
808 g_file_load_contents (file,
814 g_assert_no_error (error);
815 g_assert_cmpstr (contents, ==, original_data);
818 g_file_delete (file, NULL, &error);
819 g_assert_no_error (error);
820 g_object_unref (file);
822 /* This will only succeed if the temp file was deleted. */
823 g_file_delete (tmpdir, NULL, &error);
824 g_assert_no_error (error);
825 g_object_unref (tmpdir);
829 test_replace_symlink (void)
832 gchar *tmpdir_path = NULL;
833 GFile *tmpdir = NULL, *source_file = NULL, *target_file = NULL;
834 GFileOutputStream *stream = NULL;
835 const gchar *new_contents = "this is a test message which should be written to source and not target";
837 GFileEnumerator *enumerator = NULL;
838 GFileInfo *info = NULL;
839 gchar *contents = NULL;
841 GError *local_error = NULL;
843 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2325");
844 g_test_summary ("Test that G_FILE_CREATE_REPLACE_DESTINATION doesn’t follow symlinks");
846 /* Create a fresh, empty working directory. */
847 tmpdir_path = g_dir_make_tmp ("g_file_replace_symlink_XXXXXX", &local_error);
848 g_assert_no_error (local_error);
849 tmpdir = g_file_new_for_path (tmpdir_path);
851 g_test_message ("Using temporary directory %s", tmpdir_path);
852 g_free (tmpdir_path);
854 /* Create symlink `source` which points to `target`. */
855 source_file = g_file_get_child (tmpdir, "source");
856 target_file = g_file_get_child (tmpdir, "target");
857 g_file_make_symbolic_link (source_file, "target", NULL, &local_error);
858 g_assert_no_error (local_error);
860 /* Ensure that `target` doesn’t exist */
861 g_assert_false (g_file_query_exists (target_file, NULL));
863 /* Replace the `source` symlink with a regular file using
864 * %G_FILE_CREATE_REPLACE_DESTINATION, which should replace it *without*
865 * following the symlink */
866 stream = g_file_replace (source_file, NULL, FALSE /* no backup */,
867 G_FILE_CREATE_REPLACE_DESTINATION, NULL, &local_error);
868 g_assert_no_error (local_error);
870 g_output_stream_write_all (G_OUTPUT_STREAM (stream), new_contents, strlen (new_contents),
871 &n_written, NULL, &local_error);
872 g_assert_no_error (local_error);
873 g_assert_cmpint (n_written, ==, strlen (new_contents));
875 g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, &local_error);
876 g_assert_no_error (local_error);
878 g_clear_object (&stream);
880 /* At this point, there should still only be one file: `source`. It should
881 * now be a regular file. `target` should not exist. */
882 enumerator = g_file_enumerate_children (tmpdir,
883 G_FILE_ATTRIBUTE_STANDARD_NAME ","
884 G_FILE_ATTRIBUTE_STANDARD_TYPE,
885 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
886 g_assert_no_error (local_error);
888 info = g_file_enumerator_next_file (enumerator, NULL, &local_error);
889 g_assert_no_error (local_error);
890 g_assert_nonnull (info);
892 g_assert_cmpstr (g_file_info_get_name (info), ==, "source");
893 g_assert_cmpint (g_file_info_get_file_type (info), ==, G_FILE_TYPE_REGULAR);
895 g_clear_object (&info);
897 info = g_file_enumerator_next_file (enumerator, NULL, &local_error);
898 g_assert_no_error (local_error);
899 g_assert_null (info);
901 g_file_enumerator_close (enumerator, NULL, &local_error);
902 g_assert_no_error (local_error);
903 g_clear_object (&enumerator);
905 /* Double-check that `target` doesn’t exist */
906 g_assert_false (g_file_query_exists (target_file, NULL));
908 /* Check the content of `source`. */
909 g_file_load_contents (source_file,
915 g_assert_no_error (local_error);
916 g_assert_cmpstr (contents, ==, new_contents);
917 g_assert_cmpuint (length, ==, strlen (new_contents));
921 g_file_delete (source_file, NULL, &local_error);
922 g_assert_no_error (local_error);
924 g_file_delete (tmpdir, NULL, &local_error);
925 g_assert_no_error (local_error);
927 g_clear_object (&target_file);
928 g_clear_object (&source_file);
929 g_clear_object (&tmpdir);
930 #else /* if !G_OS_UNIX */
931 g_test_skip ("Symlink replacement tests can only be run on Unix")
936 test_replace_symlink_using_etag (void)
939 gchar *tmpdir_path = NULL;
940 GFile *tmpdir = NULL, *source_file = NULL, *target_file = NULL;
941 GFileOutputStream *stream = NULL;
942 const gchar *old_contents = "this is a test message which should be written to target and then overwritten";
943 gchar *old_etag = NULL;
944 const gchar *new_contents = "this is an updated message";
946 gchar *contents = NULL;
948 GFileInfo *info = NULL;
949 GError *local_error = NULL;
951 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2417");
952 g_test_summary ("Test that ETag checks work when replacing a file through a symlink");
954 /* Create a fresh, empty working directory. */
955 tmpdir_path = g_dir_make_tmp ("g_file_replace_symlink_using_etag_XXXXXX", &local_error);
956 g_assert_no_error (local_error);
957 tmpdir = g_file_new_for_path (tmpdir_path);
959 g_test_message ("Using temporary directory %s", tmpdir_path);
960 g_free (tmpdir_path);
962 /* Create symlink `source` which points to `target`. */
963 source_file = g_file_get_child (tmpdir, "source");
964 target_file = g_file_get_child (tmpdir, "target");
965 g_file_make_symbolic_link (source_file, "target", NULL, &local_error);
966 g_assert_no_error (local_error);
968 /* Sleep for at least 1s to ensure the mtimes of `source` and `target` differ,
969 * as that’s what _g_local_file_info_create_etag() uses to create the ETag,
970 * and one failure mode we’re testing for is that the ETags of `source` and
971 * `target` are conflated. */
974 /* Create `target` with some arbitrary content. */
975 stream = g_file_create (target_file, G_FILE_CREATE_NONE, NULL, &local_error);
976 g_assert_no_error (local_error);
977 g_output_stream_write_all (G_OUTPUT_STREAM (stream), old_contents, strlen (old_contents),
978 &n_written, NULL, &local_error);
979 g_assert_no_error (local_error);
980 g_assert_cmpint (n_written, ==, strlen (old_contents));
982 g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, &local_error);
983 g_assert_no_error (local_error);
985 old_etag = g_file_output_stream_get_etag (stream);
986 g_assert_nonnull (old_etag);
987 g_assert_cmpstr (old_etag, !=, "");
989 g_clear_object (&stream);
991 /* Sleep again to ensure the ETag changes again. */
994 /* Write out a new copy of the `target`, checking its ETag first. This should
995 * replace `target` by following the symlink. */
996 stream = g_file_replace (source_file, old_etag, FALSE /* no backup */,
997 G_FILE_CREATE_NONE, NULL, &local_error);
998 g_assert_no_error (local_error);
1000 g_output_stream_write_all (G_OUTPUT_STREAM (stream), new_contents, strlen (new_contents),
1001 &n_written, NULL, &local_error);
1002 g_assert_no_error (local_error);
1003 g_assert_cmpint (n_written, ==, strlen (new_contents));
1005 g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, &local_error);
1006 g_assert_no_error (local_error);
1008 g_clear_object (&stream);
1010 /* At this point, there should be a regular file, `target`, containing
1011 * @new_contents; and a symlink `source` which points to `target`. */
1012 g_assert_cmpint (g_file_query_file_type (source_file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL), ==, G_FILE_TYPE_SYMBOLIC_LINK);
1013 g_assert_cmpint (g_file_query_file_type (target_file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL), ==, G_FILE_TYPE_REGULAR);
1015 /* Check the content of `target`. */
1016 g_file_load_contents (target_file,
1022 g_assert_no_error (local_error);
1023 g_assert_cmpstr (contents, ==, new_contents);
1024 g_assert_cmpuint (length, ==, strlen (new_contents));
1027 /* And check its ETag value has changed. */
1028 info = g_file_query_info (target_file, G_FILE_ATTRIBUTE_ETAG_VALUE,
1029 G_FILE_QUERY_INFO_NONE, NULL, &local_error);
1030 g_assert_no_error (local_error);
1031 g_assert_cmpstr (g_file_info_get_etag (info), !=, old_etag);
1033 g_clear_object (&info);
1037 g_file_delete (target_file, NULL, &local_error);
1038 g_assert_no_error (local_error);
1040 g_file_delete (source_file, NULL, &local_error);
1041 g_assert_no_error (local_error);
1043 g_file_delete (tmpdir, NULL, &local_error);
1044 g_assert_no_error (local_error);
1046 g_clear_object (&target_file);
1047 g_clear_object (&source_file);
1048 g_clear_object (&tmpdir);
1049 #else /* if !G_OS_UNIX */
1050 g_test_skip ("Symlink replacement tests can only be run on Unix")
1054 /* FIXME: These tests have only been checked on Linux. Most of them are probably
1055 * applicable on Windows, too, but that has not been tested yet.
1056 * See https://gitlab.gnome.org/GNOME/glib/-/issues/2325 */
1059 /* Different kinds of file which create_test_file() can create. */
1062 FILE_TEST_SETUP_TYPE_NONEXISTENT,
1063 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY,
1064 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY,
1065 FILE_TEST_SETUP_TYPE_DIRECTORY,
1066 FILE_TEST_SETUP_TYPE_SOCKET,
1067 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING,
1068 FILE_TEST_SETUP_TYPE_SYMLINK_VALID,
1069 } FileTestSetupType;
1071 /* Create file `tmpdir/basename`, of type @setup_type, and chmod it to
1072 * @setup_mode. Return the #GFile representing it. Abort on any errors. */
1074 create_test_file (GFile *tmpdir,
1075 const gchar *basename,
1076 FileTestSetupType setup_type,
1079 GFile *test_file = g_file_get_child (tmpdir, basename);
1080 gchar *target_basename = g_strdup_printf ("%s-target", basename); /* for symlinks */
1081 GFile *target_file = g_file_get_child (tmpdir, target_basename);
1082 GError *local_error = NULL;
1086 case FILE_TEST_SETUP_TYPE_NONEXISTENT:
1087 /* Nothing to do here. */
1088 g_assert (setup_mode == 0);
1090 case FILE_TEST_SETUP_TYPE_REGULAR_EMPTY:
1091 case FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY:
1093 gchar *contents = NULL;
1095 if (setup_type == FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY)
1096 contents = g_strdup_printf ("this is some test content in %s", basename);
1098 contents = g_strdup ("");
1100 g_file_set_contents (g_file_peek_path (test_file), contents, -1, &local_error);
1101 g_assert_no_error (local_error);
1103 g_file_set_attribute_uint32 (test_file, G_FILE_ATTRIBUTE_UNIX_MODE,
1104 setup_mode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1105 NULL, &local_error);
1106 g_assert_no_error (local_error);
1111 case FILE_TEST_SETUP_TYPE_DIRECTORY:
1112 g_assert (setup_mode == 0);
1114 g_file_make_directory (test_file, NULL, &local_error);
1115 g_assert_no_error (local_error);
1117 case FILE_TEST_SETUP_TYPE_SOCKET:
1118 g_assert_no_errno (mknod (g_file_peek_path (test_file), S_IFSOCK | setup_mode, 0));
1120 case FILE_TEST_SETUP_TYPE_SYMLINK_VALID:
1121 g_file_set_contents (g_file_peek_path (target_file), "target file", -1, &local_error);
1122 g_assert_no_error (local_error);
1124 case FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING:
1125 /* Permissions on a symlink are not used by the kernel, so are only
1126 * applicable if the symlink is valid (and are applied to the target) */
1127 g_assert (setup_type != FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING || setup_mode == 0);
1129 g_file_make_symbolic_link (test_file, target_basename, NULL, &local_error);
1130 g_assert_no_error (local_error);
1132 if (setup_type == FILE_TEST_SETUP_TYPE_SYMLINK_VALID)
1134 g_file_set_attribute_uint32 (test_file, G_FILE_ATTRIBUTE_UNIX_MODE,
1135 setup_mode, G_FILE_QUERY_INFO_NONE,
1136 NULL, &local_error);
1137 g_assert_no_error (local_error);
1140 if (setup_type == FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING)
1142 /* Ensure that the target doesn’t exist */
1143 g_assert_false (g_file_query_exists (target_file, NULL));
1147 g_assert_not_reached ();
1150 g_clear_object (&target_file);
1151 g_free (target_basename);
1153 return g_steal_pointer (&test_file);
1156 /* Check that @test_file is of the @expected_type, has the @expected_mode, and
1157 * (if it’s a regular file) has the @expected_contents or (if it’s a symlink)
1158 * has the symlink target given by @expected_contents.
1160 * @test_file must point to the file `tmpdir/basename`.
1162 * Aborts on any errors or mismatches against the expectations.
1165 check_test_file (GFile *test_file,
1167 const gchar *basename,
1168 FileTestSetupType expected_type,
1169 guint expected_mode,
1170 const gchar *expected_contents)
1172 gchar *target_basename = g_strdup_printf ("%s-target", basename); /* for symlinks */
1173 GFile *target_file = g_file_get_child (tmpdir, target_basename);
1174 GFileType test_file_type = g_file_query_file_type (test_file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL);
1175 GFileInfo *info = NULL;
1176 GError *local_error = NULL;
1178 switch (expected_type)
1180 case FILE_TEST_SETUP_TYPE_NONEXISTENT:
1181 g_assert (expected_mode == 0);
1182 g_assert (expected_contents == NULL);
1184 g_assert_false (g_file_query_exists (test_file, NULL));
1185 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_UNKNOWN);
1187 case FILE_TEST_SETUP_TYPE_REGULAR_EMPTY:
1188 case FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY:
1189 g_assert (expected_type != FILE_TEST_SETUP_TYPE_REGULAR_EMPTY || expected_contents == NULL);
1190 g_assert (expected_type != FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY || expected_contents != NULL);
1192 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_REGULAR);
1194 info = g_file_query_info (test_file,
1195 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1196 G_FILE_ATTRIBUTE_UNIX_MODE,
1197 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1198 g_assert_no_error (local_error);
1200 if (expected_type == FILE_TEST_SETUP_TYPE_REGULAR_EMPTY)
1201 g_assert_cmpint (g_file_info_get_size (info), ==, 0);
1203 g_assert_cmpint (g_file_info_get_size (info), >, 0);
1205 if (expected_contents != NULL)
1207 gchar *contents = NULL;
1210 g_file_get_contents (g_file_peek_path (test_file), &contents, &length, &local_error);
1211 g_assert_no_error (local_error);
1213 g_assert_cmpstr (contents, ==, expected_contents);
1217 g_assert_cmpuint (g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE) & 0777, ==, expected_mode);
1220 case FILE_TEST_SETUP_TYPE_DIRECTORY:
1221 g_assert (expected_mode == 0);
1222 g_assert (expected_contents == NULL);
1224 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_DIRECTORY);
1226 case FILE_TEST_SETUP_TYPE_SOCKET:
1227 g_assert (expected_contents == NULL);
1229 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_SPECIAL);
1231 info = g_file_query_info (test_file,
1232 G_FILE_ATTRIBUTE_UNIX_MODE,
1233 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1234 g_assert_no_error (local_error);
1236 g_assert_cmpuint (g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE) & 0777, ==, expected_mode);
1238 case FILE_TEST_SETUP_TYPE_SYMLINK_VALID:
1239 case FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING:
1241 GFile *symlink_target_file = NULL;
1243 /* Permissions on a symlink are not used by the kernel, so are only
1244 * applicable if the symlink is valid (and are applied to the target) */
1245 g_assert (expected_type != FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING || expected_mode == 0);
1246 g_assert (expected_contents != NULL);
1248 g_assert_cmpint (test_file_type, ==, G_FILE_TYPE_SYMBOLIC_LINK);
1250 info = g_file_query_info (test_file,
1251 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
1252 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1253 g_assert_no_error (local_error);
1255 g_assert_cmpstr (g_file_info_get_symlink_target (info), ==, expected_contents);
1257 symlink_target_file = g_file_get_child (tmpdir, g_file_info_get_symlink_target (info));
1258 if (expected_type == FILE_TEST_SETUP_TYPE_SYMLINK_VALID)
1259 g_assert_true (g_file_query_exists (symlink_target_file, NULL));
1261 g_assert_false (g_file_query_exists (symlink_target_file, NULL));
1263 if (expected_type == FILE_TEST_SETUP_TYPE_SYMLINK_VALID)
1265 GFileInfo *target_info = NULL;
1267 /* Need to re-query the info so we follow symlinks */
1268 target_info = g_file_query_info (test_file,
1269 G_FILE_ATTRIBUTE_UNIX_MODE,
1270 G_FILE_QUERY_INFO_NONE, NULL, &local_error);
1271 g_assert_no_error (local_error);
1273 g_assert_cmpuint (g_file_info_get_attribute_uint32 (target_info, G_FILE_ATTRIBUTE_UNIX_MODE) & 0777, ==, expected_mode);
1275 g_clear_object (&target_info);
1278 g_clear_object (&symlink_target_file);
1282 g_assert_not_reached ();
1285 g_clear_object (&info);
1286 g_clear_object (&target_file);
1287 g_free (target_basename);
1290 #endif /* __linux__ */
1292 /* A big test for g_file_replace() and g_file_replace_readwrite(). The
1293 * @test_data is a boolean: %TRUE to test g_file_replace_readwrite(), %FALSE to
1294 * test g_file_replace(). The test setup and checks are identical for both
1295 * functions; in the case of testing g_file_replace_readwrite(), only the output
1296 * stream side of the returned #GIOStream is tested. i.e. We test the write
1297 * behaviour of both functions is identical.
1299 * This is intended to test all static behaviour of the function: for each test
1300 * scenario, a temporary directory is set up with a source file (and maybe some
1301 * other files) in a set configuration, g_file_replace{,_readwrite}() is called,
1302 * and the final state of the directory is checked.
1304 * This test does not check dynamic behaviour or race conditions. For example,
1305 * it does not test what happens if the source file is deleted from another
1306 * process half-way through a call to g_file_replace().
1309 test_replace (gconstpointer test_data)
1312 gboolean read_write = GPOINTER_TO_UINT (test_data);
1313 const gchar *new_contents = "this is a new test message which should be written to source";
1314 const gchar *original_source_contents = "this is some test content in source";
1315 const gchar *original_backup_contents = "this is some test content in source~";
1316 mode_t current_umask = umask (0);
1317 guint32 default_public_mode = 0666 & ~current_umask;
1318 guint32 default_private_mode = 0600;
1322 /* Arguments to pass to g_file_replace(). */
1323 gboolean replace_make_backup;
1324 GFileCreateFlags replace_flags;
1325 const gchar *replace_etag; /* (nullable) */
1327 /* File system setup. */
1328 FileTestSetupType setup_source_type;
1329 guint setup_source_mode;
1330 FileTestSetupType setup_backup_type;
1331 guint setup_backup_mode;
1333 /* Expected results. */
1334 gboolean expected_success;
1335 GQuark expected_error_domain;
1336 gint expected_error_code;
1338 /* Expected final file system state. */
1339 guint expected_n_files;
1340 FileTestSetupType expected_source_type;
1341 guint expected_source_mode;
1342 const gchar *expected_source_contents; /* content for a regular file, or target for a symlink; NULL otherwise */
1343 FileTestSetupType expected_backup_type;
1344 guint expected_backup_mode;
1345 const gchar *expected_backup_contents; /* content for a regular file, or target for a symlink; NULL otherwise */
1349 /* replace_make_backup == FALSE, replace_flags == NONE, replace_etag == NULL,
1350 * all the different values of setup_source_type, mostly with a backup
1351 * file created to check it’s not modified */
1353 FALSE, G_FILE_CREATE_NONE, NULL,
1354 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1355 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1357 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1358 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1361 FALSE, G_FILE_CREATE_NONE, NULL,
1362 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1363 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1365 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1366 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1369 FALSE, G_FILE_CREATE_NONE, NULL,
1370 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1371 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1373 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1374 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1377 FALSE, G_FILE_CREATE_NONE, NULL,
1378 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1379 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1380 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1381 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1382 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1385 FALSE, G_FILE_CREATE_NONE, NULL,
1386 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1387 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1388 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1389 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1390 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1393 FALSE, G_FILE_CREATE_NONE, NULL,
1394 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1395 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1397 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1398 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1401 FALSE, G_FILE_CREATE_NONE, NULL,
1402 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1403 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1405 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1406 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1409 /* replace_etag set to an invalid value, with setup_source_type as a
1410 * regular non-empty file; replacement should fail */
1412 FALSE, G_FILE_CREATE_NONE, "incorrect etag",
1413 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1414 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1415 FALSE, G_IO_ERROR, G_IO_ERROR_WRONG_ETAG,
1416 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1417 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1420 /* replace_make_backup == TRUE, replace_flags == NONE, replace_etag == NULL,
1421 * all the different values of setup_source_type, with a backup
1422 * file created to check it’s either replaced or the operation fails */
1424 TRUE, G_FILE_CREATE_NONE, NULL,
1425 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1426 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1428 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1429 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1432 TRUE, G_FILE_CREATE_NONE, NULL,
1433 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1434 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1436 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1437 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode, NULL,
1440 TRUE, G_FILE_CREATE_NONE, NULL,
1441 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1442 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1444 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1445 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1448 TRUE, G_FILE_CREATE_NONE, NULL,
1449 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1450 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1451 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1452 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1453 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1456 TRUE, G_FILE_CREATE_NONE, NULL,
1457 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1458 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1459 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1460 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1461 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1464 TRUE, G_FILE_CREATE_NONE, NULL,
1465 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1466 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1468 /* The final situation here is a bit odd; the backup file is a bit
1469 * pointless as the original source file was a dangling symlink.
1470 * Theoretically the backup file should be that symlink, pointing to
1471 * `source-target`, and hence no longer dangling, as that file has now
1472 * been created as the new source content, since REPLACE_DESTINATION was
1473 * not specified. However, the code instead creates an empty regular
1474 * file as the backup. FIXME: This seems acceptable for now, but not
1475 * entirely ideal and would be good to fix at some point. */
1476 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1477 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, 0777 & ~current_umask, NULL,
1480 TRUE, G_FILE_CREATE_NONE, NULL,
1481 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1482 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1484 /* FIXME: The permissions for the backup file are just the default umask,
1485 * but should probably be the same as the permissions for the source
1486 * file (`default_public_mode`). This probably arises from the fact that
1487 * symlinks don’t have permissions. */
1488 3, FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1489 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, 0777 & ~current_umask, "target file",
1492 /* replace_make_backup == TRUE, replace_flags == NONE, replace_etag == NULL,
1493 * setup_source_type is a regular file, with a backup file of every type
1494 * created to check it’s either replaced or the operation fails */
1496 TRUE, G_FILE_CREATE_NONE, NULL,
1497 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1498 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1500 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1501 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1504 TRUE, G_FILE_CREATE_NONE, NULL,
1505 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1506 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1508 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1509 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1512 TRUE, G_FILE_CREATE_NONE, NULL,
1513 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1514 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1516 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1517 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1520 TRUE, G_FILE_CREATE_NONE, NULL,
1521 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1522 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1523 FALSE, G_IO_ERROR, G_IO_ERROR_CANT_CREATE_BACKUP,
1524 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1525 FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1528 TRUE, G_FILE_CREATE_NONE, NULL,
1529 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1530 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1532 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1533 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1536 TRUE, G_FILE_CREATE_NONE, NULL,
1537 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1538 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1540 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1541 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1544 TRUE, G_FILE_CREATE_NONE, NULL,
1545 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1546 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1548 /* the third file is `source~-target`, the original target of the old
1550 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1551 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1554 /* replace_make_backup == FALSE, replace_flags == REPLACE_DESTINATION,
1555 * replace_etag == NULL, all the different values of setup_source_type,
1556 * mostly with a backup file created to check it’s not modified */
1558 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1559 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1560 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1562 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1563 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1566 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1567 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1568 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1570 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1571 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1574 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1575 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1576 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1578 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1579 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1582 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1583 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1584 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1585 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1586 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1587 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1590 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1591 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1592 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1593 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1594 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1595 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1598 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1599 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1600 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1602 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1603 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1606 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1607 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1608 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1610 /* the third file is `source-target`, the original target of the old
1612 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1613 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1616 /* replace_flags == REPLACE_DESTINATION, replace_etag set to an invalid
1617 * value, with setup_source_type as a regular non-empty file; replacement
1620 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, "incorrect etag",
1621 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1622 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1623 FALSE, G_IO_ERROR, G_IO_ERROR_WRONG_ETAG,
1624 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1625 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1628 /* replace_make_backup == TRUE, replace_flags == REPLACE_DESTINATION,
1629 * replace_etag == NULL, all the different values of setup_source_type,
1630 * with a backup file created to check it’s either replaced or the
1631 * operation fails */
1633 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1634 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1635 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1637 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1638 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1641 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1642 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1643 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1645 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1646 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode, NULL,
1649 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1650 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1651 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1653 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1654 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1657 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1658 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1659 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1660 FALSE, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1661 2, FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1662 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1665 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1666 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1667 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1668 FALSE, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE,
1669 2, FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode, NULL,
1670 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_backup_contents,
1673 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1674 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1675 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1677 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1678 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0, "source-target",
1681 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1682 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1683 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1685 /* the third file is `source-target`, the original target of the old
1687 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1688 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode, "source-target",
1691 /* replace_make_backup == TRUE, replace_flags == REPLACE_DESTINATION,
1692 * replace_etag == NULL, setup_source_type is a regular file, with a
1693 * backup file of every type created to check it’s either replaced or the
1694 * operation fails */
1696 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1697 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1698 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1700 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1701 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1704 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1705 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1706 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, default_public_mode,
1708 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1709 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1712 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1713 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1714 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1716 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1717 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1720 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1721 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1722 FILE_TEST_SETUP_TYPE_DIRECTORY, 0,
1723 FALSE, G_IO_ERROR, G_IO_ERROR_CANT_CREATE_BACKUP,
1724 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1725 FILE_TEST_SETUP_TYPE_DIRECTORY, 0, NULL,
1728 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1729 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1730 FILE_TEST_SETUP_TYPE_SOCKET, default_public_mode,
1732 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1733 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1736 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1737 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1738 FILE_TEST_SETUP_TYPE_SYMLINK_DANGLING, 0,
1740 2, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1741 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1744 TRUE, G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1745 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1746 FILE_TEST_SETUP_TYPE_SYMLINK_VALID, default_public_mode,
1748 /* the third file is `source~-target`, the original target of the old
1750 3, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1751 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, original_source_contents,
1754 /* several different setups with replace_flags == PRIVATE */
1756 FALSE, G_FILE_CREATE_PRIVATE, NULL,
1757 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1758 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1760 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_private_mode, new_contents,
1761 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1764 FALSE, G_FILE_CREATE_PRIVATE, NULL,
1765 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1766 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1768 /* the file isn’t being replaced, so it should keep its existing permissions */
1769 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode, new_contents,
1770 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1773 FALSE, G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1774 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1775 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1777 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_private_mode, new_contents,
1778 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1781 FALSE, G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION, NULL,
1782 FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_public_mode,
1783 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1785 1, FILE_TEST_SETUP_TYPE_REGULAR_NONEMPTY, default_private_mode, new_contents,
1786 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1789 /* make the initial source file unreadable, so the replace operation
1792 FALSE, G_FILE_CREATE_NONE, NULL,
1793 FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, 0 /* most restrictive permissions */,
1794 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0,
1795 FALSE, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1796 1, FILE_TEST_SETUP_TYPE_REGULAR_EMPTY, 0, NULL,
1797 FILE_TEST_SETUP_TYPE_NONEXISTENT, 0, NULL,
1802 g_test_summary ("Test various situations for g_file_replace()");
1804 /* Reset the umask after querying it above. There’s no way to query it without
1806 umask (current_umask);
1807 g_test_message ("Current umask: %u", current_umask);
1809 for (i = 0; i < G_N_ELEMENTS (tests); i++)
1811 gchar *tmpdir_path = NULL;
1812 GFile *tmpdir = NULL, *source_file = NULL, *backup_file = NULL;
1813 GFileOutputStream *output_stream = NULL;
1814 GFileIOStream *io_stream = NULL;
1815 GFileEnumerator *enumerator = NULL;
1816 GFileInfo *info = NULL;
1818 GError *local_error = NULL;
1820 /* Create a fresh, empty working directory. */
1821 tmpdir_path = g_dir_make_tmp ("g_file_replace_XXXXXX", &local_error);
1822 g_assert_no_error (local_error);
1823 tmpdir = g_file_new_for_path (tmpdir_path);
1825 g_test_message ("Test %" G_GSIZE_FORMAT ", using temporary directory %s", i, tmpdir_path);
1826 g_free (tmpdir_path);
1828 /* Set up the test directory. */
1829 source_file = create_test_file (tmpdir, "source", tests[i].setup_source_type, tests[i].setup_source_mode);
1830 backup_file = create_test_file (tmpdir, "source~", tests[i].setup_backup_type, tests[i].setup_backup_mode);
1832 /* Replace the source file. Check the error state only after finishing
1833 * writing, as the replace operation is split across g_file_replace() and
1834 * g_output_stream_close(). */
1836 io_stream = g_file_replace_readwrite (source_file,
1837 tests[i].replace_etag,
1838 tests[i].replace_make_backup,
1839 tests[i].replace_flags,
1843 output_stream = g_file_replace (source_file,
1844 tests[i].replace_etag,
1845 tests[i].replace_make_backup,
1846 tests[i].replace_flags,
1850 if (tests[i].expected_success)
1852 g_assert_no_error (local_error);
1854 g_assert_nonnull (io_stream);
1856 g_assert_nonnull (output_stream);
1859 /* Write new content to it. */
1860 if (io_stream != NULL)
1862 GOutputStream *io_output_stream = g_io_stream_get_output_stream (G_IO_STREAM (io_stream));
1865 g_output_stream_write_all (G_OUTPUT_STREAM (io_output_stream), new_contents, strlen (new_contents),
1866 &n_written, NULL, &local_error);
1868 if (tests[i].expected_success)
1870 g_assert_no_error (local_error);
1871 g_assert_cmpint (n_written, ==, strlen (new_contents));
1874 g_io_stream_close (G_IO_STREAM (io_stream), NULL, (local_error == NULL) ? &local_error : NULL);
1876 if (tests[i].expected_success)
1877 g_assert_no_error (local_error);
1879 else if (output_stream != NULL)
1883 g_output_stream_write_all (G_OUTPUT_STREAM (output_stream), new_contents, strlen (new_contents),
1884 &n_written, NULL, &local_error);
1886 if (tests[i].expected_success)
1888 g_assert_no_error (local_error);
1889 g_assert_cmpint (n_written, ==, strlen (new_contents));
1892 g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, (local_error == NULL) ? &local_error : NULL);
1894 if (tests[i].expected_success)
1895 g_assert_no_error (local_error);
1898 if (tests[i].expected_success)
1899 g_assert_no_error (local_error);
1901 g_assert_error (local_error, tests[i].expected_error_domain, tests[i].expected_error_code);
1903 g_clear_error (&local_error);
1904 g_clear_object (&io_stream);
1905 g_clear_object (&output_stream);
1907 /* Verify the final state of the directory. */
1908 enumerator = g_file_enumerate_children (tmpdir,
1909 G_FILE_ATTRIBUTE_STANDARD_NAME,
1910 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &local_error);
1911 g_assert_no_error (local_error);
1916 g_file_enumerator_iterate (enumerator, &info, NULL, NULL, &local_error);
1917 g_assert_no_error (local_error);
1922 while (info != NULL);
1924 g_clear_object (&enumerator);
1926 g_assert_cmpuint (n_files, ==, tests[i].expected_n_files);
1928 check_test_file (source_file, tmpdir, "source", tests[i].expected_source_type, tests[i].expected_source_mode, tests[i].expected_source_contents);
1929 check_test_file (backup_file, tmpdir, "source~", tests[i].expected_backup_type, tests[i].expected_backup_mode, tests[i].expected_backup_contents);
1931 /* Tidy up. Ignore failure apart from when deleting the directory, which
1932 * should be empty. */
1933 g_file_delete (source_file, NULL, NULL);
1934 g_file_delete (backup_file, NULL, NULL);
1936 /* Other files which are occasionally generated by the tests. */
1938 GFile *backup_target_file = g_file_get_child (tmpdir, "source~-target");
1939 g_file_delete (backup_target_file, NULL, NULL);
1940 g_clear_object (&backup_target_file);
1943 GFile *backup_target_file = g_file_get_child (tmpdir, "source-target");
1944 g_file_delete (backup_target_file, NULL, NULL);
1945 g_clear_object (&backup_target_file);
1948 g_file_delete (tmpdir, NULL, &local_error);
1949 g_assert_no_error (local_error);
1951 g_clear_object (&backup_file);
1952 g_clear_object (&source_file);
1953 g_clear_object (&tmpdir);
1955 #else /* if !__linux__ */
1956 g_test_skip ("File replacement tests can only be run on Linux");
1961 on_file_deleted (GObject *object,
1962 GAsyncResult *result,
1965 GError *local_error = NULL;
1966 GMainLoop *loop = user_data;
1968 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
1969 g_assert_no_error (local_error);
1971 g_main_loop_quit (loop);
1975 test_async_delete (void)
1978 GFileIOStream *iostream;
1979 GError *local_error = NULL;
1980 GError **error = &local_error;
1983 file = g_file_new_tmp ("g_file_delete_XXXXXX",
1985 g_assert_no_error (local_error);
1986 g_object_unref (iostream);
1988 g_assert_true (g_file_query_exists (file, NULL));
1990 loop = g_main_loop_new (NULL, TRUE);
1992 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
1994 g_main_loop_run (loop);
1996 g_assert_false (g_file_query_exists (file, NULL));
1998 g_main_loop_unref (loop);
1999 g_object_unref (file);
2003 test_copy_preserve_mode (void)
2006 mode_t current_umask = umask (0);
2009 guint32 source_mode;
2010 guint32 expected_destination_mode;
2011 gboolean create_destination_before_copy;
2012 GFileCopyFlags copy_flags;
2016 /* Overwriting the destination file should copy the permissions from the
2017 * source file, even if %G_FILE_COPY_ALL_METADATA is set: */
2018 { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
2019 { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
2020 /* The same behaviour should hold if the destination file is not being
2021 * overwritten because it doesn’t already exist: */
2022 { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
2023 { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS },
2024 /* Anything with %G_FILE_COPY_TARGET_DEFAULT_PERMS should use the current
2025 * umask for the destination file: */
2026 { 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 },
2027 { 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
2028 { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
2029 { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS },
2033 /* Reset the umask after querying it above. There’s no way to query it without
2035 umask (current_umask);
2036 g_test_message ("Current umask: %u", current_umask);
2038 for (i = 0; i < G_N_ELEMENTS (vectors); i++)
2041 GFile *dest_tmpfile;
2042 GFileInfo *dest_info;
2043 GFileIOStream *iostream;
2044 GError *local_error = NULL;
2045 guint32 romode = vectors[i].source_mode;
2048 g_test_message ("Vector %" G_GSIZE_FORMAT, i);
2050 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
2051 &iostream, &local_error);
2052 g_assert_no_error (local_error);
2053 g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
2054 g_assert_no_error (local_error);
2055 g_clear_object (&iostream);
2057 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
2058 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2059 NULL, &local_error);
2060 g_assert_no_error (local_error);
2062 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
2063 &iostream, &local_error);
2064 g_assert_no_error (local_error);
2065 g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
2066 g_assert_no_error (local_error);
2067 g_clear_object (&iostream);
2069 if (!vectors[i].create_destination_before_copy)
2071 g_file_delete (dest_tmpfile, NULL, &local_error);
2072 g_assert_no_error (local_error);
2075 g_file_copy (tmpfile, dest_tmpfile, vectors[i].copy_flags,
2076 NULL, NULL, NULL, &local_error);
2077 g_assert_no_error (local_error);
2079 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2080 NULL, &local_error);
2081 g_assert_no_error (local_error);
2083 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
2085 g_assert_cmpint (dest_mode & ~S_IFMT, ==, vectors[i].expected_destination_mode);
2086 g_assert_cmpint (dest_mode & S_IFMT, ==, S_IFREG);
2088 (void) g_file_delete (tmpfile, NULL, NULL);
2089 (void) g_file_delete (dest_tmpfile, NULL, NULL);
2091 g_clear_object (&tmpfile);
2092 g_clear_object (&dest_tmpfile);
2093 g_clear_object (&dest_info);
2095 #else /* if !G_OS_UNIX */
2096 g_test_skip ("File permissions tests can only be run on Unix")
2101 splice_to_string (GInputStream *stream,
2104 GMemoryOutputStream *buffer = NULL;
2107 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
2108 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
2111 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
2114 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
2117 ret = g_memory_output_stream_steal_data (buffer);
2119 g_clear_object (&buffer);
2124 get_size_from_du (const gchar *path, guint64 *size)
2130 GError *error = NULL;
2131 gchar *du_path = NULL;
2133 /* If we can’t find du, don’t try and run the test. */
2134 du_path = g_find_program_in_path ("du");
2135 if (du_path == NULL)
2139 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
2141 "du", "--bytes", "-s", path, NULL);
2142 g_assert_no_error (error);
2144 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
2145 g_assert_no_error (error);
2147 *size = g_ascii_strtoll (result, &endptr, 10);
2149 g_subprocess_wait (du, NULL, &error);
2150 g_assert_no_error (error);
2152 ok = g_subprocess_get_successful (du);
2154 g_object_unref (du);
2168 GError *error = NULL;
2172 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
2173 file = g_file_new_for_path (path);
2175 if (!get_size_from_du (path, &size))
2177 g_test_message ("du not found or fail to run, skipping byte measurement");
2181 ok = g_file_measure_disk_usage (file,
2182 G_FILE_MEASURE_APPARENT_SIZE,
2191 g_assert_no_error (error);
2194 g_assert_cmpuint (num_bytes, ==, size);
2195 g_assert_cmpuint (num_dirs, ==, 6);
2196 g_assert_cmpuint (num_files, ==, 32);
2198 g_object_unref (file);
2203 guint64 expected_bytes;
2204 guint64 expected_dirs;
2205 guint64 expected_files;
2206 gint progress_count;
2207 guint64 progress_bytes;
2208 guint64 progress_dirs;
2209 guint64 progress_files;
2213 measure_progress (gboolean reporting,
2214 guint64 current_size,
2219 MeasureData *data = user_data;
2221 data->progress_count += 1;
2223 g_assert_cmpuint (current_size, >=, data->progress_bytes);
2224 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
2225 g_assert_cmpuint (num_files, >=, data->progress_files);
2227 data->progress_bytes = current_size;
2228 data->progress_dirs = num_dirs;
2229 data->progress_files = num_files;
2233 measure_done (GObject *source,
2237 MeasureData *data = user_data;
2238 guint64 num_bytes, num_dirs, num_files;
2239 GError *error = NULL;
2242 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
2244 g_assert_no_error (error);
2246 if (data->expected_bytes > 0)
2247 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
2248 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
2249 g_assert_cmpuint (data->expected_files, ==, num_files);
2251 g_assert_cmpuint (data->progress_count, >, 0);
2252 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
2253 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
2254 g_assert_cmpuint (num_files, >=, data->progress_files);
2257 g_object_unref (source);
2261 test_measure_async (void)
2267 data = g_new (MeasureData, 1);
2269 data->progress_count = 0;
2270 data->progress_bytes = 0;
2271 data->progress_files = 0;
2272 data->progress_dirs = 0;
2274 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
2275 file = g_file_new_for_path (path);
2277 if (!get_size_from_du (path, &data->expected_bytes))
2279 g_test_message ("du not found or fail to run, skipping byte measurement");
2280 data->expected_bytes = 0;
2285 data->expected_dirs = 6;
2286 data->expected_files = 32;
2288 g_file_measure_disk_usage_async (file,
2289 G_FILE_MEASURE_APPARENT_SIZE,
2291 measure_progress, data,
2292 measure_done, data);
2296 test_load_bytes (void)
2298 gchar filename[] = "g_file_load_bytes_XXXXXX";
2299 GError *error = NULL;
2306 fd = g_mkstemp (filename);
2307 g_assert_cmpint (fd, !=, -1);
2308 len = strlen ("test_load_bytes");
2309 ret = write (fd, "test_load_bytes", len);
2310 g_assert_cmpint (ret, ==, len);
2313 file = g_file_new_for_path (filename);
2314 bytes = g_file_load_bytes (file, NULL, NULL, &error);
2315 g_assert_no_error (error);
2316 g_assert_nonnull (bytes);
2317 g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
2318 g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
2320 g_file_delete (file, NULL, NULL);
2322 g_bytes_unref (bytes);
2323 g_object_unref (file);
2328 GMainLoop *main_loop;
2331 } LoadBytesAsyncData;
2334 test_load_bytes_cb (GObject *object,
2335 GAsyncResult *result,
2338 GFile *file = G_FILE (object);
2339 LoadBytesAsyncData *data = user_data;
2340 GError *error = NULL;
2342 data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
2343 g_assert_no_error (error);
2344 g_assert_nonnull (data->bytes);
2346 g_main_loop_quit (data->main_loop);
2350 test_load_bytes_async (void)
2352 LoadBytesAsyncData data = { 0 };
2353 gchar filename[] = "g_file_load_bytes_XXXXXX";
2358 fd = g_mkstemp (filename);
2359 g_assert_cmpint (fd, !=, -1);
2360 len = strlen ("test_load_bytes_async");
2361 ret = write (fd, "test_load_bytes_async", len);
2362 g_assert_cmpint (ret, ==, len);
2365 data.main_loop = g_main_loop_new (NULL, FALSE);
2366 data.file = g_file_new_for_path (filename);
2368 g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
2369 g_main_loop_run (data.main_loop);
2371 g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
2372 g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
2374 g_file_delete (data.file, NULL, NULL);
2375 g_object_unref (data.file);
2376 g_bytes_unref (data.bytes);
2377 g_main_loop_unref (data.main_loop);
2381 test_writev_helper (GOutputVector *vectors,
2383 gboolean use_bytes_written,
2384 const guint8 *expected_contents,
2385 gsize expected_length)
2388 GFileIOStream *iostream = NULL;
2389 GOutputStream *ostream;
2390 GError *error = NULL;
2391 gsize bytes_written = 0;
2396 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2398 g_assert_nonnull (file);
2399 g_assert_nonnull (iostream);
2401 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2403 res = g_output_stream_writev_all (ostream, vectors, n_vectors, use_bytes_written ? &bytes_written : NULL, NULL, &error);
2404 g_assert_no_error (error);
2405 g_assert_true (res);
2406 if (use_bytes_written)
2407 g_assert_cmpuint (bytes_written, ==, expected_length);
2409 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2410 g_assert_no_error (error);
2411 g_assert_true (res);
2412 g_object_unref (iostream);
2414 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2415 g_assert_no_error (error);
2416 g_assert_true (res);
2418 g_assert_cmpmem (contents, length, expected_contents, expected_length);
2422 g_file_delete (file, NULL, NULL);
2423 g_object_unref (file);
2426 /* Test that writev() on local file output streams works on a non-empty vector */
2430 GOutputVector vectors[3];
2431 const guint8 buffer[] = {1, 2, 3, 4, 5,
2432 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2435 vectors[0].buffer = buffer;
2436 vectors[0].size = 5;
2438 vectors[1].buffer = buffer + 5;
2439 vectors[1].size = 12;
2441 vectors[2].buffer = buffer + 5 + 12;
2442 vectors[2].size = 3;
2444 test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, buffer, sizeof buffer);
2447 /* Test that writev() on local file output streams works on a non-empty vector without returning bytes_written */
2449 test_writev_no_bytes_written (void)
2451 GOutputVector vectors[3];
2452 const guint8 buffer[] = {1, 2, 3, 4, 5,
2453 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2456 vectors[0].buffer = buffer;
2457 vectors[0].size = 5;
2459 vectors[1].buffer = buffer + 5;
2460 vectors[1].size = 12;
2462 vectors[2].buffer = buffer + 5 + 12;
2463 vectors[2].size = 3;
2465 test_writev_helper (vectors, G_N_ELEMENTS (vectors), FALSE, buffer, sizeof buffer);
2468 /* Test that writev() on local file output streams works on 0 vectors */
2470 test_writev_no_vectors (void)
2472 test_writev_helper (NULL, 0, TRUE, NULL, 0);
2475 /* Test that writev() on local file output streams works on empty vectors */
2477 test_writev_empty_vectors (void)
2479 GOutputVector vectors[3];
2481 vectors[0].buffer = NULL;
2482 vectors[0].size = 0;
2483 vectors[1].buffer = NULL;
2484 vectors[1].size = 0;
2485 vectors[2].buffer = NULL;
2486 vectors[2].size = 0;
2488 test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, NULL, 0);
2491 /* Test that writev() fails if the sum of sizes in the vector is too big */
2493 test_writev_too_big_vectors (void)
2496 GFileIOStream *iostream = NULL;
2497 GOutputStream *ostream;
2498 GError *error = NULL;
2499 gsize bytes_written = 0;
2503 GOutputVector vectors[3];
2505 vectors[0].buffer = (void*) 1;
2506 vectors[0].size = G_MAXSIZE / 2;
2508 vectors[1].buffer = (void*) 1;
2509 vectors[1].size = G_MAXSIZE / 2;
2511 vectors[2].buffer = (void*) 1;
2512 vectors[2].size = G_MAXSIZE / 2;
2514 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2516 g_assert_nonnull (file);
2517 g_assert_nonnull (iostream);
2519 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2521 res = g_output_stream_writev_all (ostream, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
2522 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
2523 g_assert_cmpuint (bytes_written, ==, 0);
2524 g_assert_false (res);
2525 g_clear_error (&error);
2527 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2528 g_assert_no_error (error);
2529 g_assert_true (res);
2530 g_object_unref (iostream);
2532 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2533 g_assert_no_error (error);
2534 g_assert_true (res);
2536 g_assert_cmpmem (contents, length, NULL, 0);
2540 g_file_delete (file, NULL, NULL);
2541 g_object_unref (file);
2546 gsize bytes_written;
2547 GOutputVector *vectors;
2554 test_writev_async_cb (GObject *object,
2555 GAsyncResult *result,
2558 GOutputStream *ostream = G_OUTPUT_STREAM (object);
2559 WritevAsyncData *data = user_data;
2560 GError *error = NULL;
2561 gsize bytes_written;
2564 res = g_output_stream_writev_finish (ostream, result, &bytes_written, &error);
2565 g_assert_true (res);
2566 g_assert_no_error (error);
2567 data->bytes_written += bytes_written;
2569 /* skip vectors that have been written in full */
2570 while (data->n_vectors > 0 && bytes_written >= data->vectors[0].size)
2572 bytes_written -= data->vectors[0].size;
2576 /* skip partially written vector data */
2577 if (bytes_written > 0 && data->n_vectors > 0)
2579 data->vectors[0].size -= bytes_written;
2580 data->vectors[0].buffer = ((guint8 *) data->vectors[0].buffer) + bytes_written;
2583 if (data->n_vectors > 0)
2584 g_output_stream_writev_async (ostream, data->vectors, data->n_vectors, 0, NULL, test_writev_async_cb, &data);
2587 /* Test that writev_async() on local file output streams works on a non-empty vector */
2589 test_writev_async (void)
2591 WritevAsyncData data = { 0 };
2593 GFileIOStream *iostream = NULL;
2594 GOutputVector vectors[3];
2595 const guint8 buffer[] = {1, 2, 3, 4, 5,
2596 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2598 GOutputStream *ostream;
2599 GError *error = NULL;
2604 vectors[0].buffer = buffer;
2605 vectors[0].size = 5;
2607 vectors[1].buffer = buffer + 5;
2608 vectors[1].size = 12;
2610 vectors[2].buffer = buffer + 5 + 12;
2611 vectors[2].size = 3;
2613 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2615 g_assert_nonnull (file);
2616 g_assert_nonnull (iostream);
2618 data.vectors = vectors;
2619 data.n_vectors = G_N_ELEMENTS (vectors);
2621 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2623 g_output_stream_writev_async (ostream, data.vectors, data.n_vectors, 0, NULL, test_writev_async_cb, &data);
2625 while (data.n_vectors > 0)
2626 g_main_context_iteration (NULL, TRUE);
2628 g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
2630 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2631 g_assert_no_error (error);
2632 g_assert_true (res);
2633 g_object_unref (iostream);
2635 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2636 g_assert_no_error (error);
2637 g_assert_true (res);
2639 g_assert_cmpmem (contents, length, buffer, sizeof buffer);
2643 g_file_delete (file, NULL, NULL);
2644 g_object_unref (file);
2648 test_writev_all_cb (GObject *object,
2649 GAsyncResult *result,
2652 GOutputStream *ostream = G_OUTPUT_STREAM (object);
2653 WritevAsyncData *data = user_data;
2655 g_output_stream_writev_all_finish (ostream, result, &data->bytes_written, &data->error);
2659 /* Test that writev_async_all() on local file output streams works on a non-empty vector */
2661 test_writev_async_all (void)
2663 WritevAsyncData data = { 0 };
2665 GFileIOStream *iostream = NULL;
2666 GOutputStream *ostream;
2667 GOutputVector vectors[3];
2668 const guint8 buffer[] = {1, 2, 3, 4, 5,
2669 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2671 GError *error = NULL;
2676 vectors[0].buffer = buffer;
2677 vectors[0].size = 5;
2679 vectors[1].buffer = buffer + 5;
2680 vectors[1].size = 12;
2682 vectors[2].buffer = buffer + 5 + 12;
2683 vectors[2].size = 3;
2685 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2687 g_assert_nonnull (file);
2688 g_assert_nonnull (iostream);
2690 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2692 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
2695 g_main_context_iteration (NULL, TRUE);
2697 g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
2698 g_assert_no_error (data.error);
2700 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2701 g_assert_no_error (error);
2702 g_assert_true (res);
2703 g_object_unref (iostream);
2705 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2706 g_assert_no_error (error);
2707 g_assert_true (res);
2709 g_assert_cmpmem (contents, length, buffer, sizeof buffer);
2713 g_file_delete (file, NULL, NULL);
2714 g_object_unref (file);
2717 /* Test that writev_async_all() on local file output streams handles cancellation correctly */
2719 test_writev_async_all_cancellation (void)
2721 WritevAsyncData data = { 0 };
2723 GFileIOStream *iostream = NULL;
2724 GOutputVector vectors[3];
2725 const guint8 buffer[] = {1, 2, 3, 4, 5,
2726 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
2728 GOutputStream *ostream;
2729 GError *error = NULL;
2733 GCancellable *cancellable;
2735 vectors[0].buffer = buffer;
2736 vectors[0].size = 5;
2738 vectors[1].buffer = buffer + 5;
2739 vectors[1].size = 12;
2741 vectors[2].buffer = buffer + 5 + 12;
2742 vectors[2].size = 3;
2744 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2746 g_assert_nonnull (file);
2747 g_assert_nonnull (iostream);
2749 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2751 cancellable = g_cancellable_new ();
2752 g_cancellable_cancel (cancellable);
2754 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, cancellable, test_writev_all_cb, &data);
2757 g_main_context_iteration (NULL, TRUE);
2759 g_assert_cmpuint (data.bytes_written, ==, 0);
2760 g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
2761 g_clear_error (&data.error);
2763 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2764 g_assert_no_error (error);
2765 g_assert_true (res);
2766 g_object_unref (iostream);
2768 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2769 g_assert_no_error (error);
2770 g_assert_true (res);
2771 g_assert_cmpuint (length, ==, 0);
2775 g_file_delete (file, NULL, NULL);
2776 g_object_unref (file);
2777 g_object_unref (cancellable);
2780 /* Test that writev_async_all() with empty vectors is handled correctly */
2782 test_writev_async_all_empty_vectors (void)
2784 WritevAsyncData data = { 0 };
2786 GFileIOStream *iostream = NULL;
2787 GOutputVector vectors[3];
2788 GOutputStream *ostream;
2789 GError *error = NULL;
2794 vectors[0].buffer = NULL;
2795 vectors[0].size = 0;
2797 vectors[1].buffer = NULL;
2798 vectors[1].size = 0;
2800 vectors[2].buffer = NULL;
2801 vectors[2].size = 0;
2803 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2805 g_assert_nonnull (file);
2806 g_assert_nonnull (iostream);
2808 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2810 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
2813 g_main_context_iteration (NULL, TRUE);
2815 g_assert_cmpuint (data.bytes_written, ==, 0);
2816 g_assert_no_error (data.error);
2817 g_clear_error (&data.error);
2819 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2820 g_assert_no_error (error);
2821 g_assert_true (res);
2822 g_object_unref (iostream);
2824 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2825 g_assert_no_error (error);
2826 g_assert_true (res);
2827 g_assert_cmpuint (length, ==, 0);
2831 g_file_delete (file, NULL, NULL);
2832 g_object_unref (file);
2835 /* Test that writev_async_all() with no vectors is handled correctly */
2837 test_writev_async_all_no_vectors (void)
2839 WritevAsyncData data = { 0 };
2841 GFileIOStream *iostream = NULL;
2842 GOutputStream *ostream;
2843 GError *error = NULL;
2848 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2850 g_assert_nonnull (file);
2851 g_assert_nonnull (iostream);
2853 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2855 g_output_stream_writev_all_async (ostream, NULL, 0, 0, NULL, test_writev_all_cb, &data);
2858 g_main_context_iteration (NULL, TRUE);
2860 g_assert_cmpuint (data.bytes_written, ==, 0);
2861 g_assert_no_error (data.error);
2862 g_clear_error (&data.error);
2864 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2865 g_assert_no_error (error);
2866 g_assert_true (res);
2867 g_object_unref (iostream);
2869 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2870 g_assert_no_error (error);
2871 g_assert_true (res);
2872 g_assert_cmpuint (length, ==, 0);
2876 g_file_delete (file, NULL, NULL);
2877 g_object_unref (file);
2880 /* Test that writev_async_all() with too big vectors is handled correctly */
2882 test_writev_async_all_too_big_vectors (void)
2884 WritevAsyncData data = { 0 };
2886 GFileIOStream *iostream = NULL;
2887 GOutputVector vectors[3];
2888 GOutputStream *ostream;
2889 GError *error = NULL;
2894 vectors[0].buffer = (void*) 1;
2895 vectors[0].size = G_MAXSIZE / 2;
2897 vectors[1].buffer = (void*) 1;
2898 vectors[1].size = G_MAXSIZE / 2;
2900 vectors[2].buffer = (void*) 1;
2901 vectors[2].size = G_MAXSIZE / 2;
2903 file = g_file_new_tmp ("g_file_writev_XXXXXX",
2905 g_assert_nonnull (file);
2906 g_assert_nonnull (iostream);
2908 ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
2910 g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
2913 g_main_context_iteration (NULL, TRUE);
2915 g_assert_cmpuint (data.bytes_written, ==, 0);
2916 g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
2917 g_clear_error (&data.error);
2919 res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
2920 g_assert_no_error (error);
2921 g_assert_true (res);
2922 g_object_unref (iostream);
2924 res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
2925 g_assert_no_error (error);
2926 g_assert_true (res);
2927 g_assert_cmpuint (length, ==, 0);
2931 g_file_delete (file, NULL, NULL);
2932 g_object_unref (file);
2936 test_build_attribute_list_for_copy (void)
2939 GFileIOStream *iostream;
2940 GError *error = NULL;
2941 const GFileCopyFlags test_flags[] =
2944 G_FILE_COPY_TARGET_DEFAULT_PERMS,
2945 G_FILE_COPY_ALL_METADATA,
2946 G_FILE_COPY_ALL_METADATA | G_FILE_COPY_TARGET_DEFAULT_PERMS,
2950 gchar *attrs_with_commas;
2952 tmpfile = g_file_new_tmp ("tmp-build-attribute-list-for-copyXXXXXX",
2954 g_assert_no_error (error);
2955 g_io_stream_close ((GIOStream*)iostream, NULL, &error);
2956 g_assert_no_error (error);
2957 g_clear_object (&iostream);
2959 for (i = 0; i < G_N_ELEMENTS (test_flags); i++)
2961 GFileCopyFlags flags = test_flags[i];
2963 attrs = g_file_build_attribute_list_for_copy (tmpfile, flags, NULL, &error);
2964 g_test_message ("Attributes for copy: %s", attrs);
2965 g_assert_no_error (error);
2966 g_assert_nonnull (attrs);
2967 attrs_with_commas = g_strconcat (",", attrs, ",", NULL);
2970 /* See g_local_file_class_init for reference. */
2971 if (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS)
2972 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_MODE ","));
2974 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_MODE ","));
2976 if (flags & G_FILE_COPY_ALL_METADATA)
2978 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_UID ","));
2979 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_GID ","));
2983 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_UID ","));
2984 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_UNIX_GID ","));
2988 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_MODIFIED ","));
2989 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC ","));
2990 if (flags & G_FILE_COPY_ALL_METADATA)
2992 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS ","));
2993 g_assert_nonnull (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS_USEC ","));
2997 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS ","));
2998 g_assert_null (g_strstr_len (attrs_with_commas, -1, "," G_FILE_ATTRIBUTE_TIME_ACCESS_USEC ","));
3001 g_free (attrs_with_commas);
3004 (void) g_file_delete (tmpfile, NULL, NULL);
3005 g_clear_object (&tmpfile);
3009 main (int argc, char *argv[])
3011 g_test_init (&argc, &argv, NULL);
3013 g_test_add_func ("/file/basic", test_basic);
3014 g_test_add_func ("/file/build-filename", test_build_filename);
3015 g_test_add_func ("/file/parent", test_parent);
3016 g_test_add_func ("/file/child", test_child);
3017 g_test_add_func ("/file/empty-path", test_empty_path);
3018 g_test_add_func ("/file/type", test_type);
3019 g_test_add_func ("/file/parse-name", test_parse_name);
3020 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
3021 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
3022 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
3023 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
3024 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
3025 g_test_add_func ("/file/replace-load", test_replace_load);
3026 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
3027 g_test_add_func ("/file/replace-symlink", test_replace_symlink);
3028 g_test_add_func ("/file/replace-symlink/using-etag", test_replace_symlink_using_etag);
3029 g_test_add_data_func ("/file/replace/write-only", GUINT_TO_POINTER (FALSE), test_replace);
3030 g_test_add_data_func ("/file/replace/read-write", GUINT_TO_POINTER (TRUE), test_replace);
3031 g_test_add_func ("/file/async-delete", test_async_delete);
3032 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
3033 g_test_add_func ("/file/measure", test_measure);
3034 g_test_add_func ("/file/measure-async", test_measure_async);
3035 g_test_add_func ("/file/load-bytes", test_load_bytes);
3036 g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
3037 g_test_add_func ("/file/writev", test_writev);
3038 g_test_add_func ("/file/writev/no-bytes-written", test_writev_no_bytes_written);
3039 g_test_add_func ("/file/writev/no-vectors", test_writev_no_vectors);
3040 g_test_add_func ("/file/writev/empty-vectors", test_writev_empty_vectors);
3041 g_test_add_func ("/file/writev/too-big-vectors", test_writev_too_big_vectors);
3042 g_test_add_func ("/file/writev/async", test_writev_async);
3043 g_test_add_func ("/file/writev/async_all", test_writev_async_all);
3044 g_test_add_func ("/file/writev/async_all-empty-vectors", test_writev_async_all_empty_vectors);
3045 g_test_add_func ("/file/writev/async_all-no-vectors", test_writev_async_all_no_vectors);
3046 g_test_add_func ("/file/writev/async_all-to-big-vectors", test_writev_async_all_too_big_vectors);
3047 g_test_add_func ("/file/writev/async_all-cancellation", test_writev_async_all_cancellation);
3048 g_test_add_func ("/file/build-attribute-list-for-copy", test_build_attribute_list_for_copy);
3050 return g_test_run ();