ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / test / test-xfer.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-xfer.c - Test program for the xfer functions in the GNOME Virtual File
3    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 <libgnomevfs/gnome-vfs-init.h>
27 #include <libgnomevfs/gnome-vfs-xfer.h>
28 #include <popt.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 static int recursive = 0;
33 static int replace = 0;
34 static int remove_source = 0;
35 static int follow_symlinks = 0;
36 static int follow_symlinks_recursive = 0;
37
38 const struct poptOption options[] = {
39         POPT_AUTOHELP
40         {
41                 "recursive",
42                 'r',
43                 POPT_ARG_NONE,
44                 &recursive,
45                 0,
46                 "Copy directories recursively",
47                 NULL
48         },
49         {
50                 "follow-symlinks",
51                 'L',
52                 POPT_ARG_NONE,
53                 &follow_symlinks,
54                 0,
55                 "Follow symlinks",
56                 NULL
57         },
58         {
59                 "recursive-symlinks",
60                 'Z',
61                 POPT_ARG_NONE,
62                 &follow_symlinks_recursive,
63                 0,
64                 "Follow symlinks",
65                 NULL
66         },
67         {
68                 "replace",
69                 'R',
70                 POPT_ARG_NONE,
71                 &replace,
72                 0,
73                 "Replace files automatically",
74                 NULL
75         },
76         {
77                 "delete-source",
78                 'd',
79                 POPT_ARG_NONE,
80                 &remove_source,
81                 0,
82                 "Delete source files",
83                 NULL
84         },
85         {
86                 NULL,
87                 0,
88                 0,
89                 NULL,
90                 0,
91                 NULL,
92                 NULL
93         }
94 };
95
96 static void
97 show_result (GnomeVFSResult result, const gchar *what)
98 {
99         fprintf (stderr, "%s: %s\n", what, gnome_vfs_result_to_string (result));
100         if (result != GNOME_VFS_OK)
101                 exit (1);
102 }
103
104 static gint
105 xfer_progress_callback (GnomeVFSXferProgressInfo *info,
106                         gpointer data)
107 {
108         switch (info->status) {
109         case GNOME_VFS_XFER_PROGRESS_STATUS_VFSERROR:
110                 printf ("VFS Error: %s\n",
111                         gnome_vfs_result_to_string (info->vfs_status));
112                 exit (1);
113                 break;
114         case GNOME_VFS_XFER_PROGRESS_STATUS_OVERWRITE:
115                 printf ("Overwriting `%s' with `%s'\n",
116                         info->target_name, info->source_name);
117                 exit (1);
118                 break;
119         case GNOME_VFS_XFER_PROGRESS_STATUS_OK:
120                 printf ("Status: OK\n");
121                 switch (info->phase) {
122                 case GNOME_VFS_XFER_PHASE_INITIAL:
123                         printf ("Initial phase\n");
124                         return TRUE;
125                 case GNOME_VFS_XFER_PHASE_COLLECTING:
126                         printf ("Collecting file list\n");
127                         return TRUE;
128                 case GNOME_VFS_XFER_PHASE_READYTOGO:
129                         printf ("Ready to go!\n");
130                         return TRUE;
131                 case GNOME_VFS_XFER_PHASE_OPENSOURCE:
132                         printf ("Opening source\n");
133                         return TRUE;
134                 case GNOME_VFS_XFER_PHASE_OPENTARGET:
135                         printf ("Opening target\n");
136                         return TRUE;
137                 case GNOME_VFS_XFER_PHASE_COPYING:
138                         printf ("Transferring `%s' to `%s' (file %ld/%ld, byte %ld/%ld in file, "
139                                 "%" GNOME_VFS_SIZE_FORMAT_STR "/%" GNOME_VFS_SIZE_FORMAT_STR " total)\n",
140                                 info->source_name,
141                                 info->target_name,
142                                 info->file_index,
143                                 info->files_total,
144                                 (glong) info->bytes_copied,
145                                 (glong) info->file_size,
146                                 info->total_bytes_copied,
147                                 info->bytes_total);
148                         return TRUE;
149                 case GNOME_VFS_XFER_PHASE_CLOSESOURCE:
150                         printf ("Closing source\n");
151                         return TRUE;
152                 case GNOME_VFS_XFER_PHASE_CLOSETARGET:
153                         printf ("Closing target\n");
154                         return TRUE;
155                 case GNOME_VFS_XFER_PHASE_FILECOMPLETED:
156                         printf ("Done with `%s' -> `%s', going next\n",
157                                 info->source_name, info->target_name);
158                         return TRUE;
159                 case GNOME_VFS_XFER_PHASE_COMPLETED:
160                         printf ("All done.\n");
161                         return TRUE;
162                 default:
163                         printf ("Unexpected phase %d\n", info->phase);
164                         return TRUE; /* keep going anyway */
165                 }
166         case GNOME_VFS_XFER_PROGRESS_STATUS_DUPLICATE:
167                 break;
168         }
169
170         printf ("Boh!\n");
171         return FALSE;
172 }
173
174 int
175 main (int argc, const char **argv)
176 {
177         const char **args;
178         poptContext popt_context;
179         GnomeVFSURI *src_uri, *dest_uri;
180         GList *src_uri_list, *dest_uri_list;
181         GnomeVFSResult result;
182         GnomeVFSXferOptions xfer_options;
183         GnomeVFSXferOverwriteMode overwrite_mode;
184
185         if (! gnome_vfs_init ()) {
186                 fprintf (stderr,
187                          "Cannot initialize the GNOME Virtual File System.\n");
188                 return 1;
189         }
190
191         popt_context = poptGetContext ("test-directory", argc, argv,
192                                        options, 0);
193
194         while (poptGetNextOpt (popt_context) != -1)
195                 ;
196
197         args = poptGetArgs (popt_context);
198         if (args == NULL || args[1] == NULL || args[2] != NULL) {
199                 fprintf (stderr, "Usage: %s [<options>] <src> <target>\n",
200                          argv[0]);
201                 return 1;
202         }
203
204         src_uri = gnome_vfs_uri_new (args[0]);
205         if (src_uri == NULL) {
206                 fprintf (stderr, "%s: invalid URI\n", args[0]);
207                 return 1;
208         }
209         dest_uri = gnome_vfs_uri_new (args[1]);
210         if (dest_uri == NULL) {
211                 fprintf (stderr, "%s: invalid URI\n", args[1]);
212                 return 1;
213         }
214
215         poptFreeContext (popt_context);
216
217
218         xfer_options = 0;
219         overwrite_mode = GNOME_VFS_XFER_OVERWRITE_MODE_QUERY;
220         if (recursive) {
221                 fprintf (stderr, "Warning: Recursive xfer of directories.\n");
222                 xfer_options |= GNOME_VFS_XFER_RECURSIVE;
223         }
224         if (follow_symlinks) {
225                 fprintf (stderr, "Warning: Following symlinks.\n");
226                 xfer_options |= GNOME_VFS_XFER_FOLLOW_LINKS;
227         }
228         if (follow_symlinks_recursive) {
229                 fprintf (stderr, "Warning: Following symlinks recursively.\n");
230                 xfer_options |= GNOME_VFS_XFER_FOLLOW_LINKS_RECURSIVE;
231         }
232         if (replace) {
233                 fprintf (stderr, "Warning: Using replace overwrite mode.\n");
234                 overwrite_mode = GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE;
235         }
236         if (remove_source) {
237                 fprintf (stderr, "Warning: Removing source files.\n");
238                 xfer_options |= GNOME_VFS_XFER_REMOVESOURCE;
239         }
240
241         src_uri_list = g_list_append (NULL, src_uri);
242         dest_uri_list = g_list_append (NULL, dest_uri);
243         result = gnome_vfs_xfer_uri_list (src_uri_list, dest_uri_list,
244                                           xfer_options,
245                                           GNOME_VFS_XFER_ERROR_MODE_QUERY,
246                                           overwrite_mode,
247                                           xfer_progress_callback,
248                                           NULL);
249
250         show_result (result, "gnome_vfs_xfer");
251
252         gnome_vfs_uri_list_free (src_uri_list);
253         gnome_vfs_uri_list_free (dest_uri_list);
254
255         return 0;
256 }