Imported Upstream version 2.55.0
[platform/upstream/glib.git] / gio / tests / file.c
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
9
10 static void
11 test_basic_for_file (GFile       *file,
12                      const gchar *suffix)
13 {
14   gchar *s;
15
16   s = g_file_get_basename (file);
17   g_assert_cmpstr (s, ==, "testfile");
18   g_free (s);
19
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);
24
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);
29 }
30
31 static void
32 test_basic (void)
33 {
34   GFile *file;
35
36   file = g_file_new_for_path ("./some/directory/testfile");
37   test_basic_for_file (file, "/some/directory/testfile");
38   g_object_unref (file);
39 }
40
41 static void
42 test_build_filename (void)
43 {
44   GFile *file;
45
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);
49
50   file = g_file_new_build_filename ("testfile", NULL);
51   test_basic_for_file (file, "/testfile");
52   g_object_unref (file);
53 }
54
55 static void
56 test_parent (void)
57 {
58   GFile *file;
59   GFile *file2;
60   GFile *parent;
61   GFile *root;
62
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 ("/");
66
67   g_assert (g_file_has_parent (file, file2));
68
69   parent = g_file_get_parent (file);
70   g_assert (g_file_equal (parent, file2));
71   g_object_unref (parent);
72
73   g_assert (g_file_get_parent (root) == NULL);
74
75   g_object_unref (file);
76   g_object_unref (file2);
77   g_object_unref (root);
78 }
79
80 static void
81 test_child (void)
82 {
83   GFile *file;
84   GFile *child;
85   GFile *child2;
86
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));
90
91   child2 = g_file_get_child_for_display_name (file, "child2", NULL);
92   g_assert (g_file_has_parent (child2, file));
93
94   g_object_unref (child);
95   g_object_unref (child2);
96   g_object_unref (file);
97 }
98
99 static void
100 test_type (void)
101 {
102   GFile *datapath_f;
103   GFile *file;
104   GFileType type;
105   GError *error = NULL;
106
107   datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
108
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);
113
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);
117
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);
122
123   g_object_unref (datapath_f);
124 }
125
126 static void
127 test_parse_name (void)
128 {
129   GFile *file;
130   gchar *name;
131
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);
137
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);
143 }
144
145 typedef struct
146 {
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;
162
163 static void
164 monitor_changed (GFileMonitor      *monitor,
165                  GFile             *file,
166                  GFile             *other_file,
167                  GFileMonitorEvent  event_type,
168                  gpointer           user_data)
169 {
170   CreateDeleteData *data = user_data;
171   gchar *path;
172
173   path = g_file_get_path (file);
174   g_assert_cmpstr (data->monitor_path, ==, path);
175   g_free (path);
176
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++;
183 }
184
185
186 static gboolean
187 quit_idle (gpointer user_data)
188 {
189   CreateDeleteData *data = user_data;
190
191   g_source_remove (data->timeout); 
192   g_main_loop_quit (data->loop);
193
194   return FALSE;
195 }
196
197 static void
198 iclosed_cb (GObject      *source,
199             GAsyncResult *res,
200             gpointer      user_data)
201 {
202   CreateDeleteData *data = user_data;
203   GError *error;
204   gboolean ret;
205
206   error = NULL;
207   ret = g_input_stream_close_finish (data->istream, res, &error);
208   g_assert_no_error (error);
209   g_assert (ret);
210
211   g_assert (g_input_stream_is_closed (data->istream));
212
213   ret = g_file_delete (data->file, NULL, &error);
214   g_assert (ret);
215   g_assert_no_error (error);
216
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
220    */
221   g_timeout_add (2000, quit_idle, data);
222 }
223
224 static void
225 read_cb (GObject      *source,
226          GAsyncResult *res,
227          gpointer      user_data)
228 {
229   CreateDeleteData *data = user_data;
230   GError *error;
231   gssize size;
232
233   error = NULL;
234   size = g_input_stream_read_finish (data->istream, res, &error);
235   g_assert_no_error (error);
236
237   data->pos += size;
238   if (data->pos < strlen (data->data))
239     {
240       g_input_stream_read_async (data->istream,
241                                  data->buffer + data->pos,
242                                  strlen (data->data) - data->pos,
243                                  0,
244                                  NULL,
245                                  read_cb,
246                                  data);
247     }
248   else
249     {
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);
253     }
254 }
255
256 static void
257 ipending_cb (GObject      *source,
258              GAsyncResult *res,
259              gpointer      user_data)
260 {
261   CreateDeleteData *data = user_data;
262   GError *error;
263
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);
268 }
269
270 static void
271 skipped_cb (GObject      *source,
272             GAsyncResult *res,
273             gpointer      user_data)
274 {
275   CreateDeleteData *data = user_data;
276   GError *error;
277   gssize size;
278
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);
283
284   g_input_stream_read_async (data->istream,
285                              data->buffer + data->pos,
286                              strlen (data->data) - data->pos,
287                              0,
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,
295                              0,
296                              NULL,
297                              ipending_cb,
298                              data);
299 }
300
301 static void
302 opened_cb (GObject      *source,
303            GAsyncResult *res,
304            gpointer      user_data)
305 {
306   GFileInputStream *base;
307   CreateDeleteData *data = user_data;
308   GError *error;
309
310   error = NULL;
311   base = g_file_read_finish (data->file, res, &error);
312   g_assert_no_error (error);
313
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);
319
320   data->buffer = g_new0 (gchar, strlen (data->data) + 1);
321
322   /* copy initial segment directly, then skip */
323   memcpy (data->buffer, data->data, 10);
324   data->pos = 10;
325
326   g_input_stream_skip_async (data->istream,
327                              10,
328                              0,
329                              NULL,
330                              skipped_cb,
331                              data);
332 }
333
334 static void
335 oclosed_cb (GObject      *source,
336             GAsyncResult *res,
337             gpointer      user_data)
338 {
339   CreateDeleteData *data = user_data;
340   GError *error;
341   gboolean ret;
342
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));
348
349   g_file_read_async (data->file, 0, NULL, opened_cb, data);
350 }
351
352 static void
353 written_cb (GObject      *source,
354             GAsyncResult *res,
355             gpointer      user_data)
356 {
357   CreateDeleteData *data = user_data;
358   gssize size;
359   GError *error;
360
361   error = NULL;
362   size = g_output_stream_write_finish (data->ostream, res, &error);
363   g_assert_no_error (error);
364
365   data->pos += size;
366   if (data->pos < strlen (data->data))
367     {
368       g_output_stream_write_async (data->ostream,
369                                    data->data + data->pos,
370                                    strlen (data->data) - data->pos,
371                                    0,
372                                    NULL,
373                                    written_cb,
374                                    data);
375     }
376   else
377     {
378       g_assert (!g_output_stream_is_closed (data->ostream));
379       g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
380     }
381 }
382
383 static void
384 opending_cb (GObject      *source,
385              GAsyncResult *res,
386              gpointer      user_data)
387 {
388   CreateDeleteData *data = user_data;
389   GError *error;
390
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);
395 }
396
397 static void
398 created_cb (GObject      *source,
399             GAsyncResult *res,
400             gpointer      user_data)
401 {
402   GFileOutputStream *base;
403   CreateDeleteData *data = user_data;
404   GError *error;
405
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));
410
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);
416
417   g_output_stream_write_async (data->ostream,
418                                data->data,
419                                strlen (data->data),
420                                0,
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),
428                                0,
429                                NULL,
430                                opending_cb,
431                                data);
432 }
433
434 static gboolean
435 stop_timeout (gpointer data)
436 {
437   g_assert_not_reached ();
438
439   return FALSE;
440 }
441
442 /*
443  * This test does a fully async create-write-read-delete.
444  * Callbackistan.
445  */
446 static void
447 test_create_delete (gconstpointer d)
448 {
449   GError *error;
450   CreateDeleteData *data;
451   GFileIOStream *iostream;
452
453   data = g_new0 (CreateDeleteData, 1);
454
455   data->buffersize = GPOINTER_TO_INT (d);
456   data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
457   data->pos = 0;
458
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);
463
464   data->monitor_path = g_file_get_path (data->file);
465   remove (data->monitor_path);
466
467   g_assert (!g_file_query_exists  (data->file, NULL));
468
469   error = NULL;
470   data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
471   g_assert_no_error (error);
472
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.
476    */
477   if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
478     {
479       g_test_skip ("skipping test for this GFileMonitor implementation");
480       goto skip;
481     }
482
483   g_file_monitor_set_rate_limit (data->monitor, 100);
484
485   g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
486
487   data->loop = g_main_loop_new (NULL, FALSE);
488
489   data->timeout = g_timeout_add (10000, stop_timeout, NULL);
490
491   g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
492
493   g_main_loop_run (data->loop);
494
495   g_assert_cmpint (data->monitor_created, ==, 1);
496   g_assert_cmpint (data->monitor_deleted, ==, 1);
497   g_assert_cmpint (data->monitor_changed, >, 0);
498
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));
502
503   g_main_loop_unref (data->loop);
504   g_object_unref (data->ostream);
505   g_object_unref (data->istream);
506
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);
513 }
514
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";
543
544 typedef struct
545 {
546   GFile *file;
547   const gchar *data;
548   GMainLoop *loop;
549   gboolean again;
550 } ReplaceLoadData;
551
552 static void replaced_cb (GObject      *source,
553                          GAsyncResult *res,
554                          gpointer      user_data);
555
556 static void
557 loaded_cb (GObject      *source,
558            GAsyncResult *res,
559            gpointer      user_data)
560 {
561   ReplaceLoadData *data = user_data;
562   gboolean ret;
563   GError *error;
564   gchar *contents;
565   gsize length;
566
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);
573
574   g_free (contents);
575
576   if (data->again)
577     {
578       data->again = FALSE;
579       data->data = "pi pa po";
580
581       g_file_replace_contents_async (data->file,
582                                      data->data,
583                                      strlen (data->data),
584                                      NULL,
585                                      FALSE,
586                                      0,
587                                      NULL,
588                                      replaced_cb,
589                                      data);
590     }
591   else
592     {
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));
598
599        g_main_loop_quit (data->loop);
600     }
601 }
602
603 static void
604 replaced_cb (GObject      *source,
605              GAsyncResult *res,
606              gpointer      user_data)
607 {
608   ReplaceLoadData *data = user_data;
609   GError *error;
610
611   error = NULL;
612   g_file_replace_contents_finish (data->file, res, NULL, &error);
613   g_assert_no_error (error);
614
615   g_file_load_contents_async (data->file, NULL, loaded_cb, data);
616 }
617
618 static void
619 test_replace_load (void)
620 {
621   ReplaceLoadData *data;
622   gchar *path;
623   GFileIOStream *iostream;
624
625   data = g_new0 (ReplaceLoadData, 1);
626   data->again = TRUE;
627   data->data = replace_data;
628
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);
633
634   path = g_file_get_path (data->file);
635   remove (path);
636
637   g_assert (!g_file_query_exists (data->file, NULL));
638
639   data->loop = g_main_loop_new (NULL, FALSE);
640
641   g_file_replace_contents_async (data->file,
642                                  data->data,
643                                  strlen (data->data),
644                                  NULL,
645                                  FALSE,
646                                  0,
647                                  NULL,
648                                  replaced_cb,
649                                  data);
650
651   g_main_loop_run (data->loop);
652
653   g_main_loop_unref (data->loop);
654   g_object_unref (data->file);
655   g_free (data);
656   free (path);
657 }
658
659 static void
660 test_replace_cancel (void)
661 {
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;
671
672   g_test_bug ("629301");
673
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);
678
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);
686
687   ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
688   g_assert_no_error (error);
689
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));
695
696   /* At this point there should be two files; the original and the
697    * temporary.
698    */
699   fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
700   g_assert_no_error (error);
701
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);
710
711   g_file_enumerator_close (fenum, NULL, &error);
712   g_assert_no_error (error);
713   g_object_unref (fenum);
714
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;
719
720   while (TRUE)
721     {
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++;
728     }
729   g_assert_cmpint (count, ==, 2);
730
731   g_file_enumerator_close (fenum, NULL, &error);
732   g_assert_no_error (error);
733   g_object_unref (fenum);
734
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;
739
740   while (TRUE)
741     {
742       GFile *child;
743       gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
744
745       g_assert (ret);
746       g_assert_no_error (error);
747
748       if (!child)
749         break;
750
751       g_assert (G_IS_FILE (child));
752       count++;
753     }
754   g_assert_cmpint (count, ==, 2);
755
756   g_file_enumerator_close (fenum, NULL, &error);
757   g_assert_no_error (error);
758   g_object_unref (fenum);
759
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);
766
767   g_object_unref (cancellable);
768   g_object_unref (ostream);
769
770   g_file_delete (file, NULL, &error);
771   g_assert_no_error (error);
772   g_object_unref (file);
773
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);
778 }
779
780 static void
781 on_file_deleted (GObject      *object,
782                  GAsyncResult *result,
783                  gpointer      user_data)
784 {
785   GError *local_error = NULL;
786   GMainLoop *loop = user_data;
787
788   (void) g_file_delete_finish ((GFile*)object, result, &local_error);
789   g_assert_no_error (local_error);
790
791   g_main_loop_quit (loop);
792 }
793
794 static void
795 test_async_delete (void)
796 {
797   GFile *file;
798   GFileIOStream *iostream;
799   GError *local_error = NULL;
800   GError **error = &local_error;
801   GMainLoop *loop;
802
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);
807
808   g_assert (g_file_query_exists (file, NULL));
809
810   loop = g_main_loop_new (NULL, TRUE);
811
812   g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
813
814   g_main_loop_run (loop);
815
816   g_assert (!g_file_query_exists (file, NULL));
817
818   g_main_loop_unref (loop);
819   g_object_unref (file);
820 }
821
822 #ifdef G_OS_UNIX
823 static void
824 test_copy_preserve_mode (void)
825 {
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;
834
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);
841
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);
846
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);
853
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);
857
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);
861
862   dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
863   
864   g_assert_cmpint (dest_mode, ==, romode);
865
866   (void) g_file_delete (tmpfile, NULL, NULL);
867   (void) g_file_delete (dest_tmpfile, NULL, NULL);
868   
869   g_clear_object (&tmpfile);
870   g_clear_object (&dest_tmpfile);
871   g_clear_object (&dest_info);
872 }
873 #endif
874
875 static gchar *
876 splice_to_string (GInputStream   *stream,
877                   GError        **error)
878 {
879   GMemoryOutputStream *buffer = NULL;
880   char *ret = NULL;
881
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;
885
886   if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
887     goto out;
888
889   if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
890     goto out;
891
892   ret = g_memory_output_stream_steal_data (buffer);
893  out:
894   g_clear_object (&buffer);
895   return ret;
896 }
897
898 static guint64
899 get_size_from_du (const gchar *path)
900 {
901   GSubprocess *du;
902   gchar *result;
903   gchar *endptr;
904   guint64 size;
905   GError *error = NULL;
906
907   du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
908                          &error,
909                          "du", "--bytes", "-s", path, NULL);
910   g_assert_no_error (error);
911
912   result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
913   g_assert_no_error (error);
914
915   size = g_ascii_strtoll (result, &endptr, 10);
916
917   g_object_unref (du);
918   g_free (result);
919
920   return size;
921 }
922
923 static void
924 test_measure (void)
925 {
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;
934
935   path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
936   file = g_file_new_for_path (path);
937
938   if (g_find_program_in_path ("du"))
939     {
940       size = get_size_from_du (path);
941     }
942   else
943     {
944       g_test_message ("du not found, skipping byte measurement");
945       size = 0;
946     }
947
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);
959
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);
964
965   g_object_unref (file);
966   g_free (path);
967 }
968
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;
978
979 static void
980 measure_progress (gboolean reporting,
981                   guint64  current_size,
982                   guint64  num_dirs,
983                   guint64  num_files,
984                   gpointer user_data)
985 {
986   MeasureData *data = user_data;
987
988   data->progress_count += 1;
989
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);
993
994   data->progress_bytes = current_size;
995   data->progress_dirs = num_dirs;
996   data->progress_files = num_files;
997 }
998
999 static void
1000 measure_done (GObject      *source,
1001               GAsyncResult *res,
1002               gpointer      user_data)
1003 {
1004   MeasureData *data = user_data;
1005   guint64 num_bytes, num_dirs, num_files;
1006   GError *error = NULL;
1007   gboolean ok;
1008
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);
1012
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);
1017
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);
1022
1023   g_free (data);
1024   g_object_unref (source);
1025 }
1026
1027 static void
1028 test_measure_async (void)
1029 {
1030   gchar *path;
1031   GFile *file;
1032   MeasureData *data;
1033
1034   data = g_new (MeasureData, 1);
1035
1036   data->progress_count = 0;
1037   data->progress_bytes = 0;
1038   data->progress_files = 0;
1039   data->progress_dirs = 0;
1040
1041   path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1042   file = g_file_new_for_path (path);
1043
1044   if (g_find_program_in_path ("du"))
1045     {
1046       data->expected_bytes = get_size_from_du (path);
1047     }
1048   else
1049     {
1050       g_test_message ("du not found, skipping byte measurement");
1051       data->expected_bytes = 0;
1052     }
1053
1054   g_free (path);
1055
1056   data->expected_dirs = 6;
1057   data->expected_files = 30;
1058
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);
1064 }
1065
1066 static void
1067 test_load_bytes (void)
1068 {
1069   gchar filename[] = "g_file_load_bytes_XXXXXX";
1070   GError *error = NULL;
1071   GBytes *bytes;
1072   GFile *file;
1073   int len;
1074   int fd;
1075   int ret;
1076
1077   fd = g_mkstemp (filename);
1078   g_assert_cmpint (fd, !=, -1);
1079   len = strlen ("test_load_bytes");
1080   ret = write (fd, "test_load_bytes", len);
1081   g_assert_cmpint (ret, ==, len);
1082   close (fd);
1083
1084   file = g_file_new_for_path (filename);
1085   bytes = g_file_load_bytes (file, NULL, NULL, &error);
1086   g_assert_no_error (error);
1087   g_assert (bytes != NULL);
1088   g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
1089   g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
1090
1091   g_file_delete (file, NULL, NULL);
1092
1093   g_bytes_unref (bytes);
1094   g_object_unref (file);
1095 }
1096
1097 typedef struct
1098 {
1099   GMainLoop *main_loop;
1100   GFile *file;
1101   GBytes *bytes;
1102 } LoadBytesAsyncData;
1103
1104 static void
1105 test_load_bytes_cb (GObject      *object,
1106                     GAsyncResult *result,
1107                     gpointer      user_data)
1108 {
1109   GFile *file = G_FILE (object);
1110   LoadBytesAsyncData *data = user_data;
1111   GError *error = NULL;
1112
1113   data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
1114   g_assert_no_error (error);
1115   g_assert (data->bytes != NULL);
1116
1117   g_main_loop_quit (data->main_loop);
1118 }
1119
1120 static void
1121 test_load_bytes_async (void)
1122 {
1123   LoadBytesAsyncData data = { 0 };
1124   gchar filename[] = "g_file_load_bytes_XXXXXX";
1125   int len;
1126   int fd;
1127   int ret;
1128
1129   fd = g_mkstemp (filename);
1130   g_assert_cmpint (fd, !=, -1);
1131   len = strlen ("test_load_bytes_async");
1132   ret = write (fd, "test_load_bytes_async", len);
1133   g_assert_cmpint (ret, ==, len);
1134   close (fd);
1135
1136   data.main_loop = g_main_loop_new (NULL, FALSE);
1137   data.file = g_file_new_for_path (filename);
1138
1139   g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
1140   g_main_loop_run (data.main_loop);
1141
1142   g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
1143   g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
1144
1145   g_file_delete (data.file, NULL, NULL);
1146   g_object_unref (data.file);
1147   g_bytes_unref (data.bytes);
1148   g_main_loop_unref (data.main_loop);
1149 }
1150
1151 int
1152 main (int argc, char *argv[])
1153 {
1154   g_test_init (&argc, &argv, NULL);
1155
1156   g_test_bug_base ("http://bugzilla.gnome.org/");
1157
1158   g_test_add_func ("/file/basic", test_basic);
1159   g_test_add_func ("/file/build-filename", test_build_filename);
1160   g_test_add_func ("/file/parent", test_parent);
1161   g_test_add_func ("/file/child", test_child);
1162   g_test_add_func ("/file/type", test_type);
1163   g_test_add_func ("/file/parse-name", test_parse_name);
1164   g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1165   g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1166   g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1167   g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1168   g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1169   g_test_add_func ("/file/replace-load", test_replace_load);
1170   g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1171   g_test_add_func ("/file/async-delete", test_async_delete);
1172 #ifdef G_OS_UNIX
1173   g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1174 #endif
1175   g_test_add_func ("/file/measure", test_measure);
1176   g_test_add_func ("/file/measure-async", test_measure_async);
1177   g_test_add_func ("/file/load-bytes", test_load_bytes);
1178   g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
1179
1180   return g_test_run ();
1181 }