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 | #include <config.h> |
---|
25 | |
---|
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> |
---|
31 | #include <popt.h> |
---|
32 | #include <stdio.h> |
---|
33 | |
---|
34 | static GMainLoop *main_loop; |
---|
35 | |
---|
36 | static int measure_speed = 0; |
---|
37 | static int sort = 0; |
---|
38 | static int items_per_notification = 1; |
---|
39 | static int read_files = 0; |
---|
40 | |
---|
41 | static struct poptOption options[] = { |
---|
42 | { |
---|
43 | "chunk-size", |
---|
44 | 'c', |
---|
45 | POPT_ARG_INT, |
---|
46 | &items_per_notification, |
---|
47 | 0, |
---|
48 | "Number of items to send for every notification", |
---|
49 | "NUM_ITEMS" |
---|
50 | }, |
---|
51 | { |
---|
52 | "measure-speed", |
---|
53 | 'm', |
---|
54 | POPT_ARG_NONE, |
---|
55 | &measure_speed, |
---|
56 | 0, |
---|
57 | "Measure speed without displaying anything", |
---|
58 | NULL |
---|
59 | }, |
---|
60 | { |
---|
61 | "sort", |
---|
62 | 's', |
---|
63 | POPT_ARG_NONE, |
---|
64 | &sort, |
---|
65 | 0, |
---|
66 | "Sort entries", |
---|
67 | NULL |
---|
68 | }, |
---|
69 | { |
---|
70 | "read-files", |
---|
71 | 'r', |
---|
72 | POPT_ARG_NONE, |
---|
73 | &read_files, |
---|
74 | 0, |
---|
75 | "Test file reading", |
---|
76 | NULL |
---|
77 | }, |
---|
78 | { |
---|
79 | NULL, |
---|
80 | 0, |
---|
81 | 0, |
---|
82 | NULL, |
---|
83 | 0, |
---|
84 | NULL, |
---|
85 | NULL |
---|
86 | } |
---|
87 | }; |
---|
88 | |
---|
89 | static const gchar * |
---|
90 | type_to_string (GnomeVFSFileType type) |
---|
91 | { |
---|
92 | switch (type) { |
---|
93 | case GNOME_VFS_FILE_TYPE_UNKNOWN: |
---|
94 | return "Unknown"; |
---|
95 | case GNOME_VFS_FILE_TYPE_REGULAR: |
---|
96 | return "Regular"; |
---|
97 | case GNOME_VFS_FILE_TYPE_DIRECTORY: |
---|
98 | return "Directory"; |
---|
99 | case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: |
---|
100 | return "Symbolic Link"; |
---|
101 | case GNOME_VFS_FILE_TYPE_FIFO: |
---|
102 | return "FIFO"; |
---|
103 | case GNOME_VFS_FILE_TYPE_SOCKET: |
---|
104 | return "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"; |
---|
109 | default: |
---|
110 | return "???"; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | static void |
---|
115 | test_read_file_close_callback (GnomeVFSAsyncHandle *handle, |
---|
116 | GnomeVFSResult result, |
---|
117 | gpointer callback_data) |
---|
118 | { |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | static void |
---|
123 | test_read_file_succeeded (GnomeVFSAsyncHandle *handle) |
---|
124 | { |
---|
125 | gnome_vfs_async_close (handle, |
---|
126 | test_read_file_close_callback, |
---|
127 | NULL); |
---|
128 | } |
---|
129 | |
---|
130 | static void |
---|
131 | test_read_file_failed (GnomeVFSAsyncHandle *handle, GnomeVFSResult result) |
---|
132 | { |
---|
133 | gnome_vfs_async_close (handle, |
---|
134 | test_read_file_close_callback, |
---|
135 | NULL); |
---|
136 | } |
---|
137 | |
---|
138 | /* A read is complete, so we might or might not be done. */ |
---|
139 | static void |
---|
140 | test_read_file_read_callback (GnomeVFSAsyncHandle *handle, |
---|
141 | GnomeVFSResult result, |
---|
142 | gpointer buffer, |
---|
143 | GnomeVFSFileSize bytes_requested, |
---|
144 | GnomeVFSFileSize bytes_read, |
---|
145 | gpointer callback_data) |
---|
146 | { |
---|
147 | /* Check for a failure. */ |
---|
148 | if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) { |
---|
149 | test_read_file_failed (handle, result); |
---|
150 | return; |
---|
151 | } |
---|
152 | |
---|
153 | /* If at the end of the file, we win! */ |
---|
154 | test_read_file_succeeded (handle); |
---|
155 | } |
---|
156 | |
---|
157 | char buffer[256]; |
---|
158 | |
---|
159 | /* Start reading a chunk. */ |
---|
160 | static void |
---|
161 | test_read_file_read_chunk (GnomeVFSAsyncHandle *handle) |
---|
162 | { |
---|
163 | gnome_vfs_async_read (handle, |
---|
164 | buffer, |
---|
165 | 10, |
---|
166 | test_read_file_read_callback, |
---|
167 | handle); |
---|
168 | } |
---|
169 | |
---|
170 | /* Once the open is finished, read a first chunk. */ |
---|
171 | static void |
---|
172 | test_read_file_open_callback (GnomeVFSAsyncHandle *handle, |
---|
173 | GnomeVFSResult result, |
---|
174 | gpointer callback_data) |
---|
175 | { |
---|
176 | if (result != GNOME_VFS_OK) { |
---|
177 | test_read_file_failed (handle, result); |
---|
178 | return; |
---|
179 | } |
---|
180 | |
---|
181 | test_read_file_read_chunk (handle); |
---|
182 | } |
---|
183 | |
---|
184 | /* Set up the read handle and start reading. */ |
---|
185 | static GnomeVFSAsyncHandle * |
---|
186 | test_read_file_async (GnomeVFSURI *uri) |
---|
187 | { |
---|
188 | GnomeVFSAsyncHandle *result; |
---|
189 | |
---|
190 | gnome_vfs_async_open_uri (&result, |
---|
191 | uri, |
---|
192 | GNOME_VFS_OPEN_READ, |
---|
193 | 0, |
---|
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 | g_main_loop_quit (main_loop); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | int |
---|
268 | main (int argc, const char **argv) |
---|
269 | { |
---|
270 | GnomeVFSAsyncHandle *handle; |
---|
271 | poptContext popt_context; |
---|
272 | const char **args; |
---|
273 | gchar *text_uri; |
---|
274 | GTimer *timer; |
---|
275 | CallbackData callback_data; |
---|
276 | |
---|
277 | puts ("Initializing gnome-libs..."); |
---|
278 | popt_context = poptGetContext ("test-vfs", argc, argv, |
---|
279 | options, 0); |
---|
280 | |
---|
281 | while (poptGetNextOpt (popt_context) != -1) |
---|
282 | ; |
---|
283 | |
---|
284 | args = poptGetArgs (popt_context); |
---|
285 | if (args == NULL || args[1] != NULL) { |
---|
286 | fprintf (stderr, "Usage: %s [<options>] <uri>\n", argv[0]); |
---|
287 | return 1; |
---|
288 | } |
---|
289 | |
---|
290 | text_uri = g_strdup (args[0]); |
---|
291 | poptFreeContext (popt_context); |
---|
292 | |
---|
293 | puts ("Initializing gnome-vfs..."); |
---|
294 | gnome_vfs_init (); |
---|
295 | |
---|
296 | printf ("%d item(s) per notification\n", items_per_notification); |
---|
297 | |
---|
298 | if (measure_speed) { |
---|
299 | timer = g_timer_new (); |
---|
300 | g_timer_start (timer); |
---|
301 | } else { |
---|
302 | timer = NULL; |
---|
303 | } |
---|
304 | |
---|
305 | callback_data.num_entries_read = 0; |
---|
306 | callback_data.parent_uri = text_uri; |
---|
307 | async_task_counter = 1; |
---|
308 | gnome_vfs_async_load_directory |
---|
309 | (&handle, |
---|
310 | text_uri, |
---|
311 | (GNOME_VFS_FILE_INFO_GET_MIME_TYPE | GNOME_VFS_FILE_INFO_FOLLOW_LINKS), |
---|
312 | items_per_notification, |
---|
313 | 0, |
---|
314 | directory_load_callback, |
---|
315 | &callback_data); |
---|
316 | |
---|
317 | if (!measure_speed) |
---|
318 | puts ("main loop running."); |
---|
319 | |
---|
320 | main_loop = g_main_loop_new (NULL, TRUE); |
---|
321 | g_main_loop_run (main_loop); |
---|
322 | g_main_loop_unref (main_loop); |
---|
323 | |
---|
324 | if (measure_speed) { |
---|
325 | gdouble elapsed_seconds; |
---|
326 | |
---|
327 | g_timer_stop (timer); |
---|
328 | elapsed_seconds = g_timer_elapsed (timer, NULL); |
---|
329 | printf ("%.5f seconds for %d entries, %.5f entries/sec.\n", |
---|
330 | elapsed_seconds, callback_data.num_entries_read, |
---|
331 | (double) callback_data.num_entries_read / elapsed_seconds); |
---|
332 | } |
---|
333 | |
---|
334 | if (!measure_speed) |
---|
335 | puts ("GTK+ main loop finished."); fflush (stdout); |
---|
336 | |
---|
337 | puts ("All done"); |
---|
338 | |
---|
339 | gnome_vfs_shutdown (); |
---|
340 | |
---|
341 | return 0; |
---|
342 | } |
---|