ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / test / test-directory.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-directory.c - Test program for directory reading in the GNOME
3    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 <libgnomevfs/gnome-vfs-directory.h>
29 #include <libgnomevfs/gnome-vfs-init.h>
30 #include <popt.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 static int measure_speed = 0;
35
36 struct poptOption options[] = {
37         {
38                 "measure-speed",
39                 'm',
40                 POPT_ARG_NONE,
41                 &measure_speed,
42                 0,
43                 "Measure speed without displaying anything",
44                 NULL
45         },
46         {
47                 NULL,
48                 0,
49                 0,
50                 NULL,
51                 0,
52                 NULL,
53                 NULL
54         }
55 };
56
57
58 static void
59 show_result (GnomeVFSResult result, const gchar *what, const gchar *text_uri)
60 {
61         fprintf (stderr, "%s `%s': %s\n",
62                  what, text_uri, gnome_vfs_result_to_string (result));
63         if (result != GNOME_VFS_OK)
64         {
65                 fprintf (stdout, "Error: %s\n",
66                                 gnome_vfs_result_to_string (result));
67                 exit (1);
68         }
69 }
70
71 static const gchar *
72 type_to_string (GnomeVFSFileType type)
73 {
74         switch (type) {
75         case GNOME_VFS_FILE_TYPE_UNKNOWN:
76                 return "Unknown";
77         case GNOME_VFS_FILE_TYPE_REGULAR:
78                 return "Regular";
79         case GNOME_VFS_FILE_TYPE_DIRECTORY:
80                 return "Directory";
81         case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
82                 return "Symbolic Link";
83         case GNOME_VFS_FILE_TYPE_FIFO:
84                 return "FIFO";
85         case GNOME_VFS_FILE_TYPE_SOCKET:
86                 return "Socket";
87         case GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE:
88                 return "Character device";
89         case GNOME_VFS_FILE_TYPE_BLOCK_DEVICE:
90                 return "Block device";
91         default:
92                 return "???";
93         }
94 }
95
96 static void
97 print_list (GList *list)
98 {
99         GnomeVFSFileInfo *info;
100         GList *node;
101
102         if (list == NULL) {
103                 printf ("  (No files)\n");
104                 return;
105         }
106
107         for (node = list; node != NULL; node = node->next) {
108                 const gchar *mime_type;
109         
110                 info = node->data;
111
112                 mime_type = gnome_vfs_file_info_get_mime_type (info);
113                 if (mime_type == NULL)
114                         mime_type = "(Unknown)";
115
116                 printf ("  File `%s'%s%s%s (%s, %s), size %ld, mode %04o\n",
117                         info->name,
118                         GNOME_VFS_FILE_INFO_SYMLINK (info) ? " [link: " : "",
119                         GNOME_VFS_FILE_INFO_SYMLINK (info) ? info->symlink_name : "",
120                         GNOME_VFS_FILE_INFO_SYMLINK (info) ? " ]" : "",
121                         type_to_string (info->type),
122                         mime_type,
123                         (glong) info->size,
124                         info->permissions);
125         }
126 }
127
128 int
129 main (int argc, const char **argv)
130 {
131         GList *list;
132         GnomeVFSResult result;
133         GTimer *timer;
134         const char **args;
135         gchar *text_uri;
136         poptContext popt_context;
137
138         popt_context = poptGetContext ("test-directory", argc, 
139                                        (const char **) argv, options, 0);
140
141         while (poptGetNextOpt (popt_context) != -1)
142                 ;
143
144         args = poptGetArgs (popt_context);
145         if (args == NULL || args[1] != NULL) {
146                 fprintf (stderr, "Usage: %s [<options>] <uri>\n", argv[0]);
147                 return 1;
148         }
149
150         text_uri = g_strdup (args[0]);
151
152         poptFreeContext (popt_context);
153
154         gnome_vfs_init ();
155
156         printf ("Loading directory...");
157         fflush (stdout);
158
159         if (measure_speed) {
160                 timer = g_timer_new ();
161                 g_timer_start (timer);
162         } else {
163                 timer = NULL;
164         }
165
166         /* Load with without requesting any metadata.  */
167         result = gnome_vfs_directory_list_load
168                 (&list, text_uri,
169                  (GNOME_VFS_FILE_INFO_GET_MIME_TYPE
170                   | GNOME_VFS_FILE_INFO_FORCE_FAST_MIME_TYPE
171                   | GNOME_VFS_FILE_INFO_FOLLOW_LINKS));
172
173         if (result == GNOME_VFS_OK && measure_speed) {
174                 gdouble elapsed_seconds;
175                 guint num_entries;
176
177                 g_timer_stop (timer);
178                 elapsed_seconds = g_timer_elapsed (timer, NULL);
179                 num_entries = g_list_length (list);
180                 printf ("\n%.5f seconds for %d unsorted entries, %.5f entries/sec.\n",
181                         elapsed_seconds, num_entries,
182                         (double) num_entries / elapsed_seconds);
183         }
184
185         if (!measure_speed) {
186                 printf ("Ok\n");
187
188                 show_result (result, "load_directory", text_uri);
189
190                 printf ("Listing for `%s':\n", text_uri);
191                 print_list (list);
192         }
193
194         printf ("Destroying.\n");
195         gnome_vfs_file_info_list_free (list);
196
197         printf ("Done.\n");
198
199         g_free (text_uri);
200
201         return 0;
202 }