1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* test-shell.c - A small program to allow testing of a wide variety of |
---|
3 | gnome-vfs functionality |
---|
4 | |
---|
5 | Copyright (C) 2000 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: Michael Meeks <mmeeks@gnu.org> |
---|
23 | |
---|
24 | NB. This code leaks everywhere, don't loose hair. |
---|
25 | */ |
---|
26 | |
---|
27 | #ifdef HAVE_CONFIG_H |
---|
28 | #include <config.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <stdio.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <string.h> |
---|
34 | #include <ctype.h> |
---|
35 | #include <time.h> |
---|
36 | #include <errno.h> |
---|
37 | #include <unistd.h> |
---|
38 | |
---|
39 | #include <gnome.h> |
---|
40 | |
---|
41 | #include "gnome-vfs.h" |
---|
42 | |
---|
43 | #define TEST_DEBUG 0 |
---|
44 | |
---|
45 | static char delim[]=" "; |
---|
46 | static char **arg_data = NULL; |
---|
47 | static int arg_cur = 0; |
---|
48 | |
---|
49 | static char *cur_dir = NULL; |
---|
50 | |
---|
51 | static GHashTable *files = NULL; |
---|
52 | |
---|
53 | FILE *vfserr = NULL; |
---|
54 | |
---|
55 | |
---|
56 | static gboolean |
---|
57 | show_if_error (GnomeVFSResult result, const char *what, const char *what2) |
---|
58 | { |
---|
59 | if (result != GNOME_VFS_OK) { |
---|
60 | fprintf (vfserr, "%s%s `%s'\n", |
---|
61 | what, what2, gnome_vfs_result_to_string (result)); |
---|
62 | return TRUE; |
---|
63 | } else |
---|
64 | return FALSE; |
---|
65 | } |
---|
66 | |
---|
67 | static void |
---|
68 | register_file (const char *str, GnomeVFSHandle *handle) |
---|
69 | { |
---|
70 | if (!str) |
---|
71 | fprintf (vfserr, "Need a valid name"); |
---|
72 | else |
---|
73 | g_hash_table_insert (files, g_strdup (str), handle); |
---|
74 | } |
---|
75 | |
---|
76 | static GnomeVFSHandle * |
---|
77 | lookup_file (const char *str) |
---|
78 | { |
---|
79 | GnomeVFSHandle *handle; |
---|
80 | |
---|
81 | if (!str) { |
---|
82 | fprintf (vfserr, "Invalid handle '%s'\n", str); |
---|
83 | return NULL; |
---|
84 | } |
---|
85 | |
---|
86 | handle = g_hash_table_lookup (files, str); |
---|
87 | if (!handle) |
---|
88 | fprintf (vfserr, "Can't find handle '%s'\n", str); |
---|
89 | |
---|
90 | return handle; |
---|
91 | } |
---|
92 | |
---|
93 | static void |
---|
94 | close_file (const char *str) |
---|
95 | { |
---|
96 | GnomeVFSResult result; |
---|
97 | gpointer hash_key, value; |
---|
98 | |
---|
99 | if (!str) |
---|
100 | fprintf (vfserr, "Can't close NULL handles\n"); |
---|
101 | |
---|
102 | else if (g_hash_table_lookup_extended (files, str, &hash_key, &value)) { |
---|
103 | g_hash_table_remove (files, str); |
---|
104 | |
---|
105 | result = gnome_vfs_close ((GnomeVFSHandle *)value); |
---|
106 | show_if_error (result, "closing ", (char *)hash_key); |
---|
107 | |
---|
108 | g_free (hash_key); |
---|
109 | } else |
---|
110 | |
---|
111 | fprintf (vfserr, "Unknown file handle '%s'\n", str); |
---|
112 | } |
---|
113 | |
---|
114 | static gboolean |
---|
115 | kill_file_cb (gpointer key, |
---|
116 | gpointer value, |
---|
117 | gpointer user_data) |
---|
118 | { |
---|
119 | GnomeVFSResult result; |
---|
120 | |
---|
121 | result = gnome_vfs_close (value); |
---|
122 | show_if_error (result, "closing ", key); |
---|
123 | g_free (key); |
---|
124 | |
---|
125 | return TRUE; |
---|
126 | } |
---|
127 | |
---|
128 | static void |
---|
129 | close_files (void) |
---|
130 | { |
---|
131 | g_hash_table_foreach_remove (files, kill_file_cb, NULL); |
---|
132 | g_hash_table_destroy (files); |
---|
133 | } |
---|
134 | |
---|
135 | static void |
---|
136 | do_ls (void) |
---|
137 | { |
---|
138 | GnomeVFSResult result; |
---|
139 | GList *list, *node; |
---|
140 | GnomeVFSFileInfo *info; |
---|
141 | |
---|
142 | result = gnome_vfs_directory_list_load ( |
---|
143 | &list, cur_dir, GNOME_VFS_FILE_INFO_DEFAULT, |
---|
144 | NULL); |
---|
145 | if (show_if_error (result, "open directory ", cur_dir)) |
---|
146 | return; |
---|
147 | |
---|
148 | for (node = list; node != NULL; node = node->next) { |
---|
149 | char prechar = '\0', postchar = '\0'; |
---|
150 | info = node->data; |
---|
151 | |
---|
152 | if (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) { |
---|
153 | switch (info->type) { |
---|
154 | case GNOME_VFS_FILE_TYPE_DIRECTORY: |
---|
155 | prechar = '['; |
---|
156 | postchar = ']'; |
---|
157 | break; |
---|
158 | case GNOME_VFS_FILE_TYPE_UNKNOWN: |
---|
159 | prechar = '?'; |
---|
160 | break; |
---|
161 | case GNOME_VFS_FILE_TYPE_FIFO: |
---|
162 | prechar = '|'; |
---|
163 | break; |
---|
164 | case GNOME_VFS_FILE_TYPE_SOCKET: |
---|
165 | prechar = '-'; |
---|
166 | break; |
---|
167 | case GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE: |
---|
168 | case GNOME_VFS_FILE_TYPE_BLOCK_DEVICE: |
---|
169 | prechar = '@'; |
---|
170 | break; |
---|
171 | case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: |
---|
172 | prechar = '#'; |
---|
173 | break; |
---|
174 | default: |
---|
175 | prechar = '\0'; |
---|
176 | break; |
---|
177 | } |
---|
178 | if (!postchar) |
---|
179 | postchar = prechar; |
---|
180 | } |
---|
181 | printf ("%c%s%c", prechar, info->name, postchar); |
---|
182 | |
---|
183 | if (strlen (info->name) < 40) { |
---|
184 | int i, pad; |
---|
185 | |
---|
186 | pad = 40 - strlen (info->name) - |
---|
187 | (prechar?1:0) - (postchar?1:0); |
---|
188 | |
---|
189 | for (i = 0; i < pad; i++) |
---|
190 | printf (" "); |
---|
191 | } |
---|
192 | if (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) { |
---|
193 | long i = info->size; |
---|
194 | printf (" : %ld bytes", i); |
---|
195 | } |
---|
196 | printf ("\n"); |
---|
197 | } |
---|
198 | |
---|
199 | gnome_vfs_file_info_list_free (list); |
---|
200 | } |
---|
201 | |
---|
202 | static void |
---|
203 | list_commands (void) |
---|
204 | { |
---|
205 | printf ("command can be one or all of:\n"); |
---|
206 | printf ("Main operations:\n"); |
---|
207 | printf (" * ls: list files\n"); |
---|
208 | printf (" * cd: enter storage\n"); |
---|
209 | printf (" * mv: move object\n"); |
---|
210 | printf (" * rm: remove stream\n"); |
---|
211 | printf (" * mkdir: make storage\n"); |
---|
212 | printf (" * rmdir: remove storage\n"); |
---|
213 | printf (" * info,stat: get information on object\n"); |
---|
214 | printf (" * cat,type: dump text file to console\n"); |
---|
215 | printf (" * dump: dump binary file to console\n"); |
---|
216 | printf (" * sync: for sinkers\n"); |
---|
217 | printf (" * quit,exit,bye: exit\n"); |
---|
218 | printf ("File operations:\n"); |
---|
219 | printf (" * open <handle> <name>: open a file\n"); |
---|
220 | printf (" * create <handle> <name>: create a file\n"); |
---|
221 | printf (" * close <handle>: close a file\n"); |
---|
222 | printf (" * read <handle> <bytes>: read bytes from stream\n"); |
---|
223 | printf (" * seek <handle> <pos>: seek set position\n"); |
---|
224 | } |
---|
225 | |
---|
226 | static gboolean |
---|
227 | simple_regexp (const char *regexp, const char *fname) |
---|
228 | { |
---|
229 | int i, j; |
---|
230 | gboolean ret = TRUE; |
---|
231 | |
---|
232 | g_return_val_if_fail (fname != NULL, FALSE); |
---|
233 | g_return_val_if_fail (regexp != NULL, FALSE); |
---|
234 | |
---|
235 | for (i = j = 0; regexp [i] && fname [j]; j++, i++) { |
---|
236 | if (regexp [i] == '\\' && |
---|
237 | !(i > 0 && regexp [i - 1] == '\\')) { |
---|
238 | j--; |
---|
239 | continue; |
---|
240 | } |
---|
241 | |
---|
242 | if (regexp [i] == '.' && |
---|
243 | !(i > 0 && regexp [i - 1] == '\\')) |
---|
244 | continue; |
---|
245 | |
---|
246 | if (toupper (regexp [i]) != toupper (fname [j])) { |
---|
247 | ret = FALSE; |
---|
248 | break; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | if (regexp [i] && regexp [i] == '*') |
---|
253 | ret = TRUE; |
---|
254 | |
---|
255 | else if (!regexp [i] && fname [j]) |
---|
256 | ret = FALSE; |
---|
257 | |
---|
258 | else if (!fname [j] && regexp [i]) |
---|
259 | ret = FALSE; |
---|
260 | |
---|
261 | /* if (ret) |
---|
262 | printf ("'%s' matched '%s'\n", regexp, fname);*/ |
---|
263 | |
---|
264 | return ret; |
---|
265 | } |
---|
266 | |
---|
267 | static gboolean |
---|
268 | validate_path (const char *path) |
---|
269 | { |
---|
270 | GnomeVFSResult result; |
---|
271 | GList *list; |
---|
272 | |
---|
273 | result = gnome_vfs_directory_list_load ( |
---|
274 | &list, path, GNOME_VFS_FILE_INFO_DEFAULT, |
---|
275 | NULL); |
---|
276 | |
---|
277 | if (show_if_error (result, "open directory ", path)) { |
---|
278 | return FALSE; |
---|
279 | } |
---|
280 | |
---|
281 | gnome_vfs_file_info_list_free (list); |
---|
282 | |
---|
283 | return TRUE; |
---|
284 | } |
---|
285 | |
---|
286 | static char * |
---|
287 | get_regexp_name (const char *regexp, const char *path, gboolean dir) |
---|
288 | { |
---|
289 | GnomeVFSResult result; |
---|
290 | GList *list, *node; |
---|
291 | GnomeVFSFileInfo *info; |
---|
292 | char *res = NULL; |
---|
293 | |
---|
294 | result = gnome_vfs_directory_list_load ( |
---|
295 | &list, path, GNOME_VFS_FILE_INFO_DEFAULT, |
---|
296 | NULL); |
---|
297 | |
---|
298 | if (show_if_error (result, "open directory ", path)) |
---|
299 | return NULL; |
---|
300 | |
---|
301 | for (node = list; node != NULL; node = node->next) { |
---|
302 | info = node->data; |
---|
303 | |
---|
304 | if (simple_regexp (regexp, info->name)) { |
---|
305 | if (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) { |
---|
306 | if ((dir && info->type == GNOME_VFS_FILE_TYPE_DIRECTORY) || |
---|
307 | (!dir && info->type != GNOME_VFS_FILE_TYPE_DIRECTORY)) { |
---|
308 | res = g_strdup (info->name); |
---|
309 | break; |
---|
310 | } |
---|
311 | } else { |
---|
312 | fprintf (vfserr, "Can't cope with no type data"); |
---|
313 | res = g_strdup (info->name); |
---|
314 | break; |
---|
315 | } |
---|
316 | } |
---|
317 | } |
---|
318 | gnome_vfs_file_info_list_free (list); |
---|
319 | |
---|
320 | return res; |
---|
321 | } |
---|
322 | |
---|
323 | static void |
---|
324 | do_cd (void) |
---|
325 | { |
---|
326 | char *p; |
---|
327 | |
---|
328 | p = arg_data [arg_cur++]; |
---|
329 | |
---|
330 | if (!p) { |
---|
331 | fprintf (vfserr, "Takes a directory argument\n"); |
---|
332 | return; |
---|
333 | } |
---|
334 | |
---|
335 | if (!g_strcasecmp (p, "..")) { |
---|
336 | guint lp; |
---|
337 | char **tmp; |
---|
338 | GString *newp = g_string_new (""); |
---|
339 | |
---|
340 | tmp = g_strsplit (cur_dir, "/", -1); |
---|
341 | lp = 0; |
---|
342 | if (!tmp [lp]) |
---|
343 | return; |
---|
344 | |
---|
345 | while (tmp [lp + 1]) { |
---|
346 | g_string_sprintfa (newp, "%s/", tmp [lp]); |
---|
347 | lp++; |
---|
348 | } |
---|
349 | g_free (cur_dir); |
---|
350 | cur_dir = newp->str; |
---|
351 | g_string_free (newp, FALSE); |
---|
352 | } else if (!g_strcasecmp (p, ".")) { |
---|
353 | } else { |
---|
354 | char *newpath; |
---|
355 | |
---|
356 | if (strchr (p, ':') || |
---|
357 | p [0] == '/') { |
---|
358 | if (p [strlen (p) - 1] != '/') |
---|
359 | newpath = g_strconcat (p, "/", NULL); |
---|
360 | else |
---|
361 | newpath = g_strdup (p); |
---|
362 | } else { |
---|
363 | char *ptr; |
---|
364 | |
---|
365 | ptr = get_regexp_name (p, cur_dir, TRUE); |
---|
366 | if (!ptr) { |
---|
367 | fprintf (vfserr, "Can't find '%s'\n", p); |
---|
368 | return; |
---|
369 | } |
---|
370 | |
---|
371 | newpath = g_strconcat (cur_dir, ptr, "/", NULL); |
---|
372 | } |
---|
373 | |
---|
374 | if (validate_path (newpath)) { |
---|
375 | g_free (cur_dir); |
---|
376 | cur_dir = newpath; |
---|
377 | } else |
---|
378 | fprintf (vfserr, "Invalid path %s\n", newpath); |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | static char * |
---|
383 | get_fname (void) |
---|
384 | { |
---|
385 | char *fname, *reg_name, *f; |
---|
386 | |
---|
387 | if (!arg_data [arg_cur]) |
---|
388 | return NULL; |
---|
389 | |
---|
390 | reg_name = arg_data [arg_cur++]; |
---|
391 | fname = get_regexp_name (reg_name, cur_dir, FALSE); |
---|
392 | |
---|
393 | if (!fname) |
---|
394 | fname = reg_name; |
---|
395 | |
---|
396 | if (strchr (fname, ':') || |
---|
397 | fname [0] == '/') |
---|
398 | f = g_strdup (fname); |
---|
399 | else if (cur_dir) |
---|
400 | f = g_strconcat (cur_dir, fname, NULL); |
---|
401 | else |
---|
402 | f = g_strdup (fname); |
---|
403 | |
---|
404 | return f; |
---|
405 | } |
---|
406 | |
---|
407 | static void |
---|
408 | do_cat (void) |
---|
409 | { |
---|
410 | char *from; |
---|
411 | GnomeVFSHandle *from_handle; |
---|
412 | GnomeVFSResult result; |
---|
413 | |
---|
414 | from = get_fname (); |
---|
415 | |
---|
416 | result = gnome_vfs_open (&from_handle, from, GNOME_VFS_OPEN_READ); |
---|
417 | if (show_if_error (result, "open ", from)) |
---|
418 | return; |
---|
419 | |
---|
420 | while (1) { |
---|
421 | GnomeVFSFileSize bytes_read; |
---|
422 | guint8 data [1025]; |
---|
423 | |
---|
424 | result = gnome_vfs_read (from_handle, data, 1024, &bytes_read); |
---|
425 | if (show_if_error (result, "read ", from)) |
---|
426 | return; |
---|
427 | |
---|
428 | if (bytes_read == 0) |
---|
429 | break; |
---|
430 | |
---|
431 | if (bytes_read > 0 && |
---|
432 | bytes_read <= 1024) |
---|
433 | data [bytes_read] = '\0'; |
---|
434 | else { |
---|
435 | data [1024] = '\0'; |
---|
436 | g_warning ("Wierd error from vfs_read"); |
---|
437 | } |
---|
438 | fprintf (stdout, "%s", data); |
---|
439 | } |
---|
440 | |
---|
441 | result = gnome_vfs_close (from_handle); |
---|
442 | if (show_if_error (result, "close ", from)) |
---|
443 | return; |
---|
444 | fprintf (stdout, "\n"); |
---|
445 | } |
---|
446 | |
---|
447 | static void |
---|
448 | do_rm (void) |
---|
449 | { |
---|
450 | char *fname; |
---|
451 | GnomeVFSResult result; |
---|
452 | |
---|
453 | fname = get_fname (); |
---|
454 | |
---|
455 | result = gnome_vfs_unlink (fname); |
---|
456 | if (show_if_error (result, "unlink ", fname)) |
---|
457 | return; |
---|
458 | } |
---|
459 | |
---|
460 | static void |
---|
461 | do_mkdir (void) |
---|
462 | { |
---|
463 | char *fname; |
---|
464 | GnomeVFSResult result; |
---|
465 | |
---|
466 | fname = get_fname (); |
---|
467 | |
---|
468 | result = gnome_vfs_make_directory (fname, GNOME_VFS_PERM_USER_ALL); |
---|
469 | if (show_if_error (result, "mkdir ", fname)) |
---|
470 | return; |
---|
471 | } |
---|
472 | |
---|
473 | static void |
---|
474 | do_rmdir (void) |
---|
475 | { |
---|
476 | char *fname; |
---|
477 | GnomeVFSResult result; |
---|
478 | |
---|
479 | fname = get_fname (); |
---|
480 | |
---|
481 | result = gnome_vfs_remove_directory (fname); |
---|
482 | if (show_if_error (result, "rmdir ", fname)) |
---|
483 | return; |
---|
484 | } |
---|
485 | |
---|
486 | static void |
---|
487 | do_mv (void) |
---|
488 | { |
---|
489 | char *from, *to; |
---|
490 | char *msg; |
---|
491 | GnomeVFSResult result; |
---|
492 | |
---|
493 | from = get_fname (); |
---|
494 | to = get_fname (); |
---|
495 | if (!from || !to) { |
---|
496 | fprintf (vfserr, "mv <from> <to>\n"); |
---|
497 | return; |
---|
498 | } |
---|
499 | |
---|
500 | result = gnome_vfs_move (from, to, FALSE); |
---|
501 | |
---|
502 | msg = g_strdup_printf ("%s to %s", from, to); |
---|
503 | show_if_error (result, "move ", msg); |
---|
504 | g_free (msg); |
---|
505 | } |
---|
506 | |
---|
507 | static void |
---|
508 | do_findtrash (void) |
---|
509 | { |
---|
510 | char *from; |
---|
511 | char *uri_as_string; |
---|
512 | GnomeVFSResult result; |
---|
513 | GnomeVFSURI *from_uri; |
---|
514 | GnomeVFSURI *result_vfs_uri; |
---|
515 | |
---|
516 | from = get_fname (); |
---|
517 | |
---|
518 | from_uri = gnome_vfs_uri_new (from); |
---|
519 | result = gnome_vfs_find_directory (from_uri, |
---|
520 | GNOME_VFS_DIRECTORY_KIND_TRASH, |
---|
521 | &result_vfs_uri, |
---|
522 | TRUE, TRUE, 0777); |
---|
523 | |
---|
524 | if (result != GNOME_VFS_OK) { |
---|
525 | fprintf (stdout, "couldn't find or create trash there, error code %d", result); |
---|
526 | } else { |
---|
527 | uri_as_string = gnome_vfs_uri_to_string (result_vfs_uri, GNOME_VFS_URI_HIDE_NONE); |
---|
528 | fprintf (stdout, "trash found or created here: %s", uri_as_string); |
---|
529 | g_free (uri_as_string); |
---|
530 | } |
---|
531 | |
---|
532 | gnome_vfs_uri_unref (from_uri); |
---|
533 | gnome_vfs_uri_unref (result_vfs_uri); |
---|
534 | } |
---|
535 | |
---|
536 | static void |
---|
537 | do_info (void) |
---|
538 | { |
---|
539 | char *from; |
---|
540 | GnomeVFSResult result; |
---|
541 | GnomeVFSFileInfo *info; |
---|
542 | const char *mime_type; |
---|
543 | |
---|
544 | from = get_fname (); |
---|
545 | |
---|
546 | |
---|
547 | info = gnome_vfs_file_info_new (); |
---|
548 | result = gnome_vfs_get_file_info ( |
---|
549 | from, info, GNOME_VFS_FILE_INFO_DEFAULT); |
---|
550 | |
---|
551 | if (show_if_error (result, "getting info on: ", from)) |
---|
552 | return; |
---|
553 | |
---|
554 | fprintf (stdout, "Name: '%s'\n", info->name); |
---|
555 | if (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) { |
---|
556 | fprintf (stdout, "Type: "); |
---|
557 | switch (info->type) { |
---|
558 | case GNOME_VFS_FILE_TYPE_UNKNOWN: |
---|
559 | fprintf (stdout, "unknown"); |
---|
560 | break; |
---|
561 | case GNOME_VFS_FILE_TYPE_REGULAR: |
---|
562 | fprintf (stdout, "regular"); |
---|
563 | break; |
---|
564 | case GNOME_VFS_FILE_TYPE_DIRECTORY: |
---|
565 | fprintf (stdout, "directory"); |
---|
566 | break; |
---|
567 | case GNOME_VFS_FILE_TYPE_FIFO: |
---|
568 | fprintf (stdout, "fifo"); |
---|
569 | break; |
---|
570 | case GNOME_VFS_FILE_TYPE_SOCKET: |
---|
571 | fprintf (stdout, "socket"); |
---|
572 | break; |
---|
573 | case GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE: |
---|
574 | fprintf (stdout, "char"); |
---|
575 | break; |
---|
576 | case GNOME_VFS_FILE_TYPE_BLOCK_DEVICE: |
---|
577 | fprintf (stdout, "block"); |
---|
578 | break; |
---|
579 | case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: |
---|
580 | fprintf (stdout, "symlink"); |
---|
581 | break; |
---|
582 | default: |
---|
583 | fprintf (stdout, "Error; invalid value"); |
---|
584 | break; |
---|
585 | } |
---|
586 | } else |
---|
587 | fprintf (stdout, "Type invalid"); |
---|
588 | fprintf (stdout, "\n"); |
---|
589 | |
---|
590 | if (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) { |
---|
591 | long i = info->size; |
---|
592 | fprintf (stdout, "Size: %ld bytes", i); |
---|
593 | } else { |
---|
594 | fprintf (stdout, "Size invalid"); |
---|
595 | } |
---|
596 | fprintf (stdout, "\n"); |
---|
597 | |
---|
598 | mime_type = gnome_vfs_file_info_get_mime_type (info); |
---|
599 | |
---|
600 | fprintf (stdout, "Mime Type: %s \n", mime_type); |
---|
601 | |
---|
602 | fprintf (stdout, "\n"); |
---|
603 | /* FIXME bugzilla.eazel.com 2800: hack here; should dump them all */ |
---|
604 | |
---|
605 | gnome_vfs_file_info_unref (info); |
---|
606 | } |
---|
607 | |
---|
608 | static void |
---|
609 | do_cp (void) |
---|
610 | { |
---|
611 | char *from = NULL; |
---|
612 | char *to = NULL; |
---|
613 | GnomeVFSHandle *from_handle = NULL; |
---|
614 | GnomeVFSHandle *to_handle = NULL; |
---|
615 | GnomeVFSResult result; |
---|
616 | |
---|
617 | from = get_fname (); |
---|
618 | |
---|
619 | if (from) |
---|
620 | to = get_fname (); |
---|
621 | else { |
---|
622 | fprintf (vfserr, "cp <from> <to>\n"); |
---|
623 | goto out; |
---|
624 | } |
---|
625 | |
---|
626 | result = gnome_vfs_open (&from_handle, from, GNOME_VFS_OPEN_READ); |
---|
627 | if (show_if_error (result, "open ", from)) |
---|
628 | goto out; |
---|
629 | |
---|
630 | result = gnome_vfs_open (&to_handle, to, GNOME_VFS_OPEN_WRITE); |
---|
631 | if (result == GNOME_VFS_ERROR_NOT_FOUND) |
---|
632 | result = gnome_vfs_create (&to_handle, to, GNOME_VFS_OPEN_WRITE, FALSE, |
---|
633 | GNOME_VFS_PERM_USER_ALL); |
---|
634 | if (show_if_error (result, "open ", to)) |
---|
635 | goto out; |
---|
636 | |
---|
637 | while (1) { |
---|
638 | GnomeVFSFileSize bytes_read; |
---|
639 | GnomeVFSFileSize bytes_written; |
---|
640 | guint8 data [1024]; |
---|
641 | |
---|
642 | result = gnome_vfs_read (from_handle, data, 1024, &bytes_read); |
---|
643 | if (show_if_error (result, "read ", from)) |
---|
644 | goto out; |
---|
645 | |
---|
646 | if (bytes_read == 0) |
---|
647 | break; |
---|
648 | |
---|
649 | result = gnome_vfs_write (to_handle, data, bytes_read, &bytes_written); |
---|
650 | if (show_if_error (result, "write ", to)) |
---|
651 | goto out; |
---|
652 | |
---|
653 | if (bytes_read != bytes_written) |
---|
654 | fprintf (vfserr, "Didn't write it all"); |
---|
655 | } |
---|
656 | |
---|
657 | out: |
---|
658 | g_free (from); |
---|
659 | g_free (to); |
---|
660 | |
---|
661 | if (to_handle) { |
---|
662 | result = gnome_vfs_close (to_handle); |
---|
663 | if (show_if_error (result, "close ", to)) |
---|
664 | /* Nothing */; |
---|
665 | } |
---|
666 | |
---|
667 | if (from_handle) { |
---|
668 | result = gnome_vfs_close (from_handle); |
---|
669 | if (show_if_error (result, "close ", from)) |
---|
670 | /* Nothing */; |
---|
671 | } |
---|
672 | } |
---|
673 | |
---|
674 | static void |
---|
675 | ms_ole_dump (guint8 const *ptr, guint32 len, guint32 offset) |
---|
676 | { |
---|
677 | guint32 lp,lp2; |
---|
678 | guint32 off; |
---|
679 | |
---|
680 | for (lp = 0;lp<(len+15)/16;lp++) { |
---|
681 | printf ("%8x | ", lp*16 + offset); |
---|
682 | for (lp2=0;lp2<16;lp2++) { |
---|
683 | off = lp2 + (lp<<4); |
---|
684 | off<len?printf("%2x ", ptr[off]):printf("XX "); |
---|
685 | } |
---|
686 | printf ("| "); |
---|
687 | for (lp2=0;lp2<16;lp2++) { |
---|
688 | off = lp2 + (lp<<4); |
---|
689 | printf ("%c", off<len?(ptr[off]>'!'&&ptr[off]<127?ptr[off]:'.'):'*'); |
---|
690 | } |
---|
691 | printf ("\n"); |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | static void |
---|
696 | do_dump (void) |
---|
697 | { |
---|
698 | char *from; |
---|
699 | GnomeVFSHandle *from_handle; |
---|
700 | GnomeVFSResult result; |
---|
701 | guint32 offset; |
---|
702 | |
---|
703 | from = get_fname (); |
---|
704 | |
---|
705 | result = gnome_vfs_open (&from_handle, from, GNOME_VFS_OPEN_READ); |
---|
706 | if (show_if_error (result, "open ", from)) |
---|
707 | return; |
---|
708 | |
---|
709 | for (offset = 0; 1; ) { |
---|
710 | GnomeVFSFileSize bytes_read; |
---|
711 | guint8 data [1024]; |
---|
712 | |
---|
713 | result = gnome_vfs_read (from_handle, data, 1024, &bytes_read); |
---|
714 | if (show_if_error (result, "read ", from)) |
---|
715 | return; |
---|
716 | |
---|
717 | if (bytes_read == 0) |
---|
718 | break; |
---|
719 | |
---|
720 | ms_ole_dump (data, bytes_read, offset); |
---|
721 | |
---|
722 | offset += bytes_read; |
---|
723 | } |
---|
724 | |
---|
725 | result = gnome_vfs_close (from_handle); |
---|
726 | if (show_if_error (result, "close ", from)) |
---|
727 | return; |
---|
728 | } |
---|
729 | |
---|
730 | |
---|
731 | /* |
---|
732 | * --------------------------------------------------------------------- |
---|
733 | */ |
---|
734 | |
---|
735 | static char * |
---|
736 | get_handle (void) |
---|
737 | { |
---|
738 | if (!arg_data [arg_cur]) |
---|
739 | return NULL; |
---|
740 | |
---|
741 | return arg_data [arg_cur++]; |
---|
742 | } |
---|
743 | |
---|
744 | static int |
---|
745 | get_int (void) |
---|
746 | { |
---|
747 | if (!arg_data [arg_cur]) |
---|
748 | return 0; |
---|
749 | |
---|
750 | return atoi (arg_data [arg_cur++]); |
---|
751 | } |
---|
752 | |
---|
753 | static void |
---|
754 | do_open (void) |
---|
755 | { |
---|
756 | char *from, *handle; |
---|
757 | GnomeVFSHandle *from_handle; |
---|
758 | GnomeVFSResult result; |
---|
759 | |
---|
760 | handle = get_handle (); |
---|
761 | from = get_fname (); |
---|
762 | |
---|
763 | if (!handle || !from) { |
---|
764 | fprintf (vfserr, "open <handle> <filename>\n"); |
---|
765 | return; |
---|
766 | } |
---|
767 | |
---|
768 | result = gnome_vfs_open (&from_handle, from, GNOME_VFS_OPEN_READ); |
---|
769 | if (show_if_error (result, "open ", from)) |
---|
770 | return; |
---|
771 | |
---|
772 | register_file (handle, from_handle); |
---|
773 | } |
---|
774 | |
---|
775 | static void |
---|
776 | do_create (void) |
---|
777 | { |
---|
778 | char *from, *handle; |
---|
779 | GnomeVFSHandle *from_handle; |
---|
780 | GnomeVFSResult result; |
---|
781 | |
---|
782 | handle = get_handle (); |
---|
783 | from = get_fname (); |
---|
784 | |
---|
785 | if (!handle || !from) { |
---|
786 | fprintf (vfserr, "create <handle> <filename>\n"); |
---|
787 | return; |
---|
788 | } |
---|
789 | |
---|
790 | result = gnome_vfs_create (&from_handle, from, GNOME_VFS_OPEN_READ, |
---|
791 | FALSE, GNOME_VFS_PERM_USER_READ | |
---|
792 | GNOME_VFS_PERM_USER_WRITE); |
---|
793 | if (show_if_error (result, "create ", from)) |
---|
794 | return; |
---|
795 | |
---|
796 | register_file (handle, from_handle); |
---|
797 | } |
---|
798 | |
---|
799 | static void |
---|
800 | do_read (void) |
---|
801 | { |
---|
802 | char *handle; |
---|
803 | int length; |
---|
804 | GnomeVFSHandle *from_handle; |
---|
805 | GnomeVFSResult result; |
---|
806 | GnomeVFSFileSize bytes_read; |
---|
807 | guint8 *data; |
---|
808 | |
---|
809 | handle = get_handle (); |
---|
810 | length = get_int (); |
---|
811 | |
---|
812 | if (length < 0) { |
---|
813 | fprintf (vfserr, "Can't read %d bytes\n", length); |
---|
814 | return; |
---|
815 | } |
---|
816 | |
---|
817 | from_handle = lookup_file (handle); |
---|
818 | if (!from_handle) |
---|
819 | return; |
---|
820 | |
---|
821 | data = g_malloc (length); |
---|
822 | result = gnome_vfs_read (from_handle, data, length, &bytes_read); |
---|
823 | if (show_if_error (result, "read ", handle)) |
---|
824 | return; |
---|
825 | |
---|
826 | ms_ole_dump (data, bytes_read, 0); |
---|
827 | } |
---|
828 | |
---|
829 | static void |
---|
830 | do_seek (void) |
---|
831 | { |
---|
832 | char *handle; |
---|
833 | int offset; |
---|
834 | GnomeVFSHandle *from_handle; |
---|
835 | GnomeVFSResult result; |
---|
836 | |
---|
837 | handle = get_handle (); |
---|
838 | offset = get_int (); |
---|
839 | |
---|
840 | if (offset < 0) { |
---|
841 | fprintf (vfserr, "Can't seek to %d bytes offset\n", offset); |
---|
842 | return; |
---|
843 | } |
---|
844 | |
---|
845 | from_handle = lookup_file (handle); |
---|
846 | if (!from_handle) |
---|
847 | return; |
---|
848 | |
---|
849 | result = gnome_vfs_seek (from_handle, GNOME_VFS_SEEK_START, offset); |
---|
850 | if (show_if_error (result, "seek ", handle)) |
---|
851 | return; |
---|
852 | } |
---|
853 | |
---|
854 | static void |
---|
855 | do_close (void) |
---|
856 | { |
---|
857 | close_file (get_handle ()); |
---|
858 | } |
---|
859 | |
---|
860 | |
---|
861 | /* |
---|
862 | * --------------------------------------------------------------------- |
---|
863 | */ |
---|
864 | |
---|
865 | int interactive = 0; |
---|
866 | const struct poptOption options [] = { |
---|
867 | { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, |
---|
868 | "Allow interactive input", NULL }, |
---|
869 | { NULL, '\0', 0, NULL, 0 } |
---|
870 | }; |
---|
871 | |
---|
872 | int |
---|
873 | main (int argc, char **argv) |
---|
874 | { |
---|
875 | poptContext popt_context; |
---|
876 | int exit = 0; |
---|
877 | char *buffer = g_new (char, 1024) ; |
---|
878 | const char **args; |
---|
879 | FILE *instream; |
---|
880 | |
---|
881 | files = g_hash_table_new (g_str_hash, g_str_equal); |
---|
882 | |
---|
883 | gnome_init_with_popt_table ("test-vfs", "0.0", argc, argv, |
---|
884 | options, 0, &popt_context); |
---|
885 | |
---|
886 | /* make the stupid "SaveYourself" warning not come up */ |
---|
887 | gnome_client_disconnect (gnome_master_client ()); |
---|
888 | |
---|
889 | if (interactive) |
---|
890 | vfserr = stderr; |
---|
891 | else |
---|
892 | vfserr = stdout; |
---|
893 | |
---|
894 | if (!gnome_vfs_init ()) { |
---|
895 | fprintf (vfserr, "Cannot initialize gnome-vfs.\n"); |
---|
896 | return 1; |
---|
897 | } |
---|
898 | |
---|
899 | instream = stdin; |
---|
900 | args = poptGetArgs (popt_context); |
---|
901 | if (!args) |
---|
902 | cur_dir = g_get_current_dir (); |
---|
903 | else |
---|
904 | cur_dir = g_strdup (args [0]); |
---|
905 | |
---|
906 | if (cur_dir && cur_dir [strlen (cur_dir)] != '/') { |
---|
907 | char *new_dir = g_strconcat (cur_dir, "/", NULL); |
---|
908 | g_free (cur_dir); |
---|
909 | cur_dir = new_dir; |
---|
910 | } |
---|
911 | |
---|
912 | poptFreeContext (popt_context); |
---|
913 | |
---|
914 | do { |
---|
915 | char *ptr; |
---|
916 | |
---|
917 | if (interactive) { |
---|
918 | fprintf (stdout,"\n%s > ", cur_dir); |
---|
919 | fflush (stdout); |
---|
920 | } |
---|
921 | fgets (buffer, 1023, stdin); |
---|
922 | |
---|
923 | if (!buffer || buffer [0] == '#') |
---|
924 | continue; |
---|
925 | |
---|
926 | arg_data = g_strsplit (g_strchomp (buffer), delim, -1); |
---|
927 | arg_cur = 0; |
---|
928 | if ((!arg_data || !arg_data[0]) && interactive) continue; |
---|
929 | if (!interactive) |
---|
930 | printf ("Command : '%s'\n", arg_data [0]); |
---|
931 | ptr = arg_data[arg_cur++]; |
---|
932 | if (!ptr) |
---|
933 | continue; |
---|
934 | |
---|
935 | if (g_strcasecmp (ptr, "ls") == 0) |
---|
936 | do_ls (); |
---|
937 | else if (g_strcasecmp (ptr, "cd") == 0) |
---|
938 | do_cd (); |
---|
939 | else if (g_strcasecmp (ptr, "dump") == 0) |
---|
940 | do_dump (); |
---|
941 | else if (g_strcasecmp (ptr, "type") == 0 || |
---|
942 | g_strcasecmp (ptr, "cat") == 0) |
---|
943 | do_cat (); |
---|
944 | else if (g_strcasecmp (ptr, "cp") == 0) |
---|
945 | do_cp (); |
---|
946 | else if (g_strcasecmp (ptr, "rm") == 0) |
---|
947 | do_rm (); |
---|
948 | else if (g_strcasecmp (ptr, "mkdir") == 0) |
---|
949 | do_mkdir (); |
---|
950 | else if (g_strcasecmp (ptr, "rmdir") == 0) |
---|
951 | do_rmdir (); |
---|
952 | else if (g_strcasecmp (ptr, "mv") == 0) |
---|
953 | do_mv (); |
---|
954 | else if (g_strcasecmp (ptr, "info") == 0 || |
---|
955 | g_strcasecmp (ptr, "stat") == 0) |
---|
956 | do_info (); |
---|
957 | else if (g_strcasecmp (ptr, "findtrash") == 0) |
---|
958 | do_findtrash (); |
---|
959 | else if (g_strcasecmp (ptr, "sync") == 0) |
---|
960 | fprintf (vfserr, "a shell is like a boat, it lists or syncs (RMS)\n"); |
---|
961 | else if (g_strcasecmp (ptr,"help") == 0 || |
---|
962 | g_strcasecmp (ptr,"?") == 0 || |
---|
963 | g_strcasecmp (ptr,"info") == 0 || |
---|
964 | g_strcasecmp (ptr,"man") == 0) |
---|
965 | list_commands (); |
---|
966 | else if (g_strcasecmp (ptr,"exit") == 0 || |
---|
967 | g_strcasecmp (ptr,"quit") == 0 || |
---|
968 | g_strcasecmp (ptr,"q") == 0 || |
---|
969 | g_strcasecmp (ptr,"bye") == 0) |
---|
970 | exit = 1; |
---|
971 | |
---|
972 | /* File ops */ |
---|
973 | else if (g_strcasecmp (ptr, "open") == 0) |
---|
974 | do_open (); |
---|
975 | else if (g_strcasecmp (ptr, "create") == 0) |
---|
976 | do_create (); |
---|
977 | else if (g_strcasecmp (ptr, "close") == 0) |
---|
978 | do_close (); |
---|
979 | else if (g_strcasecmp (ptr, "read") == 0) |
---|
980 | do_read (); |
---|
981 | else if (g_strcasecmp (ptr, "seek") == 0) |
---|
982 | do_seek (); |
---|
983 | |
---|
984 | else |
---|
985 | fprintf (vfserr, "Unknown command '%s'", ptr); |
---|
986 | |
---|
987 | g_strfreev (arg_data); |
---|
988 | arg_data = NULL; |
---|
989 | } while (!exit); |
---|
990 | |
---|
991 | g_free (buffer); |
---|
992 | g_free (cur_dir); |
---|
993 | |
---|
994 | close_files (); |
---|
995 | |
---|
996 | return 0; |
---|
997 | } |
---|