341
|
1 /*****************************************************************************/
|
|
2 /* gftpui.c - UI related functions for gFTP. All of these functions must be */
|
|
3 /* reentrant. */
|
|
4 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
|
5 /* */
|
|
6 /* This program is free software; you can redistribute it and/or modify */
|
|
7 /* it under the terms of the GNU General Public License as published by */
|
|
8 /* the Free Software Foundation; either version 2 of the License, or */
|
|
9 /* (at your option) any later version. */
|
|
10 /* */
|
|
11 /* This program is distributed in the hope that it will be useful, */
|
|
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
14 /* GNU General Public License for more details. */
|
|
15 /* */
|
|
16 /* You should have received a copy of the GNU General Public License */
|
|
17 /* along with this program; if not, write to the Free Software */
|
|
18 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
19 /*****************************************************************************/
|
|
20
|
|
21 #include "gftpui.h"
|
|
22 static const char cvsid[] = "$Id$";
|
|
23
|
|
24 int
|
|
25 gftpui_common_run_mkdir (gftpui_callback_data * cdata)
|
|
26 {
|
367
|
27 return (gftp_make_directory (cdata->request, cdata->input_string));
|
341
|
28 }
|
|
29
|
|
30
|
|
31 int
|
|
32 gftpui_common_run_rename (gftpui_callback_data * cdata)
|
|
33 {
|
367
|
34 return (gftp_rename_file (cdata->request, cdata->source_string,
|
|
35 cdata->input_string));
|
341
|
36 }
|
|
37
|
|
38
|
|
39 int
|
|
40 gftpui_common_run_site (gftpui_callback_data * cdata)
|
|
41 {
|
633
|
42 return (gftp_site_cmd (cdata->request, cdata->toggled, cdata->input_string));
|
341
|
43 }
|
|
44
|
|
45
|
|
46 int
|
|
47 gftpui_common_run_chdir (gftpui_callback_data * cdata)
|
|
48 {
|
367
|
49 return (gftp_set_directory (cdata->request, cdata->input_string));
|
341
|
50 }
|
|
51
|
350
|
52
|
|
53 int
|
|
54 gftpui_common_run_chmod (gftpui_callback_data * cdata)
|
|
55 {
|
367
|
56 return (gftp_chmod (cdata->request, cdata->source_string,
|
|
57 strtol (cdata->input_string, NULL, 10)));
|
350
|
58 }
|
|
59
|
|
60
|
|
61 int
|
355
|
62 gftpui_common_run_ls (gftpui_callback_data * cdata)
|
|
63 {
|
443
|
64 int got, matched_filespec, have_dotdot, ret;
|
387
|
65 char *sortcol_var, *sortasds_var;
|
355
|
66 intptr_t sortcol, sortasds;
|
|
67 gftp_file * fle;
|
|
68
|
443
|
69 ret = gftp_list_files (cdata->request);
|
|
70 if (ret < 0)
|
|
71 return (ret);
|
355
|
72
|
|
73 have_dotdot = 0;
|
|
74 cdata->request->gotbytes = 0;
|
|
75 cdata->files = NULL;
|
|
76 fle = g_malloc0 (sizeof (*fle));
|
|
77 while ((got = gftp_get_next_file (cdata->request, NULL, fle)) > 0 ||
|
|
78 got == GFTP_ERETRYABLE)
|
|
79 {
|
|
80 if (cdata->source_string == NULL)
|
|
81 matched_filespec = 1;
|
|
82 else
|
|
83 matched_filespec = gftp_match_filespec (fle->file,
|
|
84 cdata->source_string);
|
|
85
|
|
86 if (got < 0 || strcmp (fle->file, ".") == 0 || !matched_filespec)
|
|
87 {
|
598
|
88 gftp_file_destroy (fle, 0);
|
355
|
89 continue;
|
|
90 }
|
|
91 else if (strcmp (fle->file, "..") == 0)
|
|
92 have_dotdot = 1;
|
|
93
|
|
94 cdata->request->gotbytes += got;
|
|
95 cdata->files = g_list_prepend (cdata->files, fle);
|
|
96 fle = g_malloc0 (sizeof (*fle));
|
|
97 }
|
|
98 g_free (fle);
|
|
99
|
|
100 gftp_end_transfer (cdata->request);
|
|
101 cdata->request->gotbytes = -1;
|
|
102
|
|
103 if (!have_dotdot)
|
|
104 {
|
|
105 fle = g_malloc0 (sizeof (*fle));
|
|
106 fle->file = g_strdup ("..");
|
|
107 fle->user = g_malloc0 (1);
|
|
108 fle->group = g_malloc0 (1);
|
592
|
109 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
|
355
|
110 cdata->files = g_list_prepend (cdata->files, fle);
|
|
111 }
|
|
112
|
|
113 if (cdata->files != NULL)
|
|
114 {
|
387
|
115 if (cdata->request->protonum == GFTP_LOCAL_NUM)
|
|
116 {
|
|
117 sortasds_var = "local_sortasds";
|
|
118 sortcol_var = "local_sortcol";
|
|
119 }
|
|
120 else
|
|
121 {
|
|
122 sortasds_var = "remote_sortasds";
|
|
123 sortcol_var = "remote_sortcol";
|
|
124 }
|
|
125
|
|
126 gftp_lookup_global_option (sortcol_var, &sortcol);
|
|
127 gftp_lookup_global_option (sortasds_var, &sortasds);
|
355
|
128
|
|
129 cdata->files = gftp_sort_filelist (cdata->files, sortcol, sortasds);
|
|
130 }
|
|
131
|
|
132 return (1);
|
|
133 }
|
|
134
|
|
135
|
507
|
136 static void
|
|
137 _gftpui_common_del_purge_cache (gpointer key, gpointer value,
|
|
138 gpointer user_data)
|
|
139 {
|
|
140 gftp_delete_cache_entry (NULL, key, 0);
|
|
141 g_free (key);
|
|
142 }
|
|
143
|
|
144
|
|
145 static int
|
|
146 _gftpui_common_rm_list (gftpui_callback_data * cdata)
|
|
147 {
|
|
148 char *tempstr, description[BUFSIZ];
|
|
149 gftp_file * tempfle;
|
|
150 GHashTable * rmhash;
|
|
151 GList * templist;
|
|
152 int success, ret;
|
|
153
|
|
154 for (templist = cdata->files;
|
|
155 templist->next != NULL;
|
|
156 templist = templist->next);
|
|
157
|
|
158 if (cdata->request->use_cache)
|
|
159 rmhash = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
160 else
|
|
161 rmhash = NULL;
|
|
162
|
514
|
163 ret = 0;
|
507
|
164 for (; templist != NULL; templist = templist->prev)
|
|
165 {
|
|
166 tempfle = templist->data;
|
|
167
|
|
168 if (S_ISDIR (tempfle->st_mode))
|
|
169 success = gftp_remove_directory (cdata->request, tempfle->file);
|
|
170 else
|
|
171 success = gftp_remove_file (cdata->request, tempfle->file);
|
|
172
|
|
173 if (success < 0)
|
|
174 ret = success;
|
|
175 else if (rmhash != NULL)
|
|
176 {
|
|
177 gftp_generate_cache_description (cdata->request, description,
|
|
178 sizeof (description), 0);
|
|
179 if (g_hash_table_lookup (rmhash, description) == NULL)
|
|
180 {
|
|
181 tempstr = g_strdup (description);
|
|
182 g_hash_table_insert (rmhash, tempstr, NULL);
|
|
183 }
|
|
184 }
|
|
185
|
|
186 if (!GFTP_IS_CONNECTED (cdata->request))
|
|
187 break;
|
|
188 }
|
|
189
|
|
190 if (rmhash != NULL)
|
|
191 {
|
|
192 g_hash_table_foreach (rmhash, _gftpui_common_del_purge_cache, NULL);
|
|
193 g_hash_table_destroy (rmhash);
|
|
194 }
|
|
195
|
|
196 return (ret);
|
|
197 }
|
|
198
|
|
199
|
355
|
200 int
|
350
|
201 gftpui_common_run_delete (gftpui_callback_data * cdata)
|
|
202 {
|
|
203 int ret;
|
|
204
|
507
|
205 if (cdata->files != NULL)
|
|
206 ret = _gftpui_common_rm_list (cdata);
|
350
|
207 else
|
507
|
208 ret = gftp_remove_file (cdata->request, cdata->input_string);
|
350
|
209
|
|
210 return (ret);
|
|
211 }
|
|
212
|
|
213
|
|
214 int
|
|
215 gftpui_common_run_rmdir (gftpui_callback_data * cdata)
|
|
216 {
|
|
217 int ret;
|
|
218
|
507
|
219 if (cdata->files != NULL)
|
|
220 ret = _gftpui_common_rm_list (cdata);
|
350
|
221 else
|
507
|
222 ret = gftp_remove_directory (cdata->request, cdata->input_string);
|
350
|
223
|
|
224 return (ret);
|
|
225 }
|
|
226
|
367
|
227
|
|
228 int
|
|
229 gftpui_common_run_connect (gftpui_callback_data * cdata)
|
|
230 {
|
|
231 return (gftp_connect (cdata->request));
|
|
232 }
|
|
233
|