Mercurial > gftp.yaz
annotate lib/protocols.c @ 988:63555c9744c2
remote charset should be specified by each bookmark entry.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Fri, 14 Aug 2009 07:54:55 +0900 |
parents | c7d7a081cd9c |
children | c99b134c6185 |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* protocols.c - Skeleton functions for the protocols gftp supports */ | |
885 | 3 /* Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program 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 */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
18 /*****************************************************************************/ | |
19 | |
20 #include "gftp.h" | |
33 | 21 static const char cvsid[] = "$Id$"; |
1 | 22 |
23 gftp_request * | |
24 gftp_request_new (void) | |
25 { | |
26 gftp_request *request; | |
27 | |
28 request = g_malloc0 (sizeof (*request)); | |
66 | 29 request->datafd = -1; |
30 request->cachefd = -1; | |
122 | 31 request->server_type = GFTP_DIRTYPE_OTHER; |
1 | 32 return (request); |
33 } | |
136 | 34 |
1 | 35 |
36 void | |
67 | 37 gftp_request_destroy (gftp_request * request, int free_request) |
1 | 38 { |
39 g_return_if_fail (request != NULL); | |
40 | |
41 gftp_disconnect (request); | |
42 | |
43 if (request->destroy != NULL) | |
44 request->destroy (request); | |
45 | |
46 if (request->hostname) | |
47 g_free (request->hostname); | |
48 if (request->username) | |
49 g_free (request->username); | |
50 if (request->password) | |
944 | 51 g_free (request->password); |
1 | 52 if (request->account) |
944 | 53 g_free (request->account); |
1 | 54 if (request->directory) |
55 g_free (request->directory); | |
56 if (request->last_ftp_response) | |
57 g_free (request->last_ftp_response); | |
58 if (request->protocol_data) | |
59 g_free (request->protocol_data); | |
67 | 60 |
944 | 61 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
62 if (request->remote_addr != NULL) | |
63 g_free (request->remote_addr); | |
64 #endif | |
65 | |
198 | 66 if (request->local_options_vars != NULL) |
67 { | |
201 | 68 gftp_config_free_options (request->local_options_vars, |
69 request->local_options_hash, | |
70 request->num_local_options_vars); | |
198 | 71 } |
72 | |
1 | 73 memset (request, 0, sizeof (*request)); |
67 | 74 |
75 if (free_request) | |
76 g_free (request); | |
77 else | |
78 { | |
79 request->datafd = -1; | |
80 request->cachefd = -1; | |
924 | 81 request->server_type = GFTP_DIRTYPE_OTHER; |
67 | 82 } |
1 | 83 } |
84 | |
85 | |
309 | 86 /* This function is called to copy protocol specific data from one request |
87 structure to another. This is typically called when a file transfer is | |
88 completed, state information can be copied back to the main window */ | |
89 void | |
90 gftp_copy_param_options (gftp_request * dest_request, | |
91 gftp_request * src_request) | |
92 { | |
93 g_return_if_fail (dest_request != NULL); | |
94 g_return_if_fail (src_request != NULL); | |
95 g_return_if_fail (dest_request->protonum == src_request->protonum); | |
96 | |
97 if (dest_request->copy_param_options) | |
98 dest_request->copy_param_options (dest_request, src_request); | |
99 } | |
100 | |
101 | |
1 | 102 void |
598 | 103 gftp_file_destroy (gftp_file * file, int free_it) |
1 | 104 { |
105 g_return_if_fail (file != NULL); | |
106 | |
107 if (file->file) | |
108 g_free (file->file); | |
109 if (file->user) | |
110 g_free (file->user); | |
111 if (file->group) | |
112 g_free (file->group); | |
113 if (file->destfile) | |
114 g_free (file->destfile); | |
598 | 115 |
116 if (free_it) | |
117 g_free (file); | |
118 else | |
119 memset (file, 0, sizeof (*file)); | |
1 | 120 } |
121 | |
122 | |
123 int | |
124 gftp_connect (gftp_request * request) | |
125 { | |
177 | 126 int ret; |
127 | |
84 | 128 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 129 |
130 if (request->connect == NULL) | |
84 | 131 return (GFTP_EFATAL); |
1 | 132 |
177 | 133 if ((ret = gftp_set_config_options (request)) < 0) |
134 return (ret); | |
1 | 135 |
136 return (request->connect (request)); | |
137 } | |
138 | |
139 | |
140 void | |
141 gftp_disconnect (gftp_request * request) | |
142 { | |
143 g_return_if_fail (request != NULL); | |
144 | |
168 | 145 #ifdef USE_SSL |
146 if (request->ssl != NULL) | |
147 { | |
148 SSL_free (request->ssl); | |
149 request->ssl = NULL; | |
150 } | |
151 #endif | |
152 | |
187 | 153 #if GLIB_MAJOR_VERSION > 1 |
899 | 154 if (request->iconv_from_initialized) |
155 { | |
156 g_iconv_close (request->iconv_from); | |
157 request->iconv_from_initialized = 0; | |
158 } | |
159 | |
160 if (request->iconv_to_initialized) | |
187 | 161 { |
899 | 162 g_iconv_close (request->iconv_to); |
163 request->iconv_to_initialized = 0; | |
187 | 164 } |
899 | 165 |
166 if (request->iconv_charset) | |
167 { | |
168 g_free (request->iconv_charset); | |
169 request->iconv_charset = NULL; | |
170 } | |
187 | 171 #endif |
172 | |
1 | 173 request->cached = 0; |
174 if (request->disconnect == NULL) | |
175 return; | |
176 request->disconnect (request); | |
177 } | |
178 | |
179 | |
58 | 180 off_t |
895 | 181 gftp_get_file (gftp_request * request, const char *filename, |
244 | 182 off_t startsize) |
1 | 183 { |
84 | 184 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 185 |
186 request->cached = 0; | |
187 if (request->get_file == NULL) | |
84 | 188 return (GFTP_EFATAL); |
244 | 189 |
895 | 190 return (request->get_file (request, filename, startsize)); |
1 | 191 } |
192 | |
193 | |
194 int | |
895 | 195 gftp_put_file (gftp_request * request, const char *filename, |
244 | 196 off_t startsize, off_t totalsize) |
1 | 197 { |
84 | 198 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 199 |
200 request->cached = 0; | |
201 if (request->put_file == NULL) | |
84 | 202 return (GFTP_EFATAL); |
566 | 203 |
895 | 204 return (request->put_file (request, filename, startsize, totalsize)); |
1 | 205 } |
206 | |
207 | |
261 | 208 off_t |
1 | 209 gftp_transfer_file (gftp_request * fromreq, const char *fromfile, |
895 | 210 off_t fromsize, gftp_request * toreq, const char *tofile, |
211 off_t tosize) | |
1 | 212 { |
469 | 213 /* Needed for systems that size(float) < size(void *) */ |
214 union { intptr_t i; float f; } maxkbs; | |
261 | 215 off_t size; |
84 | 216 int ret; |
1 | 217 |
84 | 218 g_return_val_if_fail (fromreq != NULL, GFTP_EFATAL); |
219 g_return_val_if_fail (fromfile != NULL, GFTP_EFATAL); | |
220 g_return_val_if_fail (toreq != NULL, GFTP_EFATAL); | |
221 g_return_val_if_fail (tofile != NULL, GFTP_EFATAL); | |
222 | |
469 | 223 gftp_lookup_request_option (toreq, "maxkbs", &maxkbs.f); |
224 | |
225 if (maxkbs.f > 0) | |
294 | 226 { |
227 toreq->logging_function (gftp_logging_misc, toreq, | |
228 _("File transfer will be throttled to %.2f KB/s\n"), | |
469 | 229 maxkbs.f); |
294 | 230 } |
231 | |
87 | 232 if (fromreq->protonum == toreq->protonum && |
84 | 233 fromreq->transfer_file != NULL) |
234 return (fromreq->transfer_file (fromreq, fromfile, fromsize, toreq, | |
235 tofile, tosize)); | |
1 | 236 |
237 fromreq->cached = 0; | |
238 toreq->cached = 0; | |
443 | 239 |
240 get_file: | |
895 | 241 size = gftp_get_file (fromreq, fromfile, tosize); |
443 | 242 if (size < 0) |
1 | 243 { |
443 | 244 if (size == GFTP_ETIMEDOUT) |
245 { | |
246 ret = gftp_connect (fromreq); | |
247 if (ret < 0) | |
248 return (ret); | |
249 | |
250 goto get_file; | |
251 } | |
252 | |
253 return (size); | |
254 } | |
255 | |
256 put_file: | |
895 | 257 ret = gftp_put_file (toreq, tofile, tosize, size); |
443 | 258 if (ret != 0) |
259 { | |
260 if (size == GFTP_ETIMEDOUT) | |
261 { | |
262 ret = gftp_connect (fromreq); | |
263 if (ret < 0) | |
264 return (ret); | |
265 | |
266 goto put_file; | |
267 } | |
268 | |
40 | 269 if (gftp_abort_transfer (fromreq) != 0) |
270 gftp_end_transfer (fromreq); | |
271 | |
84 | 272 return (ret); |
1 | 273 } |
274 | |
275 return (size); | |
276 } | |
277 | |
278 | |
58 | 279 ssize_t |
1 | 280 gftp_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
281 { | |
84 | 282 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
283 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 284 |
285 if (request->get_next_file_chunk != NULL) | |
286 return (request->get_next_file_chunk (request, buf, size)); | |
287 | |
168 | 288 return (request->read_function (request, buf, size, request->datafd)); |
1 | 289 } |
290 | |
291 | |
58 | 292 ssize_t |
1 | 293 gftp_put_next_file_chunk (gftp_request * request, char *buf, size_t size) |
294 { | |
84 | 295 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
296 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 297 |
298 if (request->put_next_file_chunk != NULL) | |
299 return (request->put_next_file_chunk (request, buf, size)); | |
300 | |
168 | 301 return (request->write_function (request, buf, size, request->datafd)); |
1 | 302 } |
303 | |
304 | |
305 int | |
306 gftp_end_transfer (gftp_request * request) | |
307 { | |
308 int ret; | |
309 | |
84 | 310 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 311 |
40 | 312 if (!request->cached && |
313 request->end_transfer != NULL) | |
314 ret = request->end_transfer (request); | |
315 else | |
316 ret = 0; | |
1 | 317 |
58 | 318 if (request->cachefd > 0) |
1 | 319 { |
58 | 320 close (request->cachefd); |
321 request->cachefd = -1; | |
1 | 322 } |
323 | |
324 if (request->last_dir_entry) | |
325 { | |
326 g_free (request->last_dir_entry); | |
327 request->last_dir_entry = NULL; | |
328 request->last_dir_entry_len = 0; | |
329 } | |
330 | |
331 return (ret); | |
332 } | |
333 | |
334 | |
335 int | |
40 | 336 gftp_abort_transfer (gftp_request * request) |
337 { | |
84 | 338 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
40 | 339 |
340 if (request->abort_transfer == NULL) | |
84 | 341 return (GFTP_EFATAL); |
40 | 342 |
813 | 343 /* FIXME - end the transfer if it is not successful */ |
40 | 344 return (request->abort_transfer (request)); |
345 } | |
346 | |
347 | |
520 | 348 int |
787 | 349 gftp_stat_filename (gftp_request * request, const char *filename, mode_t * mode, |
350 off_t * filesize) | |
500 | 351 { |
352 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
520 | 353 g_return_val_if_fail (filename != NULL, GFTP_EFATAL); |
500 | 354 |
355 if (request->stat_filename != NULL) | |
787 | 356 return (request->stat_filename (request, filename, mode, filesize)); |
500 | 357 else |
520 | 358 return (0); |
500 | 359 } |
360 | |
361 | |
40 | 362 int |
1 | 363 gftp_list_files (gftp_request * request) |
364 { | |
473 | 365 char *remote_lc_time, *locret; |
58 | 366 int fd; |
1 | 367 |
84 | 368 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 369 |
623 | 370 #if ENABLE_NLS |
473 | 371 gftp_lookup_request_option (request, "remote_lc_time", &remote_lc_time); |
372 if (remote_lc_time != NULL && *remote_lc_time != '\0') | |
373 locret = setlocale (LC_TIME, remote_lc_time); | |
374 else | |
375 locret = setlocale (LC_TIME, NULL); | |
376 | |
377 if (locret == NULL) | |
378 { | |
379 locret = setlocale (LC_TIME, NULL); | |
677 | 380 request->logging_function (gftp_logging_error, request, |
473 | 381 _("Error setting LC_TIME to '%s'. Falling back to '%s'\n"), |
382 remote_lc_time, locret); | |
383 } | |
623 | 384 #else |
385 locret = _("<unknown>"); | |
386 #endif | |
473 | 387 |
1 | 388 request->cached = 0; |
58 | 389 if (request->use_cache && (fd = gftp_find_cache_entry (request)) > 0) |
1 | 390 { |
186 | 391 request->logging_function (gftp_logging_misc, request, |
473 | 392 _("Loading directory listing %s from cache (LC_TIME=%s)\n"), |
393 request->directory, locret); | |
1 | 394 |
395 request->cachefd = fd; | |
396 request->cached = 1; | |
397 return (0); | |
398 } | |
399 else if (request->use_cache) | |
400 { | |
473 | 401 request->logging_function (gftp_logging_misc, request, |
402 _("Loading directory listing %s from server (LC_TIME=%s)\n"), | |
403 request->directory, locret); | |
404 | |
1 | 405 request->cachefd = gftp_new_cache_entry (request); |
406 request->cached = 0; | |
407 } | |
408 | |
409 if (request->list_files == NULL) | |
84 | 410 return (GFTP_EFATAL); |
473 | 411 |
1 | 412 return (request->list_files (request)); |
413 } | |
414 | |
415 | |
416 int | |
377 | 417 gftp_get_next_file (gftp_request * request, const char *filespec, |
418 gftp_file * fle) | |
1 | 419 { |
830 | 420 char *slashpos, *tmpfile, *utf8; |
838 | 421 size_t destlen; |
58 | 422 int fd, ret; |
1 | 423 |
84 | 424 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 425 |
426 if (request->get_next_file == NULL) | |
84 | 427 return (GFTP_EFATAL); |
1 | 428 |
58 | 429 if (request->cached && request->cachefd > 0) |
1 | 430 fd = request->cachefd; |
431 else | |
432 fd = request->datafd; | |
433 | |
434 memset (fle, 0, sizeof (*fle)); | |
435 do | |
436 { | |
598 | 437 gftp_file_destroy (fle, 0); |
1 | 438 ret = request->get_next_file (request, fle, fd); |
666 | 439 if (fle->file != NULL && (slashpos = strrchr (fle->file, '/')) != NULL) |
440 { | |
441 if (*(slashpos + 1) == '\0') | |
442 { | |
443 gftp_file_destroy (fle, 0); | |
444 continue; | |
445 } | |
446 | |
447 *slashpos = '\0'; | |
830 | 448 tmpfile = g_strdup (slashpos + 1); |
666 | 449 |
450 if (strcmp (fle->file, request->directory) != 0) | |
451 request->logging_function (gftp_logging_error, request, | |
452 _("Warning: Stripping path off of file '%s'. The stripped path (%s) doesn't match the current directory (%s)\n"), | |
830 | 453 tmpfile, fle->file, request->directory, |
666 | 454 g_strerror (errno)); |
455 | |
456 g_free (fle->file); | |
830 | 457 fle->file = tmpfile; |
666 | 458 } |
1 | 459 |
291 | 460 if (ret >= 0 && fle->file != NULL) |
830 | 461 { |
916 | 462 if (g_utf8_validate (fle->file, -1, NULL)) |
463 fle->filename_utf8_encoded = 1; | |
464 else | |
830 | 465 { |
916 | 466 utf8 = gftp_filename_to_utf8 (request, fle->file, &destlen); |
467 if (utf8 != NULL) | |
468 { | |
469 g_free (fle->file); | |
470 fle->file = utf8; | |
471 fle->filename_utf8_encoded = 1; | |
472 } | |
830 | 473 } |
474 } | |
45 | 475 |
60 | 476 if (ret >= 0 && !request->cached && request->cachefd > 0 && |
1 | 477 request->last_dir_entry != NULL) |
478 { | |
168 | 479 if (gftp_fd_write (request, request->last_dir_entry, |
60 | 480 request->last_dir_entry_len, request->cachefd) < 0) |
1 | 481 { |
186 | 482 request->logging_function (gftp_logging_error, request, |
1 | 483 _("Error: Cannot write to cache: %s\n"), |
484 g_strerror (errno)); | |
60 | 485 close (request->cachefd); |
486 request->cachefd = -1; | |
1 | 487 } |
488 } | |
821 | 489 } while (ret > 0 && !gftp_match_filespec (request, fle->file, filespec)); |
1 | 490 |
491 return (ret); | |
492 } | |
493 | |
494 | |
495 int | |
243 | 496 gftp_parse_bookmark (gftp_request * request, gftp_request * local_request, |
275 | 497 const char * bookmark, int *refresh_local) |
87 | 498 { |
499 gftp_logging_func logging_function; | |
122 | 500 gftp_bookmarks_var * tempentry; |
838 | 501 char *default_protocol; |
646 | 502 const char *email; |
173 | 503 int i, init_ret; |
87 | 504 |
505 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
506 g_return_val_if_fail (bookmark != NULL, GFTP_EFATAL); | |
507 | |
508 logging_function = request->logging_function; | |
509 gftp_request_destroy (request, 0); | |
510 request->logging_function = logging_function; | |
511 | |
122 | 512 if ((tempentry = g_hash_table_lookup (gftp_bookmarks_htable, |
513 bookmark)) == NULL) | |
87 | 514 { |
186 | 515 request->logging_function (gftp_logging_error, request, |
87 | 516 _("Error: Could not find bookmark %s\n"), |
517 bookmark); | |
518 return (GFTP_EFATAL); | |
519 } | |
520 else if (tempentry->hostname == NULL || *tempentry->hostname == '\0') | |
521 { | |
186 | 522 request->logging_function (gftp_logging_error, request, |
87 | 523 _("Bookmarks Error: The bookmark entry %s does not have a hostname\n"), bookmark); |
524 return (GFTP_EFATAL); | |
525 } | |
526 | |
527 if (tempentry->user != NULL) | |
528 gftp_set_username (request, tempentry->user); | |
529 | |
530 if (tempentry->pass != NULL) | |
646 | 531 { |
532 if (strcmp (tempentry->pass, "@EMAIL@") == 0) | |
533 { | |
534 gftp_lookup_request_option (request, "email", &email); | |
535 gftp_set_password (request, email); | |
536 } | |
537 else | |
538 gftp_set_password (request, tempentry->pass); | |
539 } | |
87 | 540 |
541 if (tempentry->acct != NULL) | |
542 gftp_set_account (request, tempentry->acct); | |
543 | |
544 gftp_set_hostname (request, tempentry->hostname); | |
838 | 545 gftp_set_directory (request, tempentry->remote_dir); |
87 | 546 gftp_set_port (request, tempentry->port); |
547 | |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
548 /* charset */ |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
549 gftp_set_remote_charset (request, tempentry->remote_charset); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
550 |
516 | 551 if (local_request != NULL && tempentry->local_dir != NULL && |
243 | 552 *tempentry->local_dir != '\0') |
275 | 553 { |
838 | 554 gftp_set_directory (local_request, tempentry->local_dir); |
516 | 555 if (refresh_local != NULL) |
556 *refresh_local = 1; | |
275 | 557 } |
516 | 558 else if (refresh_local != NULL) |
275 | 559 *refresh_local = 0; |
243 | 560 |
87 | 561 for (i = 0; gftp_protocols[i].name; i++) |
562 { | |
563 if (strcmp (gftp_protocols[i].name, tempentry->protocol) == 0) | |
564 { | |
173 | 565 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
566 { | |
567 gftp_request_destroy (request, 0); | |
568 return (init_ret); | |
569 } | |
87 | 570 break; |
571 } | |
572 } | |
573 | |
122 | 574 if (gftp_protocols[i].name == NULL) |
87 | 575 { |
122 | 576 gftp_lookup_request_option (request, "default_protocol", |
577 &default_protocol); | |
578 | |
125 | 579 if (default_protocol != NULL && *default_protocol != '\0') |
87 | 580 { |
122 | 581 for (i = 0; gftp_protocols[i].url_prefix; i++) |
582 { | |
583 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
584 break; | |
585 } | |
87 | 586 } |
587 | |
588 if (gftp_protocols[i].url_prefix == NULL) | |
589 i = GFTP_FTP_NUM; | |
590 } | |
591 | |
199 | 592 gftp_copy_local_options (&request->local_options_vars, |
593 &request->local_options_hash, | |
429 | 594 &request->num_local_options_vars, |
199 | 595 tempentry->local_options_vars, |
596 tempentry->num_local_options_vars); | |
597 | |
173 | 598 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
599 { | |
600 gftp_request_destroy (request, 0); | |
601 return (init_ret); | |
602 } | |
603 | |
87 | 604 return (0); |
605 } | |
606 | |
607 | |
608 int | |
1 | 609 gftp_parse_url (gftp_request * request, const char *url) |
610 { | |
676 | 611 char *pos, *endpos, *default_protocol, *new_url; |
67 | 612 gftp_logging_func logging_function; |
676 | 613 const char *clear_pos; |
614 int i, ret; | |
1 | 615 |
84 | 616 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
617 g_return_val_if_fail (url != NULL, GFTP_EFATAL); | |
1 | 618 |
67 | 619 logging_function = request->logging_function; |
620 gftp_request_destroy (request, 0); | |
621 request->logging_function = logging_function; | |
622 | |
676 | 623 for (clear_pos = url; |
624 *clear_pos == ' ' || *clear_pos == '\t'; | |
625 clear_pos++); | |
626 | |
627 new_url = g_strdup (clear_pos); | |
628 | |
629 for (pos = new_url + strlen (new_url) - 1; | |
630 *pos == ' ' || *pos == '\t'; | |
631 pos--) | |
168 | 632 *pos = '\0'; |
1 | 633 |
676 | 634 /* See if the URL has a protocol... */ |
635 if ((pos = strstr (new_url, "://")) != NULL) | |
1 | 636 { |
637 *pos = '\0'; | |
638 | |
639 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
640 { | |
676 | 641 if (strcmp (gftp_protocols[i].url_prefix, new_url) == 0) |
1 | 642 break; |
643 } | |
644 | |
645 if (gftp_protocols[i].url_prefix == NULL) | |
168 | 646 { |
677 | 647 request->logging_function (gftp_logging_error, NULL, |
173 | 648 _("The protocol '%s' is currently not supported.\n"), |
676 | 649 new_url); |
650 g_free (new_url); | |
168 | 651 return (GFTP_EFATAL); |
652 } | |
173 | 653 |
654 *pos = ':'; | |
676 | 655 pos += 3; |
1 | 656 } |
657 else | |
658 { | |
122 | 659 gftp_lookup_request_option (request, "default_protocol", |
660 &default_protocol); | |
661 | |
676 | 662 i = GFTP_FTP_NUM; |
125 | 663 if (default_protocol != NULL && *default_protocol != '\0') |
1 | 664 { |
122 | 665 for (i = 0; gftp_protocols[i].url_prefix; i++) |
666 { | |
667 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
668 break; | |
669 } | |
1 | 670 } |
676 | 671 |
672 if (gftp_protocols[i].url_prefix == NULL) | |
673 { | |
677 | 674 request->logging_function (gftp_logging_error, NULL, |
676 | 675 _("The protocol '%s' is currently not supported.\n"), |
676 default_protocol); | |
677 g_free (new_url); | |
678 return (GFTP_EFATAL); | |
679 } | |
680 | |
681 pos = new_url; | |
122 | 682 } |
1 | 683 |
676 | 684 if ((ret = gftp_protocols[i].init (request)) < 0) |
173 | 685 { |
686 gftp_request_destroy (request, 0); | |
676 | 687 return (ret); |
688 } | |
689 | |
690 if ((endpos = strchr (pos, '/')) != NULL) | |
691 { | |
692 gftp_set_directory (request, endpos); | |
693 *endpos = '\0'; | |
173 | 694 } |
1 | 695 |
696 if (request->parse_url != NULL) | |
168 | 697 { |
676 | 698 ret = request->parse_url (request, new_url); |
699 g_free (new_url); | |
700 return (ret); | |
168 | 701 } |
702 | |
676 | 703 if (*pos != '\0') |
1 | 704 { |
676 | 705 if (endpos == NULL) |
706 endpos = pos + strlen (pos) - 1; | |
707 else | |
708 endpos--; | |
709 | |
710 for (; isdigit (*endpos); endpos--); | |
711 | |
712 if (*endpos == ':' && isdigit (*(endpos + 1))) | |
168 | 713 { |
676 | 714 gftp_set_port (request, strtol (endpos + 1, NULL, 10)); |
715 *endpos = '\0'; | |
168 | 716 } |
1 | 717 |
676 | 718 if ((endpos = strrchr (pos, '@')) != NULL) |
168 | 719 { |
676 | 720 gftp_set_hostname (request, endpos + 1); |
168 | 721 *endpos = '\0'; |
676 | 722 |
723 if ((endpos = strchr (pos, ':')) != NULL) | |
724 { | |
725 *endpos = '\0'; | |
726 gftp_set_username (request, pos); | |
727 gftp_set_password (request, endpos + 1); | |
728 } | |
729 else | |
730 { | |
731 gftp_set_username (request, pos); | |
732 gftp_set_password (request, ""); | |
733 } | |
168 | 734 } |
676 | 735 else |
736 gftp_set_hostname (request, pos); | |
1 | 737 } |
738 | |
676 | 739 g_free (new_url); |
1 | 740 return (0); |
741 } | |
742 | |
743 | |
744 void | |
745 gftp_set_hostname (gftp_request * request, const char *hostname) | |
746 { | |
747 g_return_if_fail (request != NULL); | |
748 g_return_if_fail (hostname != NULL); | |
749 | |
750 if (request->hostname) | |
751 g_free (request->hostname); | |
124 | 752 request->hostname = g_strdup (hostname); |
1 | 753 } |
754 | |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
755 void |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
756 gftp_set_remote_charset(gftp_request * request, const char *remote_charset) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
757 { |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
758 g_return_if_fail (request != NULL); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
759 g_return_if_fail (remote_charset != NULL); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
760 |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
761 if (request->remote_charset) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
762 g_free (request->remote_charset); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
763 request->remote_charset = g_strdup (remote_charset); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
950
diff
changeset
|
764 } |
1 | 765 |
766 void | |
767 gftp_set_username (gftp_request * request, const char *username) | |
768 { | |
769 g_return_if_fail (request != NULL); | |
770 | |
771 if (request->username) | |
772 g_free (request->username); | |
169 | 773 |
774 if (username != NULL) | |
775 request->username = g_strdup (username); | |
776 else | |
777 request->username = NULL; | |
1 | 778 } |
779 | |
780 | |
781 void | |
782 gftp_set_password (gftp_request * request, const char *password) | |
783 { | |
784 g_return_if_fail (request != NULL); | |
785 | |
786 if (request->password) | |
787 g_free (request->password); | |
169 | 788 |
789 if (password != NULL) | |
790 request->password = g_strdup (password); | |
791 else | |
792 request->password = NULL; | |
1 | 793 } |
794 | |
795 | |
796 void | |
797 gftp_set_account (gftp_request * request, const char *account) | |
798 { | |
799 g_return_if_fail (request != NULL); | |
800 g_return_if_fail (account != NULL); | |
801 | |
802 if (request->account) | |
803 g_free (request->account); | |
124 | 804 request->account = g_strdup (account); |
1 | 805 } |
806 | |
807 | |
808 int | |
809 gftp_set_directory (gftp_request * request, const char *directory) | |
810 { | |
84 | 811 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
812 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
1 | 813 |
814 | |
169 | 815 if (request->datafd <= 0 && !request->always_connected) |
1 | 816 { |
817 if (directory != request->directory) | |
168 | 818 { |
819 if (request->directory) | |
820 g_free (request->directory); | |
821 request->directory = g_strdup (directory); | |
822 } | |
1 | 823 return (0); |
824 } | |
825 else if (request->chdir == NULL) | |
84 | 826 return (GFTP_EFATAL); |
1 | 827 return (request->chdir (request, directory)); |
828 } | |
829 | |
830 | |
831 void | |
832 gftp_set_port (gftp_request * request, unsigned int port) | |
833 { | |
834 g_return_if_fail (request != NULL); | |
835 | |
836 request->port = port; | |
837 } | |
838 | |
839 | |
840 int | |
841 gftp_remove_directory (gftp_request * request, const char *directory) | |
842 { | |
84 | 843 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 844 |
845 if (request->rmdir == NULL) | |
84 | 846 return (GFTP_EFATAL); |
1 | 847 return (request->rmdir (request, directory)); |
848 } | |
849 | |
850 | |
851 int | |
852 gftp_remove_file (gftp_request * request, const char *file) | |
853 { | |
84 | 854 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 855 |
856 if (request->rmfile == NULL) | |
84 | 857 return (GFTP_EFATAL); |
1 | 858 return (request->rmfile (request, file)); |
859 } | |
860 | |
861 | |
862 int | |
863 gftp_make_directory (gftp_request * request, const char *directory) | |
864 { | |
84 | 865 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 866 |
867 if (request->mkdir == NULL) | |
84 | 868 return (GFTP_EFATAL); |
291 | 869 |
838 | 870 return (request->mkdir (request, directory)); |
1 | 871 } |
872 | |
873 | |
874 int | |
875 gftp_rename_file (gftp_request * request, const char *oldname, | |
168 | 876 const char *newname) |
1 | 877 { |
84 | 878 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 879 |
880 if (request->rename == NULL) | |
84 | 881 return (GFTP_EFATAL); |
291 | 882 |
838 | 883 return (request->rename (request, oldname, newname)); |
1 | 884 } |
885 | |
886 | |
887 int | |
499 | 888 gftp_chmod (gftp_request * request, const char *file, mode_t mode) |
1 | 889 { |
84 | 890 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 891 |
892 if (request->chmod == NULL) | |
84 | 893 return (GFTP_EFATAL); |
504 | 894 |
895 mode &= S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX; | |
1 | 896 return (request->chmod (request, file, mode)); |
897 } | |
898 | |
899 | |
900 int | |
901 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
902 { | |
84 | 903 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 904 |
905 if (request->set_file_time == NULL) | |
84 | 906 return (GFTP_EFATAL); |
1 | 907 return (request->set_file_time (request, file, datetime)); |
908 } | |
909 | |
910 | |
765 | 911 int |
478 | 912 gftp_site_cmd (gftp_request * request, int specify_site, const char *command) |
1 | 913 { |
84 | 914 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 915 |
916 if (request->site == NULL) | |
84 | 917 return (GFTP_EFATAL); |
478 | 918 return (request->site (request, specify_site, command)); |
1 | 919 } |
920 | |
921 | |
58 | 922 off_t |
1 | 923 gftp_get_file_size (gftp_request * request, const char *filename) |
924 { | |
925 g_return_val_if_fail (request != NULL, 0); | |
926 | |
927 if (request->get_file_size == NULL) | |
928 return (0); | |
929 return (request->get_file_size (request, filename)); | |
930 } | |
931 | |
932 | |
48 | 933 static GHashTable * |
469 | 934 gftp_gen_dir_hash (gftp_request * request, int *ret) |
1 | 935 { |
48 | 936 GHashTable * dirhash; |
937 gftp_file * fle; | |
516 | 938 off_t *newsize; |
48 | 939 |
851 | 940 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); |
850 | 941 *ret = gftp_list_files (request); |
851 | 942 if (*ret == 0) |
48 | 943 { |
851 | 944 fle = g_malloc0 (sizeof (*fle)); |
945 while (gftp_get_next_file (request, NULL, fle) > 0) | |
48 | 946 { |
944 | 947 newsize = g_malloc0 (sizeof (*newsize)); |
851 | 948 *newsize = fle->size; |
949 g_hash_table_insert (dirhash, fle->file, newsize); | |
950 fle->file = NULL; | |
598 | 951 gftp_file_destroy (fle, 0); |
48 | 952 } |
851 | 953 gftp_end_transfer (request); |
954 g_free (fle); | |
48 | 955 } |
851 | 956 else |
957 { | |
958 g_hash_table_destroy (dirhash); | |
959 dirhash = NULL; | |
960 } | |
509 | 961 |
48 | 962 return (dirhash); |
963 } | |
39 | 964 |
48 | 965 |
966 static void | |
967 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
968 { | |
39 | 969 |
48 | 970 g_free (key); |
971 g_free (value); | |
972 } | |
973 | |
974 | |
975 static void | |
976 gftp_destroy_dir_hash (GHashTable * dirhash) | |
977 { | |
787 | 978 if (dirhash == NULL) |
979 return; | |
980 | |
48 | 981 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); |
982 g_hash_table_destroy (dirhash); | |
1 | 983 } |
984 | |
985 | |
986 static GList * | |
469 | 987 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir, int *ret) |
1 | 988 { |
989 GHashTable * dirhash; | |
990 GList * templist; | |
991 gftp_file * fle; | |
516 | 992 off_t *newsize; |
1 | 993 char *newname; |
994 | |
787 | 995 if (getothdir && transfer->toreq != NULL) |
469 | 996 { |
997 dirhash = gftp_gen_dir_hash (transfer->toreq, ret); | |
787 | 998 if (*ret == GFTP_EFATAL) |
469 | 999 return (NULL); |
1000 } | |
1 | 1001 else |
1002 dirhash = NULL; | |
1003 | |
509 | 1004 *ret = gftp_list_files (transfer->fromreq); |
1005 if (*ret < 0) | |
787 | 1006 { |
1007 gftp_destroy_dir_hash (dirhash); | |
1008 return (NULL); | |
1009 } | |
1 | 1010 |
944 | 1011 fle = g_malloc0 (sizeof (*fle)); |
1 | 1012 templist = NULL; |
1013 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
1014 { | |
851 | 1015 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) |
1 | 1016 { |
598 | 1017 gftp_file_destroy (fle, 0); |
1 | 1018 continue; |
1019 } | |
1020 | |
1021 if (dirhash && | |
1022 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
787 | 1023 { |
1024 fle->exists_other_side = 1; | |
1025 fle->startsize = *newsize; | |
1026 } | |
1027 else | |
1028 fle->exists_other_side = 0; | |
1 | 1029 |
381 | 1030 if (transfer->toreq && fle->destfile == NULL) |
555 | 1031 fle->destfile = gftp_build_path (transfer->toreq, |
1032 transfer->toreq->directory, | |
245 | 1033 fle->file, NULL); |
1034 | |
381 | 1035 if (transfer->fromreq->directory != NULL && |
1036 *transfer->fromreq->directory != '\0' && | |
1037 *fle->file != '/') | |
1038 { | |
555 | 1039 newname = gftp_build_path (transfer->fromreq, |
1040 transfer->fromreq->directory, | |
381 | 1041 fle->file, NULL); |
1042 | |
1043 g_free (fle->file); | |
1044 fle->file = newname; | |
1045 } | |
1 | 1046 |
1047 templist = g_list_append (templist, fle); | |
1048 | |
787 | 1049 fle = g_malloc0 (sizeof (*fle)); |
1 | 1050 } |
1051 gftp_end_transfer (transfer->fromreq); | |
1052 | |
598 | 1053 gftp_file_destroy (fle, 1); |
787 | 1054 gftp_destroy_dir_hash (dirhash); |
1 | 1055 |
1056 return (templist); | |
1057 } | |
1058 | |
1059 | |
787 | 1060 static void |
1061 _cleanup_get_all_subdirs (gftp_transfer * transfer, char *oldfromdir, | |
1062 char *oldtodir, | |
1063 void (*update_func) (gftp_transfer * transfer)) | |
1 | 1064 { |
787 | 1065 if (update_func != NULL) |
1066 { | |
1067 transfer->numfiles = transfer->numdirs = -1; | |
1068 update_func (transfer); | |
1069 } | |
1070 | |
1071 if (oldfromdir != NULL) | |
1072 g_free (oldfromdir); | |
1073 | |
1074 if (oldtodir != NULL) | |
1075 g_free (oldtodir); | |
1076 } | |
1077 | |
1078 | |
1079 static GList * | |
1080 _setup_current_directory_transfer (gftp_transfer * transfer, int *ret) | |
1081 { | |
1 | 1082 GHashTable * dirhash; |
787 | 1083 char *pos, *newname; |
1 | 1084 gftp_file * curfle; |
787 | 1085 GList * lastlist; |
516 | 1086 off_t *newsize; |
787 | 1087 |
1088 *ret = 0; | |
1 | 1089 if (transfer->toreq != NULL) |
469 | 1090 { |
787 | 1091 dirhash = gftp_gen_dir_hash (transfer->toreq, ret); |
1092 if (*ret == GFTP_EFATAL) | |
1093 return (NULL); | |
469 | 1094 } |
1 | 1095 else |
1096 dirhash = NULL; | |
1097 | |
1098 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
1099 { | |
1100 curfle = lastlist->data; | |
381 | 1101 |
1102 if ((pos = strrchr (curfle->file, '/')) != NULL) | |
1103 pos++; | |
1104 else | |
1105 pos = curfle->file; | |
1106 | |
1107 if (dirhash != NULL && | |
1108 (newsize = g_hash_table_lookup (dirhash, pos)) != NULL) | |
787 | 1109 { |
1110 curfle->exists_other_side = 1; | |
1111 curfle->startsize = *newsize; | |
1112 } | |
1113 else | |
1114 curfle->exists_other_side = 0; | |
1 | 1115 |
381 | 1116 if (curfle->size < 0 && GFTP_IS_CONNECTED (transfer->fromreq)) |
509 | 1117 { |
1118 curfle->size = gftp_get_file_size (transfer->fromreq, curfle->file); | |
787 | 1119 if (curfle->size == GFTP_EFATAL) |
1120 { | |
1121 gftp_destroy_dir_hash (dirhash); | |
1122 *ret = curfle->size; | |
1123 return (NULL); | |
1124 } | |
509 | 1125 } |
381 | 1126 |
1127 if (transfer->toreq && curfle->destfile == NULL) | |
555 | 1128 curfle->destfile = gftp_build_path (transfer->toreq, |
1129 transfer->toreq->directory, | |
381 | 1130 curfle->file, NULL); |
1131 | |
1132 if (transfer->fromreq->directory != NULL && | |
509 | 1133 *transfer->fromreq->directory != '\0' && *curfle->file != '/') |
381 | 1134 { |
555 | 1135 newname = gftp_build_path (transfer->fromreq, |
1136 transfer->fromreq->directory, | |
381 | 1137 curfle->file, NULL); |
1138 g_free (curfle->file); | |
1139 curfle->file = newname; | |
1140 } | |
1 | 1141 |
1142 if (lastlist->next == NULL) | |
1143 break; | |
1144 } | |
1145 | |
787 | 1146 gftp_destroy_dir_hash (dirhash); |
1147 | |
1148 return (lastlist); | |
1149 } | |
1150 | |
1151 | |
852 | 1152 static int |
1153 _lookup_curfle_in_device_hash (gftp_request * request, gftp_file * curfle, | |
1154 GHashTable * device_hash) | |
1155 { | |
1156 GHashTable * inode_hash; | |
1157 | |
1158 if (curfle->st_dev == 0 || curfle->st_ino == 0) | |
1159 return (0); | |
1160 | |
1161 if ((inode_hash = g_hash_table_lookup (device_hash, | |
1162 GUINT_TO_POINTER ((guint) curfle->st_dev))) != NULL) | |
1163 { | |
1164 if (g_hash_table_lookup (inode_hash, | |
1165 GUINT_TO_POINTER ((guint) curfle->st_ino))) | |
1166 { | |
1167 request->logging_function (gftp_logging_error, request, | |
1168 _("Found recursive symbolic link %s\n"), | |
1169 curfle->file); | |
1170 return (1); | |
1171 } | |
1172 | |
1173 g_hash_table_insert (inode_hash, GUINT_TO_POINTER ((guint) curfle->st_ino), | |
1174 GUINT_TO_POINTER (1)); | |
1175 return (0); | |
1176 } | |
1177 else | |
1178 { | |
1179 inode_hash = g_hash_table_new (uint_hash_function, uint_hash_compare); | |
1180 g_hash_table_insert (inode_hash, GUINT_TO_POINTER ((guint) curfle->st_ino), | |
1181 GUINT_TO_POINTER (1)); | |
1182 g_hash_table_insert (device_hash, GUINT_TO_POINTER ((guint) curfle->st_dev), | |
1183 inode_hash); | |
1184 return (0); | |
1185 } | |
1186 | |
1187 } | |
1188 | |
1189 | |
1190 static void | |
1191 _free_inode_hash (gpointer key, gpointer value, gpointer user_data) | |
1192 { | |
1193 g_hash_table_destroy (value); | |
1194 } | |
1195 | |
1196 | |
1197 static void | |
1198 _free_device_hash (GHashTable * device_hash) | |
1199 { | |
1200 g_hash_table_foreach (device_hash, _free_inode_hash, NULL); | |
1201 g_hash_table_destroy (device_hash); | |
1202 } | |
1203 | |
1204 | |
787 | 1205 int |
1206 gftp_get_all_subdirs (gftp_transfer * transfer, | |
1207 void (*update_func) (gftp_transfer * transfer)) | |
1208 { | |
1209 GList * templist, * lastlist; | |
1210 char *oldfromdir, *oldtodir; | |
852 | 1211 GHashTable * device_hash; |
787 | 1212 gftp_file * curfle; |
1213 off_t linksize; | |
1214 mode_t st_mode; | |
1215 int ret; | |
1216 | |
1217 g_return_val_if_fail (transfer != NULL, GFTP_EFATAL); | |
1218 g_return_val_if_fail (transfer->fromreq != NULL, GFTP_EFATAL); | |
1219 g_return_val_if_fail (transfer->files != NULL, GFTP_EFATAL); | |
1220 | |
1221 if (transfer->files == NULL) | |
1222 return (0); | |
1223 | |
1224 ret = 0; | |
1225 lastlist = _setup_current_directory_transfer (transfer, &ret); | |
1226 if (lastlist == NULL) | |
1227 return (ret); | |
1 | 1228 |
1229 oldfromdir = oldtodir = NULL; | |
852 | 1230 device_hash = g_hash_table_new (uint_hash_function, uint_hash_compare); |
787 | 1231 |
1 | 1232 for (templist = transfer->files; templist != NULL; templist = templist->next) |
1233 { | |
1234 curfle = templist->data; | |
1235 | |
852 | 1236 if (_lookup_curfle_in_device_hash (transfer->fromreq, curfle, |
1237 device_hash)) | |
1238 continue; | |
1239 | |
500 | 1240 if (S_ISLNK (curfle->st_mode) && !S_ISDIR (curfle->st_mode)) |
1241 { | |
520 | 1242 st_mode = 0; |
787 | 1243 linksize = 0; |
1244 ret = gftp_stat_filename (transfer->fromreq, curfle->file, &st_mode, | |
1245 &linksize); | |
855 | 1246 if (ret == GFTP_EFATAL) |
787 | 1247 { |
1248 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
1249 update_func); | |
1250 return (ret); | |
1251 } | |
855 | 1252 else if (ret == 0) |
1253 { | |
1254 if (S_ISDIR (st_mode)) | |
1255 curfle->st_mode = st_mode; | |
1256 else | |
1257 curfle->size = linksize; | |
1258 } | |
500 | 1259 } |
1260 | |
826 | 1261 if (!S_ISDIR (curfle->st_mode)) |
1 | 1262 { |
787 | 1263 transfer->numfiles++; |
1264 continue; | |
1265 } | |
1266 | |
1267 /* Got a directory... */ | |
826 | 1268 transfer->numdirs++; |
1269 | |
787 | 1270 if (oldfromdir == NULL) |
1271 oldfromdir = g_strdup (transfer->fromreq->directory); | |
1272 | |
1273 ret = gftp_set_directory (transfer->fromreq, curfle->file); | |
1274 if (ret < 0) | |
1275 { | |
1276 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
1277 update_func); | |
852 | 1278 _free_device_hash (device_hash); |
787 | 1279 return (ret); |
1280 } | |
1281 | |
1282 if (transfer->toreq != NULL) | |
1283 { | |
1284 if (oldtodir == NULL) | |
1285 oldtodir = g_strdup (transfer->toreq->directory); | |
1286 | |
1287 if (curfle->exists_other_side) | |
1 | 1288 { |
787 | 1289 ret = gftp_set_directory (transfer->toreq, curfle->destfile); |
1290 if (ret == GFTP_EFATAL) | |
1291 { | |
1292 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
1293 update_func); | |
852 | 1294 _free_device_hash (device_hash); |
787 | 1295 return (ret); |
1296 } | |
1297 } | |
1298 else | |
509 | 1299 { |
787 | 1300 if (transfer->toreq->directory != NULL) |
1301 g_free (transfer->toreq->directory); | |
1302 | |
1303 transfer->toreq->directory = g_strdup (curfle->destfile); | |
1 | 1304 } |
787 | 1305 } |
1306 | |
1307 ret = 0; | |
1308 lastlist->next = gftp_get_dir_listing (transfer, | |
1309 curfle->exists_other_side, &ret); | |
1310 if (ret < 0) | |
1311 { | |
1312 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
1313 update_func); | |
852 | 1314 _free_device_hash (device_hash); |
787 | 1315 return (ret); |
1 | 1316 } |
787 | 1317 |
1318 if (lastlist->next != NULL) | |
1319 { | |
1320 lastlist->next->prev = lastlist; | |
1321 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
1322 } | |
1323 | |
1324 if (update_func != NULL) | |
1325 update_func (transfer); | |
1 | 1326 } |
1327 | |
852 | 1328 _free_device_hash (device_hash); |
1329 | |
787 | 1330 if (oldfromdir != NULL) |
509 | 1331 { |
787 | 1332 ret = gftp_set_directory (transfer->fromreq, oldfromdir); |
855 | 1333 if (ret == GFTP_EFATAL) |
787 | 1334 { |
1335 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
1336 update_func); | |
1337 return (ret); | |
1338 } | |
509 | 1339 } |
1340 | |
787 | 1341 if (oldtodir != NULL) |
509 | 1342 { |
787 | 1343 ret = gftp_set_directory (transfer->toreq, oldtodir); |
855 | 1344 if (ret == GFTP_EFATAL) |
787 | 1345 { |
1346 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, | |
1347 update_func); | |
1348 return (ret); | |
1349 } | |
509 | 1350 } |
1351 | |
787 | 1352 _cleanup_get_all_subdirs (transfer, oldfromdir, oldtodir, update_func); |
509 | 1353 |
1 | 1354 return (0); |
1355 } | |
1356 | |
1357 | |
177 | 1358 int |
1 | 1359 gftp_set_config_options (gftp_request * request) |
1360 { | |
58 | 1361 if (request->set_config_options != NULL) |
177 | 1362 return (request->set_config_options (request)); |
1363 else | |
1364 return (0); | |
1 | 1365 } |
1366 | |
1367 | |
1368 void | |
1369 print_file_list (GList * list) | |
1370 { | |
1371 gftp_file * tempfle; | |
1372 GList * templist; | |
499 | 1373 char *attribs; |
1 | 1374 |
1375 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
1376 for (templist = list; ; templist = templist->next) | |
1377 { | |
1378 tempfle = templist->data; | |
499 | 1379 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
1380 | |
532 | 1381 printf ("%s:%s:" GFTP_OFF_T_PRINTF_MOD ":" GFTP_OFF_T_PRINTF_MOD ":%s:%s:%s\n", |
372 | 1382 tempfle->file, tempfle->destfile, |
516 | 1383 tempfle->size, tempfle->startsize, |
499 | 1384 tempfle->user, tempfle->group, attribs); |
1385 | |
1386 g_free (attribs); | |
1 | 1387 if (templist->next == NULL) |
1388 break; | |
1389 } | |
1390 | |
1391 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
1392 for (; ; templist = templist->prev) | |
1393 { | |
1394 tempfle = templist->data; | |
499 | 1395 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
1396 | |
532 | 1397 printf ("%s:%s:" GFTP_OFF_T_PRINTF_MOD ":" GFTP_OFF_T_PRINTF_MOD ":%s:%s:%s\n", |
372 | 1398 tempfle->file, tempfle->destfile, |
516 | 1399 tempfle->size, tempfle->startsize, |
499 | 1400 tempfle->user, tempfle->group, attribs); |
1401 | |
1402 g_free (attribs); | |
1 | 1403 if (templist == list) |
1404 break; | |
1405 } | |
1406 printf ("--END OF FILE LISTING--\n"); | |
1407 } | |
1408 | |
41 | 1409 |
201 | 1410 void |
63 | 1411 gftp_swap_socks (gftp_request * dest, gftp_request * source) |
1412 { | |
1413 g_return_if_fail (dest != NULL); | |
1414 g_return_if_fail (source != NULL); | |
1415 g_return_if_fail (dest->protonum == source->protonum); | |
1416 | |
1417 dest->datafd = source->datafd; | |
1418 dest->cached = 0; | |
397 | 1419 #ifdef USE_SSL |
1420 dest->ssl = source->ssl; | |
1421 #endif | |
1422 | |
63 | 1423 if (!source->always_connected) |
1424 { | |
1425 source->datafd = -1; | |
1426 source->cached = 1; | |
397 | 1427 #ifdef USE_SSL |
1428 source->ssl = NULL; | |
1429 #endif | |
63 | 1430 } |
1431 | |
516 | 1432 if (dest->swap_socks != NULL) |
63 | 1433 dest->swap_socks (dest, source); |
1434 } | |
1435 | |
122 | 1436 |
1437 void | |
1438 gftp_calc_kbs (gftp_transfer * tdata, ssize_t num_read) | |
1439 { | |
469 | 1440 /* Needed for systems that size(float) < size(void *) */ |
1441 union { intptr_t i; float f; } maxkbs; | |
122 | 1442 unsigned long waitusecs; |
220 | 1443 double start_difftime; |
122 | 1444 struct timeval tv; |
222 | 1445 int waited; |
122 | 1446 |
469 | 1447 gftp_lookup_request_option (tdata->fromreq, "maxkbs", &maxkbs.f); |
122 | 1448 |
1449 if (g_thread_supported ()) | |
1450 g_static_mutex_lock (&tdata->statmutex); | |
1451 | |
220 | 1452 gettimeofday (&tv, NULL); |
1453 | |
122 | 1454 tdata->trans_bytes += num_read; |
1455 tdata->curtrans += num_read; | |
1456 tdata->stalled = 0; | |
1457 | |
220 | 1458 start_difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0); |
1459 | |
1460 if (start_difftime <= 0) | |
1461 tdata->kbs = tdata->trans_bytes / 1024.0; | |
122 | 1462 else |
220 | 1463 tdata->kbs = tdata->trans_bytes / 1024.0 / start_difftime; |
1464 | |
222 | 1465 waited = 0; |
469 | 1466 if (maxkbs.f > 0 && tdata->kbs > maxkbs.f) |
122 | 1467 { |
469 | 1468 waitusecs = num_read / 1024.0 / maxkbs.f * 1000000.0 - start_difftime; |
122 | 1469 |
1470 if (waitusecs > 0) | |
1471 { | |
1472 if (g_thread_supported ()) | |
1473 g_static_mutex_unlock (&tdata->statmutex); | |
1474 | |
222 | 1475 waited = 1; |
122 | 1476 usleep (waitusecs); |
1477 | |
1478 if (g_thread_supported ()) | |
1479 g_static_mutex_lock (&tdata->statmutex); | |
1480 } | |
222 | 1481 |
122 | 1482 } |
1483 | |
222 | 1484 if (waited) |
1485 gettimeofday (&tdata->lasttime, NULL); | |
1486 else | |
1487 memcpy (&tdata->lasttime, &tv, sizeof (tdata->lasttime)); | |
122 | 1488 |
1489 if (g_thread_supported ()) | |
1490 g_static_mutex_unlock (&tdata->statmutex); | |
1491 } | |
1492 | |
125 | 1493 |
764 | 1494 static int |
1495 _do_sleep (int sleep_time) | |
1496 { | |
1497 struct timeval tv; | |
1498 | |
1499 tv.tv_sec = sleep_time; | |
1500 tv.tv_usec = 0; | |
1501 | |
872 | 1502 return (select (0, NULL, NULL, NULL, &tv)); |
764 | 1503 } |
1504 | |
1505 | |
125 | 1506 int |
1507 gftp_get_transfer_status (gftp_transfer * tdata, ssize_t num_read) | |
1508 { | |
325 | 1509 intptr_t retries, sleep_time; |
125 | 1510 gftp_file * tempfle; |
498 | 1511 int ret1, ret2; |
125 | 1512 |
1513 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
1514 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
1515 | |
1516 if (g_thread_supported ()) | |
1517 g_static_mutex_lock (&tdata->structmutex); | |
1518 | |
1519 if (tdata->curfle == NULL) | |
1520 { | |
1521 if (g_thread_supported ()) | |
1522 g_static_mutex_unlock (&tdata->structmutex); | |
1523 | |
1524 return (GFTP_EFATAL); | |
1525 } | |
1526 | |
1527 tempfle = tdata->curfle->data; | |
1528 | |
1529 if (g_thread_supported ()) | |
1530 g_static_mutex_unlock (&tdata->structmutex); | |
1531 | |
1532 gftp_disconnect (tdata->fromreq); | |
1533 gftp_disconnect (tdata->toreq); | |
1534 | |
764 | 1535 if (tdata->cancel || num_read == GFTP_EFATAL) |
1536 return (GFTP_EFATAL); | |
1537 else if (num_read >= 0 && !tdata->skip_file) | |
1538 return (0); | |
1539 | |
1540 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout) | |
125 | 1541 { |
764 | 1542 if (retries != 0 && |
1543 tdata->current_file_retries >= retries) | |
1544 { | |
1545 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, | |
1546 _("Error: Remote site %s disconnected. Max retries reached...giving up\n"), | |
1547 tdata->fromreq->hostname != NULL ? | |
1548 tdata->fromreq->hostname : tdata->toreq->hostname); | |
1549 return (GFTP_EFATAL); | |
1550 } | |
1551 else | |
125 | 1552 { |
764 | 1553 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, |
1554 _("Error: Remote site %s disconnected. Will reconnect in %d seconds\n"), | |
1555 tdata->fromreq->hostname != NULL ? | |
1556 tdata->fromreq->hostname : tdata->toreq->hostname, | |
1557 sleep_time); | |
1558 } | |
1559 } | |
1560 | |
1561 while (retries == 0 || | |
1562 tdata->current_file_retries <= retries) | |
1563 { | |
1564 /* Look up the options in case the user changes them... */ | |
1565 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
1566 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
1567 | |
1568 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout && | |
1569 !tdata->skip_file) | |
1570 _do_sleep (sleep_time); | |
1571 | |
1572 tdata->current_file_retries++; | |
1573 | |
1574 ret1 = ret2 = 0; | |
1575 if ((ret1 = gftp_connect (tdata->fromreq)) == 0 && | |
1576 (ret2 = gftp_connect (tdata->toreq)) == 0) | |
1577 { | |
1578 if (g_thread_supported ()) | |
1579 g_static_mutex_lock (&tdata->structmutex); | |
1580 | |
1581 tdata->resumed_bytes = tdata->resumed_bytes + tdata->trans_bytes - tdata->curresumed - tdata->curtrans; | |
1582 tdata->trans_bytes = 0; | |
1583 if (tdata->skip_file) | |
303 | 1584 { |
764 | 1585 tdata->total_bytes -= tempfle->size; |
1586 tdata->curtrans = 0; | |
1587 | |
1588 tdata->curfle = tdata->curfle->next; | |
1589 tdata->next_file = 1; | |
1590 tdata->skip_file = 0; | |
1591 tdata->cancel = 0; | |
1592 tdata->fromreq->cancel = 0; | |
1593 tdata->toreq->cancel = 0; | |
303 | 1594 } |
1595 else | |
1596 { | |
764 | 1597 tempfle->transfer_action = GFTP_TRANS_ACTION_RESUME; |
1598 tempfle->startsize = tdata->curtrans + tdata->curresumed; | |
1599 /* We decrement this here because it will be incremented in | |
1600 the loop again */ | |
1601 tdata->curresumed = 0; | |
1602 tdata->current_file_number--; /* Decrement this because it | |
1603 will be incremented when we | |
1604 continue in the loop */ | |
125 | 1605 } |
1606 | |
764 | 1607 gettimeofday (&tdata->starttime, NULL); |
1608 | |
1609 if (g_thread_supported ()) | |
1610 g_static_mutex_unlock (&tdata->structmutex); | |
1611 | |
1612 return (GFTP_ERETRYABLE); | |
1613 } | |
1614 else if (ret1 == GFTP_EFATAL || ret2 == GFTP_EFATAL) | |
1615 { | |
1616 gftp_disconnect (tdata->fromreq); | |
1617 gftp_disconnect (tdata->toreq); | |
1618 return (GFTP_EFATAL); | |
125 | 1619 } |
1620 } | |
1621 | |
1622 return (0); | |
1623 } | |
1624 | |
182 | 1625 |
1626 int | |
1627 gftp_fd_open (gftp_request * request, const char *pathname, int flags, mode_t mode) | |
1628 { | |
1629 int fd; | |
1630 | |
227 | 1631 if (mode == 0) |
1632 fd = open (pathname, flags); | |
1633 else | |
1634 fd = open (pathname, flags, mode); | |
1635 | |
1636 if (fd < 0) | |
182 | 1637 { |
1638 if (request != NULL) | |
186 | 1639 request->logging_function (gftp_logging_error, request, |
182 | 1640 _("Error: Cannot open local file %s: %s\n"), |
1641 pathname, g_strerror (errno)); | |
1642 return (GFTP_ERETRYABLE); | |
1643 } | |
1644 | |
1645 if (fcntl (fd, F_SETFD, 1) == -1) | |
1646 { | |
1647 if (request != NULL) | |
186 | 1648 request->logging_function (gftp_logging_error, request, |
182 | 1649 _("Error: Cannot set close on exec flag: %s\n"), |
1650 g_strerror (errno)); | |
1651 | |
1652 return (-1); | |
1653 } | |
1654 | |
1655 return (fd); | |
1656 } | |
422 | 1657 |
1658 | |
1659 void | |
792 | 1660 gftp_setup_startup_directory (gftp_request * request, const char *option_name) |
422 | 1661 { |
1662 char *startup_directory, *tempstr; | |
1663 | |
792 | 1664 gftp_lookup_request_option (request, option_name, &startup_directory); |
422 | 1665 |
1666 if (*startup_directory != '\0' && | |
555 | 1667 (tempstr = gftp_expand_path (request, startup_directory)) != NULL) |
422 | 1668 { |
1669 gftp_set_directory (request, tempstr); | |
1670 g_free (tempstr); | |
1671 } | |
1672 } | |
1673 | |
499 | 1674 |
1675 char * | |
1676 gftp_convert_attributes_from_mode_t (mode_t mode) | |
1677 { | |
1678 char *str; | |
1679 | |
765 | 1680 str = g_malloc0 (11UL); |
499 | 1681 |
1682 str[0] = '?'; | |
1683 if (S_ISREG (mode)) | |
1684 str[0] = '-'; | |
1685 | |
1686 if (S_ISLNK (mode)) | |
1687 str[0] = 'l'; | |
1688 | |
1689 if (S_ISBLK (mode)) | |
1690 str[0] = 'b'; | |
1691 | |
1692 if (S_ISCHR (mode)) | |
1693 str[0] = 'c'; | |
1694 | |
1695 if (S_ISFIFO (mode)) | |
1696 str[0] = 'p'; | |
1697 | |
1698 if (S_ISSOCK (mode)) | |
1699 str[0] = 's'; | |
1700 | |
1701 if (S_ISDIR (mode)) | |
1702 str[0] = 'd'; | |
1703 | |
1704 str[1] = mode & S_IRUSR ? 'r' : '-'; | |
1705 str[2] = mode & S_IWUSR ? 'w' : '-'; | |
1706 | |
1707 if ((mode & S_ISUID) && (mode & S_IXUSR)) | |
1708 str[3] = 's'; | |
1709 else if (mode & S_ISUID) | |
1710 str[3] = 'S'; | |
1711 else if (mode & S_IXUSR) | |
1712 str[3] = 'x'; | |
1713 else | |
1714 str[3] = '-'; | |
1715 | |
1716 str[4] = mode & S_IRGRP ? 'r' : '-'; | |
1717 str[5] = mode & S_IWGRP ? 'w' : '-'; | |
1718 | |
1719 if ((mode & S_ISGID) && (mode & S_IXGRP)) | |
1720 str[6] = 's'; | |
1721 else if (mode & S_ISGID) | |
1722 str[6] = 'S'; | |
1723 else if (mode & S_IXGRP) | |
1724 str[6] = 'x'; | |
1725 else | |
1726 str[6] = '-'; | |
1727 | |
1728 str[7] = mode & S_IROTH ? 'r' : '-'; | |
1729 str[8] = mode & S_IWOTH ? 'w' : '-'; | |
1730 | |
1731 if ((mode & S_ISVTX) && (mode & S_IXOTH)) | |
1732 str[9] = 't'; | |
1733 else if (mode & S_ISVTX) | |
1734 str[9] = 'T'; | |
1735 else if (mode & S_IXOTH) | |
1736 str[9] = 'x'; | |
1737 else | |
1738 str[9] = '-'; | |
1739 | |
1740 return (str); | |
1741 } | |
1742 | |
1743 | |
1744 mode_t | |
1745 gftp_convert_attributes_to_mode_t (char *attribs) | |
1746 { | |
1747 mode_t mode; | |
1748 | |
1749 if (attribs[0] == 'd') | |
1750 mode = S_IFDIR; | |
1751 else if (attribs[0] == 'l') | |
1752 mode = S_IFLNK; | |
1753 else if (attribs[0] == 's') | |
1754 mode = S_IFSOCK; | |
1755 else if (attribs[0] == 'b') | |
1756 mode = S_IFBLK; | |
1757 else if (attribs[0] == 'c') | |
1758 mode = S_IFCHR; | |
1759 else | |
1760 mode = S_IFREG; | |
1761 | |
1762 if (attribs[1] == 'r') | |
1763 mode |= S_IRUSR; | |
1764 if (attribs[2] == 'w') | |
1765 mode |= S_IWUSR; | |
1766 if (attribs[3] == 'x' || attribs[3] == 's') | |
1767 mode |= S_IXUSR; | |
1768 if (attribs[3] == 's' || attribs[3] == 'S') | |
1769 mode |= S_ISUID; | |
1770 | |
1771 if (attribs[4] == 'r') | |
1772 mode |= S_IRGRP; | |
1773 if (attribs[5] == 'w') | |
1774 mode |= S_IWGRP; | |
1775 if (attribs[6] == 'x' || | |
1776 attribs[6] == 's') | |
1777 mode |= S_IXGRP; | |
1778 if (attribs[6] == 's' || attribs[6] == 'S') | |
1779 mode |= S_ISGID; | |
1780 | |
1781 if (attribs[7] == 'r') | |
1782 mode |= S_IROTH; | |
1783 if (attribs[8] == 'w') | |
1784 mode |= S_IWOTH; | |
1785 if (attribs[9] == 'x' || | |
1786 attribs[9] == 's') | |
1787 mode |= S_IXOTH; | |
1788 if (attribs[9] == 't' || attribs[9] == 'T') | |
1789 mode |= S_ISVTX; | |
1790 | |
1791 return (mode); | |
1792 } | |
1793 | |
542 | 1794 |
1795 unsigned int | |
1796 gftp_protocol_default_port (gftp_request * request) | |
1797 { | |
1798 struct servent serv_struct; | |
1799 | |
1800 if (r_getservbyname (gftp_protocols[request->protonum].url_prefix, "tcp", | |
1801 &serv_struct, NULL) == NULL) | |
1802 return (gftp_protocols[request->protonum].default_port); | |
1803 else | |
1804 return (ntohs (serv_struct.s_port)); | |
1805 } | |
1806 |