1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-async-directory.c - Test program for asynchronous directory
3 reading with the GNOME Virtual File System.
5 Copyright (C) 1999 Free Software Foundation
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Gnome Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Author: Ettore Perazzoli <ettore@comm2000.it> */
26 #include <glib/gstrfuncs.h>
27 #include <glib/gtimer.h>
28 #include <glib/gmain.h>
29 #include <libgnomevfs/gnome-vfs-async-ops.h>
30 #include <libgnomevfs/gnome-vfs-init.h>
34 static GMainLoop *main_loop;
36 static int measure_speed = 0;
38 static int items_per_notification = 1;
39 static int read_files = 0;
41 static struct poptOption options[] = {
46 &items_per_notification,
48 "Number of items to send for every notification",
57 "Measure speed without displaying anything",
90 type_to_string (GnomeVFSFileType type)
93 case GNOME_VFS_FILE_TYPE_UNKNOWN:
95 case GNOME_VFS_FILE_TYPE_REGULAR:
97 case GNOME_VFS_FILE_TYPE_DIRECTORY:
99 case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
100 return "Symbolic Link";
101 case GNOME_VFS_FILE_TYPE_FIFO:
103 case GNOME_VFS_FILE_TYPE_SOCKET:
105 case GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE:
106 return "Character device";
107 case GNOME_VFS_FILE_TYPE_BLOCK_DEVICE:
108 return "Block device";
115 test_read_file_close_callback (GnomeVFSAsyncHandle *handle,
116 GnomeVFSResult result,
117 gpointer callback_data)
123 test_read_file_succeeded (GnomeVFSAsyncHandle *handle)
125 gnome_vfs_async_close (handle,
126 test_read_file_close_callback,
131 test_read_file_failed (GnomeVFSAsyncHandle *handle, GnomeVFSResult result)
133 gnome_vfs_async_close (handle,
134 test_read_file_close_callback,
138 /* A read is complete, so we might or might not be done. */
140 test_read_file_read_callback (GnomeVFSAsyncHandle *handle,
141 GnomeVFSResult result,
143 GnomeVFSFileSize bytes_requested,
144 GnomeVFSFileSize bytes_read,
145 gpointer callback_data)
147 /* Check for a failure. */
148 if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) {
149 test_read_file_failed (handle, result);
153 /* If at the end of the file, we win! */
154 test_read_file_succeeded (handle);
159 /* Start reading a chunk. */
161 test_read_file_read_chunk (GnomeVFSAsyncHandle *handle)
163 gnome_vfs_async_read (handle,
166 test_read_file_read_callback,
170 /* Once the open is finished, read a first chunk. */
172 test_read_file_open_callback (GnomeVFSAsyncHandle *handle,
173 GnomeVFSResult result,
174 gpointer callback_data)
176 if (result != GNOME_VFS_OK) {
177 test_read_file_failed (handle, result);
181 test_read_file_read_chunk (handle);
184 /* Set up the read handle and start reading. */
185 static GnomeVFSAsyncHandle *
186 test_read_file_async (GnomeVFSURI *uri)
188 GnomeVFSAsyncHandle *result;
190 gnome_vfs_async_open_uri (&result,
194 test_read_file_open_callback,
201 const char *parent_uri;
202 int num_entries_read;
205 volatile int async_task_counter;
208 directory_load_callback (GnomeVFSAsyncHandle *handle,
209 GnomeVFSResult result,
212 gpointer callback_data)
215 GnomeVFSFileInfo *info;
216 GnomeVFSURI *parent_uri;
221 data = (CallbackData *)callback_data;
224 if (!measure_speed) {
225 printf ("Directory load callback: %s, %d entries, callback_data `%s'\n",
226 gnome_vfs_result_to_string (result),
228 (gchar *) data->parent_uri);
231 parent_uri = gnome_vfs_uri_new (data->parent_uri);
232 for (i = 0, node = list; i < entries_read && node != NULL; i++, node = node->next) {
234 if (!measure_speed) {
235 printf (" File `%s'%s (%s, %s), "
236 "size %"GNOME_VFS_SIZE_FORMAT_STR", mode %04o\n",
238 (info->flags & GNOME_VFS_FILE_FLAGS_SYMLINK) ? " [link]" : "",
239 type_to_string (info->type),
240 gnome_vfs_file_info_get_mime_type (info),
241 info->size, info->permissions);
245 if ((info->type & GNOME_VFS_FILE_TYPE_REGULAR) != 0) {
246 uri = gnome_vfs_uri_append_file_name (parent_uri, info->name);
247 test_read_file_async (uri);
248 gnome_vfs_uri_unref (uri);
250 if (!measure_speed) {
251 printf ("reading a bit of %s\n", info->name);
257 data->num_entries_read += entries_read;
259 gnome_vfs_uri_unref (parent_uri);
260 if (result != GNOME_VFS_OK) {
261 if (--async_task_counter == 0) {
262 g_main_loop_quit (main_loop);
268 main (int argc, const char **argv)
270 GnomeVFSAsyncHandle *handle;
271 poptContext popt_context;
275 CallbackData callback_data;
277 puts ("Initializing gnome-libs...");
278 popt_context = poptGetContext ("test-vfs", argc, argv,
281 args = poptGetArgs (popt_context);
282 if (args == NULL || args[1] != NULL) {
283 fprintf (stderr, "Usage: %s [<options>] <uri>\n", argv[0]);
287 text_uri = g_strdup (args[0]);
288 poptFreeContext (popt_context);
290 puts ("Initializing gnome-vfs...");
293 printf ("%d item(s) per notification\n", items_per_notification);
296 timer = g_timer_new ();
297 g_timer_start (timer);
302 callback_data.num_entries_read = 0;
303 callback_data.parent_uri = text_uri;
304 async_task_counter = 1;
305 gnome_vfs_async_load_directory
308 (GNOME_VFS_FILE_INFO_GET_MIME_TYPE | GNOME_VFS_FILE_INFO_FOLLOW_LINKS),
309 items_per_notification,
311 directory_load_callback,
315 puts ("main loop running.");
317 main_loop = g_main_loop_new (NULL, TRUE);
318 g_main_loop_run (main_loop);
319 g_main_loop_unref (main_loop);
322 gdouble elapsed_seconds;
324 g_timer_stop (timer);
325 elapsed_seconds = g_timer_elapsed (timer, NULL);
326 printf ("%.5f seconds for %d entries, %.5f entries/sec.\n",
327 elapsed_seconds, callback_data.num_entries_read,
328 (double) callback_data.num_entries_read / elapsed_seconds);
332 puts ("GTK+ main loop finished."); fflush (stdout);
336 gnome_vfs_shutdown ();