1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-vfs.c - Test program for the GNOME Virtual File System.
4 Copyright (C) 1999 Free Software Foundation
6 The Gnome Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The Gnome Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Author: Ettore Perazzoli <ettore@comm2000.it>
26 #include <glib/gmain.h>
27 #include <libgnomevfs/gnome-vfs-async-ops.h>
28 #include <libgnomevfs/gnome-vfs-init.h>
31 #define QUEUE_LENGTH 4000
33 static GMainLoop *main_loop;
37 close_callback (GnomeVFSAsyncHandle *handle,
38 GnomeVFSResult result,
39 gpointer callback_data)
41 fprintf (stderr, "Close: %s.\n", gnome_vfs_result_to_string (result));
42 g_main_loop_quit (main_loop);
46 file_control_callback (GnomeVFSAsyncHandle *handle,
47 GnomeVFSResult result,
48 gpointer operation_data,
49 gpointer callback_data)
51 if (result != GNOME_VFS_OK) {
52 fprintf (stderr, "file_control failed: %s\n",
53 gnome_vfs_result_to_string (result));
55 printf ("file_control result: %s\n", *(char **)operation_data);
58 fprintf (stderr, "Now closing the file.\n");
59 gnome_vfs_async_close (handle, close_callback, "close");
63 read_callback (GnomeVFSAsyncHandle *handle,
64 GnomeVFSResult result,
66 GnomeVFSFileSize bytes_requested,
67 GnomeVFSFileSize bytes_read,
68 gpointer callback_data)
72 if (result != GNOME_VFS_OK) {
73 fprintf (stderr, "Read failed: %s\n",
74 gnome_vfs_result_to_string (result));
76 printf ("%"GNOME_VFS_SIZE_FORMAT_STR"/"
77 "%"GNOME_VFS_SIZE_FORMAT_STR" "
78 "byte(s) read, callback data `%s'\n",
79 bytes_read, bytes_requested,
80 (gchar *) callback_data);
81 *((gchar *) buffer + bytes_read) = 0;
82 fprintf (stderr, "%s", (char *) buffer);
85 fprintf (stderr, "Now testing file_control.\n");
86 op_data = g_new (char *, 1);
87 gnome_vfs_async_file_control (handle, "file:test", op_data, g_free, file_control_callback, "file_control");
91 open_callback (GnomeVFSAsyncHandle *handle,
92 GnomeVFSResult result,
93 gpointer callback_data)
95 if (result != GNOME_VFS_OK) {
96 fprintf (stderr, "Open failed: %s.\n",
97 gnome_vfs_result_to_string (result));
98 g_main_loop_quit (main_loop);
101 const gulong buffer_size = 1024;
103 fprintf (stderr, "File opened correctly, data `%s'.\n",
104 (gchar *) callback_data);
106 buffer = g_malloc (buffer_size);
107 gnome_vfs_async_read (handle,
116 dummy_close_callback (GnomeVFSAsyncHandle *handle,
117 GnomeVFSResult result,
118 gpointer callback_data)
123 async_queue_callback (GnomeVFSAsyncHandle *handle,
124 GnomeVFSResult result,
125 gpointer callback_data)
127 int *completed = callback_data;
131 if (result == GNOME_VFS_OK)
132 gnome_vfs_async_close (handle, dummy_close_callback, NULL);
136 main (int argc, char **argv)
139 GnomeVFSAsyncHandle *handle;
142 fprintf (stderr, "Usage: %s <uri of text file>\n", argv[0]);
146 fprintf (stderr, "Initializing gnome-vfs...\n");
149 fprintf (stderr, "Creating async context...\n");
151 fprintf (stderr, "Starting open for `%s'...\n", argv[1]);
152 gnome_vfs_async_open (&handle, argv[1], GNOME_VFS_OPEN_READ,
153 GNOME_VFS_PRIORITY_MIN,
154 open_callback, "open_callback");
156 fprintf (stderr, "Main loop running.\n");
157 main_loop = g_main_loop_new (NULL, TRUE);
158 g_main_loop_run (main_loop);
160 fprintf (stderr, "Main loop finished.\n");
162 fprintf (stderr, "Test async queue efficiency ...");
164 for (completed = i = 0; i < QUEUE_LENGTH; i++) {
165 gnome_vfs_async_open (&handle, argv [1], GNOME_VFS_OPEN_READ, 0,
166 async_queue_callback, &completed);
169 while (completed < QUEUE_LENGTH)
170 g_main_context_iteration (NULL, TRUE);
172 fprintf (stderr, "Passed\n");
174 g_main_loop_unref (main_loop);
176 fprintf (stderr, "All done\n");
178 gnome_vfs_shutdown ();