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