5 #include <gio/gfiledescriptorbased.h>
13 file = g_file_new_for_path ("./some/directory/testfile");
15 s = g_file_get_basename (file);
16 g_assert_cmpstr (s, ==, "testfile");
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"));
24 g_assert (g_file_has_uri_scheme (file, "file"));
25 s = g_file_get_uri_scheme (file);
26 g_assert_cmpstr (s, ==, "file");
29 g_object_unref (file);
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 ("/");
44 g_assert (g_file_has_parent (file, file2));
46 parent = g_file_get_parent (file);
47 g_assert (g_file_equal (parent, file2));
48 g_object_unref (parent);
50 g_assert (g_file_get_parent (root) == NULL);
52 g_object_unref (file);
53 g_object_unref (file2);
54 g_object_unref (root);
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));
68 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
69 g_assert (g_file_has_parent (child2, file));
71 g_object_unref (child);
72 g_object_unref (child2);
73 g_object_unref (file);
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);
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);
92 g_file_read (file, NULL, &error);
93 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
95 g_object_unref (file);
102 GFileMonitor *monitor;
103 GOutputStream *ostream;
104 GInputStream *istream;
107 gint monitor_created;
108 gint monitor_deleted;
109 gint monitor_changed;
118 monitor_changed (GFileMonitor *monitor,
121 GFileMonitorEvent event_type,
124 CreateDeleteData *data = user_data;
126 g_assert_cmpstr (data->monitor_path, ==, g_file_get_path (file));
128 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
129 data->monitor_created++;
130 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
131 data->monitor_deleted++;
132 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
133 data->monitor_changed++;
138 quit_idle (gpointer user_data)
140 CreateDeleteData *data = user_data;
142 g_source_remove (data->timeout);
143 g_main_loop_quit (data->loop);
149 iclosed_cb (GObject *source,
153 CreateDeleteData *data = user_data;
158 ret = g_input_stream_close_finish (data->istream, res, &error);
159 g_assert_no_error (error);
162 g_assert (g_input_stream_is_closed (data->istream));
164 ret = g_file_delete (data->file, NULL, &error);
166 g_assert_no_error (error);
168 /* work around file monitor bug:
169 * inotify events are only processed every 1000 ms, regardless
170 * of the rate limit set on the file monitor
172 g_timeout_add (2000, quit_idle, data);
176 read_cb (GObject *source,
180 CreateDeleteData *data = user_data;
185 size = g_input_stream_read_finish (data->istream, res, &error);
186 g_assert_no_error (error);
189 if (data->pos < strlen (data->data))
191 g_input_stream_read_async (data->istream,
192 data->buffer + data->pos,
193 strlen (data->data) - data->pos,
201 g_assert_cmpstr (data->buffer, ==, data->data);
202 g_assert (!g_input_stream_is_closed (data->istream));
203 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
208 ipending_cb (GObject *source,
212 CreateDeleteData *data = user_data;
216 g_input_stream_read_finish (data->istream, res, &error);
217 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
218 g_error_free (error);
222 skipped_cb (GObject *source,
226 CreateDeleteData *data = user_data;
231 size = g_input_stream_skip_finish (data->istream, res, &error);
232 g_assert_no_error (error);
233 g_assert_cmpint (size, ==, data->pos);
235 g_input_stream_read_async (data->istream,
236 data->buffer + data->pos,
237 strlen (data->data) - data->pos,
242 /* check that we get a pending error */
243 g_input_stream_read_async (data->istream,
244 data->buffer + data->pos,
245 strlen (data->data) - data->pos,
253 opened_cb (GObject *source,
257 GFileInputStream *base;
258 CreateDeleteData *data = user_data;
262 base = g_file_read_finish (data->file, res, &error);
263 g_assert_no_error (error);
265 if (data->buffersize == 0)
266 data->istream = G_INPUT_STREAM (g_object_ref (base));
268 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
269 g_object_unref (base);
271 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
273 /* copy initial segment directly, then skip */
274 memcpy (data->buffer, data->data, 10);
277 g_input_stream_skip_async (data->istream,
286 oclosed_cb (GObject *source,
290 CreateDeleteData *data = user_data;
295 ret = g_output_stream_close_finish (data->ostream, res, &error);
296 g_assert_no_error (error);
298 g_assert (g_output_stream_is_closed (data->ostream));
300 g_file_read_async (data->file, 0, NULL, opened_cb, data);
304 written_cb (GObject *source,
308 CreateDeleteData *data = user_data;
313 size = g_output_stream_write_finish (data->ostream, res, &error);
314 g_assert_no_error (error);
317 if (data->pos < strlen (data->data))
319 g_output_stream_write_async (data->ostream,
320 data->data + data->pos,
321 strlen (data->data) - data->pos,
329 g_assert (!g_output_stream_is_closed (data->ostream));
330 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
335 opending_cb (GObject *source,
339 CreateDeleteData *data = user_data;
343 g_output_stream_write_finish (data->ostream, res, &error);
344 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
345 g_error_free (error);
349 created_cb (GObject *source,
353 GFileOutputStream *base;
354 CreateDeleteData *data = user_data;
358 base = g_file_create_finish (G_FILE (source), res, &error);
359 g_assert_no_error (error);
360 g_assert (g_file_query_exists (data->file, NULL));
362 if (data->buffersize == 0)
363 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
365 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
366 g_object_unref (base);
368 g_output_stream_write_async (data->ostream,
375 /* check that we get a pending error */
376 g_output_stream_write_async (data->ostream,
386 stop_timeout (gpointer data)
388 g_assert_not_reached ();
394 * This test does a fully async create-write-read-delete.
398 test_create_delete (gconstpointer d)
401 CreateDeleteData *data;
402 GFileIOStream *iostream;
404 data = g_new0 (CreateDeleteData, 1);
406 data->buffersize = GPOINTER_TO_INT (d);
407 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
410 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
412 g_assert (data->file != NULL);
413 g_object_unref (iostream);
415 data->monitor_path = g_file_get_path (data->file);
416 remove (data->monitor_path);
418 g_assert (!g_file_query_exists (data->file, NULL));
421 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
422 g_assert_no_error (error);
423 g_file_monitor_set_rate_limit (data->monitor, 100);
425 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
427 data->loop = g_main_loop_new (NULL, FALSE);
429 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
431 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
433 g_main_loop_run (data->loop);
435 g_assert_cmpint (data->monitor_created, ==, 1);
436 g_assert_cmpint (data->monitor_deleted, ==, 1);
437 g_assert_cmpint (data->monitor_changed, >, 0);
439 g_assert (!g_file_monitor_is_cancelled (data->monitor));
440 g_file_monitor_cancel (data->monitor);
441 g_assert (g_file_monitor_is_cancelled (data->monitor));
443 g_main_loop_unref (data->loop);
444 g_object_unref (data->monitor);
445 g_object_unref (data->ostream);
446 g_object_unref (data->istream);
447 g_object_unref (data->file);
448 free (data->monitor_path);
449 g_free (data->buffer);
461 static void replaced_cb (GObject *source,
466 loaded_cb (GObject *source,
470 ReplaceLoadData *data = user_data;
477 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
479 g_assert_no_error (error);
480 g_assert_cmpint (length, ==, strlen (data->data));
481 g_assert_cmpstr (contents, ==, data->data);
488 data->data = "pi pa po";
490 g_file_replace_contents_async (data->file,
503 ret = g_file_delete (data->file, NULL, &error);
504 g_assert_no_error (error);
506 g_assert (!g_file_query_exists (data->file, NULL));
508 g_main_loop_quit (data->loop);
513 replaced_cb (GObject *source,
517 ReplaceLoadData *data = user_data;
521 g_file_replace_contents_finish (data->file, res, NULL, &error);
522 g_assert_no_error (error);
524 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
528 test_replace_load (void)
530 ReplaceLoadData *data;
532 GFileIOStream *iostream;
534 data = g_new0 (ReplaceLoadData, 1);
538 " * g_file_replace_contents_async:\n"
539 " * @file: input #GFile.\n"
540 " * @contents: string of contents to replace the file with.\n"
541 " * @length: the length of @contents in bytes.\n"
542 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
543 " * @make_backup: %TRUE if a backup should be created.\n"
544 " * @flags: a set of #GFileCreateFlags.\n"
545 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
546 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
547 " * @user_data: the data to pass to callback function\n"
549 " * Starts an asynchronous replacement of @file with the given \n"
550 " * @contents of @length bytes. @etag will replace the document's\n"
551 " * current entity tag.\n"
553 " * When this operation has completed, @callback will be called with\n"
554 " * @user_user data, and the operation can be finalized with \n"
555 " * g_file_replace_contents_finish().\n"
557 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
558 " * triggering the cancellable object from another thread. If the operation\n"
559 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
561 " * If @make_backup is %TRUE, this function will attempt to \n"
562 " * make a backup of @file.\n"
565 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
567 g_assert (data->file != NULL);
568 g_object_unref (iostream);
570 path = g_file_get_path (data->file);
573 g_assert (!g_file_query_exists (data->file, NULL));
575 data->loop = g_main_loop_new (NULL, FALSE);
577 g_file_replace_contents_async (data->file,
587 g_main_loop_run (data->loop);
589 g_main_loop_unref (data->loop);
590 g_object_unref (data->file);
596 main (int argc, char *argv[])
600 g_test_init (&argc, &argv, NULL);
602 g_test_add_func ("/file/basic", test_basic);
603 g_test_add_func ("/file/parent", test_parent);
604 g_test_add_func ("/file/child", test_child);
605 g_test_add_func ("/file/type", test_type);
606 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
607 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
608 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
609 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
610 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
611 g_test_add_func ("/file/replace-load", test_replace_load);
613 return g_test_run ();