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 | |
---|
207 | static void |
---|
208 | directory_load_callback (GnomeVFSAsyncHandle *handle, |
---|
209 | GnomeVFSResult result, |
---|
210 | GList *list, |
---|
211 | guint entries_read, |
---|
212 | gpointer callback_data) |
---|
213 | { |
---|
214 | CallbackData *data; |
---|
215 | GnomeVFSFileInfo *info; |
---|
216 | GnomeVFSURI *parent_uri; |
---|
217 | GnomeVFSURI *uri; |
---|
218 | GList *node; |
---|
219 | guint i; |
---|
220 | |
---|
221 | data = (CallbackData *)callback_data; |
---|
222 | |
---|
223 | |
---|
224 | if (!measure_speed) { |
---|
225 | printf ("Directory load callback: %s, %d entries, callback_data `%s'\n", |
---|
226 | gnome_vfs_result_to_string (result), |
---|
227 | entries_read, |
---|
228 | (gchar *) data->parent_uri); |
---|
229 | } |
---|
230 | |
---|
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) { |
---|
233 | info = node->data; |
---|
234 | if (!measure_speed) { |
---|
235 | printf (" File `%s'%s (%s, %s), " |
---|
236 | "size %"GNOME_VFS_SIZE_FORMAT_STR", mode %04o\n", |
---|
237 | info->name, |
---|
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); |
---|
242 | fflush (stdout); |
---|
243 | } |
---|
244 | if (read_files) { |
---|
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); |
---|
249 | } |
---|
250 | if (!measure_speed) { |
---|
251 | printf ("reading a bit of %s\n", info->name); |
---|
252 | } |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | data->num_entries_read += entries_read; |
---|
258 | |
---|
259 | gnome_vfs_uri_unref (parent_uri); |
---|
260 | if (result != GNOME_VFS_OK) { |
---|
261 | if (--async_task_counter == 0) { |
---|
262 | gtk_main_quit (); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | int |
---|
268 | main (int argc, char **argv) |
---|
269 | { |
---|
270 | GnomeVFSAsyncHandle *handle; |
---|
271 | poptContext popt_context; |
---|
272 | const char **args; |
---|
273 | gchar *text_uri; |
---|
274 | GTimer *timer; |
---|
275 | #ifdef WITH_CORBA |
---|
276 | CORBA_Environment ev; |
---|
277 | #endif |
---|
278 | CallbackData callback_data; |
---|
279 | |
---|
280 | #ifdef WITH_PTHREAD |
---|
281 | puts ("Initializing threads..."); |
---|
282 | g_thread_init (NULL); |
---|
283 | #endif |
---|
284 | |
---|
285 | #ifdef WITH_CORBA |
---|
286 | CORBA_exception_init (&ev); |
---|
287 | puts ("Initializing gnome-libs with CORBA..."); |
---|
288 | gnome_CORBA_init_with_popt_table ("test-vfs", "0.0", &argc, argv, |
---|
289 | options, 0, &popt_context, 0, &ev); |
---|
290 | #else |
---|
291 | puts ("Initializing gnome-libs..."); |
---|
292 | gnome_init_with_popt_table ("test-vfs", "0.0", argc, argv, |
---|
293 | options, 0, &popt_context); |
---|
294 | #endif |
---|
295 | |
---|
296 | args = poptGetArgs (popt_context); |
---|
297 | if (args == NULL || args[1] != NULL) { |
---|
298 | fprintf (stderr, "Usage: %s [<options>] <uri>\n", argv[0]); |
---|
299 | return 1; |
---|
300 | } |
---|
301 | |
---|
302 | text_uri = g_strdup (args[0]); |
---|
303 | poptFreeContext (popt_context); |
---|
304 | |
---|
305 | puts ("Initializing gnome-vfs..."); |
---|
306 | gnome_vfs_init (); |
---|
307 | |
---|
308 | printf ("%d item(s) per notification\n", items_per_notification); |
---|
309 | |
---|
310 | if (measure_speed) { |
---|
311 | timer = g_timer_new (); |
---|
312 | g_timer_start (timer); |
---|
313 | } else { |
---|
314 | timer = NULL; |
---|
315 | } |
---|
316 | |
---|
317 | callback_data.num_entries_read = 0; |
---|
318 | callback_data.parent_uri = text_uri; |
---|
319 | async_task_counter = 1; |
---|
320 | gnome_vfs_async_load_directory |
---|
321 | (&handle, |
---|
322 | text_uri, |
---|
323 | (GNOME_VFS_FILE_INFO_GET_MIME_TYPE | GNOME_VFS_FILE_INFO_FOLLOW_LINKS), |
---|
324 | GNOME_VFS_DIRECTORY_FILTER_NONE, |
---|
325 | 0, NULL, |
---|
326 | items_per_notification, |
---|
327 | directory_load_callback, |
---|
328 | &callback_data); |
---|
329 | |
---|
330 | if (!measure_speed) |
---|
331 | puts ("GTK+ main loop running."); |
---|
332 | |
---|
333 | gtk_main (); |
---|
334 | |
---|
335 | if (measure_speed) { |
---|
336 | gdouble elapsed_seconds; |
---|
337 | |
---|
338 | g_timer_stop (timer); |
---|
339 | elapsed_seconds = g_timer_elapsed (timer, NULL); |
---|
340 | printf ("%.5f seconds for %d entries, %.5f entries/sec.\n", |
---|
341 | elapsed_seconds, callback_data.num_entries_read, |
---|
342 | (double) callback_data.num_entries_read / elapsed_seconds); |
---|
343 | } |
---|
344 | |
---|
345 | if (!measure_speed) |
---|
346 | puts ("GTK+ main loop finished."); fflush (stdout); |
---|
347 | |
---|
348 | #ifdef WITH_CORBA |
---|
349 | CORBA_exception_free (&ev); |
---|
350 | #endif |
---|
351 | |
---|
352 | puts ("All done"); |
---|
353 | |
---|
354 | gnome_vfs_shutdown (); |
---|
355 | |
---|
356 | return 0; |
---|
357 | } |
---|