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);
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);
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);
97 GFileMonitor *monitor;
98 GOutputStream *ostream;
99 GInputStream *istream;
102 gint monitor_created;
103 gint monitor_deleted;
104 gint monitor_changed;
113 monitor_changed (GFileMonitor *monitor,
116 GFileMonitorEvent event_type,
119 CreateDeleteData *data = user_data;
121 g_assert_cmpstr (data->monitor_path, ==, g_file_get_path (file));
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++;
133 quit_idle (gpointer user_data)
135 CreateDeleteData *data = user_data;
137 g_source_remove (data->timeout);
138 g_main_loop_quit (data->loop);
144 iclosed_cb (GObject *source,
148 CreateDeleteData *data = user_data;
153 ret = g_input_stream_close_finish (data->istream, res, &error);
154 g_assert_no_error (error);
157 g_assert (g_input_stream_is_closed (data->istream));
159 ret = g_file_delete (data->file, NULL, &error);
161 g_assert_no_error (error);
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
167 g_timeout_add (2000, quit_idle, data);
171 read_cb (GObject *source,
175 CreateDeleteData *data = user_data;
180 size = g_input_stream_read_finish (data->istream, res, &error);
181 g_assert_no_error (error);
184 if (data->pos < strlen (data->data))
186 g_input_stream_read_async (data->istream,
187 data->buffer + data->pos,
188 strlen (data->data) - data->pos,
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);
203 ipending_cb (GObject *source,
207 CreateDeleteData *data = user_data;
212 size = g_input_stream_read_finish (data->istream, res, &error);
213 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
214 g_error_free (error);
218 skipped_cb (GObject *source,
222 CreateDeleteData *data = user_data;
227 size = g_input_stream_skip_finish (data->istream, res, &error);
228 g_assert_no_error (error);
229 g_assert_cmpint (size, ==, data->pos);
231 g_input_stream_read_async (data->istream,
232 data->buffer + data->pos,
233 strlen (data->data) - data->pos,
238 /* check that we get a pending error */
239 g_input_stream_read_async (data->istream,
240 data->buffer + data->pos,
241 strlen (data->data) - data->pos,
249 opened_cb (GObject *source,
253 GFileInputStream *base;
254 CreateDeleteData *data = user_data;
258 base = g_file_read_finish (data->file, res, &error);
259 g_assert_no_error (error);
261 if (data->buffersize == 0)
262 data->istream = G_INPUT_STREAM (g_object_ref (base));
264 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
265 g_object_unref (base);
267 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
269 /* copy initial segment directly, then skip */
270 memcpy (data->buffer, data->data, 10);
273 g_input_stream_skip_async (data->istream,
282 oclosed_cb (GObject *source,
286 CreateDeleteData *data = user_data;
291 ret = g_output_stream_close_finish (data->ostream, res, &error);
292 g_assert_no_error (error);
294 g_assert (g_output_stream_is_closed (data->ostream));
296 g_file_read_async (data->file, 0, NULL, opened_cb, data);
300 written_cb (GObject *source,
304 CreateDeleteData *data = user_data;
309 size = g_output_stream_write_finish (data->ostream, res, &error);
310 g_assert_no_error (error);
313 if (data->pos < strlen (data->data))
315 g_output_stream_write_async (data->ostream,
316 data->data + data->pos,
317 strlen (data->data) - data->pos,
325 g_assert (!g_output_stream_is_closed (data->ostream));
326 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
331 opending_cb (GObject *source,
335 CreateDeleteData *data = user_data;
340 size = g_output_stream_write_finish (data->ostream, res, &error);
341 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
342 g_error_free (error);
346 created_cb (GObject *source,
350 GFileOutputStream *base;
351 CreateDeleteData *data = user_data;
355 base = g_file_create_finish (G_FILE (source), res, &error);
356 g_assert_no_error (error);
357 g_assert (g_file_query_exists (data->file, NULL));
359 if (data->buffersize == 0)
360 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
362 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
363 g_object_unref (base);
365 g_output_stream_write_async (data->ostream,
372 /* check that we get a pending error */
373 g_output_stream_write_async (data->ostream,
383 stop_timeout (gpointer data)
385 g_assert_not_reached ();
391 * This test does a fully async create-write-read-delete.
395 test_create_delete (gconstpointer d)
398 CreateDeleteData *data;
400 data = g_new0 (CreateDeleteData, 1);
402 data->buffersize = GPOINTER_TO_INT (d);
403 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
406 data->monitor_path = tempnam ("/tmp", "g_file_create_delete_");
408 data->file = g_file_new_for_path (data->monitor_path);
409 g_assert (!g_file_query_exists (data->file, NULL));
412 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
413 g_assert_no_error (error);
414 g_file_monitor_set_rate_limit (data->monitor, 100);
416 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
418 data->loop = g_main_loop_new (NULL, FALSE);
420 data->timeout = g_timeout_add (5000, stop_timeout, NULL);
422 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
424 g_main_loop_run (data->loop);
426 g_assert_cmpint (data->monitor_created, ==, 1);
427 g_assert_cmpint (data->monitor_deleted, ==, 1);
428 g_assert_cmpint (data->monitor_changed, >, 0);
430 g_assert (!g_file_monitor_is_cancelled (data->monitor));
431 g_file_monitor_cancel (data->monitor);
432 g_assert (g_file_monitor_is_cancelled (data->monitor));
434 g_main_loop_unref (data->loop);
435 g_object_unref (data->monitor);
436 g_object_unref (data->ostream);
437 g_object_unref (data->istream);
438 g_object_unref (data->file);
439 free (data->monitor_path);
440 g_free (data->buffer);
452 static void replaced_cb (GObject *source,
457 loaded_cb (GObject *source,
461 ReplaceLoadData *data = user_data;
468 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
470 g_assert_no_error (error);
471 g_assert_cmpint (length, ==, strlen (data->data));
472 g_assert_cmpstr (contents, ==, data->data);
479 data->data = "pi pa po";
481 g_file_replace_contents_async (data->file,
494 ret = g_file_delete (data->file, NULL, &error);
495 g_assert_no_error (error);
497 g_assert (!g_file_query_exists (data->file, NULL));
499 g_main_loop_quit (data->loop);
504 replaced_cb (GObject *source,
508 ReplaceLoadData *data = user_data;
513 ret = g_file_replace_contents_finish (data->file, res, NULL, &error);
514 g_assert_no_error (error);
516 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
520 test_replace_load (void)
522 ReplaceLoadData *data;
525 data = g_new0 (ReplaceLoadData, 1);
529 " * g_file_replace_contents_async:\n"
530 " * @file: input #GFile.\n"
531 " * @contents: string of contents to replace the file with.\n"
532 " * @length: the length of @contents in bytes.\n"
533 " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
534 " * @make_backup: %TRUE if a backup should be created.\n"
535 " * @flags: a set of #GFileCreateFlags.\n"
536 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
537 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
538 " * @user_data: the data to pass to callback function\n"
540 " * Starts an asynchronous replacement of @file with the given \n"
541 " * @contents of @length bytes. @etag will replace the document's\n"
542 " * current entity tag.\n"
544 " * When this operation has completed, @callback will be called with\n"
545 " * @user_user data, and the operation can be finalized with \n"
546 " * g_file_replace_contents_finish().\n"
548 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
549 " * triggering the cancellable object from another thread. If the operation\n"
550 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
552 " * If @make_backup is %TRUE, this function will attempt to \n"
553 " * make a backup of @file.\n"
556 path = tempnam ("/tmp", "g_file_replace_load_");
557 data->file = g_file_new_for_path (path);
558 g_assert (!g_file_query_exists (data->file, NULL));
560 data->loop = g_main_loop_new (NULL, FALSE);
562 g_file_replace_contents_async (data->file,
572 g_main_loop_run (data->loop);
574 g_main_loop_unref (data->loop);
575 g_object_unref (data->file);
581 main (int argc, char *argv[])
585 g_test_init (&argc, &argv, NULL);
587 g_test_add_func ("/file/basic", test_basic);
588 g_test_add_func ("/file/parent", test_parent);
589 g_test_add_func ("/file/child", test_child);
590 g_test_add_func ("/file/type", test_type);
591 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
592 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
593 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
594 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
595 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
596 g_test_add_func ("/file/replace-load", test_replace_load);
598 return g_test_run ();