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 | #include <config.h> |
---|
23 | |
---|
24 | #include <glib/gmain.h> |
---|
25 | #include <libgnomevfs/gnome-vfs-async-ops.h> |
---|
26 | #include <libgnomevfs/gnome-vfs-init.h> |
---|
27 | #include <libgnomevfs/gnome-vfs-ops.h> |
---|
28 | #include <popt.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | static GMainLoop *main_loop; |
---|
33 | |
---|
34 | typedef struct { |
---|
35 | GnomeVFSResult expected_result; |
---|
36 | const char *uri; |
---|
37 | const char *target_uri; |
---|
38 | const char *target_reference; |
---|
39 | } CallbackData; |
---|
40 | |
---|
41 | static int measure_speed = 0; |
---|
42 | static int sort = 0; |
---|
43 | static int items_per_notification = 1; |
---|
44 | |
---|
45 | struct poptOption options[] = { |
---|
46 | { |
---|
47 | "chunk-size", |
---|
48 | 'c', |
---|
49 | POPT_ARG_INT, |
---|
50 | &items_per_notification, |
---|
51 | 0, |
---|
52 | "Number of items to send for every notification", |
---|
53 | "NUM_ITEMS" |
---|
54 | }, |
---|
55 | { |
---|
56 | "measure-speed", |
---|
57 | 'm', |
---|
58 | POPT_ARG_NONE, |
---|
59 | &measure_speed, |
---|
60 | 0, |
---|
61 | "Measure speed without displaying anything", |
---|
62 | NULL |
---|
63 | }, |
---|
64 | { |
---|
65 | "sort", |
---|
66 | 's', |
---|
67 | POPT_ARG_NONE, |
---|
68 | &sort, |
---|
69 | 0, |
---|
70 | "Sort entries", |
---|
71 | NULL |
---|
72 | }, |
---|
73 | { |
---|
74 | NULL, |
---|
75 | 0, |
---|
76 | 0, |
---|
77 | NULL, |
---|
78 | 0, |
---|
79 | NULL, |
---|
80 | NULL |
---|
81 | } |
---|
82 | }; |
---|
83 | |
---|
84 | static int |
---|
85 | deal_with_result (GnomeVFSResult result, GnomeVFSResult expected_result, |
---|
86 | const char *uri, const char *target_uri, const char *target_reference, |
---|
87 | gboolean unlink) |
---|
88 | { |
---|
89 | char read_buffer[1024]; |
---|
90 | const char *write_buffer = "this is test data...we should read the same thing"; |
---|
91 | GnomeVFSHandle *handle; |
---|
92 | GnomeVFSFileSize bytes_written, temp; |
---|
93 | GnomeVFSFileInfo info_uri, info_target; |
---|
94 | int return_value = 1; |
---|
95 | const gchar *result_string; |
---|
96 | GnomeVFSResult error; |
---|
97 | GnomeVFSURI *real_uri, *real_uri_target; |
---|
98 | GnomeVFSFileInfo *info; |
---|
99 | |
---|
100 | real_uri = gnome_vfs_uri_new (uri); |
---|
101 | real_uri_target = gnome_vfs_uri_new (target_uri); |
---|
102 | |
---|
103 | if (result != expected_result) { |
---|
104 | result_string = gnome_vfs_result_to_string (result); |
---|
105 | printf ("creating a link from %s to %s returned %s instead of %s.\n", uri, target_reference, |
---|
106 | result_string, gnome_vfs_result_to_string (expected_result)); |
---|
107 | return_value = 0; |
---|
108 | } else if (result == GNOME_VFS_OK) { |
---|
109 | info = gnome_vfs_file_info_new(); |
---|
110 | error = gnome_vfs_get_file_info_uri (real_uri, info, GNOME_VFS_FILE_INFO_DEFAULT); |
---|
111 | if ((error != GNOME_VFS_OK) || (info->type != GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK)) { |
---|
112 | printf ("Symlink problem: gnome_vfs_file_info returns wrong for link %s\n", uri); |
---|
113 | } else { |
---|
114 | /* our link seems to have been created correctly - lets see if its real */ |
---|
115 | error = gnome_vfs_open_uri (&handle, real_uri_target, GNOME_VFS_OPEN_WRITE); |
---|
116 | if (error == GNOME_VFS_ERROR_NOT_FOUND) |
---|
117 | error = gnome_vfs_create_uri (&handle, real_uri_target, GNOME_VFS_OPEN_WRITE, 0, GNOME_VFS_PERM_USER_ALL); |
---|
118 | if (error == GNOME_VFS_OK) { |
---|
119 | /* write stuff to our link location */ |
---|
120 | error = gnome_vfs_write (handle, write_buffer, strlen (write_buffer) + 1, &bytes_written); |
---|
121 | error = gnome_vfs_close (handle); |
---|
122 | error = gnome_vfs_open_uri (&handle, real_uri, GNOME_VFS_OPEN_READ); |
---|
123 | if (error == GNOME_VFS_OK) { |
---|
124 | error = gnome_vfs_read (handle, read_buffer, bytes_written, &temp); |
---|
125 | read_buffer[temp] = 0; |
---|
126 | error = gnome_vfs_close (handle); |
---|
127 | if (strcmp (read_buffer, write_buffer) != 0) { |
---|
128 | printf ("Symlink problem: value written is not the same as the value read!\n"); |
---|
129 | printf ("Written to %s: #%s# \nRead from link %s: #%s#\n", |
---|
130 | target_uri, write_buffer, uri, read_buffer); |
---|
131 | return_value = 0; |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | gnome_vfs_get_file_info_uri (real_uri, &info_uri, GNOME_VFS_FILE_INFO_FOLLOW_LINKS); |
---|
136 | gnome_vfs_get_file_info_uri (real_uri_target, &info_target, GNOME_VFS_FILE_INFO_FOLLOW_LINKS); |
---|
137 | if (info_uri.inode != info_target.inode) { |
---|
138 | printf ("Symlink problem: link following is not working\n"); |
---|
139 | printf ("File: %s Link: %s\n", target_uri, uri); |
---|
140 | } |
---|
141 | gnome_vfs_get_file_info_uri (real_uri, &info_uri, GNOME_VFS_FILE_INFO_DEFAULT); |
---|
142 | gnome_vfs_get_file_info_uri (real_uri_target, &info_target, GNOME_VFS_FILE_INFO_DEFAULT); |
---|
143 | if (info_uri.inode == info_target.inode) { |
---|
144 | printf ("Symlink problem: link following is happening when it shouldn't be.\n"); |
---|
145 | printf ("File: %s Link: %s\n", target_uri, uri); |
---|
146 | } |
---|
147 | } |
---|
148 | gnome_vfs_file_info_unref (info); |
---|
149 | if (unlink) { |
---|
150 | gnome_vfs_unlink_from_uri (real_uri_target); |
---|
151 | error = gnome_vfs_unlink_from_uri (real_uri); |
---|
152 | if (error != GNOME_VFS_OK) { |
---|
153 | printf ("Problem unlinking URI %s", uri); |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | gnome_vfs_uri_unref (real_uri); |
---|
160 | gnome_vfs_uri_unref (real_uri_target); |
---|
161 | |
---|
162 | return return_value; |
---|
163 | } |
---|
164 | |
---|
165 | static void |
---|
166 | create_link_callback (GnomeVFSAsyncHandle *handle, |
---|
167 | GnomeVFSResult result, |
---|
168 | gpointer callback_data) |
---|
169 | { |
---|
170 | const char *uri, *target_uri, *target_reference; |
---|
171 | GnomeVFSResult expected_result; |
---|
172 | CallbackData *info; |
---|
173 | |
---|
174 | info = (CallbackData*) callback_data; |
---|
175 | |
---|
176 | uri = info->uri; |
---|
177 | target_uri = info->target_uri; |
---|
178 | expected_result = info->expected_result; |
---|
179 | target_reference = info->target_reference; |
---|
180 | |
---|
181 | deal_with_result (result, expected_result, uri, target_uri, target_reference, TRUE); |
---|
182 | |
---|
183 | g_free (callback_data); |
---|
184 | g_main_loop_quit (main_loop); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | static int |
---|
189 | make_link (const char *uri, const char *target_reference, const char *target_uri, GnomeVFSResult expected_result, gboolean unlink) |
---|
190 | { |
---|
191 | GnomeVFSURI *real_uri, *real_uri_target; |
---|
192 | GnomeVFSResult result; |
---|
193 | |
---|
194 | int return_value = 1; |
---|
195 | |
---|
196 | real_uri = gnome_vfs_uri_new (uri); |
---|
197 | real_uri_target = gnome_vfs_uri_new (target_uri); |
---|
198 | |
---|
199 | result = gnome_vfs_create_symbolic_link (real_uri, target_reference); |
---|
200 | |
---|
201 | return_value = deal_with_result(result, expected_result, uri, target_uri, target_reference, unlink); |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | gnome_vfs_uri_unref (real_uri); |
---|
206 | gnome_vfs_uri_unref (real_uri_target); |
---|
207 | |
---|
208 | return return_value; |
---|
209 | } |
---|
210 | |
---|
211 | static void |
---|
212 | make_link_async (const char *uri, const char *target_reference, const char *target_uri, GnomeVFSResult expected_result) |
---|
213 | { |
---|
214 | CallbackData *info; |
---|
215 | GnomeVFSAsyncHandle *handle; |
---|
216 | |
---|
217 | info = g_malloc (sizeof (CallbackData)); |
---|
218 | info->uri = uri; |
---|
219 | info->target_uri = target_uri; |
---|
220 | info->expected_result = expected_result; |
---|
221 | info->target_reference = target_reference; |
---|
222 | |
---|
223 | gnome_vfs_async_create_symbolic_link (&handle, gnome_vfs_uri_new(uri), target_reference, 0, create_link_callback, info); |
---|
224 | } |
---|
225 | |
---|
226 | static void |
---|
227 | check_broken_links (const char *uri) |
---|
228 | { |
---|
229 | GnomeVFSHandle *handle; |
---|
230 | GnomeVFSResult error; |
---|
231 | GnomeVFSURI *real_uri, *real_uri_target; |
---|
232 | |
---|
233 | real_uri = gnome_vfs_uri_new (uri); |
---|
234 | real_uri_target = gnome_vfs_uri_new ("file:///tmp/deadlink"); |
---|
235 | |
---|
236 | gnome_vfs_unlink_from_uri (real_uri_target); |
---|
237 | gnome_vfs_create_symbolic_link (real_uri, "deadlink"); |
---|
238 | |
---|
239 | error = gnome_vfs_open_uri (&handle, real_uri, GNOME_VFS_OPEN_READ); |
---|
240 | if (error != GNOME_VFS_ERROR_NOT_FOUND) { |
---|
241 | printf ("GNOME_VFS_BROKEN_SYMLINK not returned open attempting to open a broken symlink.\n"); |
---|
242 | printf ("Value returned: %d\n", error); |
---|
243 | } |
---|
244 | |
---|
245 | gnome_vfs_unlink_from_uri (real_uri); |
---|
246 | gnome_vfs_unlink_from_uri (real_uri_target); |
---|
247 | |
---|
248 | gnome_vfs_uri_unref (real_uri); |
---|
249 | gnome_vfs_uri_unref (real_uri_target); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | int |
---|
254 | main (int argc, const char **argv) |
---|
255 | { |
---|
256 | GnomeVFSURI *directory, *file_to_delete; |
---|
257 | |
---|
258 | poptContext popt_context; |
---|
259 | |
---|
260 | popt_context = poptGetContext ("test-vfs", argc, argv, |
---|
261 | options, 0); |
---|
262 | |
---|
263 | if (argc != 2) { |
---|
264 | fprintf (stderr, "Usage: %s <directory>\n", argv[0]); |
---|
265 | return 1; |
---|
266 | } |
---|
267 | |
---|
268 | gnome_vfs_init (); |
---|
269 | directory = gnome_vfs_uri_new ("file:///tmp/tmp"); |
---|
270 | |
---|
271 | gnome_vfs_make_directory_for_uri (directory, GNOME_VFS_PERM_USER_ALL); |
---|
272 | |
---|
273 | make_link ("file:///tmp/link_to_ditz", "file:///tmp/ditz", "file:///tmp/ditz", GNOME_VFS_OK, TRUE); |
---|
274 | make_link ("file:///tmp/link_to_ditz_relative", "ditz", "file:///tmp/ditz", GNOME_VFS_OK, TRUE); |
---|
275 | make_link ("file:///tmp/tmp/link_to_ditz", "../ditz", "file:///tmp/ditz", GNOME_VFS_OK, FALSE); |
---|
276 | make_link ("file:///tmp/link_to_link", "tmp/link_to_ditz", "file:///tmp/tmp/link_to_ditz", GNOME_VFS_OK, TRUE); |
---|
277 | |
---|
278 | gnome_vfs_remove_directory_from_uri (directory); |
---|
279 | gnome_vfs_uri_unref (directory); |
---|
280 | |
---|
281 | file_to_delete = gnome_vfs_uri_new ("file:///tmp/ditz"); |
---|
282 | gnome_vfs_unlink_from_uri (file_to_delete); |
---|
283 | gnome_vfs_uri_unref (file_to_delete); |
---|
284 | |
---|
285 | check_broken_links("file:///tmp/link"); |
---|
286 | |
---|
287 | make_link ("file:///tmp/link_to_ditz_offfs", "http://www.a.com/ditz", "http://www.a.com/ditz", GNOME_VFS_ERROR_NOT_SUPPORTED, TRUE); |
---|
288 | make_link ("http://www.eazel.com/link_to_ditz", "file:///tmp/ditz", "file:///tmp/ditz", GNOME_VFS_ERROR_NOT_SUPPORTED, TRUE); |
---|
289 | make_link ("http://www.a.com/link_to_ditz_relative", "ditz", "http://www.a.com/ditz", GNOME_VFS_ERROR_NOT_SUPPORTED, TRUE); |
---|
290 | |
---|
291 | make_link_async ("file:///tmp/async_link", "file:///tmp/link", "file:///tmp/link", GNOME_VFS_OK); |
---|
292 | |
---|
293 | main_loop = g_main_loop_new (NULL, TRUE); |
---|
294 | g_main_loop_run (main_loop); |
---|
295 | g_main_loop_unref (main_loop); |
---|
296 | |
---|
297 | return 0; |
---|
298 | } |
---|