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 (g_str_has_prefix (s, "file://"));
22 g_assert (g_str_has_suffix (s, suffix));
25 g_assert (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 (g_file_has_parent (file, file2));
69 parent = g_file_get_parent (file);
70 g_assert (g_file_equal (parent, file2));
71 g_object_unref (parent);
73 g_assert (g_file_get_parent (root) == NULL);
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 (g_file_has_parent (child, file));
91 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
92 g_assert (g_file_has_parent (child2, file));
94 g_object_unref (child);
95 g_object_unref (child2);
96 g_object_unref (file);
105 GError *error = NULL;
107 datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
109 file = g_file_get_child (datapath_f, "g-icon.c");
110 type = g_file_query_file_type (file, 0, NULL);
111 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
112 g_object_unref (file);
114 file = g_file_get_child (datapath_f, "cert-tests");
115 type = g_file_query_file_type (file, 0, NULL);
116 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
118 g_file_read (file, NULL, &error);
119 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
120 g_error_free (error);
121 g_object_unref (file);
123 g_object_unref (datapath_f);
127 test_parse_name (void)
132 file = g_file_new_for_uri ("file://somewhere");
133 name = g_file_get_parse_name (file);
134 g_assert_cmpstr (name, ==, "file://somewhere");
135 g_object_unref (file);
138 file = g_file_parse_name ("~foo");
139 name = g_file_get_parse_name (file);
140 g_assert (name != NULL);
141 g_object_unref (file);
148 GFileMonitor *monitor;
149 GOutputStream *ostream;
150 GInputStream *istream;
153 gint monitor_created;
154 gint monitor_deleted;
155 gint monitor_changed;
164 monitor_changed (GFileMonitor *monitor,
167 GFileMonitorEvent event_type,
170 CreateDeleteData *data = user_data;
172 const gchar *peeked_path;
174 path = g_file_get_path (file);
175 peeked_path = g_file_peek_path (file);
176 g_assert_cmpstr (data->monitor_path, ==, path);
177 g_assert_cmpstr (path, ==, peeked_path);
180 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
181 data->monitor_created++;
182 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
183 data->monitor_deleted++;
184 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
185 data->monitor_changed++;
190 quit_idle (gpointer user_data)
192 CreateDeleteData *data = user_data;
194 g_source_remove (data->timeout);
195 g_main_loop_quit (data->loop);
201 iclosed_cb (GObject *source,
205 CreateDeleteData *data = user_data;
210 ret = g_input_stream_close_finish (data->istream, res, &error);
211 g_assert_no_error (error);
214 g_assert (g_input_stream_is_closed (data->istream));
216 ret = g_file_delete (data->file, NULL, &error);
218 g_assert_no_error (error);
220 /* work around file monitor bug:
221 * inotify events are only processed every 1000 ms, regardless
222 * of the rate limit set on the file monitor
224 g_timeout_add (2000, quit_idle, data);
228 read_cb (GObject *source,
232 CreateDeleteData *data = user_data;
237 size = g_input_stream_read_finish (data->istream, res, &error);
238 g_assert_no_error (error);
241 if (data->pos < strlen (data->data))
243 g_input_stream_read_async (data->istream,
244 data->buffer + data->pos,
245 strlen (data->data) - data->pos,
253 g_assert_cmpstr (data->buffer, ==, data->data);
254 g_assert (!g_input_stream_is_closed (data->istream));
255 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
260 ipending_cb (GObject *source,
264 CreateDeleteData *data = user_data;
268 g_input_stream_read_finish (data->istream, res, &error);
269 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
270 g_error_free (error);
274 skipped_cb (GObject *source,
278 CreateDeleteData *data = user_data;
283 size = g_input_stream_skip_finish (data->istream, res, &error);
284 g_assert_no_error (error);
285 g_assert_cmpint (size, ==, data->pos);
287 g_input_stream_read_async (data->istream,
288 data->buffer + data->pos,
289 strlen (data->data) - data->pos,
294 /* check that we get a pending error */
295 g_input_stream_read_async (data->istream,
296 data->buffer + data->pos,
297 strlen (data->data) - data->pos,
305 opened_cb (GObject *source,
309 GFileInputStream *base;
310 CreateDeleteData *data = user_data;
314 base = g_file_read_finish (data->file, res, &error);
315 g_assert_no_error (error);
317 if (data->buffersize == 0)
318 data->istream = G_INPUT_STREAM (g_object_ref (base));
320 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
321 g_object_unref (base);
323 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
325 /* copy initial segment directly, then skip */
326 memcpy (data->buffer, data->data, 10);
329 g_input_stream_skip_async (data->istream,
338 oclosed_cb (GObject *source,
342 CreateDeleteData *data = user_data;
347 ret = g_output_stream_close_finish (data->ostream, res, &error);
348 g_assert_no_error (error);
350 g_assert (g_output_stream_is_closed (data->ostream));
352 g_file_read_async (data->file, 0, NULL, opened_cb, data);
356 written_cb (GObject *source,
360 CreateDeleteData *data = user_data;
365 size = g_output_stream_write_finish (data->ostream, res, &error);
366 g_assert_no_error (error);
369 if (data->pos < strlen (data->data))
371 g_output_stream_write_async (data->ostream,
372 data->data + data->pos,
373 strlen (data->data) - data->pos,
381 g_assert (!g_output_stream_is_closed (data->ostream));
382 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
387 opending_cb (GObject *source,
391 CreateDeleteData *data = user_data;
395 g_output_stream_write_finish (data->ostream, res, &error);
396 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
397 g_error_free (error);
401 created_cb (GObject *source,
405 GFileOutputStream *base;
406 CreateDeleteData *data = user_data;
410 base = g_file_create_finish (G_FILE (source), res, &error);
411 g_assert_no_error (error);
412 g_assert (g_file_query_exists (data->file, NULL));
414 if (data->buffersize == 0)
415 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
417 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
418 g_object_unref (base);
420 g_output_stream_write_async (data->ostream,
427 /* check that we get a pending error */
428 g_output_stream_write_async (data->ostream,
438 stop_timeout (gpointer data)
440 g_assert_not_reached ();
446 * This test does a fully async create-write-read-delete.
450 test_create_delete (gconstpointer d)
453 CreateDeleteData *data;
454 GFileIOStream *iostream;
456 data = g_new0 (CreateDeleteData, 1);
458 data->buffersize = GPOINTER_TO_INT (d);
459 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
462 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
464 g_assert (data->file != NULL);
465 g_object_unref (iostream);
467 data->monitor_path = g_file_get_path (data->file);
468 remove (data->monitor_path);
470 g_assert (!g_file_query_exists (data->file, NULL));
473 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
474 g_assert_no_error (error);
476 /* This test doesn't work with GPollFileMonitor, because it assumes
477 * that the monitor will notice a create immediately followed by a
478 * delete, rather than coalescing them into nothing.
480 /* This test also doesn't work with GKqueueFileMonitor because of
481 * the same reason. Kqueue is able to return a kevent when a file is
482 * created or deleted in a directory. However, the kernel doesn't tell
483 * the program file names, so GKqueueFileMonitor has to calculate the
484 * difference itself. This is usually too slow for rapid file creation
485 * and deletion tests.
487 if (strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor") == 0 ||
488 strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GKqueueFileMonitor") == 0)
490 g_test_skip ("skipping test for this GFileMonitor implementation");
494 g_file_monitor_set_rate_limit (data->monitor, 100);
496 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
498 data->loop = g_main_loop_new (NULL, FALSE);
500 data->timeout = g_timeout_add (10000, stop_timeout, NULL);
502 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
504 g_main_loop_run (data->loop);
506 g_assert_cmpint (data->monitor_created, ==, 1);
507 g_assert_cmpint (data->monitor_deleted, ==, 1);
508 g_assert_cmpint (data->monitor_changed, >, 0);
510 g_assert (!g_file_monitor_is_cancelled (data->monitor));
511 g_file_monitor_cancel (data->monitor);
512 g_assert (g_file_monitor_is_cancelled (data->monitor));
514 g_main_loop_unref (data->loop);
515 g_object_unref (data->ostream);
516 g_object_unref (data->istream);
519 g_object_unref (data->monitor);
520 g_object_unref (data->file);
521 g_free (data->monitor_path);
522 g_free (data->buffer);
526 static const gchar *replace_data =
528 " * g_file_replace_contents_async:\n"
529 " * @file: input #GFile.\n"
530 " * @contents: string of contents to replace the file with.\n"
531 " * @length: the length of @contents in bytes.\n"
532 " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
533 " * @make_backup: %TRUE if a backup should be created.\n"
534 " * @flags: a set of #GFileCreateFlags.\n"
535 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
536 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
537 " * @user_data: the data to pass to callback function\n"
539 " * Starts an asynchronous replacement of @file with the given \n"
540 " * @contents of @length bytes. @etag will replace the document's\n"
541 " * current entity tag.\n"
543 " * When this operation has completed, @callback will be called with\n"
544 " * @user_user data, and the operation can be finalized with \n"
545 " * g_file_replace_contents_finish().\n"
547 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
548 " * triggering the cancellable object from another thread. If the operation\n"
549 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
551 " * If @make_backup is %TRUE, this function will attempt to \n"
552 " * make a backup of @file.\n"
563 static void replaced_cb (GObject *source,
568 loaded_cb (GObject *source,
572 ReplaceLoadData *data = user_data;
579 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
581 g_assert_no_error (error);
582 g_assert_cmpint (length, ==, strlen (data->data));
583 g_assert_cmpstr (contents, ==, data->data);
590 data->data = "pi pa po";
592 g_file_replace_contents_async (data->file,
605 ret = g_file_delete (data->file, NULL, &error);
606 g_assert_no_error (error);
608 g_assert (!g_file_query_exists (data->file, NULL));
610 g_main_loop_quit (data->loop);
615 replaced_cb (GObject *source,
619 ReplaceLoadData *data = user_data;
623 g_file_replace_contents_finish (data->file, res, NULL, &error);
624 g_assert_no_error (error);
626 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
630 test_replace_load (void)
632 ReplaceLoadData *data;
634 GFileIOStream *iostream;
636 data = g_new0 (ReplaceLoadData, 1);
638 data->data = replace_data;
640 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
642 g_assert (data->file != NULL);
643 g_object_unref (iostream);
645 path = g_file_peek_path (data->file);
648 g_assert (!g_file_query_exists (data->file, NULL));
650 data->loop = g_main_loop_new (NULL, FALSE);
652 g_file_replace_contents_async (data->file,
662 g_main_loop_run (data->loop);
664 g_main_loop_unref (data->loop);
665 g_object_unref (data->file);
670 test_replace_cancel (void)
672 GFile *tmpdir, *file;
673 GFileOutputStream *ostream;
674 GFileEnumerator *fenum;
676 GCancellable *cancellable;
680 GError *error = NULL;
682 g_test_bug ("629301");
684 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
685 g_assert_no_error (error);
686 tmpdir = g_file_new_for_path (path);
689 file = g_file_get_child (tmpdir, "file");
690 g_file_replace_contents (file,
692 strlen (replace_data),
693 NULL, FALSE, 0, NULL,
695 g_assert_no_error (error);
697 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
698 g_assert_no_error (error);
700 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
701 replace_data, strlen (replace_data),
702 &nwrote, NULL, &error);
703 g_assert_no_error (error);
704 g_assert_cmpint (nwrote, ==, strlen (replace_data));
706 /* At this point there should be two files; the original and the
709 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
710 g_assert_no_error (error);
712 info = g_file_enumerator_next_file (fenum, NULL, &error);
713 g_assert_no_error (error);
714 g_assert (info != NULL);
715 g_object_unref (info);
716 info = g_file_enumerator_next_file (fenum, NULL, &error);
717 g_assert_no_error (error);
718 g_assert (info != NULL);
719 g_object_unref (info);
721 g_file_enumerator_close (fenum, NULL, &error);
722 g_assert_no_error (error);
723 g_object_unref (fenum);
725 /* Also test the g_file_enumerator_iterate() API */
726 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
727 g_assert_no_error (error);
732 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
734 g_assert_no_error (error);
739 g_assert_cmpint (count, ==, 2);
741 g_file_enumerator_close (fenum, NULL, &error);
742 g_assert_no_error (error);
743 g_object_unref (fenum);
745 /* Now test just getting child from the g_file_enumerator_iterate() API */
746 fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
747 g_assert_no_error (error);
753 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
756 g_assert_no_error (error);
761 g_assert (G_IS_FILE (child));
764 g_assert_cmpint (count, ==, 2);
766 g_file_enumerator_close (fenum, NULL, &error);
767 g_assert_no_error (error);
768 g_object_unref (fenum);
770 /* Make sure the temporary gets deleted even if we cancel. */
771 cancellable = g_cancellable_new ();
772 g_cancellable_cancel (cancellable);
773 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
774 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
775 g_clear_error (&error);
777 g_object_unref (cancellable);
778 g_object_unref (ostream);
780 g_file_delete (file, NULL, &error);
781 g_assert_no_error (error);
782 g_object_unref (file);
784 /* This will only succeed if the temp file was deleted. */
785 g_file_delete (tmpdir, NULL, &error);
786 g_assert_no_error (error);
787 g_object_unref (tmpdir);
791 on_file_deleted (GObject *object,
792 GAsyncResult *result,
795 GError *local_error = NULL;
796 GMainLoop *loop = user_data;
798 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
799 g_assert_no_error (local_error);
801 g_main_loop_quit (loop);
805 test_async_delete (void)
808 GFileIOStream *iostream;
809 GError *local_error = NULL;
810 GError **error = &local_error;
813 file = g_file_new_tmp ("g_file_delete_XXXXXX",
815 g_assert_no_error (local_error);
816 g_object_unref (iostream);
818 g_assert (g_file_query_exists (file, NULL));
820 loop = g_main_loop_new (NULL, TRUE);
822 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
824 g_main_loop_run (loop);
826 g_assert (!g_file_query_exists (file, NULL));
828 g_main_loop_unref (loop);
829 g_object_unref (file);
834 test_copy_preserve_mode (void)
838 GFileInfo *dest_info;
839 GFileIOStream *iostream;
840 GError *local_error = NULL;
841 GError **error = &local_error;
842 guint32 romode = S_IFREG | 0600;
845 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
847 g_assert_no_error (local_error);
848 g_io_stream_close ((GIOStream*)iostream, NULL, error);
849 g_assert_no_error (local_error);
850 g_clear_object (&iostream);
852 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
853 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
855 g_assert_no_error (local_error);
857 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
859 g_assert_no_error (local_error);
860 g_io_stream_close ((GIOStream*)iostream, NULL, error);
861 g_assert_no_error (local_error);
862 g_clear_object (&iostream);
864 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
865 NULL, NULL, NULL, error);
866 g_assert_no_error (local_error);
868 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
870 g_assert_no_error (local_error);
872 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
874 g_assert_cmpint (dest_mode, ==, romode);
876 (void) g_file_delete (tmpfile, NULL, NULL);
877 (void) g_file_delete (dest_tmpfile, NULL, NULL);
879 g_clear_object (&tmpfile);
880 g_clear_object (&dest_tmpfile);
881 g_clear_object (&dest_info);
886 splice_to_string (GInputStream *stream,
889 GMemoryOutputStream *buffer = NULL;
892 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
893 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
896 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
899 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
902 ret = g_memory_output_stream_steal_data (buffer);
904 g_clear_object (&buffer);
909 get_size_from_du (const gchar *path, guint64 *size)
915 GError *error = NULL;
916 gchar *du_path = NULL;
918 /* If we can’t find du, don’t try and run the test. */
919 du_path = g_find_program_in_path ("du");
924 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
926 "du", "--bytes", "-s", path, NULL);
927 g_assert_no_error (error);
929 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
930 g_assert_no_error (error);
932 *size = g_ascii_strtoll (result, &endptr, 10);
934 g_subprocess_wait (du, NULL, &error);
935 g_assert_no_error (error);
937 ok = g_subprocess_get_successful (du);
953 GError *error = NULL;
957 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
958 file = g_file_new_for_path (path);
960 if (!get_size_from_du (path, &size))
962 g_test_message ("du not found or fail to run, skipping byte measurement");
966 ok = g_file_measure_disk_usage (file,
967 G_FILE_MEASURE_APPARENT_SIZE,
976 g_assert_no_error (error);
979 g_assert_cmpuint (num_bytes, ==, size);
980 g_assert_cmpuint (num_dirs, ==, 6);
981 g_assert_cmpuint (num_files, ==, 31);
983 g_object_unref (file);
988 guint64 expected_bytes;
989 guint64 expected_dirs;
990 guint64 expected_files;
992 guint64 progress_bytes;
993 guint64 progress_dirs;
994 guint64 progress_files;
998 measure_progress (gboolean reporting,
999 guint64 current_size,
1004 MeasureData *data = user_data;
1006 data->progress_count += 1;
1008 g_assert_cmpuint (current_size, >=, data->progress_bytes);
1009 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1010 g_assert_cmpuint (num_files, >=, data->progress_files);
1012 data->progress_bytes = current_size;
1013 data->progress_dirs = num_dirs;
1014 data->progress_files = num_files;
1018 measure_done (GObject *source,
1022 MeasureData *data = user_data;
1023 guint64 num_bytes, num_dirs, num_files;
1024 GError *error = NULL;
1027 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
1029 g_assert_no_error (error);
1031 if (data->expected_bytes > 0)
1032 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
1033 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
1034 g_assert_cmpuint (data->expected_files, ==, num_files);
1036 g_assert_cmpuint (data->progress_count, >, 0);
1037 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
1038 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1039 g_assert_cmpuint (num_files, >=, data->progress_files);
1042 g_object_unref (source);
1046 test_measure_async (void)
1052 data = g_new (MeasureData, 1);
1054 data->progress_count = 0;
1055 data->progress_bytes = 0;
1056 data->progress_files = 0;
1057 data->progress_dirs = 0;
1059 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1060 file = g_file_new_for_path (path);
1062 if (!get_size_from_du (path, &data->expected_bytes))
1064 g_test_message ("du not found or fail to run, skipping byte measurement");
1065 data->expected_bytes = 0;
1070 data->expected_dirs = 6;
1071 data->expected_files = 31;
1073 g_file_measure_disk_usage_async (file,
1074 G_FILE_MEASURE_APPARENT_SIZE,
1076 measure_progress, data,
1077 measure_done, data);
1081 test_load_bytes (void)
1083 gchar filename[] = "g_file_load_bytes_XXXXXX";
1084 GError *error = NULL;
1091 fd = g_mkstemp (filename);
1092 g_assert_cmpint (fd, !=, -1);
1093 len = strlen ("test_load_bytes");
1094 ret = write (fd, "test_load_bytes", len);
1095 g_assert_cmpint (ret, ==, len);
1098 file = g_file_new_for_path (filename);
1099 bytes = g_file_load_bytes (file, NULL, NULL, &error);
1100 g_assert_no_error (error);
1101 g_assert (bytes != NULL);
1102 g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
1103 g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
1105 g_file_delete (file, NULL, NULL);
1107 g_bytes_unref (bytes);
1108 g_object_unref (file);
1113 GMainLoop *main_loop;
1116 } LoadBytesAsyncData;
1119 test_load_bytes_cb (GObject *object,
1120 GAsyncResult *result,
1123 GFile *file = G_FILE (object);
1124 LoadBytesAsyncData *data = user_data;
1125 GError *error = NULL;
1127 data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
1128 g_assert_no_error (error);
1129 g_assert (data->bytes != NULL);
1131 g_main_loop_quit (data->main_loop);
1135 test_load_bytes_async (void)
1137 LoadBytesAsyncData data = { 0 };
1138 gchar filename[] = "g_file_load_bytes_XXXXXX";
1143 fd = g_mkstemp (filename);
1144 g_assert_cmpint (fd, !=, -1);
1145 len = strlen ("test_load_bytes_async");
1146 ret = write (fd, "test_load_bytes_async", len);
1147 g_assert_cmpint (ret, ==, len);
1150 data.main_loop = g_main_loop_new (NULL, FALSE);
1151 data.file = g_file_new_for_path (filename);
1153 g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
1154 g_main_loop_run (data.main_loop);
1156 g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
1157 g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
1159 g_file_delete (data.file, NULL, NULL);
1160 g_object_unref (data.file);
1161 g_bytes_unref (data.bytes);
1162 g_main_loop_unref (data.main_loop);
1166 main (int argc, char *argv[])
1168 g_test_init (&argc, &argv, NULL);
1170 g_test_bug_base ("http://bugzilla.gnome.org/");
1172 g_test_add_func ("/file/basic", test_basic);
1173 g_test_add_func ("/file/build-filename", test_build_filename);
1174 g_test_add_func ("/file/parent", test_parent);
1175 g_test_add_func ("/file/child", test_child);
1176 g_test_add_func ("/file/type", test_type);
1177 g_test_add_func ("/file/parse-name", test_parse_name);
1178 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1179 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1180 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1181 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1182 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1183 g_test_add_func ("/file/replace-load", test_replace_load);
1184 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1185 g_test_add_func ("/file/async-delete", test_async_delete);
1187 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1189 g_test_add_func ("/file/measure", test_measure);
1190 g_test_add_func ("/file/measure-async", test_measure_async);
1191 g_test_add_func ("/file/load-bytes", test_load_bytes);
1192 g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
1194 return g_test_run ();