Merge branch 'wip/lantw/freebsd-kqueue-simple' into 'master'
[glib.git] / gio / tests / file.c
blob9d5a6e8cdcb902be763b17df630a93674871738e
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 const 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;
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);
178 g_free (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++;
189 static gboolean
190 quit_idle (gpointer user_data)
192 CreateDeleteData *data = user_data;
194 g_source_remove (data->timeout);
195 g_main_loop_quit (data->loop);
197 return FALSE;
200 static void
201 iclosed_cb (GObject *source,
202 GAsyncResult *res,
203 gpointer user_data)
205 CreateDeleteData *data = user_data;
206 GError *error;
207 gboolean ret;
209 error = NULL;
210 ret = g_input_stream_close_finish (data->istream, res, &error);
211 g_assert_no_error (error);
212 g_assert (ret);
214 g_assert (g_input_stream_is_closed (data->istream));
216 ret = g_file_delete (data->file, NULL, &error);
217 g_assert (ret);
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);
227 static void
228 read_cb (GObject *source,
229 GAsyncResult *res,
230 gpointer user_data)
232 CreateDeleteData *data = user_data;
233 GError *error;
234 gssize size;
236 error = NULL;
237 size = g_input_stream_read_finish (data->istream, res, &error);
238 g_assert_no_error (error);
240 data->pos += size;
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,
247 NULL,
248 read_cb,
249 data);
251 else
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);
259 static void
260 ipending_cb (GObject *source,
261 GAsyncResult *res,
262 gpointer user_data)
264 CreateDeleteData *data = user_data;
265 GError *error;
267 error = NULL;
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);
273 static void
274 skipped_cb (GObject *source,
275 GAsyncResult *res,
276 gpointer user_data)
278 CreateDeleteData *data = user_data;
279 GError *error;
280 gssize size;
282 error = NULL;
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,
291 NULL,
292 read_cb,
293 data);
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,
299 NULL,
300 ipending_cb,
301 data);
304 static void
305 opened_cb (GObject *source,
306 GAsyncResult *res,
307 gpointer user_data)
309 GFileInputStream *base;
310 CreateDeleteData *data = user_data;
311 GError *error;
313 error = NULL;
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));
319 else
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);
327 data->pos = 10;
329 g_input_stream_skip_async (data->istream,
332 NULL,
333 skipped_cb,
334 data);
337 static void
338 oclosed_cb (GObject *source,
339 GAsyncResult *res,
340 gpointer user_data)
342 CreateDeleteData *data = user_data;
343 GError *error;
344 gboolean ret;
346 error = NULL;
347 ret = g_output_stream_close_finish (data->ostream, res, &error);
348 g_assert_no_error (error);
349 g_assert (ret);
350 g_assert (g_output_stream_is_closed (data->ostream));
352 g_file_read_async (data->file, 0, NULL, opened_cb, data);
355 static void
356 written_cb (GObject *source,
357 GAsyncResult *res,
358 gpointer user_data)
360 CreateDeleteData *data = user_data;
361 gssize size;
362 GError *error;
364 error = NULL;
365 size = g_output_stream_write_finish (data->ostream, res, &error);
366 g_assert_no_error (error);
368 data->pos += size;
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,
375 NULL,
376 written_cb,
377 data);
379 else
381 g_assert (!g_output_stream_is_closed (data->ostream));
382 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
386 static void
387 opending_cb (GObject *source,
388 GAsyncResult *res,
389 gpointer user_data)
391 CreateDeleteData *data = user_data;
392 GError *error;
394 error = NULL;
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);
400 static void
401 created_cb (GObject *source,
402 GAsyncResult *res,
403 gpointer user_data)
405 GFileOutputStream *base;
406 CreateDeleteData *data = user_data;
407 GError *error;
409 error = NULL;
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));
416 else
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,
421 data->data,
422 strlen (data->data),
424 NULL,
425 written_cb,
426 data);
427 /* check that we get a pending error */
428 g_output_stream_write_async (data->ostream,
429 data->data,
430 strlen (data->data),
432 NULL,
433 opending_cb,
434 data);
437 static gboolean
438 stop_timeout (gpointer data)
440 g_assert_not_reached ();
442 return FALSE;
446 * This test does a fully async create-write-read-delete.
447 * Callbackistan.
449 static void
450 test_create_delete (gconstpointer d)
452 GError *error;
453 CreateDeleteData *data;
454 GFileIOStream *iostream;
456 data = g_new0 (CreateDeleteData, 1);
458 data->buffersize = GPOINTER_TO_INT (d);
459 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
460 data->pos = 0;
462 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
463 &iostream, NULL);
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));
472 error = 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");
491 goto skip;
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);
518 skip:
519 g_object_unref (data->monitor);
520 g_object_unref (data->file);
521 g_free (data->monitor_path);
522 g_free (data->buffer);
523 g_free (data);
526 static const gchar *replace_data =
527 "/**\n"
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"
538 " * \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"
542 " * \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"
546 " * \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"
550 " * \n"
551 " * If @make_backup is %TRUE, this function will attempt to \n"
552 " * make a backup of @file.\n"
553 " **/\n";
555 typedef struct
557 GFile *file;
558 const gchar *data;
559 GMainLoop *loop;
560 gboolean again;
561 } ReplaceLoadData;
563 static void replaced_cb (GObject *source,
564 GAsyncResult *res,
565 gpointer user_data);
567 static void
568 loaded_cb (GObject *source,
569 GAsyncResult *res,
570 gpointer user_data)
572 ReplaceLoadData *data = user_data;
573 gboolean ret;
574 GError *error;
575 gchar *contents;
576 gsize length;
578 error = NULL;
579 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
580 g_assert (ret);
581 g_assert_no_error (error);
582 g_assert_cmpint (length, ==, strlen (data->data));
583 g_assert_cmpstr (contents, ==, data->data);
585 g_free (contents);
587 if (data->again)
589 data->again = FALSE;
590 data->data = "pi pa po";
592 g_file_replace_contents_async (data->file,
593 data->data,
594 strlen (data->data),
595 NULL,
596 FALSE,
598 NULL,
599 replaced_cb,
600 data);
602 else
604 error = NULL;
605 ret = g_file_delete (data->file, NULL, &error);
606 g_assert_no_error (error);
607 g_assert (ret);
608 g_assert (!g_file_query_exists (data->file, NULL));
610 g_main_loop_quit (data->loop);
614 static void
615 replaced_cb (GObject *source,
616 GAsyncResult *res,
617 gpointer user_data)
619 ReplaceLoadData *data = user_data;
620 GError *error;
622 error = NULL;
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);
629 static void
630 test_replace_load (void)
632 ReplaceLoadData *data;
633 const gchar *path;
634 GFileIOStream *iostream;
636 data = g_new0 (ReplaceLoadData, 1);
637 data->again = TRUE;
638 data->data = replace_data;
640 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
641 &iostream, NULL);
642 g_assert (data->file != NULL);
643 g_object_unref (iostream);
645 path = g_file_peek_path (data->file);
646 remove (path);
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,
653 data->data,
654 strlen (data->data),
655 NULL,
656 FALSE,
658 NULL,
659 replaced_cb,
660 data);
662 g_main_loop_run (data->loop);
664 g_main_loop_unref (data->loop);
665 g_object_unref (data->file);
666 g_free (data);
669 static void
670 test_replace_cancel (void)
672 GFile *tmpdir, *file;
673 GFileOutputStream *ostream;
674 GFileEnumerator *fenum;
675 GFileInfo *info;
676 GCancellable *cancellable;
677 gchar *path;
678 gsize nwrote;
679 guint count;
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);
687 g_free (path);
689 file = g_file_get_child (tmpdir, "file");
690 g_file_replace_contents (file,
691 replace_data,
692 strlen (replace_data),
693 NULL, FALSE, 0, NULL,
694 NULL, &error);
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
707 * temporary.
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);
728 count = 0;
730 while (TRUE)
732 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
733 g_assert (ret);
734 g_assert_no_error (error);
735 if (!info)
736 break;
737 count++;
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);
748 count = 0;
750 while (TRUE)
752 GFile *child;
753 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
755 g_assert (ret);
756 g_assert_no_error (error);
758 if (!child)
759 break;
761 g_assert (G_IS_FILE (child));
762 count++;
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);
790 static void
791 on_file_deleted (GObject *object,
792 GAsyncResult *result,
793 gpointer user_data)
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);
804 static void
805 test_async_delete (void)
807 GFile *file;
808 GFileIOStream *iostream;
809 GError *local_error = NULL;
810 GError **error = &local_error;
811 GMainLoop *loop;
813 file = g_file_new_tmp ("g_file_delete_XXXXXX",
814 &iostream, error);
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);
832 #ifdef G_OS_UNIX
833 static void
834 test_copy_preserve_mode (void)
836 GFile *tmpfile;
837 GFile *dest_tmpfile;
838 GFileInfo *dest_info;
839 GFileIOStream *iostream;
840 GError *local_error = NULL;
841 GError **error = &local_error;
842 guint32 romode = S_IFREG | 0600;
843 guint32 dest_mode;
845 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
846 &iostream, error);
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,
854 NULL, error);
855 g_assert_no_error (local_error);
857 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
858 &iostream, error);
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,
869 NULL, error);
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);
883 #endif
885 static gchar *
886 splice_to_string (GInputStream *stream,
887 GError **error)
889 GMemoryOutputStream *buffer = NULL;
890 char *ret = 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)
894 goto out;
896 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
897 goto out;
899 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
900 goto out;
902 ret = g_memory_output_stream_steal_data (buffer);
903 out:
904 g_clear_object (&buffer);
905 return ret;
908 static gboolean
909 get_size_from_du (const gchar *path, guint64 *size)
911 GSubprocess *du;
912 gboolean ok;
913 gchar *result;
914 gchar *endptr;
915 GError *error = NULL;
917 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
918 &error,
919 "du", "--bytes", "-s", path, NULL);
920 g_assert_no_error (error);
922 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
923 g_assert_no_error (error);
925 *size = g_ascii_strtoll (result, &endptr, 10);
927 g_subprocess_wait (du, NULL, &error);
928 g_assert_no_error (error);
930 ok = g_subprocess_get_successful (du);
932 g_object_unref (du);
933 g_free (result);
935 return ok;
938 static void
939 test_measure (void)
941 GFile *file;
942 guint64 size;
943 guint64 num_bytes;
944 guint64 num_dirs;
945 guint64 num_files;
946 GError *error = NULL;
947 gboolean ok;
948 gchar *path;
950 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
951 file = g_file_new_for_path (path);
953 if (!g_find_program_in_path ("du") || !get_size_from_du (path, &size))
955 g_test_message ("du not found or fail to run, skipping byte measurement");
956 size = 0;
959 ok = g_file_measure_disk_usage (file,
960 G_FILE_MEASURE_APPARENT_SIZE,
961 NULL,
962 NULL,
963 NULL,
964 &num_bytes,
965 &num_dirs,
966 &num_files,
967 &error);
968 g_assert (ok);
969 g_assert_no_error (error);
971 if (size > 0)
972 g_assert_cmpuint (num_bytes, ==, size);
973 g_assert_cmpuint (num_dirs, ==, 6);
974 g_assert_cmpuint (num_files, ==, 31);
976 g_object_unref (file);
977 g_free (path);
980 typedef struct {
981 guint64 expected_bytes;
982 guint64 expected_dirs;
983 guint64 expected_files;
984 gint progress_count;
985 guint64 progress_bytes;
986 guint64 progress_dirs;
987 guint64 progress_files;
988 } MeasureData;
990 static void
991 measure_progress (gboolean reporting,
992 guint64 current_size,
993 guint64 num_dirs,
994 guint64 num_files,
995 gpointer user_data)
997 MeasureData *data = user_data;
999 data->progress_count += 1;
1001 g_assert_cmpuint (current_size, >=, data->progress_bytes);
1002 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1003 g_assert_cmpuint (num_files, >=, data->progress_files);
1005 data->progress_bytes = current_size;
1006 data->progress_dirs = num_dirs;
1007 data->progress_files = num_files;
1010 static void
1011 measure_done (GObject *source,
1012 GAsyncResult *res,
1013 gpointer user_data)
1015 MeasureData *data = user_data;
1016 guint64 num_bytes, num_dirs, num_files;
1017 GError *error = NULL;
1018 gboolean ok;
1020 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
1021 g_assert (ok);
1022 g_assert_no_error (error);
1024 if (data->expected_bytes > 0)
1025 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
1026 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
1027 g_assert_cmpuint (data->expected_files, ==, num_files);
1029 g_assert_cmpuint (data->progress_count, >, 0);
1030 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
1031 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1032 g_assert_cmpuint (num_files, >=, data->progress_files);
1034 g_free (data);
1035 g_object_unref (source);
1038 static void
1039 test_measure_async (void)
1041 gchar *path;
1042 GFile *file;
1043 MeasureData *data;
1045 data = g_new (MeasureData, 1);
1047 data->progress_count = 0;
1048 data->progress_bytes = 0;
1049 data->progress_files = 0;
1050 data->progress_dirs = 0;
1052 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1053 file = g_file_new_for_path (path);
1055 if (!g_find_program_in_path ("du") ||
1056 !get_size_from_du (path, &data->expected_bytes))
1058 g_test_message ("du not found or fail to run, skipping byte measurement");
1059 data->expected_bytes = 0;
1062 g_free (path);
1064 data->expected_dirs = 6;
1065 data->expected_files = 31;
1067 g_file_measure_disk_usage_async (file,
1068 G_FILE_MEASURE_APPARENT_SIZE,
1069 0, NULL,
1070 measure_progress, data,
1071 measure_done, data);
1074 static void
1075 test_load_bytes (void)
1077 gchar filename[] = "g_file_load_bytes_XXXXXX";
1078 GError *error = NULL;
1079 GBytes *bytes;
1080 GFile *file;
1081 int len;
1082 int fd;
1083 int ret;
1085 fd = g_mkstemp (filename);
1086 g_assert_cmpint (fd, !=, -1);
1087 len = strlen ("test_load_bytes");
1088 ret = write (fd, "test_load_bytes", len);
1089 g_assert_cmpint (ret, ==, len);
1090 close (fd);
1092 file = g_file_new_for_path (filename);
1093 bytes = g_file_load_bytes (file, NULL, NULL, &error);
1094 g_assert_no_error (error);
1095 g_assert (bytes != NULL);
1096 g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
1097 g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
1099 g_file_delete (file, NULL, NULL);
1101 g_bytes_unref (bytes);
1102 g_object_unref (file);
1105 typedef struct
1107 GMainLoop *main_loop;
1108 GFile *file;
1109 GBytes *bytes;
1110 } LoadBytesAsyncData;
1112 static void
1113 test_load_bytes_cb (GObject *object,
1114 GAsyncResult *result,
1115 gpointer user_data)
1117 GFile *file = G_FILE (object);
1118 LoadBytesAsyncData *data = user_data;
1119 GError *error = NULL;
1121 data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
1122 g_assert_no_error (error);
1123 g_assert (data->bytes != NULL);
1125 g_main_loop_quit (data->main_loop);
1128 static void
1129 test_load_bytes_async (void)
1131 LoadBytesAsyncData data = { 0 };
1132 gchar filename[] = "g_file_load_bytes_XXXXXX";
1133 int len;
1134 int fd;
1135 int ret;
1137 fd = g_mkstemp (filename);
1138 g_assert_cmpint (fd, !=, -1);
1139 len = strlen ("test_load_bytes_async");
1140 ret = write (fd, "test_load_bytes_async", len);
1141 g_assert_cmpint (ret, ==, len);
1142 close (fd);
1144 data.main_loop = g_main_loop_new (NULL, FALSE);
1145 data.file = g_file_new_for_path (filename);
1147 g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
1148 g_main_loop_run (data.main_loop);
1150 g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
1151 g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
1153 g_file_delete (data.file, NULL, NULL);
1154 g_object_unref (data.file);
1155 g_bytes_unref (data.bytes);
1156 g_main_loop_unref (data.main_loop);
1160 main (int argc, char *argv[])
1162 g_test_init (&argc, &argv, NULL);
1164 g_test_bug_base ("http://bugzilla.gnome.org/");
1166 g_test_add_func ("/file/basic", test_basic);
1167 g_test_add_func ("/file/build-filename", test_build_filename);
1168 g_test_add_func ("/file/parent", test_parent);
1169 g_test_add_func ("/file/child", test_child);
1170 g_test_add_func ("/file/type", test_type);
1171 g_test_add_func ("/file/parse-name", test_parse_name);
1172 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1173 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1174 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1175 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1176 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1177 g_test_add_func ("/file/replace-load", test_replace_load);
1178 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1179 g_test_add_func ("/file/async-delete", test_async_delete);
1180 #ifdef G_OS_UNIX
1181 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1182 #endif
1183 g_test_add_func ("/file/measure", test_measure);
1184 g_test_add_func ("/file/measure-async", test_measure_async);
1185 g_test_add_func ("/file/load-bytes", test_load_bytes);
1186 g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
1188 return g_test_run ();