gresourcefile: simplify path canonicalization
[glib.git] / gio / tests / file.c
blob2537031a59ffdacd84879bf109735869c791ea1f
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <gio/gio.h>
5 #include <gio/gfiledescriptorbased.h>
6 #ifdef G_OS_UNIX
7 #include <sys/stat.h>
8 #endif
10 static void
11 test_basic_for_file (GFile *file,
12 const gchar *suffix)
14 gchar *s;
16 s = g_file_get_basename (file);
17 g_assert_cmpstr (s, ==, "testfile");
18 g_free (s);
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));
23 g_free (s);
25 g_assert (g_file_has_uri_scheme (file, "file"));
26 s = g_file_get_uri_scheme (file);
27 g_assert_cmpstr (s, ==, "file");
28 g_free (s);
31 static void
32 test_basic (void)
34 GFile *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);
41 static void
42 test_build_filename (void)
44 GFile *file;
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);
55 static void
56 test_parent (void)
58 GFile *file;
59 GFile *file2;
60 GFile *parent;
61 GFile *root;
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);
80 static void
81 test_child (void)
83 GFile *file;
84 GFile *child;
85 GFile *child2;
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);
99 static void
100 test_type (void)
102 GFile *datapath_f;
103 GFile *file;
104 GFileType type;
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);
126 static void
127 test_parse_name (void)
129 GFile *file;
130 gchar *name;
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);
136 g_free (name);
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);
142 g_free (name);
145 typedef struct
147 GFile *file;
148 GFileMonitor *monitor;
149 GOutputStream *ostream;
150 GInputStream *istream;
151 GMainLoop *loop;
152 gint buffersize;
153 gint monitor_created;
154 gint monitor_deleted;
155 gint monitor_changed;
156 gchar *monitor_path;
157 gint pos;
158 gchar *data;
159 gchar *buffer;
160 guint timeout;
161 } CreateDeleteData;
163 static void
164 monitor_changed (GFileMonitor *monitor,
165 GFile *file,
166 GFile *other_file,
167 GFileMonitorEvent event_type,
168 gpointer user_data)
170 CreateDeleteData *data = user_data;
171 gchar *path;
173 path = g_file_get_path (file);
174 g_assert_cmpstr (data->monitor_path, ==, path);
175 g_free (path);
177 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
178 data->monitor_created++;
179 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
180 data->monitor_deleted++;
181 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
182 data->monitor_changed++;
186 static gboolean
187 quit_idle (gpointer user_data)
189 CreateDeleteData *data = user_data;
191 g_source_remove (data->timeout);
192 g_main_loop_quit (data->loop);
194 return FALSE;
197 static void
198 iclosed_cb (GObject *source,
199 GAsyncResult *res,
200 gpointer user_data)
202 CreateDeleteData *data = user_data;
203 GError *error;
204 gboolean ret;
206 error = NULL;
207 ret = g_input_stream_close_finish (data->istream, res, &error);
208 g_assert_no_error (error);
209 g_assert (ret);
211 g_assert (g_input_stream_is_closed (data->istream));
213 ret = g_file_delete (data->file, NULL, &error);
214 g_assert (ret);
215 g_assert_no_error (error);
217 /* work around file monitor bug:
218 * inotify events are only processed every 1000 ms, regardless
219 * of the rate limit set on the file monitor
221 g_timeout_add (2000, quit_idle, data);
224 static void
225 read_cb (GObject *source,
226 GAsyncResult *res,
227 gpointer user_data)
229 CreateDeleteData *data = user_data;
230 GError *error;
231 gssize size;
233 error = NULL;
234 size = g_input_stream_read_finish (data->istream, res, &error);
235 g_assert_no_error (error);
237 data->pos += size;
238 if (data->pos < strlen (data->data))
240 g_input_stream_read_async (data->istream,
241 data->buffer + data->pos,
242 strlen (data->data) - data->pos,
244 NULL,
245 read_cb,
246 data);
248 else
250 g_assert_cmpstr (data->buffer, ==, data->data);
251 g_assert (!g_input_stream_is_closed (data->istream));
252 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
256 static void
257 ipending_cb (GObject *source,
258 GAsyncResult *res,
259 gpointer user_data)
261 CreateDeleteData *data = user_data;
262 GError *error;
264 error = NULL;
265 g_input_stream_read_finish (data->istream, res, &error);
266 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
267 g_error_free (error);
270 static void
271 skipped_cb (GObject *source,
272 GAsyncResult *res,
273 gpointer user_data)
275 CreateDeleteData *data = user_data;
276 GError *error;
277 gssize size;
279 error = NULL;
280 size = g_input_stream_skip_finish (data->istream, res, &error);
281 g_assert_no_error (error);
282 g_assert_cmpint (size, ==, data->pos);
284 g_input_stream_read_async (data->istream,
285 data->buffer + data->pos,
286 strlen (data->data) - data->pos,
288 NULL,
289 read_cb,
290 data);
291 /* check that we get a pending error */
292 g_input_stream_read_async (data->istream,
293 data->buffer + data->pos,
294 strlen (data->data) - data->pos,
296 NULL,
297 ipending_cb,
298 data);
301 static void
302 opened_cb (GObject *source,
303 GAsyncResult *res,
304 gpointer user_data)
306 GFileInputStream *base;
307 CreateDeleteData *data = user_data;
308 GError *error;
310 error = NULL;
311 base = g_file_read_finish (data->file, res, &error);
312 g_assert_no_error (error);
314 if (data->buffersize == 0)
315 data->istream = G_INPUT_STREAM (g_object_ref (base));
316 else
317 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
318 g_object_unref (base);
320 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
322 /* copy initial segment directly, then skip */
323 memcpy (data->buffer, data->data, 10);
324 data->pos = 10;
326 g_input_stream_skip_async (data->istream,
329 NULL,
330 skipped_cb,
331 data);
334 static void
335 oclosed_cb (GObject *source,
336 GAsyncResult *res,
337 gpointer user_data)
339 CreateDeleteData *data = user_data;
340 GError *error;
341 gboolean ret;
343 error = NULL;
344 ret = g_output_stream_close_finish (data->ostream, res, &error);
345 g_assert_no_error (error);
346 g_assert (ret);
347 g_assert (g_output_stream_is_closed (data->ostream));
349 g_file_read_async (data->file, 0, NULL, opened_cb, data);
352 static void
353 written_cb (GObject *source,
354 GAsyncResult *res,
355 gpointer user_data)
357 CreateDeleteData *data = user_data;
358 gssize size;
359 GError *error;
361 error = NULL;
362 size = g_output_stream_write_finish (data->ostream, res, &error);
363 g_assert_no_error (error);
365 data->pos += size;
366 if (data->pos < strlen (data->data))
368 g_output_stream_write_async (data->ostream,
369 data->data + data->pos,
370 strlen (data->data) - data->pos,
372 NULL,
373 written_cb,
374 data);
376 else
378 g_assert (!g_output_stream_is_closed (data->ostream));
379 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
383 static void
384 opending_cb (GObject *source,
385 GAsyncResult *res,
386 gpointer user_data)
388 CreateDeleteData *data = user_data;
389 GError *error;
391 error = NULL;
392 g_output_stream_write_finish (data->ostream, res, &error);
393 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
394 g_error_free (error);
397 static void
398 created_cb (GObject *source,
399 GAsyncResult *res,
400 gpointer user_data)
402 GFileOutputStream *base;
403 CreateDeleteData *data = user_data;
404 GError *error;
406 error = NULL;
407 base = g_file_create_finish (G_FILE (source), res, &error);
408 g_assert_no_error (error);
409 g_assert (g_file_query_exists (data->file, NULL));
411 if (data->buffersize == 0)
412 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
413 else
414 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
415 g_object_unref (base);
417 g_output_stream_write_async (data->ostream,
418 data->data,
419 strlen (data->data),
421 NULL,
422 written_cb,
423 data);
424 /* check that we get a pending error */
425 g_output_stream_write_async (data->ostream,
426 data->data,
427 strlen (data->data),
429 NULL,
430 opending_cb,
431 data);
434 static gboolean
435 stop_timeout (gpointer data)
437 g_assert_not_reached ();
439 return FALSE;
443 * This test does a fully async create-write-read-delete.
444 * Callbackistan.
446 static void
447 test_create_delete (gconstpointer d)
449 GError *error;
450 CreateDeleteData *data;
451 GFileIOStream *iostream;
453 data = g_new0 (CreateDeleteData, 1);
455 data->buffersize = GPOINTER_TO_INT (d);
456 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
457 data->pos = 0;
459 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
460 &iostream, NULL);
461 g_assert (data->file != NULL);
462 g_object_unref (iostream);
464 data->monitor_path = g_file_get_path (data->file);
465 remove (data->monitor_path);
467 g_assert (!g_file_query_exists (data->file, NULL));
469 error = NULL;
470 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
471 g_assert_no_error (error);
473 /* This test doesn't work with GPollFileMonitor, because it assumes
474 * that the monitor will notice a create immediately followed by a
475 * delete, rather than coalescing them into nothing.
477 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
479 g_test_skip ("skipping test for this GFileMonitor implementation");
480 goto skip;
483 g_file_monitor_set_rate_limit (data->monitor, 100);
485 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
487 data->loop = g_main_loop_new (NULL, FALSE);
489 data->timeout = g_timeout_add (10000, stop_timeout, NULL);
491 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
493 g_main_loop_run (data->loop);
495 g_assert_cmpint (data->monitor_created, ==, 1);
496 g_assert_cmpint (data->monitor_deleted, ==, 1);
497 g_assert_cmpint (data->monitor_changed, >, 0);
499 g_assert (!g_file_monitor_is_cancelled (data->monitor));
500 g_file_monitor_cancel (data->monitor);
501 g_assert (g_file_monitor_is_cancelled (data->monitor));
503 g_main_loop_unref (data->loop);
504 g_object_unref (data->ostream);
505 g_object_unref (data->istream);
507 skip:
508 g_object_unref (data->monitor);
509 g_object_unref (data->file);
510 free (data->monitor_path);
511 g_free (data->buffer);
512 g_free (data);
515 static const gchar *replace_data =
516 "/**\n"
517 " * g_file_replace_contents_async:\n"
518 " * @file: input #GFile.\n"
519 " * @contents: string of contents to replace the file with.\n"
520 " * @length: the length of @contents in bytes.\n"
521 " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
522 " * @make_backup: %TRUE if a backup should be created.\n"
523 " * @flags: a set of #GFileCreateFlags.\n"
524 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
525 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
526 " * @user_data: the data to pass to callback function\n"
527 " * \n"
528 " * Starts an asynchronous replacement of @file with the given \n"
529 " * @contents of @length bytes. @etag will replace the document's\n"
530 " * current entity tag.\n"
531 " * \n"
532 " * When this operation has completed, @callback will be called with\n"
533 " * @user_user data, and the operation can be finalized with \n"
534 " * g_file_replace_contents_finish().\n"
535 " * \n"
536 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
537 " * triggering the cancellable object from another thread. If the operation\n"
538 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
539 " * \n"
540 " * If @make_backup is %TRUE, this function will attempt to \n"
541 " * make a backup of @file.\n"
542 " **/\n";
544 typedef struct
546 GFile *file;
547 const gchar *data;
548 GMainLoop *loop;
549 gboolean again;
550 } ReplaceLoadData;
552 static void replaced_cb (GObject *source,
553 GAsyncResult *res,
554 gpointer user_data);
556 static void
557 loaded_cb (GObject *source,
558 GAsyncResult *res,
559 gpointer user_data)
561 ReplaceLoadData *data = user_data;
562 gboolean ret;
563 GError *error;
564 gchar *contents;
565 gsize length;
567 error = NULL;
568 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
569 g_assert (ret);
570 g_assert_no_error (error);
571 g_assert_cmpint (length, ==, strlen (data->data));
572 g_assert_cmpstr (contents, ==, data->data);
574 g_free (contents);
576 if (data->again)
578 data->again = FALSE;
579 data->data = "pi pa po";
581 g_file_replace_contents_async (data->file,
582 data->data,
583 strlen (data->data),
584 NULL,
585 FALSE,
587 NULL,
588 replaced_cb,
589 data);
591 else
593 error = NULL;
594 ret = g_file_delete (data->file, NULL, &error);
595 g_assert_no_error (error);
596 g_assert (ret);
597 g_assert (!g_file_query_exists (data->file, NULL));
599 g_main_loop_quit (data->loop);
603 static void
604 replaced_cb (GObject *source,
605 GAsyncResult *res,
606 gpointer user_data)
608 ReplaceLoadData *data = user_data;
609 GError *error;
611 error = NULL;
612 g_file_replace_contents_finish (data->file, res, NULL, &error);
613 g_assert_no_error (error);
615 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
618 static void
619 test_replace_load (void)
621 ReplaceLoadData *data;
622 gchar *path;
623 GFileIOStream *iostream;
625 data = g_new0 (ReplaceLoadData, 1);
626 data->again = TRUE;
627 data->data = replace_data;
629 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
630 &iostream, NULL);
631 g_assert (data->file != NULL);
632 g_object_unref (iostream);
634 path = g_file_get_path (data->file);
635 remove (path);
637 g_assert (!g_file_query_exists (data->file, NULL));
639 data->loop = g_main_loop_new (NULL, FALSE);
641 g_file_replace_contents_async (data->file,
642 data->data,
643 strlen (data->data),
644 NULL,
645 FALSE,
647 NULL,
648 replaced_cb,
649 data);
651 g_main_loop_run (data->loop);
653 g_main_loop_unref (data->loop);
654 g_object_unref (data->file);
655 g_free (data);
656 free (path);
659 static void
660 test_replace_cancel (void)
662 GFile *tmpdir, *file;
663 GFileOutputStream *ostream;
664 GFileEnumerator *fenum;
665 GFileInfo *info;
666 GCancellable *cancellable;
667 gchar *path;
668 gsize nwrote;
669 guint count;
670 GError *error = NULL;
672 g_test_bug ("629301");
674 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
675 g_assert_no_error (error);
676 tmpdir = g_file_new_for_path (path);
677 g_free (path);
679 file = g_file_get_child (tmpdir, "file");
680 g_file_replace_contents (file,
681 replace_data,
682 strlen (replace_data),
683 NULL, FALSE, 0, NULL,
684 NULL, &error);
685 g_assert_no_error (error);
687 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
688 g_assert_no_error (error);
690 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
691 replace_data, strlen (replace_data),
692 &nwrote, NULL, &error);
693 g_assert_no_error (error);
694 g_assert_cmpint (nwrote, ==, strlen (replace_data));
696 /* At this point there should be two files; the original and the
697 * temporary.
699 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
700 g_assert_no_error (error);
702 info = g_file_enumerator_next_file (fenum, NULL, &error);
703 g_assert_no_error (error);
704 g_assert (info != NULL);
705 g_object_unref (info);
706 info = g_file_enumerator_next_file (fenum, NULL, &error);
707 g_assert_no_error (error);
708 g_assert (info != NULL);
709 g_object_unref (info);
711 g_file_enumerator_close (fenum, NULL, &error);
712 g_assert_no_error (error);
713 g_object_unref (fenum);
715 /* Also test the g_file_enumerator_iterate() API */
716 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
717 g_assert_no_error (error);
718 count = 0;
720 while (TRUE)
722 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
723 g_assert (ret);
724 g_assert_no_error (error);
725 if (!info)
726 break;
727 count++;
729 g_assert_cmpint (count, ==, 2);
731 g_file_enumerator_close (fenum, NULL, &error);
732 g_assert_no_error (error);
733 g_object_unref (fenum);
735 /* Now test just getting child from the g_file_enumerator_iterate() API */
736 fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
737 g_assert_no_error (error);
738 count = 0;
740 while (TRUE)
742 GFile *child;
743 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
745 g_assert (ret);
746 g_assert_no_error (error);
748 if (!child)
749 break;
751 g_assert (G_IS_FILE (child));
752 count++;
754 g_assert_cmpint (count, ==, 2);
756 g_file_enumerator_close (fenum, NULL, &error);
757 g_assert_no_error (error);
758 g_object_unref (fenum);
760 /* Make sure the temporary gets deleted even if we cancel. */
761 cancellable = g_cancellable_new ();
762 g_cancellable_cancel (cancellable);
763 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
764 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
765 g_clear_error (&error);
767 g_object_unref (cancellable);
768 g_object_unref (ostream);
770 g_file_delete (file, NULL, &error);
771 g_assert_no_error (error);
772 g_object_unref (file);
774 /* This will only succeed if the temp file was deleted. */
775 g_file_delete (tmpdir, NULL, &error);
776 g_assert_no_error (error);
777 g_object_unref (tmpdir);
780 static void
781 on_file_deleted (GObject *object,
782 GAsyncResult *result,
783 gpointer user_data)
785 GError *local_error = NULL;
786 GMainLoop *loop = user_data;
788 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
789 g_assert_no_error (local_error);
791 g_main_loop_quit (loop);
794 static void
795 test_async_delete (void)
797 GFile *file;
798 GFileIOStream *iostream;
799 GError *local_error = NULL;
800 GError **error = &local_error;
801 GMainLoop *loop;
803 file = g_file_new_tmp ("g_file_delete_XXXXXX",
804 &iostream, error);
805 g_assert_no_error (local_error);
806 g_object_unref (iostream);
808 g_assert (g_file_query_exists (file, NULL));
810 loop = g_main_loop_new (NULL, TRUE);
812 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
814 g_main_loop_run (loop);
816 g_assert (!g_file_query_exists (file, NULL));
818 g_main_loop_unref (loop);
819 g_object_unref (file);
822 #ifdef G_OS_UNIX
823 static void
824 test_copy_preserve_mode (void)
826 GFile *tmpfile;
827 GFile *dest_tmpfile;
828 GFileInfo *dest_info;
829 GFileIOStream *iostream;
830 GError *local_error = NULL;
831 GError **error = &local_error;
832 guint32 romode = S_IFREG | 0600;
833 guint32 dest_mode;
835 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
836 &iostream, error);
837 g_assert_no_error (local_error);
838 g_io_stream_close ((GIOStream*)iostream, NULL, error);
839 g_assert_no_error (local_error);
840 g_clear_object (&iostream);
842 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
843 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
844 NULL, error);
845 g_assert_no_error (local_error);
847 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
848 &iostream, error);
849 g_assert_no_error (local_error);
850 g_io_stream_close ((GIOStream*)iostream, NULL, error);
851 g_assert_no_error (local_error);
852 g_clear_object (&iostream);
854 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
855 NULL, NULL, NULL, error);
856 g_assert_no_error (local_error);
858 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
859 NULL, error);
860 g_assert_no_error (local_error);
862 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
864 g_assert_cmpint (dest_mode, ==, romode);
866 (void) g_file_delete (tmpfile, NULL, NULL);
867 (void) g_file_delete (dest_tmpfile, NULL, NULL);
869 g_clear_object (&tmpfile);
870 g_clear_object (&dest_tmpfile);
871 g_clear_object (&dest_info);
873 #endif
875 static gchar *
876 splice_to_string (GInputStream *stream,
877 GError **error)
879 GMemoryOutputStream *buffer = NULL;
880 char *ret = NULL;
882 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
883 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
884 goto out;
886 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
887 goto out;
889 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
890 goto out;
892 ret = g_memory_output_stream_steal_data (buffer);
893 out:
894 g_clear_object (&buffer);
895 return ret;
898 static guint64
899 get_size_from_du (const gchar *path)
901 GSubprocess *du;
902 gchar *result;
903 gchar *endptr;
904 guint64 size;
905 GError *error = NULL;
907 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
908 &error,
909 "du", "--bytes", "-s", path, NULL);
910 g_assert_no_error (error);
912 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
913 g_assert_no_error (error);
915 size = g_ascii_strtoll (result, &endptr, 10);
917 g_object_unref (du);
918 g_free (result);
920 return size;
923 static void
924 test_measure (void)
926 GFile *file;
927 guint64 size;
928 guint64 num_bytes;
929 guint64 num_dirs;
930 guint64 num_files;
931 GError *error = NULL;
932 gboolean ok;
933 gchar *path;
935 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
936 file = g_file_new_for_path (path);
938 if (g_find_program_in_path ("du"))
940 size = get_size_from_du (path);
942 else
944 g_test_message ("du not found, skipping byte measurement");
945 size = 0;
948 ok = g_file_measure_disk_usage (file,
949 G_FILE_MEASURE_APPARENT_SIZE,
950 NULL,
951 NULL,
952 NULL,
953 &num_bytes,
954 &num_dirs,
955 &num_files,
956 &error);
957 g_assert (ok);
958 g_assert_no_error (error);
960 if (size > 0)
961 g_assert_cmpuint (num_bytes, ==, size);
962 g_assert_cmpuint (num_dirs, ==, 6);
963 g_assert_cmpuint (num_files, ==, 30);
965 g_object_unref (file);
966 g_free (path);
969 typedef struct {
970 guint64 expected_bytes;
971 guint64 expected_dirs;
972 guint64 expected_files;
973 gint progress_count;
974 guint64 progress_bytes;
975 guint64 progress_dirs;
976 guint64 progress_files;
977 } MeasureData;
979 static void
980 measure_progress (gboolean reporting,
981 guint64 current_size,
982 guint64 num_dirs,
983 guint64 num_files,
984 gpointer user_data)
986 MeasureData *data = user_data;
988 data->progress_count += 1;
990 g_assert_cmpuint (current_size, >=, data->progress_bytes);
991 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
992 g_assert_cmpuint (num_files, >=, data->progress_files);
994 data->progress_bytes = current_size;
995 data->progress_dirs = num_dirs;
996 data->progress_files = num_files;
999 static void
1000 measure_done (GObject *source,
1001 GAsyncResult *res,
1002 gpointer user_data)
1004 MeasureData *data = user_data;
1005 guint64 num_bytes, num_dirs, num_files;
1006 GError *error = NULL;
1007 gboolean ok;
1009 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
1010 g_assert (ok);
1011 g_assert_no_error (error);
1013 if (data->expected_bytes > 0)
1014 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
1015 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
1016 g_assert_cmpuint (data->expected_files, ==, num_files);
1018 g_assert_cmpuint (data->progress_count, >, 0);
1019 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
1020 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1021 g_assert_cmpuint (num_files, >=, data->progress_files);
1023 g_free (data);
1024 g_object_unref (source);
1027 static void
1028 test_measure_async (void)
1030 gchar *path;
1031 GFile *file;
1032 MeasureData *data;
1034 data = g_new (MeasureData, 1);
1036 data->progress_count = 0;
1037 data->progress_bytes = 0;
1038 data->progress_files = 0;
1039 data->progress_dirs = 0;
1041 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1042 file = g_file_new_for_path (path);
1044 if (g_find_program_in_path ("du"))
1046 data->expected_bytes = get_size_from_du (path);
1048 else
1050 g_test_message ("du not found, skipping byte measurement");
1051 data->expected_bytes = 0;
1054 g_free (path);
1056 data->expected_dirs = 6;
1057 data->expected_files = 30;
1059 g_file_measure_disk_usage_async (file,
1060 G_FILE_MEASURE_APPARENT_SIZE,
1061 0, NULL,
1062 measure_progress, data,
1063 measure_done, data);
1067 main (int argc, char *argv[])
1069 g_test_init (&argc, &argv, NULL);
1071 g_test_bug_base ("http://bugzilla.gnome.org/");
1073 g_test_add_func ("/file/basic", test_basic);
1074 g_test_add_func ("/file/build-filename", test_build_filename);
1075 g_test_add_func ("/file/parent", test_parent);
1076 g_test_add_func ("/file/child", test_child);
1077 g_test_add_func ("/file/type", test_type);
1078 g_test_add_func ("/file/parse-name", test_parse_name);
1079 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1080 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1081 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1082 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1083 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1084 g_test_add_func ("/file/replace-load", test_replace_load);
1085 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1086 g_test_add_func ("/file/async-delete", test_async_delete);
1087 #ifdef G_OS_UNIX
1088 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1089 #endif
1090 g_test_add_func ("/file/measure", test_measure);
1091 g_test_add_func ("/file/measure-async", test_measure_async);
1093 return g_test_run ();