gio: Don't leak the temp file when g_file_replace() fails or is cancelled
[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
7 static void
8 test_basic (void)
9 {
10   GFile *file;
11   gchar *s;
12
13   file = g_file_new_for_path ("./some/directory/testfile");
14
15   s = g_file_get_basename (file);
16   g_assert_cmpstr (s, ==, "testfile");
17   g_free (s);
18
19   s = g_file_get_uri (file);
20   g_assert (g_str_has_prefix (s, "file://"));
21   g_assert (g_str_has_suffix (s, "/some/directory/testfile"));
22   g_free (s);
23
24   g_assert (g_file_has_uri_scheme (file, "file"));
25   s = g_file_get_uri_scheme (file);
26   g_assert_cmpstr (s, ==, "file");
27   g_free (s);
28
29   g_object_unref (file);
30 }
31
32 static void
33 test_parent (void)
34 {
35   GFile *file;
36   GFile *file2;
37   GFile *parent;
38   GFile *root;
39
40   file = g_file_new_for_path ("./some/directory/testfile");
41   file2 = g_file_new_for_path ("./some/directory");
42   root = g_file_new_for_path ("/");
43
44   g_assert (g_file_has_parent (file, file2));
45
46   parent = g_file_get_parent (file);
47   g_assert (g_file_equal (parent, file2));
48   g_object_unref (parent);
49
50   g_assert (g_file_get_parent (root) == NULL);
51
52   g_object_unref (file);
53   g_object_unref (file2);
54   g_object_unref (root);
55 }
56
57 static void
58 test_child (void)
59 {
60   GFile *file;
61   GFile *child;
62   GFile *child2;
63
64   file = g_file_new_for_path ("./some/directory");
65   child = g_file_get_child (file, "child");
66   g_assert (g_file_has_parent (child, file));
67
68   child2 = g_file_get_child_for_display_name (file, "child2", NULL);
69   g_assert (g_file_has_parent (child2, file));
70
71   g_object_unref (child);
72   g_object_unref (child2);
73   g_object_unref (file);
74 }
75
76 static void
77 test_type (void)
78 {
79   GFile *file;
80   GFileType type;
81   GError *error = NULL;
82
83   file = g_file_new_for_path (SRCDIR "/file.c");
84   type = g_file_query_file_type (file, 0, NULL);
85   g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
86   g_object_unref (file);
87
88   file = g_file_new_for_path (SRCDIR "/schema-tests");
89   type = g_file_query_file_type (file, 0, NULL);
90   g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
91
92   g_file_read (file, NULL, &error);
93   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
94   g_error_free (error);
95   g_object_unref (file);
96 }
97
98
99 typedef struct
100 {
101   GFile *file;
102   GFileMonitor *monitor;
103   GOutputStream *ostream;
104   GInputStream *istream;
105   GMainLoop *loop;
106   gint buffersize;
107   gint monitor_created;
108   gint monitor_deleted;
109   gint monitor_changed;
110   gchar *monitor_path;
111   gint pos;
112   gchar *data;
113   gchar *buffer;
114   guint timeout;
115 } CreateDeleteData;
116
117 static void
118 monitor_changed (GFileMonitor      *monitor,
119                  GFile             *file,
120                  GFile             *other_file,
121                  GFileMonitorEvent  event_type,
122                  gpointer           user_data)
123 {
124   CreateDeleteData *data = user_data;
125   gchar *path;
126
127   path = g_file_get_path (file);
128   g_assert_cmpstr (data->monitor_path, ==, path);
129   g_free (path);
130
131   if (event_type == G_FILE_MONITOR_EVENT_CREATED)
132     data->monitor_created++;
133   if (event_type == G_FILE_MONITOR_EVENT_DELETED)
134     data->monitor_deleted++;
135   if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
136     data->monitor_changed++;
137 }
138
139
140 static gboolean
141 quit_idle (gpointer user_data)
142 {
143   CreateDeleteData *data = user_data;
144
145   g_source_remove (data->timeout); 
146   g_main_loop_quit (data->loop);
147
148   return FALSE;
149 }
150
151 static void
152 iclosed_cb (GObject      *source,
153             GAsyncResult *res,
154             gpointer      user_data)
155 {
156   CreateDeleteData *data = user_data;
157   GError *error;
158   gboolean ret;
159
160   error = NULL;
161   ret = g_input_stream_close_finish (data->istream, res, &error);
162   g_assert_no_error (error);
163   g_assert (ret);
164
165   g_assert (g_input_stream_is_closed (data->istream));
166
167   ret = g_file_delete (data->file, NULL, &error);
168   g_assert (ret);
169   g_assert_no_error (error);
170
171   /* work around file monitor bug:
172    * inotify events are only processed every 1000 ms, regardless
173    * of the rate limit set on the file monitor
174    */
175   g_timeout_add (2000, quit_idle, data);
176 }
177
178 static void
179 read_cb (GObject      *source,
180          GAsyncResult *res,
181          gpointer      user_data)
182 {
183   CreateDeleteData *data = user_data;
184   GError *error;
185   gssize size;
186
187   error = NULL;
188   size = g_input_stream_read_finish (data->istream, res, &error);
189   g_assert_no_error (error);
190
191   data->pos += size;
192   if (data->pos < strlen (data->data))
193     {
194       g_input_stream_read_async (data->istream,
195                                  data->buffer + data->pos,
196                                  strlen (data->data) - data->pos,
197                                  0,
198                                  NULL,
199                                  read_cb,
200                                  data);
201     }
202   else
203     {
204       g_assert_cmpstr (data->buffer, ==, data->data);
205       g_assert (!g_input_stream_is_closed (data->istream));
206       g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
207     }
208 }
209
210 static void
211 ipending_cb (GObject      *source,
212              GAsyncResult *res,
213              gpointer      user_data)
214 {
215   CreateDeleteData *data = user_data;
216   GError *error;
217
218   error = NULL;
219   g_input_stream_read_finish (data->istream, res, &error);
220   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
221   g_error_free (error);
222 }
223
224 static void
225 skipped_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_skip_finish (data->istream, res, &error);
235   g_assert_no_error (error);
236   g_assert_cmpint (size, ==, data->pos);
237
238   g_input_stream_read_async (data->istream,
239                              data->buffer + data->pos,
240                              strlen (data->data) - data->pos,
241                              0,
242                              NULL,
243                              read_cb,
244                              data);
245   /* check that we get a pending error */
246   g_input_stream_read_async (data->istream,
247                              data->buffer + data->pos,
248                              strlen (data->data) - data->pos,
249                              0,
250                              NULL,
251                              ipending_cb,
252                              data);
253 }
254
255 static void
256 opened_cb (GObject      *source,
257            GAsyncResult *res,
258            gpointer      user_data)
259 {
260   GFileInputStream *base;
261   CreateDeleteData *data = user_data;
262   GError *error;
263
264   error = NULL;
265   base = g_file_read_finish (data->file, res, &error);
266   g_assert_no_error (error);
267
268   if (data->buffersize == 0)
269     data->istream = G_INPUT_STREAM (g_object_ref (base));
270   else
271     data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
272   g_object_unref (base);
273
274   data->buffer = g_new0 (gchar, strlen (data->data) + 1);
275
276   /* copy initial segment directly, then skip */
277   memcpy (data->buffer, data->data, 10);
278   data->pos = 10;
279
280   g_input_stream_skip_async (data->istream,
281                              10,
282                              0,
283                              NULL,
284                              skipped_cb,
285                              data);
286 }
287
288 static void
289 oclosed_cb (GObject      *source,
290             GAsyncResult *res,
291             gpointer      user_data)
292 {
293   CreateDeleteData *data = user_data;
294   GError *error;
295   gboolean ret;
296
297   error = NULL;
298   ret = g_output_stream_close_finish (data->ostream, res, &error);
299   g_assert_no_error (error);
300   g_assert (ret);
301   g_assert (g_output_stream_is_closed (data->ostream));
302
303   g_file_read_async (data->file, 0, NULL, opened_cb, data);
304 }
305
306 static void
307 written_cb (GObject      *source,
308             GAsyncResult *res,
309             gpointer      user_data)
310 {
311   CreateDeleteData *data = user_data;
312   gssize size;
313   GError *error;
314
315   error = NULL;
316   size = g_output_stream_write_finish (data->ostream, res, &error);
317   g_assert_no_error (error);
318
319   data->pos += size;
320   if (data->pos < strlen (data->data))
321     {
322       g_output_stream_write_async (data->ostream,
323                                    data->data + data->pos,
324                                    strlen (data->data) - data->pos,
325                                    0,
326                                    NULL,
327                                    written_cb,
328                                    data);
329     }
330   else
331     {
332       g_assert (!g_output_stream_is_closed (data->ostream));
333       g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
334     }
335 }
336
337 static void
338 opending_cb (GObject      *source,
339              GAsyncResult *res,
340              gpointer      user_data)
341 {
342   CreateDeleteData *data = user_data;
343   GError *error;
344
345   error = NULL;
346   g_output_stream_write_finish (data->ostream, res, &error);
347   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
348   g_error_free (error);
349 }
350
351 static void
352 created_cb (GObject      *source,
353             GAsyncResult *res,
354             gpointer      user_data)
355 {
356   GFileOutputStream *base;
357   CreateDeleteData *data = user_data;
358   GError *error;
359
360   error = NULL;
361   base = g_file_create_finish (G_FILE (source), res, &error);
362   g_assert_no_error (error);
363   g_assert (g_file_query_exists  (data->file, NULL));
364
365   if (data->buffersize == 0)
366     data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
367   else
368     data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
369   g_object_unref (base);
370
371   g_output_stream_write_async (data->ostream,
372                                data->data,
373                                strlen (data->data),
374                                0,
375                                NULL,
376                                written_cb,
377                                data);
378   /* check that we get a pending error */
379   g_output_stream_write_async (data->ostream,
380                                data->data,
381                                strlen (data->data),
382                                0,
383                                NULL,
384                                opending_cb,
385                                data);
386 }
387
388 static gboolean
389 stop_timeout (gpointer data)
390 {
391   g_assert_not_reached ();
392
393   return FALSE;
394 }
395
396 /*
397  * This test does a fully async create-write-read-delete.
398  * Callbackistan.
399  */
400 static void
401 test_create_delete (gconstpointer d)
402 {
403   GError *error;
404   CreateDeleteData *data;
405   GFileIOStream *iostream;
406
407   data = g_new0 (CreateDeleteData, 1);
408
409   data->buffersize = GPOINTER_TO_INT (d);
410   data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
411   data->pos = 0;
412
413   data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
414                                &iostream, NULL);
415   g_assert (data->file != NULL);
416   g_object_unref (iostream);
417
418   data->monitor_path = g_file_get_path (data->file);
419   remove (data->monitor_path);
420
421   g_assert (!g_file_query_exists  (data->file, NULL));
422
423   error = NULL;
424   data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
425   g_assert_no_error (error);
426
427   /* This test doesn't work with GPollFileMonitor, because it assumes
428    * that the monitor will notice a create immediately followed by a
429    * delete, rather than coalescing them into nothing.
430    */
431   if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
432     {
433       g_print ("skipping test for this GFileMonitor implementation");
434       goto skip;
435     }
436
437   g_file_monitor_set_rate_limit (data->monitor, 100);
438
439   g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
440
441   data->loop = g_main_loop_new (NULL, FALSE);
442
443   data->timeout = g_timeout_add (5000, stop_timeout, NULL);
444
445   g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
446
447   g_main_loop_run (data->loop);
448
449   g_assert_cmpint (data->monitor_created, ==, 1);
450   g_assert_cmpint (data->monitor_deleted, ==, 1);
451   g_assert_cmpint (data->monitor_changed, >, 0);
452
453   g_assert (!g_file_monitor_is_cancelled (data->monitor));
454   g_file_monitor_cancel (data->monitor);
455   g_assert (g_file_monitor_is_cancelled (data->monitor));
456
457   g_main_loop_unref (data->loop);
458   g_object_unref (data->ostream);
459   g_object_unref (data->istream);
460
461  skip:
462   g_object_unref (data->monitor);
463   g_object_unref (data->file);
464   free (data->monitor_path);
465   g_free (data->buffer);
466   g_free (data);
467 }
468
469 static const gchar *replace_data =
470     "/**\n"
471     " * g_file_replace_contents_async:\n"
472     " * @file: input #GFile.\n"
473     " * @contents: string of contents to replace the file with.\n"
474     " * @length: the length of @contents in bytes.\n"
475     " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
476     " * @make_backup: %TRUE if a backup should be created.\n"
477     " * @flags: a set of #GFileCreateFlags.\n"
478     " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
479     " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
480     " * @user_data: the data to pass to callback function\n"
481     " * \n"
482     " * Starts an asynchronous replacement of @file with the given \n"
483     " * @contents of @length bytes. @etag will replace the document's\n"
484     " * current entity tag.\n"
485     " * \n"
486     " * When this operation has completed, @callback will be called with\n"
487     " * @user_user data, and the operation can be finalized with \n"
488     " * g_file_replace_contents_finish().\n"
489     " * \n"
490     " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
491     " * triggering the cancellable object from another thread. If the operation\n"
492     " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
493     " * \n"
494     " * If @make_backup is %TRUE, this function will attempt to \n"
495     " * make a backup of @file.\n"
496     " **/\n";
497
498 typedef struct
499 {
500   GFile *file;
501   const gchar *data;
502   GMainLoop *loop;
503   gboolean again;
504 } ReplaceLoadData;
505
506 static void replaced_cb (GObject      *source,
507                          GAsyncResult *res,
508                          gpointer      user_data);
509
510 static void
511 loaded_cb (GObject      *source,
512            GAsyncResult *res,
513            gpointer      user_data)
514 {
515   ReplaceLoadData *data = user_data;
516   gboolean ret;
517   GError *error;
518   gchar *contents;
519   gsize length;
520
521   error = NULL;
522   ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
523   g_assert (ret);
524   g_assert_no_error (error);
525   g_assert_cmpint (length, ==, strlen (data->data));
526   g_assert_cmpstr (contents, ==, data->data);
527
528   g_free (contents);
529
530   if (data->again)
531     {
532       data->again = FALSE;
533       data->data = "pi pa po";
534
535       g_file_replace_contents_async (data->file,
536                                      data->data,
537                                      strlen (data->data),
538                                      NULL,
539                                      FALSE,
540                                      0,
541                                      NULL,
542                                      replaced_cb,
543                                      data);
544     }
545   else
546     {
547        error = NULL;
548        ret = g_file_delete (data->file, NULL, &error);
549        g_assert_no_error (error);
550        g_assert (ret);
551        g_assert (!g_file_query_exists (data->file, NULL));
552
553        g_main_loop_quit (data->loop);
554     }
555 }
556
557 static void
558 replaced_cb (GObject      *source,
559              GAsyncResult *res,
560              gpointer      user_data)
561 {
562   ReplaceLoadData *data = user_data;
563   GError *error;
564
565   error = NULL;
566   g_file_replace_contents_finish (data->file, res, NULL, &error);
567   g_assert_no_error (error);
568
569   g_file_load_contents_async (data->file, NULL, loaded_cb, data);
570 }
571
572 static void
573 test_replace_load (void)
574 {
575   ReplaceLoadData *data;
576   gchar *path;
577   GFileIOStream *iostream;
578
579   data = g_new0 (ReplaceLoadData, 1);
580   data->again = TRUE;
581   data->data = replace_data;
582
583   data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
584                                &iostream, NULL);
585   g_assert (data->file != NULL);
586   g_object_unref (iostream);
587
588   path = g_file_get_path (data->file);
589   remove (path);
590
591   g_assert (!g_file_query_exists (data->file, NULL));
592
593   data->loop = g_main_loop_new (NULL, FALSE);
594
595   g_file_replace_contents_async (data->file,
596                                  data->data,
597                                  strlen (data->data),
598                                  NULL,
599                                  FALSE,
600                                  0,
601                                  NULL,
602                                  replaced_cb,
603                                  data);
604
605   g_main_loop_run (data->loop);
606
607   g_main_loop_unref (data->loop);
608   g_object_unref (data->file);
609   g_free (data);
610   free (path);
611 }
612
613 static void
614 test_replace_cancel (void)
615 {
616   GFile *tmpdir, *file;
617   GFileOutputStream *ostream;
618   GFileEnumerator *fenum;
619   GFileInfo *info;
620   GCancellable *cancellable;
621   gchar *path;
622   gsize nwrote;
623   GError *error = NULL;
624
625   g_test_bug ("629301");
626
627   path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
628   g_assert_no_error (error);
629   tmpdir = g_file_new_for_path (path);
630   g_free (path);
631
632   file = g_file_get_child (tmpdir, "file");
633   g_file_replace_contents (file,
634                            replace_data,
635                            strlen (replace_data),
636                            NULL, FALSE, 0, NULL,
637                            NULL, &error);
638   g_assert_no_error (error);
639
640   ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
641   g_assert_no_error (error);
642
643   g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
644                              replace_data, strlen (replace_data),
645                              &nwrote, NULL, &error);
646   g_assert_no_error (error);
647   g_assert_cmpint (nwrote, ==, strlen (replace_data));
648
649   /* At this point there should be two files; the original and the
650    * temporary.
651    */
652   fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
653   g_assert_no_error (error);
654
655   info = g_file_enumerator_next_file (fenum, NULL, &error);
656   g_assert_no_error (error);
657   g_assert (info != NULL);
658   g_object_unref (info);
659   info = g_file_enumerator_next_file (fenum, NULL, &error);
660   g_assert_no_error (error);
661   g_assert (info != NULL);
662   g_object_unref (info);
663
664   g_file_enumerator_close (fenum, NULL, &error);
665   g_assert_no_error (error);
666   g_object_unref (fenum);
667
668   /* Make sure the temporary gets deleted even if we cancel. */
669   cancellable = g_cancellable_new ();
670   g_cancellable_cancel (cancellable);
671   g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
672   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
673   g_clear_error (&error);
674
675   g_object_unref (cancellable);
676   g_object_unref (ostream);
677
678   g_file_delete (file, NULL, &error);
679   g_assert_no_error (error);
680   g_object_unref (file);
681
682   /* This will only succeed if the temp file was deleted. */
683   g_file_delete (tmpdir, NULL, &error);
684   g_assert_no_error (error);
685   g_object_unref (tmpdir);
686 }
687
688 static void
689 on_file_deleted (GObject      *object,
690                  GAsyncResult *result,
691                  gpointer      user_data)
692 {
693   GError *local_error = NULL;
694   GMainLoop *loop = user_data;
695
696   (void) g_file_delete_finish ((GFile*)object, result, &local_error);
697   g_assert_no_error (local_error);
698
699   g_main_loop_quit (loop);
700 }
701
702 static void
703 test_async_delete (void)
704 {
705   GFile *file;
706   GFileIOStream *iostream;
707   GError *local_error = NULL;
708   GError **error = &local_error;
709   GMainLoop *loop;
710
711   file = g_file_new_tmp ("g_file_delete_XXXXXX",
712                          &iostream, error);
713   g_assert_no_error (local_error);
714   g_object_unref (iostream);
715
716   g_assert (g_file_query_exists (file, NULL));
717
718   loop = g_main_loop_new (NULL, TRUE);
719
720   g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
721
722   g_main_loop_run (loop);
723
724   g_assert (!g_file_query_exists (file, NULL));
725
726   g_main_loop_unref (loop);
727   g_object_unref (file);
728 }
729
730 int
731 main (int argc, char *argv[])
732 {
733   g_test_init (&argc, &argv, NULL);
734
735   g_test_bug_base ("http://bugzilla.gnome.org/");
736
737   g_test_add_func ("/file/basic", test_basic);
738   g_test_add_func ("/file/parent", test_parent);
739   g_test_add_func ("/file/child", test_child);
740   g_test_add_func ("/file/type", test_type);
741   g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
742   g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
743   g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
744   g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
745   g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
746   g_test_add_func ("/file/replace-load", test_replace_load);
747   g_test_add_func ("/file/replace-cancel", test_replace_cancel);
748   g_test_add_func ("/file/async-delete", test_async_delete);
749
750   return g_test_run ();
751 }