Silence a bunch of -Wunused-but-set-variable warnings
[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
82   file = g_file_new_for_path (SRCDIR "/file.c");
83   type = g_file_query_file_type (file, 0, NULL);
84   g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
85   g_object_unref (file);
86
87   file = g_file_new_for_path (SRCDIR "/schema-tests");
88   type = g_file_query_file_type (file, 0, NULL);
89   g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
90   g_object_unref (file);
91 }
92
93
94 typedef struct
95 {
96   GFile *file;
97   GFileMonitor *monitor;
98   GOutputStream *ostream;
99   GInputStream *istream;
100   GMainLoop *loop;
101   gint buffersize;
102   gint monitor_created;
103   gint monitor_deleted;
104   gint monitor_changed;
105   gchar *monitor_path;
106   gint pos;
107   gchar *data;
108   gchar *buffer;
109   guint timeout;
110 } CreateDeleteData;
111
112 static void
113 monitor_changed (GFileMonitor      *monitor,
114                  GFile             *file,
115                  GFile             *other_file,
116                  GFileMonitorEvent  event_type,
117                  gpointer           user_data)
118 {
119   CreateDeleteData *data = user_data;
120
121   g_assert_cmpstr (data->monitor_path, ==, g_file_get_path (file));
122
123   if (event_type == G_FILE_MONITOR_EVENT_CREATED)
124     data->monitor_created++;
125   if (event_type == G_FILE_MONITOR_EVENT_DELETED)
126     data->monitor_deleted++;
127   if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
128     data->monitor_changed++;
129 }
130
131
132 static gboolean
133 quit_idle (gpointer user_data)
134 {
135   CreateDeleteData *data = user_data;
136
137   g_source_remove (data->timeout); 
138   g_main_loop_quit (data->loop);
139
140   return FALSE;
141 }
142
143 static void
144 iclosed_cb (GObject      *source,
145             GAsyncResult *res,
146             gpointer      user_data)
147 {
148   CreateDeleteData *data = user_data;
149   GError *error;
150   gboolean ret;
151
152   error = NULL;
153   ret = g_input_stream_close_finish (data->istream, res, &error);
154   g_assert_no_error (error);
155   g_assert (ret);
156
157   g_assert (g_input_stream_is_closed (data->istream));
158
159   ret = g_file_delete (data->file, NULL, &error);
160   g_assert (ret);
161   g_assert_no_error (error);
162
163   /* work around file monitor bug:
164    * inotify events are only processed every 1000 ms, regardless
165    * of the rate limit set on the file monitor
166    */
167   g_timeout_add (2000, quit_idle, data);
168 }
169
170 static void
171 read_cb (GObject      *source,
172          GAsyncResult *res,
173          gpointer      user_data)
174 {
175   CreateDeleteData *data = user_data;
176   GError *error;
177   gssize size;
178
179   error = NULL;
180   size = g_input_stream_read_finish (data->istream, res, &error);
181   g_assert_no_error (error);
182
183   data->pos += size;
184   if (data->pos < strlen (data->data))
185     {
186       g_input_stream_read_async (data->istream,
187                                  data->buffer + data->pos,
188                                  strlen (data->data) - data->pos,
189                                  0,
190                                  NULL,
191                                  read_cb,
192                                  data);
193     }
194   else
195     {
196       g_assert_cmpstr (data->buffer, ==, data->data);
197       g_assert (!g_input_stream_is_closed (data->istream));
198       g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
199     }
200 }
201
202 static void
203 ipending_cb (GObject      *source,
204              GAsyncResult *res,
205              gpointer      user_data)
206 {
207   CreateDeleteData *data = user_data;
208   GError *error;
209
210   error = NULL;
211   g_input_stream_read_finish (data->istream, res, &error);
212   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
213   g_error_free (error);
214 }
215
216 static void
217 skipped_cb (GObject      *source,
218             GAsyncResult *res,
219             gpointer      user_data)
220 {
221   CreateDeleteData *data = user_data;
222   GError *error;
223   gssize size;
224
225   error = NULL;
226   size = g_input_stream_skip_finish (data->istream, res, &error);
227   g_assert_no_error (error);
228   g_assert_cmpint (size, ==, data->pos);
229
230   g_input_stream_read_async (data->istream,
231                              data->buffer + data->pos,
232                              strlen (data->data) - data->pos,
233                              0,
234                              NULL,
235                              read_cb,
236                              data);
237   /* check that we get a pending error */
238   g_input_stream_read_async (data->istream,
239                              data->buffer + data->pos,
240                              strlen (data->data) - data->pos,
241                              0,
242                              NULL,
243                              ipending_cb,
244                              data);
245 }
246
247 static void
248 opened_cb (GObject      *source,
249            GAsyncResult *res,
250            gpointer      user_data)
251 {
252   GFileInputStream *base;
253   CreateDeleteData *data = user_data;
254   GError *error;
255
256   error = NULL;
257   base = g_file_read_finish (data->file, res, &error);
258   g_assert_no_error (error);
259
260   if (data->buffersize == 0)
261     data->istream = G_INPUT_STREAM (g_object_ref (base));
262   else
263     data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
264   g_object_unref (base);
265
266   data->buffer = g_new0 (gchar, strlen (data->data) + 1);
267
268   /* copy initial segment directly, then skip */
269   memcpy (data->buffer, data->data, 10);
270   data->pos = 10;
271
272   g_input_stream_skip_async (data->istream,
273                              10,
274                              0,
275                              NULL,
276                              skipped_cb,
277                              data);
278 }
279
280 static void
281 oclosed_cb (GObject      *source,
282             GAsyncResult *res,
283             gpointer      user_data)
284 {
285   CreateDeleteData *data = user_data;
286   GError *error;
287   gboolean ret;
288
289   error = NULL;
290   ret = g_output_stream_close_finish (data->ostream, res, &error);
291   g_assert_no_error (error);
292   g_assert (ret);
293   g_assert (g_output_stream_is_closed (data->ostream));
294
295   g_file_read_async (data->file, 0, NULL, opened_cb, data);
296 }
297
298 static void
299 written_cb (GObject      *source,
300             GAsyncResult *res,
301             gpointer      user_data)
302 {
303   CreateDeleteData *data = user_data;
304   gssize size;
305   GError *error;
306
307   error = NULL;
308   size = g_output_stream_write_finish (data->ostream, res, &error);
309   g_assert_no_error (error);
310
311   data->pos += size;
312   if (data->pos < strlen (data->data))
313     {
314       g_output_stream_write_async (data->ostream,
315                                    data->data + data->pos,
316                                    strlen (data->data) - data->pos,
317                                    0,
318                                    NULL,
319                                    written_cb,
320                                    data);
321     }
322   else
323     {
324       g_assert (!g_output_stream_is_closed (data->ostream));
325       g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
326     }
327 }
328
329 static void
330 opending_cb (GObject      *source,
331              GAsyncResult *res,
332              gpointer      user_data)
333 {
334   CreateDeleteData *data = user_data;
335   GError *error;
336
337   error = NULL;
338   g_output_stream_write_finish (data->ostream, res, &error);
339   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
340   g_error_free (error);
341 }
342
343 static void
344 created_cb (GObject      *source,
345             GAsyncResult *res,
346             gpointer      user_data)
347 {
348   GFileOutputStream *base;
349   CreateDeleteData *data = user_data;
350   GError *error;
351
352   error = NULL;
353   base = g_file_create_finish (G_FILE (source), res, &error);
354   g_assert_no_error (error);
355   g_assert (g_file_query_exists  (data->file, NULL));
356
357   if (data->buffersize == 0)
358     data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
359   else
360     data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
361   g_object_unref (base);
362
363   g_output_stream_write_async (data->ostream,
364                                data->data,
365                                strlen (data->data),
366                                0,
367                                NULL,
368                                written_cb,
369                                data);
370   /* check that we get a pending error */
371   g_output_stream_write_async (data->ostream,
372                                data->data,
373                                strlen (data->data),
374                                0,
375                                NULL,
376                                opending_cb,
377                                data);
378 }
379
380 static gboolean
381 stop_timeout (gpointer data)
382 {
383   g_assert_not_reached ();
384
385   return FALSE;
386 }
387
388 /*
389  * This test does a fully async create-write-read-delete.
390  * Callbackistan.
391  */
392 static void
393 test_create_delete (gconstpointer d)
394 {
395   GError *error;
396   CreateDeleteData *data;
397   int tmpfd;
398
399   data = g_new0 (CreateDeleteData, 1);
400
401   data->buffersize = GPOINTER_TO_INT (d);
402   data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
403   data->pos = 0;
404
405   /* Using tempnam() would be easier here, but causes a compile warning */
406   tmpfd = g_file_open_tmp ("g_file_create_delete_XXXXXX",
407                            &data->monitor_path, NULL);
408   g_assert_cmpint (tmpfd, !=, -1);
409   close (tmpfd);
410   remove (data->monitor_path);
411
412   data->file = g_file_new_for_path (data->monitor_path);
413   g_assert (!g_file_query_exists  (data->file, NULL));
414
415   error = NULL;
416   data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
417   g_assert_no_error (error);
418   g_file_monitor_set_rate_limit (data->monitor, 100);
419
420   g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
421
422   data->loop = g_main_loop_new (NULL, FALSE);
423
424   data->timeout = g_timeout_add (5000, stop_timeout, NULL);
425
426   g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
427
428   g_main_loop_run (data->loop);
429
430   g_assert_cmpint (data->monitor_created, ==, 1);
431   g_assert_cmpint (data->monitor_deleted, ==, 1);
432   g_assert_cmpint (data->monitor_changed, >, 0);
433
434   g_assert (!g_file_monitor_is_cancelled (data->monitor));
435   g_file_monitor_cancel (data->monitor);
436   g_assert (g_file_monitor_is_cancelled (data->monitor));
437
438   g_main_loop_unref (data->loop);
439   g_object_unref (data->monitor);
440   g_object_unref (data->ostream);
441   g_object_unref (data->istream);
442   g_object_unref (data->file);
443   free (data->monitor_path);
444   g_free (data->buffer);
445   g_free (data);
446 }
447
448 typedef struct
449 {
450   GFile *file;
451   const gchar *data;
452   GMainLoop *loop;
453   gboolean again;
454 } ReplaceLoadData;
455
456 static void replaced_cb (GObject      *source,
457                          GAsyncResult *res,
458                          gpointer      user_data);
459
460 static void
461 loaded_cb (GObject      *source,
462            GAsyncResult *res,
463            gpointer      user_data)
464 {
465   ReplaceLoadData *data = user_data;
466   gboolean ret;
467   GError *error;
468   gchar *contents;
469   gsize length;
470
471   error = NULL;
472   ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
473   g_assert (ret);
474   g_assert_no_error (error);
475   g_assert_cmpint (length, ==, strlen (data->data));
476   g_assert_cmpstr (contents, ==, data->data);
477
478   g_free (contents);
479
480   if (data->again)
481     {
482       data->again = FALSE;
483       data->data = "pi pa po";
484
485       g_file_replace_contents_async (data->file,
486                                      data->data,
487                                      strlen (data->data),
488                                      NULL,
489                                      FALSE,
490                                      0,
491                                      NULL,
492                                      replaced_cb,
493                                      data);
494     }
495   else
496     {
497        error = NULL;
498        ret = g_file_delete (data->file, NULL, &error);
499        g_assert_no_error (error);
500        g_assert (ret);
501        g_assert (!g_file_query_exists (data->file, NULL));
502
503        g_main_loop_quit (data->loop);
504     }
505 }
506
507 static void
508 replaced_cb (GObject      *source,
509              GAsyncResult *res,
510              gpointer      user_data)
511 {
512   ReplaceLoadData *data = user_data;
513   GError *error;
514
515   error = NULL;
516   g_file_replace_contents_finish (data->file, res, NULL, &error);
517   g_assert_no_error (error);
518
519   g_file_load_contents_async (data->file, NULL, loaded_cb, data);
520 }
521
522 static void
523 test_replace_load (void)
524 {
525   ReplaceLoadData *data;
526   gchar *path;
527   int tmpfd;
528
529   data = g_new0 (ReplaceLoadData, 1);
530   data->again = TRUE;
531   data->data =
532     "/**\n"
533     " * g_file_replace_contents_async:\n"
534     " * @file: input #GFile.\n"
535     " * @contents: string of contents to replace the file with.\n"
536     " * @length: the length of @contents in bytes.\n"
537     " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
538     " * @make_backup: %TRUE if a backup should be created.\n"
539     " * @flags: a set of #GFileCreateFlags.\n"
540     " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
541     " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
542     " * @user_data: the data to pass to callback function\n"
543     " * \n"
544     " * Starts an asynchronous replacement of @file with the given \n"
545     " * @contents of @length bytes. @etag will replace the document's\n"
546     " * current entity tag.\n"
547     " * \n"
548     " * When this operation has completed, @callback will be called with\n"
549     " * @user_user data, and the operation can be finalized with \n"
550     " * g_file_replace_contents_finish().\n"
551     " * \n"
552     " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
553     " * triggering the cancellable object from another thread. If the operation\n"
554     " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
555     " * \n"
556     " * If @make_backup is %TRUE, this function will attempt to \n"
557     " * make a backup of @file.\n"
558     " **/\n";
559
560   /* Using tempnam() would be easier here, but causes a compile warning */
561   tmpfd = g_file_open_tmp ("g_file_replace_load_XXXXXX",
562                            &path, NULL);
563   g_assert_cmpint (tmpfd, !=, -1);
564   close (tmpfd);
565   remove (path);
566
567   data->file = g_file_new_for_path (path);
568   g_assert (!g_file_query_exists (data->file, NULL));
569
570   data->loop = g_main_loop_new (NULL, FALSE);
571
572   g_file_replace_contents_async (data->file,
573                                  data->data,
574                                  strlen (data->data),
575                                  NULL,
576                                  FALSE,
577                                  0,
578                                  NULL,
579                                  replaced_cb,
580                                  data);
581
582   g_main_loop_run (data->loop);
583
584   g_main_loop_unref (data->loop);
585   g_object_unref (data->file);
586   g_free (data);
587   free (path);
588 }
589
590 int
591 main (int argc, char *argv[])
592 {
593   g_type_init ();
594
595   g_test_init (&argc, &argv, NULL);
596
597   g_test_add_func ("/file/basic", test_basic);
598   g_test_add_func ("/file/parent", test_parent);
599   g_test_add_func ("/file/child", test_child);
600   g_test_add_func ("/file/type", test_type);
601   g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
602   g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
603   g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
604   g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
605   g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
606   g_test_add_func ("/file/replace-load", test_replace_load);
607
608   return g_test_run ();
609 }