1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* test-symlinks.c: verifies that symlinks are being created properly |
---|
3 | Copyright (C) 2000 Eazel |
---|
4 | |
---|
5 | The Gnome Library is free software; you can redistribute it and/or |
---|
6 | modify it under the terms of the GNU Library General Public License as |
---|
7 | published by the Free Software Foundation; either version 2 of the |
---|
8 | License, or (at your option) any later version. |
---|
9 | |
---|
10 | The Gnome Library is distributed in the hope that it will be useful, |
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | Library General Public License for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU Library General Public |
---|
16 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
18 | Boston, MA 02111-1307, USA. |
---|
19 | |
---|
20 | Author: Seth Nickell <snickell@stanford.edu> */ |
---|
21 | |
---|
22 | #ifdef HAVE_CONFIG_H |
---|
23 | #include <config.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <gnome.h> |
---|
28 | |
---|
29 | #include <orb/orbit.h> |
---|
30 | #include <libgnorba/gnorba.h> |
---|
31 | |
---|
32 | #include "gnome-vfs.h" |
---|
33 | |
---|
34 | #define WITH_PTHREAD |
---|
35 | |
---|
36 | typedef struct { |
---|
37 | GnomeVFSResult expected_result; |
---|
38 | const char *uri; |
---|
39 | const char *target_uri; |
---|
40 | const char *target_reference; |
---|
41 | } CallbackData; |
---|
42 | |
---|
43 | static int measure_speed = 0; |
---|
44 | static int sort = 0; |
---|
45 | static int items_per_notification = 1; |
---|
46 | |
---|
47 | struct poptOption options[] = { |
---|
48 | { |
---|
49 | "chunk-size", |
---|
50 | 'c', |
---|
51 | POPT_ARG_INT, |
---|
52 | &items_per_notification, |
---|
53 | 0, |
---|
54 | "Number of items to send for every notification", |
---|
55 | "NUM_ITEMS" |
---|
56 | }, |
---|
57 | { |
---|
58 | "measure-speed", |
---|
59 | 'm', |
---|
60 | POPT_ARG_NONE, |
---|
61 | &measure_speed, |
---|
62 | 0, |
---|
63 | "Measure speed without displaying anything", |
---|
64 | NULL |
---|
65 | }, |
---|
66 | { |
---|
67 | "sort", |
---|
68 | 's', |
---|
69 | POPT_ARG_NONE, |
---|
70 | &sort, |
---|
71 | 0, |
---|
72 | "Sort entries", |
---|
73 | NULL |
---|
74 | }, |
---|
75 | { |
---|
76 | NULL, |
---|
77 | 0, |
---|
78 | 0, |
---|
79 | NULL, |
---|
80 | 0, |
---|
81 | NULL, |
---|
82 | NULL |
---|
83 | } |
---|
84 | }; |
---|
85 | |
---|
86 | static int |
---|
87 | deal_with_result (GnomeVFSResult result, GnomeVFSResult expected_result, |
---|
88 | const char *uri, const char *target_uri, const char *target_reference, |
---|
89 | gboolean unlink) |
---|
90 | { |
---|
91 | char read_buffer[1024]; |
---|
92 | const char *write_buffer = "this is test data...we should read the same thing"; |
---|
93 | GnomeVFSHandle *handle; |
---|
94 | GnomeVFSFileSize bytes_written, temp; |
---|
95 | GnomeVFSFileInfo info_uri, info_target; |
---|
96 | int return_value = 1; |
---|
97 | const gchar *result_string; |
---|
98 | GnomeVFSResult error; |
---|
99 | GnomeVFSURI *real_uri, *real_uri_target; |
---|
100 | GnomeVFSFileInfo *info; |
---|
101 | |
---|
102 | real_uri = gnome_vfs_uri_new (uri); |
---|
103 | real_uri_target = gnome_vfs_uri_new (target_uri); |
---|
104 | |
---|
105 | if (result != expected_result) { |
---|
106 | result_string = gnome_vfs_result_to_string (result); |
---|
107 | printf ("creating a link from %s to %s returned %s instead of %s.\n", uri, target_reference, |
---|
108 | result_string, gnome_vfs_result_to_string (expected_result)); |
---|
109 | return_value = 0; |
---|
110 | } else if (result == GNOME_VFS_OK) { |
---|
111 | info = gnome_vfs_file_info_new(); |
---|
112 | error = gnome_vfs_get_file_info_uri (real_uri, info, GNOME_VFS_FILE_INFO_DEFAULT); |
---|
113 | if ((error != GNOME_VFS_OK) || (info->type != GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK)) { |
---|
114 | printf ("Symlink problem: gnome_vfs_file_info returns wrong for link %s\n", uri); |
---|
115 | } else { |
---|
116 | /* our link seems to have been created correctly - lets see if its real */ |
---|
117 | error = gnome_vfs_open_uri (&handle, real_uri_target, GNOME_VFS_OPEN_WRITE); |
---|
118 | if (error == GNOME_VFS_ERROR_NOT_FOUND) |
---|
119 | error = gnome_vfs_create_uri (&handle, real_uri_target, GNOME_VFS_OPEN_WRITE, 0, GNOME_VFS_PERM_USER_ALL); |
---|
120 | if (error == GNOME_VFS_OK) { |
---|
121 | /* write stuff to our link location */ |
---|
122 | error = gnome_vfs_write (handle, write_buffer, strlen (write_buffer) + 1, &bytes_written); |
---|
123 | error = gnome_vfs_close (handle); |
---|
124 | error = gnome_vfs_open_uri (&handle, real_uri, GNOME_VFS_OPEN_READ); |
---|
125 | if (error == GNOME_VFS_OK) { |
---|
126 | error = gnome_vfs_read (handle, read_buffer, bytes_written, &temp); |
---|
127 | read_buffer[temp] = 0; |
---|
128 | error = gnome_vfs_close (handle); |
---|
129 | if (strcmp (read_buffer, write_buffer) != 0) { |
---|
130 | printf ("Symlink problem: value written is not the same as the value read!\n"); |
---|
131 | printf ("Written to %s: #%s# \nRead from link %s: #%s#\n", |
---|
132 | target_uri, write_buffer, uri, read_buffer); |
---|
133 | return_value = 0; |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | gnome_vfs_get_file_info_uri (real_uri, &info_uri, GNOME_VFS_FILE_INFO_FOLLOW_LINKS); |
---|
138 | gnome_vfs_get_file_info_uri (real_uri_target, &info_target, GNOME_VFS_FILE_INFO_FOLLOW_LINKS); |
---|
139 | if (info_uri.inode != info_target.inode) { |
---|
140 | printf ("Symlink problem: link following is not working\n"); |
---|
141 | printf ("File: %s Link: %s\n", target_uri, uri); |
---|
142 | } |
---|
143 | gnome_vfs_get_file_info_uri (real_uri, &info_uri, GNOME_VFS_FILE_INFO_DEFAULT); |
---|
144 | gnome_vfs_get_file_info_uri (real_uri_target, &info_target, GNOME_VFS_FILE_INFO_DEFAULT); |
---|
145 | if (info_uri.inode == info_target.inode) { |
---|
146 | printf ("Symlink problem: link following is happening when it shouldn't be.\n"); |
---|
147 | printf ("File: %s Link: %s\n", target_uri, uri); |
---|
148 | } |
---|
149 | } |
---|
150 | gnome_vfs_file_info_unref (info); |
---|
151 | if (unlink) { |
---|
152 | gnome_vfs_unlink_from_uri (real_uri_target); |
---|
153 | error = gnome_vfs_unlink_from_uri (real_uri); |
---|
154 | if (error != GNOME_VFS_OK) { |
---|
155 | printf ("Problem unlinking URI %s", uri); |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | gnome_vfs_uri_unref (real_uri); |
---|
162 | gnome_vfs_uri_unref (real_uri_target); |
---|
163 | |
---|
164 | return return_value; |
---|
165 | } |
---|
166 | |
---|
167 | static void |
---|
168 | create_link_callback (GnomeVFSAsyncHandle *handle, |
---|
169 | GnomeVFSResult result, |
---|
170 | gpointer callback_data) |
---|
171 | { |
---|
172 | const char *uri, *target_uri, *target_reference; |
---|
173 | GnomeVFSResult expected_result; |
---|
174 | CallbackData *info; |
---|
175 | |
---|
176 | info = (CallbackData*) callback_data; |
---|
177 | |
---|
178 | uri = info->uri; |
---|
179 | target_uri = info->target_uri; |
---|
180 | expected_result = info->expected_result; |
---|
181 | target_reference = info->target_reference; |
---|
182 | |
---|
183 | deal_with_result (result, expected_result, uri, target_uri, target_reference, TRUE); |
---|
184 | |
---|
185 | g_free (callback_data); |
---|
186 | gtk_main_quit (); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | static int |
---|
191 | make_link (const char *uri, const char *target_reference, const char *target_uri, GnomeVFSResult expected_result, gboolean unlink) |
---|
192 | { |
---|
193 | GnomeVFSURI *real_uri, *real_uri_target; |
---|
194 | GnomeVFSResult result; |
---|
195 | |
---|
196 | int return_value = 1; |
---|
197 | |
---|
198 | real_uri = gnome_vfs_uri_new (uri); |
---|
199 | real_uri_target = gnome_vfs_uri_new (target_uri); |
---|
200 | |
---|
201 | result = gnome_vfs_create_symbolic_link (real_uri, target_reference); |
---|
202 | |
---|
203 | return_value = deal_with_result(result, expected_result, uri, target_uri, target_reference, unlink); |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | gnome_vfs_uri_unref (real_uri); |
---|
208 | gnome_vfs_uri_unref (real_uri_target); |
---|
209 | |
---|
210 | return return_value; |
---|
211 | } |
---|
212 | |
---|
213 | static void |
---|
214 | make_link_async (const char *uri, const char *target_reference, const char *target_uri, GnomeVFSResult expected_result) |
---|
215 | { |
---|
216 | CallbackData *info; |
---|
217 | GnomeVFSAsyncHandle *handle; |
---|
218 | |
---|
219 | info = g_malloc (sizeof (CallbackData)); |
---|
220 | info->uri = uri; |
---|
221 | info->target_uri = target_uri; |
---|
222 | info->expected_result = expected_result; |
---|
223 | info->target_reference = target_reference; |
---|
224 | |
---|
225 | gnome_vfs_async_create_symbolic_link (&handle, gnome_vfs_uri_new(uri), target_reference, create_link_callback, info); |
---|
226 | } |
---|
227 | |
---|
228 | static void |
---|
229 | check_broken_links (const char *uri) |
---|
230 | { |
---|
231 | GnomeVFSHandle *handle; |
---|
232 | GnomeVFSResult error; |
---|
233 | GnomeVFSURI *real_uri, *real_uri_target; |
---|
234 | |
---|
235 | real_uri = gnome_vfs_uri_new (uri); |
---|
236 | real_uri_target = gnome_vfs_uri_new ("file:///tmp/deadlink"); |
---|
237 | |
---|
238 | gnome_vfs_unlink_from_uri (real_uri_target); |
---|
239 | gnome_vfs_create_symbolic_link (real_uri, "deadlink"); |
---|
240 | |
---|
241 | error = gnome_vfs_open_uri (&handle, real_uri, GNOME_VFS_OPEN_READ); |
---|
242 | if (error != GNOME_VFS_ERROR_NOT_FOUND) { |
---|
243 | printf ("GNOME_VFS_BROKEN_SYMLINK not returned open attempting to open a broken symlink.\n"); |
---|
244 | printf ("Value returned: %d\n", error); |
---|
245 | } |
---|
246 | |
---|
247 | gnome_vfs_unlink_from_uri (real_uri); |
---|
248 | gnome_vfs_unlink_from_uri (real_uri_target); |
---|
249 | |
---|
250 | gnome_vfs_uri_unref (real_uri); |
---|
251 | gnome_vfs_uri_unref (real_uri_target); |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | int |
---|
256 | main (int argc, char **argv) |
---|
257 | { |
---|
258 | GnomeVFSURI *directory, *file_to_delete; |
---|
259 | |
---|
260 | poptContext popt_context; |
---|
261 | #ifdef WITH_CORBA |
---|
262 | CORBA_Environment ev; |
---|
263 | #endif |
---|
264 | |
---|
265 | #ifdef WITH_PTHREAD |
---|
266 | g_thread_init (NULL); |
---|
267 | #endif |
---|
268 | |
---|
269 | #ifdef WITH_CORBA |
---|
270 | CORBA_exception_init (&ev); |
---|
271 | gnome_CORBA_init_with_popt_table ("test-vfs", "0.0", &argc, argv, |
---|
272 | options, 0, &popt_context, 0, &ev); |
---|
273 | #else |
---|
274 | gnome_init_with_popt_table ("test-vfs", "0.0", argc, argv, |
---|
275 | options, 0, &popt_context); |
---|
276 | #endif |
---|
277 | |
---|
278 | |
---|
279 | if (argc != 2) { |
---|
280 | fprintf (stderr, "Usage: %s <directory>\n", argv[0]); |
---|
281 | return 1; |
---|
282 | } |
---|
283 | |
---|
284 | gnome_vfs_init (); |
---|
285 | directory = gnome_vfs_uri_new ("file:///tmp/tmp"); |
---|
286 | |
---|
287 | gnome_vfs_make_directory_for_uri (directory, GNOME_VFS_PERM_USER_ALL); |
---|
288 | |
---|
289 | make_link ("file:///tmp/link_to_ditz", "file:///tmp/ditz", "file:///tmp/ditz", GNOME_VFS_OK, TRUE); |
---|
290 | make_link ("file:///tmp/link_to_ditz_relative", "ditz", "file:///tmp/ditz", GNOME_VFS_OK, TRUE); |
---|
291 | make_link ("file:///tmp/tmp/link_to_ditz", "../ditz", "file:///tmp/ditz", GNOME_VFS_OK, FALSE); |
---|
292 | make_link ("file:///tmp/link_to_link", "tmp/link_to_ditz", "file:///tmp/tmp/link_to_ditz", GNOME_VFS_OK, TRUE); |
---|
293 | |
---|
294 | gnome_vfs_remove_directory_from_uri (directory); |
---|
295 | gnome_vfs_uri_unref (directory); |
---|
296 | |
---|
297 | file_to_delete = gnome_vfs_uri_new ("file:///tmp/ditz"); |
---|
298 | gnome_vfs_unlink_from_uri (file_to_delete); |
---|
299 | gnome_vfs_uri_unref (file_to_delete); |
---|
300 | |
---|
301 | check_broken_links("file:///tmp/link"); |
---|
302 | |
---|
303 | make_link ("file:///tmp/link_to_ditz_offfs", "http://www.a.com/ditz", "http://www.a.com/ditz", GNOME_VFS_ERROR_NOT_SUPPORTED, TRUE); |
---|
304 | make_link ("http://www.eazel.com/link_to_ditz", "file:///tmp/ditz", "file:///tmp/ditz", GNOME_VFS_ERROR_NOT_SUPPORTED, TRUE); |
---|
305 | make_link ("http://www.a.com/link_to_ditz_relative", "ditz", "http://www.a.com/ditz", GNOME_VFS_ERROR_NOT_SUPPORTED, TRUE); |
---|
306 | |
---|
307 | make_link_async ("file:///tmp/async_link", "file:///tmp/link", "file:///tmp/link", GNOME_VFS_OK); |
---|
308 | |
---|
309 | gtk_main (); |
---|
310 | |
---|
311 | #ifdef WITH_CORBA |
---|
312 | CORBA_exception_free (&ev); |
---|
313 | #endif |
---|
314 | |
---|
315 | |
---|
316 | return 0; |
---|
317 | } |
---|