5 #include <gio/gfiledescriptorbased.h>
16 file = g_file_new_for_path ("./some/directory/testfile");
18 s = g_file_get_basename (file);
19 g_assert_cmpstr (s, ==, "testfile");
22 s = g_file_get_uri (file);
23 g_assert (g_str_has_prefix (s, "file://"));
24 g_assert (g_str_has_suffix (s, "/some/directory/testfile"));
27 g_assert (g_file_has_uri_scheme (file, "file"));
28 s = g_file_get_uri_scheme (file);
29 g_assert_cmpstr (s, ==, "file");
32 g_object_unref (file);
43 file = g_file_new_for_path ("./some/directory/testfile");
44 file2 = g_file_new_for_path ("./some/directory");
45 root = g_file_new_for_path ("/");
47 g_assert (g_file_has_parent (file, file2));
49 parent = g_file_get_parent (file);
50 g_assert (g_file_equal (parent, file2));
51 g_object_unref (parent);
53 g_assert (g_file_get_parent (root) == NULL);
55 g_object_unref (file);
56 g_object_unref (file2);
57 g_object_unref (root);
67 file = g_file_new_for_path ("./some/directory");
68 child = g_file_get_child (file, "child");
69 g_assert (g_file_has_parent (child, file));
71 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
72 g_assert (g_file_has_parent (child2, file));
74 g_object_unref (child);
75 g_object_unref (child2);
76 g_object_unref (file);
87 datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
89 file = g_file_get_child (datapath_f, "g-icon.c");
90 type = g_file_query_file_type (file, 0, NULL);
91 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
92 g_object_unref (file);
94 file = g_file_get_child (datapath_f, "cert-tests");
95 type = g_file_query_file_type (file, 0, NULL);
96 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
98 g_file_read (file, NULL, &error);
99 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
100 g_error_free (error);
101 g_object_unref (file);
103 g_object_unref (datapath_f);
107 test_parse_name (void)
112 file = g_file_new_for_uri ("file://somewhere");
113 name = g_file_get_parse_name (file);
114 g_assert_cmpstr (name, ==, "file://somewhere");
115 g_object_unref (file);
118 file = g_file_parse_name ("~foo");
119 name = g_file_get_parse_name (file);
120 g_assert (name != NULL);
121 g_object_unref (file);
128 GFileMonitor *monitor;
129 GOutputStream *ostream;
130 GInputStream *istream;
133 gint monitor_created;
134 gint monitor_deleted;
135 gint monitor_changed;
144 monitor_changed (GFileMonitor *monitor,
147 GFileMonitorEvent event_type,
150 CreateDeleteData *data = user_data;
153 path = g_file_get_path (file);
154 g_assert_cmpstr (data->monitor_path, ==, path);
157 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
158 data->monitor_created++;
159 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
160 data->monitor_deleted++;
161 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
162 data->monitor_changed++;
167 quit_idle (gpointer user_data)
169 CreateDeleteData *data = user_data;
171 g_source_remove (data->timeout);
172 g_main_loop_quit (data->loop);
178 iclosed_cb (GObject *source,
182 CreateDeleteData *data = user_data;
187 ret = g_input_stream_close_finish (data->istream, res, &error);
188 g_assert_no_error (error);
191 g_assert (g_input_stream_is_closed (data->istream));
193 ret = g_file_delete (data->file, NULL, &error);
195 g_assert_no_error (error);
197 /* work around file monitor bug:
198 * inotify events are only processed every 1000 ms, regardless
199 * of the rate limit set on the file monitor
201 g_timeout_add (2000, quit_idle, data);
205 read_cb (GObject *source,
209 CreateDeleteData *data = user_data;
214 size = g_input_stream_read_finish (data->istream, res, &error);
215 g_assert_no_error (error);
218 if (data->pos < strlen (data->data))
220 g_input_stream_read_async (data->istream,
221 data->buffer + data->pos,
222 strlen (data->data) - data->pos,
230 g_assert_cmpstr (data->buffer, ==, data->data);
231 g_assert (!g_input_stream_is_closed (data->istream));
232 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
237 ipending_cb (GObject *source,
241 CreateDeleteData *data = user_data;
245 g_input_stream_read_finish (data->istream, res, &error);
246 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
247 g_error_free (error);
251 skipped_cb (GObject *source,
255 CreateDeleteData *data = user_data;
260 size = g_input_stream_skip_finish (data->istream, res, &error);
261 g_assert_no_error (error);
262 g_assert_cmpint (size, ==, data->pos);
264 g_input_stream_read_async (data->istream,
265 data->buffer + data->pos,
266 strlen (data->data) - data->pos,
271 /* check that we get a pending error */
272 g_input_stream_read_async (data->istream,
273 data->buffer + data->pos,
274 strlen (data->data) - data->pos,
282 opened_cb (GObject *source,
286 GFileInputStream *base;
287 CreateDeleteData *data = user_data;
291 base = g_file_read_finish (data->file, res, &error);
292 g_assert_no_error (error);
294 if (data->buffersize == 0)
295 data->istream = G_INPUT_STREAM (g_object_ref (base));
297 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
298 g_object_unref (base);
300 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
302 /* copy initial segment directly, then skip */
303 memcpy (data->buffer, data->data, 10);
306 g_input_stream_skip_async (data->istream,
315 oclosed_cb (GObject *source,
319 CreateDeleteData *data = user_data;
324 ret = g_output_stream_close_finish (data->ostream, res, &error);
325 g_assert_no_error (error);
327 g_assert (g_output_stream_is_closed (data->ostream));
329 g_file_read_async (data->file, 0, NULL, opened_cb, data);
333 written_cb (GObject *source,
337 CreateDeleteData *data = user_data;
342 size = g_output_stream_write_finish (data->ostream, res, &error);
343 g_assert_no_error (error);
346 if (data->pos < strlen (data->data))
348 g_output_stream_write_async (data->ostream,
349 data->data + data->pos,
350 strlen (data->data) - data->pos,
358 g_assert (!g_output_stream_is_closed (data->ostream));
359 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
364 opending_cb (GObject *source,
368 CreateDeleteData *data = user_data;
372 g_output_stream_write_finish (data->ostream, res, &error);
373 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
374 g_error_free (error);
378 created_cb (GObject *source,
382 GFileOutputStream *base;
383 CreateDeleteData *data = user_data;
387 base = g_file_create_finish (G_FILE (source), res, &error);
388 g_assert_no_error (error);
389 g_assert (g_file_query_exists (data->file, NULL));
391 if (data->buffersize == 0)
392 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
394 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
395 g_object_unref (base);
397 g_output_stream_write_async (data->ostream,
404 /* check that we get a pending error */
405 g_output_stream_write_async (data->ostream,
415 stop_timeout (gpointer data)
417 g_assert_not_reached ();
423 * This test does a fully async create-write-read-delete.
427 test_create_delete (gconstpointer d)
430 CreateDeleteData *data;
431 GFileIOStream *iostream;
433 data = g_new0 (CreateDeleteData, 1);
435 data->buffersize = GPOINTER_TO_INT (d);
436 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
439 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
441 g_assert (data->file != NULL);
442 g_object_unref (iostream);
444 data->monitor_path = g_file_get_path (data->file);
445 remove (data->monitor_path);
447 g_assert (!g_file_query_exists (data->file, NULL));
450 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
451 g_assert_no_error (error);
453 /* This test doesn't work with GPollFileMonitor, because it assumes
454 * that the monitor will notice a create immediately followed by a
455 * delete, rather than coalescing them into nothing.
457 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
459 g_print ("skipping test for this GFileMonitor implementation");
463 g_file_monitor_set_rate_limit (data->monitor, 100);
465 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
467 data->loop = g_main_loop_new (NULL, FALSE);
469 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
471 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
473 g_main_loop_run (data->loop);
475 g_assert_cmpint (data->monitor_created, ==, 1);
476 g_assert_cmpint (data->monitor_deleted, ==, 1);
477 g_assert_cmpint (data->monitor_changed, >, 0);
479 g_assert (!g_file_monitor_is_cancelled (data->monitor));
480 g_file_monitor_cancel (data->monitor);
481 g_assert (g_file_monitor_is_cancelled (data->monitor));
483 g_main_loop_unref (data->loop);
484 g_object_unref (data->ostream);
485 g_object_unref (data->istream);
488 g_object_unref (data->monitor);
489 g_object_unref (data->file);
490 free (data->monitor_path);
491 g_free (data->buffer);
495 static const gchar *replace_data =
497 " * g_file_replace_contents_async:\n"
498 " * @file: input #GFile.\n"
499 " * @contents: string of contents to replace the file with.\n"
500 " * @length: the length of @contents in bytes.\n"
501 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
502 " * @make_backup: %TRUE if a backup should be created.\n"
503 " * @flags: a set of #GFileCreateFlags.\n"
504 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
505 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
506 " * @user_data: the data to pass to callback function\n"
508 " * Starts an asynchronous replacement of @file with the given \n"
509 " * @contents of @length bytes. @etag will replace the document's\n"
510 " * current entity tag.\n"
512 " * When this operation has completed, @callback will be called with\n"
513 " * @user_user data, and the operation can be finalized with \n"
514 " * g_file_replace_contents_finish().\n"
516 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
517 " * triggering the cancellable object from another thread. If the operation\n"
518 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
520 " * If @make_backup is %TRUE, this function will attempt to \n"
521 " * make a backup of @file.\n"
532 static void replaced_cb (GObject *source,
537 loaded_cb (GObject *source,
541 ReplaceLoadData *data = user_data;
548 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
550 g_assert_no_error (error);
551 g_assert_cmpint (length, ==, strlen (data->data));
552 g_assert_cmpstr (contents, ==, data->data);
559 data->data = "pi pa po";
561 g_file_replace_contents_async (data->file,
574 ret = g_file_delete (data->file, NULL, &error);
575 g_assert_no_error (error);
577 g_assert (!g_file_query_exists (data->file, NULL));
579 g_main_loop_quit (data->loop);
584 replaced_cb (GObject *source,
588 ReplaceLoadData *data = user_data;
592 g_file_replace_contents_finish (data->file, res, NULL, &error);
593 g_assert_no_error (error);
595 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
599 test_replace_load (void)
601 ReplaceLoadData *data;
603 GFileIOStream *iostream;
605 data = g_new0 (ReplaceLoadData, 1);
607 data->data = replace_data;
609 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
611 g_assert (data->file != NULL);
612 g_object_unref (iostream);
614 path = g_file_get_path (data->file);
617 g_assert (!g_file_query_exists (data->file, NULL));
619 data->loop = g_main_loop_new (NULL, FALSE);
621 g_file_replace_contents_async (data->file,
631 g_main_loop_run (data->loop);
633 g_main_loop_unref (data->loop);
634 g_object_unref (data->file);
640 test_replace_cancel (void)
642 GFile *tmpdir, *file;
643 GFileOutputStream *ostream;
644 GFileEnumerator *fenum;
646 GCancellable *cancellable;
649 GError *error = NULL;
651 g_test_bug ("629301");
653 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
654 g_assert_no_error (error);
655 tmpdir = g_file_new_for_path (path);
658 file = g_file_get_child (tmpdir, "file");
659 g_file_replace_contents (file,
661 strlen (replace_data),
662 NULL, FALSE, 0, NULL,
664 g_assert_no_error (error);
666 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
667 g_assert_no_error (error);
669 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
670 replace_data, strlen (replace_data),
671 &nwrote, NULL, &error);
672 g_assert_no_error (error);
673 g_assert_cmpint (nwrote, ==, strlen (replace_data));
675 /* At this point there should be two files; the original and the
678 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
679 g_assert_no_error (error);
681 info = g_file_enumerator_next_file (fenum, NULL, &error);
682 g_assert_no_error (error);
683 g_assert (info != NULL);
684 g_object_unref (info);
685 info = g_file_enumerator_next_file (fenum, NULL, &error);
686 g_assert_no_error (error);
687 g_assert (info != NULL);
688 g_object_unref (info);
690 g_file_enumerator_close (fenum, NULL, &error);
691 g_assert_no_error (error);
692 g_object_unref (fenum);
694 /* Make sure the temporary gets deleted even if we cancel. */
695 cancellable = g_cancellable_new ();
696 g_cancellable_cancel (cancellable);
697 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
698 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
699 g_clear_error (&error);
701 g_object_unref (cancellable);
702 g_object_unref (ostream);
704 g_file_delete (file, NULL, &error);
705 g_assert_no_error (error);
706 g_object_unref (file);
708 /* This will only succeed if the temp file was deleted. */
709 g_file_delete (tmpdir, NULL, &error);
710 g_assert_no_error (error);
711 g_object_unref (tmpdir);
715 on_file_deleted (GObject *object,
716 GAsyncResult *result,
719 GError *local_error = NULL;
720 GMainLoop *loop = user_data;
722 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
723 g_assert_no_error (local_error);
725 g_main_loop_quit (loop);
729 test_async_delete (void)
732 GFileIOStream *iostream;
733 GError *local_error = NULL;
734 GError **error = &local_error;
737 file = g_file_new_tmp ("g_file_delete_XXXXXX",
739 g_assert_no_error (local_error);
740 g_object_unref (iostream);
742 g_assert (g_file_query_exists (file, NULL));
744 loop = g_main_loop_new (NULL, TRUE);
746 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
748 g_main_loop_run (loop);
750 g_assert (!g_file_query_exists (file, NULL));
752 g_main_loop_unref (loop);
753 g_object_unref (file);
758 test_copy_preserve_mode (void)
762 GFileInfo *dest_info;
763 GFileIOStream *iostream;
764 GError *local_error = NULL;
765 GError **error = &local_error;
766 guint32 romode = S_IFREG | 0600;
769 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
771 g_assert_no_error (local_error);
772 g_io_stream_close ((GIOStream*)iostream, NULL, error);
773 g_assert_no_error (local_error);
774 g_clear_object (&iostream);
776 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
777 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
779 g_assert_no_error (local_error);
781 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
783 g_assert_no_error (local_error);
784 g_io_stream_close ((GIOStream*)iostream, NULL, error);
785 g_assert_no_error (local_error);
786 g_clear_object (&iostream);
788 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
789 NULL, NULL, NULL, error);
790 g_assert_no_error (local_error);
792 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
794 g_assert_no_error (local_error);
796 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
798 g_assert_cmpint (dest_mode, ==, romode);
800 (void) g_file_delete (tmpfile, NULL, NULL);
801 (void) g_file_delete (dest_tmpfile, NULL, NULL);
803 g_clear_object (&tmpfile);
804 g_clear_object (&dest_tmpfile);
805 g_clear_object (&dest_info);
810 splice_to_string (GInputStream *stream,
813 GMemoryOutputStream *buffer = NULL;
816 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
817 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
820 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
823 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
826 ret = g_memory_output_stream_steal_data (buffer);
828 g_clear_object (&buffer);
833 get_size_from_du (const gchar *path)
839 GError *error = NULL;
841 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
843 "du", "--bytes", "-s", path, NULL);
844 g_assert_no_error (error);
846 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
847 g_assert_no_error (error);
849 size = g_ascii_strtoll (result, &endptr, 10);
865 GError *error = NULL;
869 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
870 file = g_file_new_for_path (path);
872 if (g_find_program_in_path ("du"))
874 size = get_size_from_du (path);
878 g_test_message ("du not found, skipping byte measurement");
882 ok = g_file_measure_disk_usage (file,
883 G_FILE_MEASURE_APPARENT_SIZE,
892 g_assert_no_error (error);
895 g_assert_cmpuint (num_bytes, ==, size);
896 g_assert_cmpuint (num_dirs, ==, 6);
897 g_assert_cmpuint (num_files, ==, 30);
899 g_object_unref (file);
904 guint64 expected_bytes;
905 guint64 expected_dirs;
906 guint64 expected_files;
908 guint64 progress_bytes;
909 guint64 progress_dirs;
910 guint64 progress_files;
914 measure_progress (gboolean reporting,
915 guint64 current_size,
920 MeasureData *data = user_data;
922 data->progress_count += 1;
924 g_assert_cmpuint (current_size, >=, data->progress_bytes);
925 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
926 g_assert_cmpuint (num_files, >=, data->progress_files);
928 data->progress_bytes = current_size;
929 data->progress_dirs = num_dirs;
930 data->progress_files = num_files;
934 measure_done (GObject *source,
938 MeasureData *data = user_data;
939 guint64 num_bytes, num_dirs, num_files;
940 GError *error = NULL;
943 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
945 g_assert_no_error (error);
947 if (data->expected_bytes > 0)
948 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
949 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
950 g_assert_cmpuint (data->expected_files, ==, num_files);
952 g_assert_cmpuint (data->progress_count, >, 0);
953 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
954 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
955 g_assert_cmpuint (num_files, >=, data->progress_files);
958 g_object_unref (source);
962 test_measure_async (void)
968 data = g_new (MeasureData, 1);
970 data->progress_count = 0;
971 data->progress_bytes = 0;
972 data->progress_files = 0;
973 data->progress_dirs = 0;
975 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
976 file = g_file_new_for_path (path);
978 if (g_find_program_in_path ("du"))
980 data->expected_bytes = get_size_from_du (path);
984 g_test_message ("du not found, skipping byte measurement");
985 data->expected_bytes = 0;
990 data->expected_dirs = 6;
991 data->expected_files = 30;
993 g_file_measure_disk_usage_async (file,
994 G_FILE_MEASURE_APPARENT_SIZE,
996 measure_progress, data,
1001 main (int argc, char *argv[])
1003 g_test_init (&argc, &argv, NULL);
1005 g_test_bug_base ("http://bugzilla.gnome.org/");
1007 g_test_add_func ("/file/basic", test_basic);
1008 g_test_add_func ("/file/parent", test_parent);
1009 g_test_add_func ("/file/child", test_child);
1010 g_test_add_func ("/file/type", test_type);
1011 g_test_add_func ("/file/parse-name", test_parse_name);
1012 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1013 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1014 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1015 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1016 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1017 g_test_add_func ("/file/replace-load", test_replace_load);
1018 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1019 g_test_add_func ("/file/async-delete", test_async_delete);
1021 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1023 g_test_add_func ("/file/measure", test_measure);
1024 g_test_add_func ("/file/measure-async", test_measure_async);
1026 return g_test_run ();