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. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
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. |
---|
11 | |
---|
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. |
---|
16 | |
---|
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. |
---|
21 | |
---|
22 | Author: Ettore Perazzoli <ettore@comm2000.it> */ |
---|
23 | |
---|
24 | #ifdef HAVE_CONFIG_H |
---|
25 | #include <config.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <stdio.h> |
---|
29 | |
---|
30 | #include <gnome.h> |
---|
31 | |
---|
32 | #include <orb/orbit.h> |
---|
33 | #include <libgnorba/gnorba.h> |
---|
34 | |
---|
35 | #include "gnome-vfs.h" |
---|
36 | |
---|
37 | static int measure_speed = 0; |
---|
38 | static int sort = 0; |
---|
39 | static int items_per_notification = 1; |
---|
40 | static int read_files = 0; |
---|
41 | |
---|
42 | static struct poptOption options[] = { |
---|
43 | { |
---|
44 | "chunk-size", |
---|
45 | 'c', |
---|
46 | POPT_ARG_INT, |
---|
47 | &items_per_notification, |
---|
48 | 0, |
---|
49 | "Number of items to send for every notification", |
---|
50 | "NUM_ITEMS" |
---|
51 | }, |
---|
52 | { |
---|
53 | "measure-speed", |
---|
54 | 'm', |
---|
55 | POPT_ARG_NONE, |
---|
56 | &measure_speed, |
---|
57 | 0, |
---|
58 | "Measure speed without displaying anything", |
---|
59 | NULL |
---|
60 | }, |
---|
61 | { |
---|
62 | "sort", |
---|
63 | 's', |
---|
64 | POPT_ARG_NONE, |
---|
65 | &sort, |
---|
66 | 0, |
---|
67 | "Sort entries", |
---|
68 | NULL |
---|
69 | }, |
---|
70 | { |
---|
71 | "read-files", |
---|
72 | 'r', |
---|
73 | POPT_ARG_NONE, |
---|
74 | &read_files, |
---|
75 | 0, |
---|
76 | "Test file reading", |
---|
77 | NULL |
---|
78 | }, |
---|
79 | { |
---|
80 | NULL, |
---|
81 | 0, |
---|
82 | 0, |
---|
83 | NULL, |
---|
84 | 0, |
---|
85 | NULL, |
---|
86 | NULL |
---|
87 | } |
---|
88 | }; |
---|
89 | |
---|
90 | static const gchar * |
---|
91 | type_to_string (GnomeVFSFileType type) |
---|
92 | { |
---|
93 | switch (type) { |
---|
94 | case GNOME_VFS_FILE_TYPE_UNKNOWN: |
---|
95 | return "Unknown"; |
---|
96 | case GNOME_VFS_FILE_TYPE_REGULAR: |
---|
97 | return "Regular"; |
---|
98 | case GNOME_VFS_FILE_TYPE_DIRECTORY: |
---|
99 | return "Directory"; |
---|
100 | case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: |
---|
101 | return "Symbolic Link"; |
---|
102 | case GNOME_VFS_FILE_TYPE_FIFO: |
---|
103 | return "FIFO"; |
---|
104 | case GNOME_VFS_FILE_TYPE_SOCKET: |
---|
105 | return "Socket"; |
---|
106 | case GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE: |
---|
107 | return "Character device"; |
---|
108 | case GNOME_VFS_FILE_TYPE_BLOCK_DEVICE: |
---|
109 | return "Block device"; |
---|
110 | default: |
---|
111 | return "???"; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | test_read_file_close_callback (GnomeVFSAsyncHandle *handle, |
---|
117 | GnomeVFSResult result, |
---|
118 | gpointer callback_data) |
---|
119 | { |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | static void |
---|
124 | test_read_file_succeeded (GnomeVFSAsyncHandle *handle) |
---|
125 | { |
---|
126 | gnome_vfs_async_close (handle, |
---|
127 | test_read_file_close_callback, |
---|
128 | NULL); |
---|
129 | } |
---|
130 | |
---|
131 | static void |
---|
132 | test_read_file_failed (GnomeVFSAsyncHandle *handle, GnomeVFSResult result) |
---|
133 | { |
---|
134 | gnome_vfs_async_close (handle, |
---|
135 | test_read_file_close_callback, |
---|
136 | NULL); |
---|
137 | } |
---|
138 | |
---|
139 | /* A read is complete, so we might or might not be done. */ |
---|
140 | static void |
---|
141 | test_read_file_read_callback (GnomeVFSAsyncHandle *handle, |
---|
142 | GnomeVFSResult result, |
---|
143 | gpointer buffer, |
---|
144 | GnomeVFSFileSize bytes_requested, |
---|
145 | GnomeVFSFileSize bytes_read, |
---|
146 | gpointer callback_data) |
---|
147 | { |
---|
148 | /* Check for a failure. */ |
---|
149 | if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) { |
---|
150 | test_read_file_failed (handle, result); |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | /* If at the end of the file, we win! */ |
---|
155 | test_read_file_succeeded (handle); |
---|
156 | } |
---|
157 | |
---|
158 | char buffer[256]; |
---|
159 | |
---|
160 | /* Start reading a chunk. */ |
---|
161 | static void |
---|
162 | test_read_file_read_chunk (GnomeVFSAsyncHandle *handle) |
---|
163 | { |
---|
164 | gnome_vfs_async_read (handle, |
---|
165 | buffer, |
---|
166 | 10, |
---|
167 | test_read_file_read_callback, |
---|
168 | handle); |
---|
169 | } |
---|
170 | |
---|
171 | /* Once the open is finished, read a first chunk. */ |
---|
172 | static void |
---|
173 | test_read_file_open_callback (GnomeVFSAsyncHandle *handle, |
---|
174 | GnomeVFSResult result, |
---|
175 | gpointer callback_data) |
---|
176 | { |
---|
177 | if (result != GNOME_VFS_OK) { |
---|
178 | test_read_file_failed (handle, result); |
---|
179 | return; |
---|
180 | } |
---|
181 | |
---|
182 | test_read_file_read_chunk (handle); |
---|
183 | } |
---|
184 | |
---|
185 | /* Set up the read handle and start reading. */ |
---|
186 | static GnomeVFSAsyncHandle * |
---|
187 | test_read_file_async (GnomeVFSURI *uri) |
---|
188 | { |
---|
189 | GnomeVFSAsyncHandle *result; |
---|
190 | |
---|
191 | gnome_vfs_async_open_uri (&result, |
---|
192 | uri, |
---|
193 | GNOME_VFS_OPEN_READ, |
---|
194 | test_read_file_open_callback, |
---|
195 | NULL); |
---|
196 | |
---|
197 | return result; |
---|
198 | } |
---|
199 | |
---|
200 | typedef struct { |
---|
201 | const char *parent_uri; |
---|
202 | int num_entries_read; |
---|
203 | } CallbackData; |
---|
204 | |
---|
205 | volatile int async_task_counter; |
---|
206 | static void |
---|
207 | directory_load_callback (GnomeVFSAsyncHandle *handle, |
---|
208 | GnomeVFSResult result, |
---|
209 | GnomeVFSDirectoryList *list, |
---|
210 | guint entries_read, |
---|
211 | gpointer callback_data) |
---|
212 | { |
---|
213 | CallbackData *data; |
---|
214 | GnomeVFSFileInfo *info; |
---|
215 | GnomeVFSURI *parent_uri; |
---|
216 | GnomeVFSURI *uri; |
---|
217 | guint i; |
---|
218 | |
---|
219 | data = (CallbackData *)callback_data; |
---|
220 | |
---|
221 | |
---|
222 | if (!measure_speed) { |
---|
223 | printf ("Directory load callback: %s, %d entries, callback_data `%s'\n", |
---|
224 | gnome_vfs_result_to_string (result), |
---|
225 | entries_read, |
---|
226 | (gchar *) data->parent_uri); |
---|
227 | } |
---|
228 | |
---|
229 | parent_uri = gnome_vfs_uri_new (data->parent_uri); |
---|
230 | info = gnome_vfs_directory_list_current (list); |
---|
231 | for (i = 0; i < entries_read && info != NULL; i++) { |
---|
232 | if (!measure_speed) { |
---|
233 | printf (" File `%s'%s (%s, %s), " |
---|
234 | "size %"GNOME_VFS_SIZE_FORMAT_STR", mode %04o\n", |
---|
235 | info->name, |
---|
236 | (info->flags & GNOME_VFS_FILE_FLAGS_SYMLINK) ? " [link]" : "", |
---|
237 | type_to_string (info->type), |
---|
238 | gnome_vfs_file_info_get_mime_type (info), |
---|
239 | info->size, info->permissions); |
---|
240 | fflush (stdout); |
---|
241 | } |
---|
242 | if (read_files) { |
---|
243 | if ((info->type & GNOME_VFS_FILE_TYPE_REGULAR) != 0) { |
---|
244 | uri = gnome_vfs_uri_append_file_name (parent_uri, info->name); |
---|
245 | test_read_file_async (uri); |
---|
246 | gnome_vfs_uri_unref (uri); |
---|
247 | } |
---|
248 | if (!measure_speed) { |
---|
249 | printf ("reading a bit of %s\n", info->name); |
---|
250 | } |
---|
251 | } |
---|
252 | info = gnome_vfs_directory_list_next (list); |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | data->num_entries_read += entries_read; |
---|
257 | |
---|
258 | gnome_vfs_uri_unref (parent_uri); |
---|
259 | if (result != GNOME_VFS_OK) { |
---|
260 | if (--async_task_counter == 0) { |
---|
261 | gtk_main_quit (); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | int |
---|
267 | main (int argc, char **argv) |
---|
268 | { |
---|
269 | GnomeVFSAsyncHandle *handle; |
---|
270 | poptContext popt_context; |
---|
271 | const char **args; |
---|
272 | gchar *text_uri; |
---|
273 | GnomeVFSDirectorySortRule sort_rules[] = { |
---|
274 | GNOME_VFS_DIRECTORY_SORT_BYNAME, |
---|
275 | GNOME_VFS_DIRECTORY_SORT_NONE |
---|
276 | }; |
---|
277 | GTimer *timer; |
---|
278 | #ifdef WITH_CORBA |
---|
279 | CORBA_Environment ev; |
---|
280 | #endif |
---|
281 | CallbackData callback_data; |
---|
282 | |
---|
283 | #ifdef WITH_PTHREAD |
---|
284 | puts ("Initializing threads..."); |
---|
285 | g_thread_init (NULL); |
---|
286 | #endif |
---|
287 | |
---|
288 | #ifdef WITH_CORBA |
---|
289 | CORBA_exception_init (&ev); |
---|
290 | puts ("Initializing gnome-libs with CORBA..."); |
---|
291 | gnome_CORBA_init_with_popt_table ("test-vfs", "0.0", &argc, argv, |
---|
292 | options, 0, &popt_context, 0, &ev); |
---|
293 | #else |
---|
294 | puts ("Initializing gnome-libs..."); |
---|
295 | gnome_init_with_popt_table ("test-vfs", "0.0", argc, argv, |
---|
296 | options, 0, &popt_context); |
---|
297 | #endif |
---|
298 | |
---|
299 | args = poptGetArgs (popt_context); |
---|
300 | if (args == NULL || args[1] != NULL) { |
---|
301 | fprintf (stderr, "Usage: %s [<options>] <uri>\n", argv[0]); |
---|
302 | return 1; |
---|
303 | } |
---|
304 | |
---|
305 | text_uri = g_strdup (args[0]); |
---|
306 | poptFreeContext (popt_context); |
---|
307 | |
---|
308 | puts ("Initializing gnome-vfs..."); |
---|
309 | gnome_vfs_init (); |
---|
310 | |
---|
311 | printf ("%d item(s) per notification\n", items_per_notification); |
---|
312 | |
---|
313 | if (measure_speed) { |
---|
314 | timer = g_timer_new (); |
---|
315 | g_timer_start (timer); |
---|
316 | } else { |
---|
317 | timer = NULL; |
---|
318 | } |
---|
319 | |
---|
320 | callback_data.num_entries_read = 0; |
---|
321 | callback_data.parent_uri = text_uri; |
---|
322 | async_task_counter = 1; |
---|
323 | gnome_vfs_async_load_directory |
---|
324 | (&handle, /* handle */ |
---|
325 | text_uri, /* text_uri */ |
---|
326 | (GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
---|
327 | | GNOME_VFS_FILE_INFO_FOLLOW_LINKS), /* options */ |
---|
328 | sort ? sort_rules : NULL, /* sort_rules */ |
---|
329 | FALSE, /* reverse_order */ |
---|
330 | GNOME_VFS_DIRECTORY_FILTER_NONE, /* filter_type */ |
---|
331 | 0, /* filter_options */ |
---|
332 | NULL, /* filter_pattern */ |
---|
333 | items_per_notification, /* items_per_notification */ |
---|
334 | directory_load_callback, /* callback */ |
---|
335 | &callback_data); /* callback_data */ |
---|
336 | |
---|
337 | #if 0 |
---|
338 | async_task_counter++; |
---|
339 | gnome_vfs_async_load_directory |
---|
340 | (&handle, /* handle */ |
---|
341 | text_uri, /* text_uri */ |
---|
342 | (GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
---|
343 | | GNOME_VFS_FILE_INFO_FOLLOW_LINKS), /* options */ |
---|
344 | sort ? sort_rules : NULL, /* sort_rules */ |
---|
345 | FALSE, /* reverse_order */ |
---|
346 | GNOME_VFS_DIRECTORY_FILTER_NONE, /* filter_type */ |
---|
347 | 0, /* filter_options */ |
---|
348 | NULL, /* filter_pattern */ |
---|
349 | items_per_notification, /* items_per_notification */ |
---|
350 | directory_load_callback, /* callback */ |
---|
351 | &callback_data); /* callback_data */ |
---|
352 | |
---|
353 | async_task_counter++; |
---|
354 | gnome_vfs_async_load_directory |
---|
355 | (&handle, /* handle */ |
---|
356 | text_uri, /* text_uri */ |
---|
357 | (GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
---|
358 | | GNOME_VFS_FILE_INFO_FOLLOW_LINKS), /* options */ |
---|
359 | sort ? sort_rules : NULL, /* sort_rules */ |
---|
360 | FALSE, /* reverse_order */ |
---|
361 | GNOME_VFS_DIRECTORY_FILTER_NONE, /* filter_type */ |
---|
362 | 0, /* filter_options */ |
---|
363 | NULL, /* filter_pattern */ |
---|
364 | items_per_notification, /* items_per_notification */ |
---|
365 | directory_load_callback, /* callback */ |
---|
366 | &callback_data); /* callback_data */ |
---|
367 | #endif |
---|
368 | |
---|
369 | if (!measure_speed) |
---|
370 | puts ("GTK+ main loop running."); |
---|
371 | |
---|
372 | gtk_main (); |
---|
373 | |
---|
374 | if (measure_speed) { |
---|
375 | gdouble elapsed_seconds; |
---|
376 | |
---|
377 | g_timer_stop (timer); |
---|
378 | elapsed_seconds = g_timer_elapsed (timer, NULL); |
---|
379 | printf ("%.5f seconds for %d entries, %.5f entries/sec.\n", |
---|
380 | elapsed_seconds, callback_data.num_entries_read, |
---|
381 | (double) callback_data.num_entries_read / elapsed_seconds); |
---|
382 | } |
---|
383 | |
---|
384 | if (!measure_speed) |
---|
385 | puts ("GTK+ main loop finished."); fflush (stdout); |
---|
386 | |
---|
387 | #ifdef WITH_CORBA |
---|
388 | CORBA_exception_free (&ev); |
---|
389 | #endif |
---|
390 | |
---|
391 | puts ("All done"); |
---|
392 | |
---|
393 | gnome_vfs_shutdown (); |
---|
394 | |
---|
395 | return 0; |
---|
396 | } |
---|