Mercurial > gftp.yaz
annotate lib/protocols.c @ 764:4ed80df6729d
2006-7-7 Brian Masney <masneyb@gftp.org>
* lib/protocols.c (gftp_get_transfer_status) - split this function into
several smaller functions. Fixed bug where the number of retries wasn't
being honored
| author | masneyb |
|---|---|
| date | Fri, 07 Jul 2006 20:04:42 +0000 |
| parents | 8990a8a26ccf |
| children | d553d14a2565 |
| rev | line source |
|---|---|
| 1 | 1 /*****************************************************************************/ |
| 2 /* protocols.c - Skeleton functions for the protocols gftp supports */ | |
| 122 | 3 /* Copyright (C) 1998-2003 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) | |
| 51 { | |
| 52 memset (request->password, 0, strlen (request->password)); | |
| 53 g_free (request->password); | |
| 54 } | |
| 55 if (request->account) | |
| 56 { | |
| 57 memset (request->account, 0, strlen (request->account)); | |
| 58 g_free (request->account); | |
| 59 } | |
| 60 if (request->directory) | |
| 61 g_free (request->directory); | |
| 62 if (request->last_ftp_response) | |
| 63 g_free (request->last_ftp_response); | |
| 64 if (request->protocol_data) | |
| 65 g_free (request->protocol_data); | |
| 67 | 66 |
| 198 | 67 if (request->local_options_vars != NULL) |
| 68 { | |
| 201 | 69 gftp_config_free_options (request->local_options_vars, |
| 70 request->local_options_hash, | |
| 71 request->num_local_options_vars); | |
| 198 | 72 } |
| 73 | |
| 1 | 74 memset (request, 0, sizeof (*request)); |
| 67 | 75 |
| 76 if (free_request) | |
| 77 g_free (request); | |
| 78 else | |
| 79 { | |
| 80 request->datafd = -1; | |
| 81 request->cachefd = -1; | |
| 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); | |
| 349 | 109 if (file->utf8_file) |
| 110 g_free (file->utf8_file); | |
| 1 | 111 if (file->user) |
| 112 g_free (file->user); | |
| 113 if (file->group) | |
| 114 g_free (file->group); | |
| 115 if (file->destfile) | |
| 116 g_free (file->destfile); | |
| 598 | 117 |
| 118 if (free_it) | |
| 119 g_free (file); | |
| 120 else | |
| 121 memset (file, 0, sizeof (*file)); | |
| 1 | 122 } |
| 123 | |
| 124 | |
| 125 int | |
| 126 gftp_connect (gftp_request * request) | |
| 127 { | |
| 177 | 128 int ret; |
| 129 | |
| 84 | 130 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 131 |
| 132 if (request->connect == NULL) | |
| 84 | 133 return (GFTP_EFATAL); |
| 1 | 134 |
| 177 | 135 if ((ret = gftp_set_config_options (request)) < 0) |
| 136 return (ret); | |
| 1 | 137 |
| 138 return (request->connect (request)); | |
| 139 } | |
| 140 | |
| 141 | |
| 142 void | |
| 143 gftp_disconnect (gftp_request * request) | |
| 144 { | |
| 145 g_return_if_fail (request != NULL); | |
| 146 | |
| 147 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 151 | 148 if (request->free_hostp && request->hostp != NULL) |
| 1 | 149 freeaddrinfo (request->hostp); |
| 150 #endif | |
| 151 request->hostp = NULL; | |
| 152 | |
| 168 | 153 #ifdef USE_SSL |
| 154 if (request->ssl != NULL) | |
| 155 { | |
| 156 SSL_free (request->ssl); | |
| 157 request->ssl = NULL; | |
| 158 } | |
| 159 #endif | |
| 160 | |
| 187 | 161 #if GLIB_MAJOR_VERSION > 1 |
| 162 if (request->iconv_initialized) | |
| 163 { | |
| 164 g_iconv_close (request->iconv); | |
| 165 request->iconv_initialized = 0; | |
| 166 } | |
| 167 #endif | |
| 168 | |
| 1 | 169 request->cached = 0; |
| 170 if (request->disconnect == NULL) | |
| 171 return; | |
| 172 request->disconnect (request); | |
| 173 } | |
| 174 | |
| 175 | |
| 58 | 176 off_t |
| 177 gftp_get_file (gftp_request * request, const char *filename, int fd, | |
| 244 | 178 off_t startsize) |
| 1 | 179 { |
| 84 | 180 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 181 |
| 182 request->cached = 0; | |
| 183 if (request->get_file == NULL) | |
| 84 | 184 return (GFTP_EFATAL); |
| 244 | 185 |
| 1 | 186 return (request->get_file (request, filename, fd, startsize)); |
| 187 } | |
| 188 | |
| 189 | |
| 190 int | |
| 58 | 191 gftp_put_file (gftp_request * request, const char *filename, int fd, |
| 244 | 192 off_t startsize, off_t totalsize) |
| 1 | 193 { |
| 84 | 194 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 195 |
| 196 request->cached = 0; | |
| 197 if (request->put_file == NULL) | |
| 84 | 198 return (GFTP_EFATAL); |
| 566 | 199 |
| 599 | 200 return (request->put_file (request, filename, fd, startsize, totalsize)); |
| 1 | 201 } |
| 202 | |
| 203 | |
| 261 | 204 off_t |
| 1 | 205 gftp_transfer_file (gftp_request * fromreq, const char *fromfile, |
| 244 | 206 int fromfd, off_t fromsize, |
| 1 | 207 gftp_request * toreq, const char *tofile, |
| 244 | 208 int tofd, off_t tosize) |
| 1 | 209 { |
| 469 | 210 /* Needed for systems that size(float) < size(void *) */ |
| 211 union { intptr_t i; float f; } maxkbs; | |
| 261 | 212 off_t size; |
| 84 | 213 int ret; |
| 1 | 214 |
| 84 | 215 g_return_val_if_fail (fromreq != NULL, GFTP_EFATAL); |
| 216 g_return_val_if_fail (fromfile != NULL, GFTP_EFATAL); | |
| 217 g_return_val_if_fail (toreq != NULL, GFTP_EFATAL); | |
| 218 g_return_val_if_fail (tofile != NULL, GFTP_EFATAL); | |
| 219 | |
| 469 | 220 gftp_lookup_request_option (toreq, "maxkbs", &maxkbs.f); |
| 221 | |
| 222 if (maxkbs.f > 0) | |
| 294 | 223 { |
| 224 toreq->logging_function (gftp_logging_misc, toreq, | |
| 225 _("File transfer will be throttled to %.2f KB/s\n"), | |
| 469 | 226 maxkbs.f); |
| 294 | 227 } |
| 228 | |
| 87 | 229 if (fromreq->protonum == toreq->protonum && |
| 84 | 230 fromreq->transfer_file != NULL) |
| 231 return (fromreq->transfer_file (fromreq, fromfile, fromsize, toreq, | |
| 232 tofile, tosize)); | |
| 1 | 233 |
| 234 fromreq->cached = 0; | |
| 235 toreq->cached = 0; | |
| 443 | 236 |
| 237 get_file: | |
| 238 size = gftp_get_file (fromreq, fromfile, fromfd, tosize); | |
| 239 if (size < 0) | |
| 1 | 240 { |
| 443 | 241 if (size == GFTP_ETIMEDOUT) |
| 242 { | |
| 243 ret = gftp_connect (fromreq); | |
| 244 if (ret < 0) | |
| 245 return (ret); | |
| 246 | |
| 247 goto get_file; | |
| 248 } | |
| 249 | |
| 250 return (size); | |
| 251 } | |
| 252 | |
| 253 put_file: | |
| 254 ret = gftp_put_file (toreq, tofile, tofd, tosize, size); | |
| 255 if (ret != 0) | |
| 256 { | |
| 257 if (size == GFTP_ETIMEDOUT) | |
| 258 { | |
| 259 ret = gftp_connect (fromreq); | |
| 260 if (ret < 0) | |
| 261 return (ret); | |
| 262 | |
| 263 goto put_file; | |
| 264 } | |
| 265 | |
| 40 | 266 if (gftp_abort_transfer (fromreq) != 0) |
| 267 gftp_end_transfer (fromreq); | |
| 268 | |
| 84 | 269 return (ret); |
| 1 | 270 } |
| 271 | |
| 272 return (size); | |
| 273 } | |
| 274 | |
| 275 | |
| 58 | 276 ssize_t |
| 1 | 277 gftp_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
| 278 { | |
| 84 | 279 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 280 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
| 1 | 281 |
| 282 if (request->get_next_file_chunk != NULL) | |
| 283 return (request->get_next_file_chunk (request, buf, size)); | |
| 284 | |
| 168 | 285 return (request->read_function (request, buf, size, request->datafd)); |
| 1 | 286 } |
| 287 | |
| 288 | |
| 58 | 289 ssize_t |
| 1 | 290 gftp_put_next_file_chunk (gftp_request * request, char *buf, size_t size) |
| 291 { | |
| 84 | 292 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 293 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
| 1 | 294 |
| 295 if (request->put_next_file_chunk != NULL) | |
| 296 return (request->put_next_file_chunk (request, buf, size)); | |
| 297 | |
| 298 if (size == 0) | |
| 299 return (0); | |
| 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 |
| 343 return (request->abort_transfer (request)); | |
| 344 } | |
| 345 | |
| 346 | |
| 520 | 347 int |
| 348 gftp_stat_filename (gftp_request * request, const char *filename, mode_t * mode) | |
| 500 | 349 { |
| 350 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
| 520 | 351 g_return_val_if_fail (filename != NULL, GFTP_EFATAL); |
| 500 | 352 |
| 353 if (request->stat_filename != NULL) | |
| 520 | 354 return (request->stat_filename (request, filename, mode)); |
| 500 | 355 else |
| 520 | 356 return (0); |
| 500 | 357 } |
| 358 | |
| 359 | |
| 40 | 360 int |
| 1 | 361 gftp_list_files (gftp_request * request) |
| 362 { | |
| 473 | 363 char *remote_lc_time, *locret; |
| 58 | 364 int fd; |
| 1 | 365 |
| 84 | 366 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 367 |
| 623 | 368 #if ENABLE_NLS |
| 473 | 369 gftp_lookup_request_option (request, "remote_lc_time", &remote_lc_time); |
| 370 if (remote_lc_time != NULL && *remote_lc_time != '\0') | |
| 371 locret = setlocale (LC_TIME, remote_lc_time); | |
| 372 else | |
| 373 locret = setlocale (LC_TIME, NULL); | |
| 374 | |
| 375 if (locret == NULL) | |
| 376 { | |
| 377 locret = setlocale (LC_TIME, NULL); | |
| 677 | 378 request->logging_function (gftp_logging_error, request, |
| 473 | 379 _("Error setting LC_TIME to '%s'. Falling back to '%s'\n"), |
| 380 remote_lc_time, locret); | |
| 381 } | |
| 623 | 382 #else |
| 383 locret = _("<unknown>"); | |
| 384 #endif | |
| 473 | 385 |
| 1 | 386 request->cached = 0; |
| 58 | 387 if (request->use_cache && (fd = gftp_find_cache_entry (request)) > 0) |
| 1 | 388 { |
| 186 | 389 request->logging_function (gftp_logging_misc, request, |
| 473 | 390 _("Loading directory listing %s from cache (LC_TIME=%s)\n"), |
| 391 request->directory, locret); | |
| 1 | 392 |
| 393 request->cachefd = fd; | |
| 394 request->cached = 1; | |
| 395 return (0); | |
| 396 } | |
| 397 else if (request->use_cache) | |
| 398 { | |
| 473 | 399 request->logging_function (gftp_logging_misc, request, |
| 400 _("Loading directory listing %s from server (LC_TIME=%s)\n"), | |
| 401 request->directory, locret); | |
| 402 | |
| 1 | 403 request->cachefd = gftp_new_cache_entry (request); |
| 404 request->cached = 0; | |
| 405 } | |
| 406 | |
| 407 if (request->list_files == NULL) | |
| 84 | 408 return (GFTP_EFATAL); |
| 473 | 409 |
| 1 | 410 return (request->list_files (request)); |
| 411 } | |
| 412 | |
| 413 | |
| 184 | 414 #if GLIB_MAJOR_VERSION > 1 |
| 291 | 415 |
| 184 | 416 static char * |
| 423 | 417 _gftp_get_next_charset (char **curpos) |
| 184 | 418 { |
| 419 char *ret, *endpos; | |
| 420 | |
| 421 if (**curpos == '\0') | |
| 422 return (NULL); | |
| 423 | |
| 424 ret = *curpos; | |
| 185 | 425 if ((endpos = strchr (*curpos, ',')) == NULL) |
| 184 | 426 *curpos += strlen (*curpos); |
| 427 else | |
| 428 { | |
| 429 *endpos = '\0'; | |
| 423 | 430 *curpos = endpos + 1; |
| 184 | 431 } |
| 432 | |
| 433 return (ret); | |
| 434 } | |
| 435 | |
| 436 | |
| 186 | 437 char * |
| 291 | 438 gftp_string_to_utf8 (gftp_request * request, const char *str) |
| 184 | 439 { |
| 423 | 440 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; |
| 598 | 441 GError * error = NULL; |
| 184 | 442 gsize bread, bwrite; |
| 443 | |
| 188 | 444 if (request == NULL) |
| 445 return (NULL); | |
| 446 | |
| 579 | 447 if (g_utf8_validate (str, -1, NULL)) |
| 448 return (NULL); | |
| 449 else if (request->iconv_initialized) | |
| 582 | 450 { |
| 451 ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 452 &error); | |
| 453 if (ret == NULL) | |
| 454 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), | |
| 455 str, _("<unknown>"), "UTF-8", error->message); | |
| 456 | |
| 457 return (ret); | |
| 458 } | |
| 184 | 459 |
| 423 | 460 gftp_lookup_request_option (request, "remote_charsets", &tempstr); |
| 461 if (*tempstr == '\0') | |
| 184 | 462 { |
| 463 error = NULL; | |
| 464 if ((ret = g_locale_to_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) | |
| 465 return (ret); | |
| 466 | |
| 579 | 467 /* Don't use request->logging_function since the strings must be in UTF-8 |
| 468 for the GTK+ 2.x port */ | |
| 469 printf (_("Error converting string '%s' to UTF-8 from current locale: %s\n"), | |
| 470 str, error->message); | |
| 184 | 471 return (NULL); |
| 472 } | |
| 473 | |
| 423 | 474 remote_charsets = g_strdup (tempstr); |
| 184 | 475 ret = NULL; |
| 476 stpos = remote_charsets; | |
| 423 | 477 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) |
| 184 | 478 { |
| 479 if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1) | |
| 480 continue; | |
| 481 | |
| 482 error = NULL; | |
| 483 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 484 &error)) == NULL) | |
| 485 { | |
| 582 | 486 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), |
| 487 str, cur_charset, "UTF-8", error->message); | |
| 488 | |
| 184 | 489 g_iconv_close (request->iconv); |
| 490 request->iconv = NULL; | |
| 491 continue; | |
| 492 } | |
| 493 else | |
| 494 { | |
| 495 request->iconv_initialized = 1; | |
| 496 break; | |
| 497 } | |
| 498 } | |
| 499 | |
| 423 | 500 g_free (remote_charsets); |
| 501 | |
| 184 | 502 return (ret); |
| 503 } | |
| 291 | 504 |
| 505 | |
| 506 char * | |
| 507 gftp_string_from_utf8 (gftp_request * request, const char *str) | |
| 508 { | |
| 423 | 509 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; |
| 598 | 510 GError * error = NULL; |
| 291 | 511 gsize bread, bwrite; |
| 512 | |
| 513 if (request == NULL) | |
| 514 return (NULL); | |
| 515 | |
| 582 | 516 /* If the string isn't in UTF-8 format, assume it is already in the current |
| 517 locale... */ | |
| 518 if (!g_utf8_validate (str, -1, NULL)) | |
| 519 return (NULL); | |
| 520 else if (request->iconv_initialized) | |
| 521 { | |
| 522 ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 523 &error); | |
| 524 if (ret == NULL) | |
| 525 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), | |
| 526 str, "UTF-8", _("<unknown>"), error->message); | |
| 527 | |
| 528 return (ret); | |
| 529 } | |
| 291 | 530 |
| 423 | 531 gftp_lookup_request_option (request, "remote_charsets", &tempstr); |
| 532 if (*tempstr == '\0') | |
| 291 | 533 { |
| 534 error = NULL; | |
| 535 if ((ret = g_locale_from_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) | |
| 536 return (ret); | |
| 537 | |
| 579 | 538 /* Don't use request->logging_function since the strings must be in UTF-8 |
| 539 for the GTK+ 2.x port */ | |
| 540 printf (_("Error converting string '%s' to current locale from UTF-8: %s\n"), | |
| 541 str, error->message); | |
| 291 | 542 return (NULL); |
| 543 } | |
| 544 | |
| 423 | 545 remote_charsets = g_strdup (tempstr); |
| 291 | 546 ret = NULL; |
| 547 stpos = remote_charsets; | |
| 423 | 548 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) |
| 291 | 549 { |
| 550 if ((request->iconv = g_iconv_open (cur_charset, "UTF-8")) == (GIConv) -1) | |
| 551 continue; | |
| 552 | |
| 553 error = NULL; | |
| 554 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, | |
| 555 &error)) == NULL) | |
| 556 { | |
| 582 | 557 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), |
| 558 str, "UTF-8", cur_charset, error->message); | |
| 559 | |
| 291 | 560 g_iconv_close (request->iconv); |
| 561 request->iconv = NULL; | |
| 562 continue; | |
| 563 } | |
| 564 else | |
| 565 { | |
| 566 request->iconv_initialized = 1; | |
| 567 break; | |
| 568 } | |
| 569 } | |
| 570 | |
| 423 | 571 g_free (remote_charsets); |
| 572 | |
| 291 | 573 return (ret); |
| 574 } | |
| 575 | |
| 576 #else | |
| 577 | |
| 578 char * | |
| 579 gftp_string_to_utf8 (gftp_request * request, const char *str) | |
| 580 { | |
| 581 return (NULL); | |
| 582 } | |
| 583 | |
| 584 | |
| 585 char * | |
| 586 gftp_string_from_utf8 (gftp_request * request, const char *str) | |
| 587 { | |
| 588 return (NULL); | |
| 589 } | |
| 590 | |
| 184 | 591 #endif |
| 592 | |
| 593 | |
| 1 | 594 int |
| 377 | 595 gftp_get_next_file (gftp_request * request, const char *filespec, |
| 596 gftp_file * fle) | |
| 1 | 597 { |
| 666 | 598 char *slashpos, *newfile; |
| 58 | 599 int fd, ret; |
| 1 | 600 |
| 84 | 601 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 602 |
| 603 if (request->get_next_file == NULL) | |
| 84 | 604 return (GFTP_EFATAL); |
| 1 | 605 |
| 58 | 606 if (request->cached && request->cachefd > 0) |
| 1 | 607 fd = request->cachefd; |
| 608 else | |
| 609 fd = request->datafd; | |
| 610 | |
| 611 memset (fle, 0, sizeof (*fle)); | |
| 612 do | |
| 613 { | |
| 598 | 614 gftp_file_destroy (fle, 0); |
| 1 | 615 ret = request->get_next_file (request, fle, fd); |
| 666 | 616 if (fle->file != NULL && (slashpos = strrchr (fle->file, '/')) != NULL) |
| 617 { | |
| 618 if (*(slashpos + 1) == '\0') | |
| 619 { | |
| 620 gftp_file_destroy (fle, 0); | |
| 621 continue; | |
| 622 } | |
| 623 | |
| 624 *slashpos = '\0'; | |
| 625 newfile = g_strdup (slashpos + 1); | |
| 626 | |
| 627 if (strcmp (fle->file, request->directory) != 0) | |
| 628 request->logging_function (gftp_logging_error, request, | |
| 629 _("Warning: Stripping path off of file '%s'. The stripped path (%s) doesn't match the current directory (%s)\n"), | |
| 630 newfile, fle->file, request->directory, | |
| 631 g_strerror (errno)); | |
| 632 | |
| 633 g_free (fle->file); | |
| 634 fle->file = newfile; | |
| 635 } | |
| 1 | 636 |
| 291 | 637 if (ret >= 0 && fle->file != NULL) |
| 184 | 638 fle->utf8_file = gftp_string_to_utf8 (request, fle->file); |
| 45 | 639 |
| 60 | 640 if (ret >= 0 && !request->cached && request->cachefd > 0 && |
| 1 | 641 request->last_dir_entry != NULL) |
| 642 { | |
| 168 | 643 if (gftp_fd_write (request, request->last_dir_entry, |
| 60 | 644 request->last_dir_entry_len, request->cachefd) < 0) |
| 1 | 645 { |
| 186 | 646 request->logging_function (gftp_logging_error, request, |
| 1 | 647 _("Error: Cannot write to cache: %s\n"), |
| 648 g_strerror (errno)); | |
| 60 | 649 close (request->cachefd); |
| 650 request->cachefd = -1; | |
| 1 | 651 } |
| 652 } | |
| 653 } while (ret > 0 && !gftp_match_filespec (fle->file, filespec)); | |
| 654 | |
| 655 return (ret); | |
| 656 } | |
| 657 | |
| 658 | |
| 659 int | |
| 243 | 660 gftp_parse_bookmark (gftp_request * request, gftp_request * local_request, |
| 275 | 661 const char * bookmark, int *refresh_local) |
| 87 | 662 { |
| 663 gftp_logging_func logging_function; | |
| 122 | 664 gftp_bookmarks_var * tempentry; |
| 578 | 665 char *default_protocol, *utf8; |
| 646 | 666 const char *email; |
| 173 | 667 int i, init_ret; |
| 87 | 668 |
| 669 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
| 670 g_return_val_if_fail (bookmark != NULL, GFTP_EFATAL); | |
| 671 | |
| 672 logging_function = request->logging_function; | |
| 673 gftp_request_destroy (request, 0); | |
| 674 request->logging_function = logging_function; | |
| 675 | |
| 122 | 676 if ((tempentry = g_hash_table_lookup (gftp_bookmarks_htable, |
| 677 bookmark)) == NULL) | |
| 87 | 678 { |
| 186 | 679 request->logging_function (gftp_logging_error, request, |
| 87 | 680 _("Error: Could not find bookmark %s\n"), |
| 681 bookmark); | |
| 682 return (GFTP_EFATAL); | |
| 683 } | |
| 684 else if (tempentry->hostname == NULL || *tempentry->hostname == '\0') | |
| 685 { | |
| 186 | 686 request->logging_function (gftp_logging_error, request, |
| 87 | 687 _("Bookmarks Error: The bookmark entry %s does not have a hostname\n"), bookmark); |
| 688 return (GFTP_EFATAL); | |
| 689 } | |
| 690 | |
| 691 if (tempentry->user != NULL) | |
| 692 gftp_set_username (request, tempentry->user); | |
| 693 | |
| 694 if (tempentry->pass != NULL) | |
| 646 | 695 { |
| 696 if (strcmp (tempentry->pass, "@EMAIL@") == 0) | |
| 697 { | |
| 698 gftp_lookup_request_option (request, "email", &email); | |
| 699 gftp_set_password (request, email); | |
| 700 } | |
| 701 else | |
| 702 gftp_set_password (request, tempentry->pass); | |
| 703 } | |
| 87 | 704 |
| 705 if (tempentry->acct != NULL) | |
| 706 gftp_set_account (request, tempentry->acct); | |
| 707 | |
| 708 gftp_set_hostname (request, tempentry->hostname); | |
| 578 | 709 |
| 710 utf8 = gftp_string_from_utf8 (request, tempentry->remote_dir); | |
| 711 if (utf8 != NULL) | |
| 712 { | |
| 713 gftp_set_directory (request, utf8); | |
| 714 g_free (utf8); | |
| 715 } | |
| 716 else | |
| 717 gftp_set_directory (request, tempentry->remote_dir); | |
| 718 | |
| 87 | 719 gftp_set_port (request, tempentry->port); |
| 720 | |
| 516 | 721 if (local_request != NULL && tempentry->local_dir != NULL && |
| 243 | 722 *tempentry->local_dir != '\0') |
| 275 | 723 { |
| 578 | 724 utf8 = gftp_string_from_utf8 (request, tempentry->local_dir); |
| 725 if (utf8 != NULL) | |
| 726 { | |
| 727 gftp_set_directory (local_request, utf8); | |
| 728 g_free (utf8); | |
| 729 } | |
| 730 else | |
| 731 gftp_set_directory (local_request, tempentry->local_dir); | |
| 732 | |
| 516 | 733 if (refresh_local != NULL) |
| 734 *refresh_local = 1; | |
| 275 | 735 } |
| 516 | 736 else if (refresh_local != NULL) |
| 275 | 737 *refresh_local = 0; |
| 243 | 738 |
| 87 | 739 for (i = 0; gftp_protocols[i].name; i++) |
| 740 { | |
| 741 if (strcmp (gftp_protocols[i].name, tempentry->protocol) == 0) | |
| 742 { | |
| 173 | 743 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
| 744 { | |
| 745 gftp_request_destroy (request, 0); | |
| 746 return (init_ret); | |
| 747 } | |
| 87 | 748 break; |
| 749 } | |
| 750 } | |
| 751 | |
| 122 | 752 if (gftp_protocols[i].name == NULL) |
| 87 | 753 { |
| 122 | 754 gftp_lookup_request_option (request, "default_protocol", |
| 755 &default_protocol); | |
| 756 | |
| 125 | 757 if (default_protocol != NULL && *default_protocol != '\0') |
| 87 | 758 { |
| 122 | 759 for (i = 0; gftp_protocols[i].url_prefix; i++) |
| 760 { | |
| 761 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
| 762 break; | |
| 763 } | |
| 87 | 764 } |
| 765 | |
| 766 if (gftp_protocols[i].url_prefix == NULL) | |
| 767 i = GFTP_FTP_NUM; | |
| 768 } | |
| 769 | |
| 199 | 770 gftp_copy_local_options (&request->local_options_vars, |
| 771 &request->local_options_hash, | |
| 429 | 772 &request->num_local_options_vars, |
| 199 | 773 tempentry->local_options_vars, |
| 774 tempentry->num_local_options_vars); | |
| 775 | |
| 173 | 776 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
| 777 { | |
| 778 gftp_request_destroy (request, 0); | |
| 779 return (init_ret); | |
| 780 } | |
| 781 | |
| 87 | 782 return (0); |
| 783 } | |
| 784 | |
| 785 | |
| 786 int | |
| 1 | 787 gftp_parse_url (gftp_request * request, const char *url) |
| 788 { | |
| 676 | 789 char *pos, *endpos, *default_protocol, *new_url; |
| 67 | 790 gftp_logging_func logging_function; |
| 676 | 791 const char *clear_pos; |
| 792 int i, ret; | |
| 1 | 793 |
| 84 | 794 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 795 g_return_val_if_fail (url != NULL, GFTP_EFATAL); | |
| 1 | 796 |
| 67 | 797 logging_function = request->logging_function; |
| 798 gftp_request_destroy (request, 0); | |
| 799 request->logging_function = logging_function; | |
| 800 | |
| 676 | 801 for (clear_pos = url; |
| 802 *clear_pos == ' ' || *clear_pos == '\t'; | |
| 803 clear_pos++); | |
| 804 | |
| 805 new_url = g_strdup (clear_pos); | |
| 806 | |
| 807 for (pos = new_url + strlen (new_url) - 1; | |
| 808 *pos == ' ' || *pos == '\t'; | |
| 809 pos--) | |
| 168 | 810 *pos = '\0'; |
| 1 | 811 |
| 676 | 812 /* See if the URL has a protocol... */ |
| 813 if ((pos = strstr (new_url, "://")) != NULL) | |
| 1 | 814 { |
| 815 *pos = '\0'; | |
| 816 | |
| 817 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
| 818 { | |
| 676 | 819 if (strcmp (gftp_protocols[i].url_prefix, new_url) == 0) |
| 1 | 820 break; |
| 821 } | |
| 822 | |
| 823 if (gftp_protocols[i].url_prefix == NULL) | |
| 168 | 824 { |
| 677 | 825 request->logging_function (gftp_logging_error, NULL, |
| 173 | 826 _("The protocol '%s' is currently not supported.\n"), |
| 676 | 827 new_url); |
| 828 g_free (new_url); | |
| 168 | 829 return (GFTP_EFATAL); |
| 830 } | |
| 173 | 831 |
| 832 *pos = ':'; | |
| 676 | 833 pos += 3; |
| 1 | 834 } |
| 835 else | |
| 836 { | |
| 122 | 837 gftp_lookup_request_option (request, "default_protocol", |
| 838 &default_protocol); | |
| 839 | |
| 676 | 840 i = GFTP_FTP_NUM; |
| 125 | 841 if (default_protocol != NULL && *default_protocol != '\0') |
| 1 | 842 { |
| 122 | 843 for (i = 0; gftp_protocols[i].url_prefix; i++) |
| 844 { | |
| 845 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
| 846 break; | |
| 847 } | |
| 1 | 848 } |
| 676 | 849 |
| 850 if (gftp_protocols[i].url_prefix == NULL) | |
| 851 { | |
| 677 | 852 request->logging_function (gftp_logging_error, NULL, |
| 676 | 853 _("The protocol '%s' is currently not supported.\n"), |
| 854 default_protocol); | |
| 855 g_free (new_url); | |
| 856 return (GFTP_EFATAL); | |
| 857 } | |
| 858 | |
| 859 pos = new_url; | |
| 122 | 860 } |
| 1 | 861 |
| 676 | 862 if ((ret = gftp_protocols[i].init (request)) < 0) |
| 173 | 863 { |
| 864 gftp_request_destroy (request, 0); | |
| 676 | 865 return (ret); |
| 866 } | |
| 867 | |
| 868 if ((endpos = strchr (pos, '/')) != NULL) | |
| 869 { | |
| 870 gftp_set_directory (request, endpos); | |
| 871 *endpos = '\0'; | |
| 173 | 872 } |
| 1 | 873 |
| 874 if (request->parse_url != NULL) | |
| 168 | 875 { |
| 676 | 876 ret = request->parse_url (request, new_url); |
| 877 g_free (new_url); | |
| 878 return (ret); | |
| 168 | 879 } |
| 880 | |
| 676 | 881 if (*pos != '\0') |
| 1 | 882 { |
| 676 | 883 if (endpos == NULL) |
| 884 endpos = pos + strlen (pos) - 1; | |
| 885 else | |
| 886 endpos--; | |
| 887 | |
| 888 for (; isdigit (*endpos); endpos--); | |
| 889 | |
| 890 if (*endpos == ':' && isdigit (*(endpos + 1))) | |
| 168 | 891 { |
| 676 | 892 gftp_set_port (request, strtol (endpos + 1, NULL, 10)); |
| 893 *endpos = '\0'; | |
| 168 | 894 } |
| 1 | 895 |
| 676 | 896 if ((endpos = strrchr (pos, '@')) != NULL) |
| 168 | 897 { |
| 676 | 898 gftp_set_hostname (request, endpos + 1); |
| 168 | 899 *endpos = '\0'; |
| 676 | 900 |
| 901 if ((endpos = strchr (pos, ':')) != NULL) | |
| 902 { | |
| 903 *endpos = '\0'; | |
| 904 gftp_set_username (request, pos); | |
| 905 gftp_set_password (request, endpos + 1); | |
| 906 } | |
| 907 else | |
| 908 { | |
| 909 gftp_set_username (request, pos); | |
| 910 gftp_set_password (request, ""); | |
| 911 } | |
| 168 | 912 } |
| 676 | 913 else |
| 914 gftp_set_hostname (request, pos); | |
| 1 | 915 } |
| 916 | |
| 676 | 917 g_free (new_url); |
| 1 | 918 return (0); |
| 919 } | |
| 920 | |
| 921 | |
| 922 void | |
| 923 gftp_set_hostname (gftp_request * request, const char *hostname) | |
| 924 { | |
| 925 g_return_if_fail (request != NULL); | |
| 926 g_return_if_fail (hostname != NULL); | |
| 927 | |
| 928 if (request->hostname) | |
| 929 g_free (request->hostname); | |
| 124 | 930 request->hostname = g_strdup (hostname); |
| 1 | 931 } |
| 932 | |
| 933 | |
| 934 void | |
| 935 gftp_set_username (gftp_request * request, const char *username) | |
| 936 { | |
| 937 g_return_if_fail (request != NULL); | |
| 938 | |
| 939 if (request->username) | |
| 940 g_free (request->username); | |
| 169 | 941 |
| 942 if (username != NULL) | |
| 943 request->username = g_strdup (username); | |
| 944 else | |
| 945 request->username = NULL; | |
| 1 | 946 } |
| 947 | |
| 948 | |
| 949 void | |
| 950 gftp_set_password (gftp_request * request, const char *password) | |
| 951 { | |
| 952 g_return_if_fail (request != NULL); | |
| 953 | |
| 954 if (request->password) | |
| 955 g_free (request->password); | |
| 169 | 956 |
| 957 if (password != NULL) | |
| 958 request->password = g_strdup (password); | |
| 959 else | |
| 960 request->password = NULL; | |
| 1 | 961 } |
| 962 | |
| 963 | |
| 964 void | |
| 965 gftp_set_account (gftp_request * request, const char *account) | |
| 966 { | |
| 967 g_return_if_fail (request != NULL); | |
| 968 g_return_if_fail (account != NULL); | |
| 969 | |
| 970 if (request->account) | |
| 971 g_free (request->account); | |
| 124 | 972 request->account = g_strdup (account); |
| 1 | 973 } |
| 974 | |
| 975 | |
| 976 int | |
| 977 gftp_set_directory (gftp_request * request, const char *directory) | |
| 978 { | |
| 84 | 979 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 980 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
| 1 | 981 |
| 982 | |
| 169 | 983 if (request->datafd <= 0 && !request->always_connected) |
| 1 | 984 { |
| 985 if (directory != request->directory) | |
| 168 | 986 { |
| 987 if (request->directory) | |
| 988 g_free (request->directory); | |
| 989 request->directory = g_strdup (directory); | |
| 990 } | |
| 1 | 991 return (0); |
| 992 } | |
| 993 else if (request->chdir == NULL) | |
| 84 | 994 return (GFTP_EFATAL); |
| 1 | 995 return (request->chdir (request, directory)); |
| 996 } | |
| 997 | |
| 998 | |
| 999 void | |
| 1000 gftp_set_port (gftp_request * request, unsigned int port) | |
| 1001 { | |
| 1002 g_return_if_fail (request != NULL); | |
| 1003 | |
| 1004 request->port = port; | |
| 1005 } | |
| 1006 | |
| 1007 | |
| 1008 int | |
| 1009 gftp_remove_directory (gftp_request * request, const char *directory) | |
| 1010 { | |
| 84 | 1011 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1012 |
| 1013 if (request->rmdir == NULL) | |
| 84 | 1014 return (GFTP_EFATAL); |
| 1 | 1015 return (request->rmdir (request, directory)); |
| 1016 } | |
| 1017 | |
| 1018 | |
| 1019 int | |
| 1020 gftp_remove_file (gftp_request * request, const char *file) | |
| 1021 { | |
| 84 | 1022 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1023 |
| 1024 if (request->rmfile == NULL) | |
| 84 | 1025 return (GFTP_EFATAL); |
| 1 | 1026 return (request->rmfile (request, file)); |
| 1027 } | |
| 1028 | |
| 1029 | |
| 1030 int | |
| 1031 gftp_make_directory (gftp_request * request, const char *directory) | |
| 1032 { | |
| 291 | 1033 char *utf8; |
| 1034 int ret; | |
| 1035 | |
| 84 | 1036 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1037 |
| 1038 if (request->mkdir == NULL) | |
| 84 | 1039 return (GFTP_EFATAL); |
| 291 | 1040 |
| 1041 utf8 = gftp_string_from_utf8 (request, directory); | |
| 1042 if (utf8 != NULL) | |
| 1043 { | |
| 1044 ret = request->mkdir (request, utf8); | |
| 1045 g_free (utf8); | |
| 1046 } | |
| 1047 else | |
| 1048 ret = request->mkdir (request, directory); | |
| 1049 | |
| 1050 return (ret); | |
| 1 | 1051 } |
| 1052 | |
| 1053 | |
| 1054 int | |
| 1055 gftp_rename_file (gftp_request * request, const char *oldname, | |
| 168 | 1056 const char *newname) |
| 1 | 1057 { |
| 291 | 1058 char *utf8; |
| 1059 int ret; | |
| 1060 | |
| 84 | 1061 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1062 |
| 1063 if (request->rename == NULL) | |
| 84 | 1064 return (GFTP_EFATAL); |
| 291 | 1065 |
| 1066 utf8 = gftp_string_from_utf8 (request, newname); | |
| 1067 if (utf8 != NULL) | |
| 1068 { | |
| 1069 ret = request->rename (request, oldname, utf8); | |
| 1070 g_free (utf8); | |
| 1071 } | |
| 1072 else | |
| 1073 ret = request->rename (request, oldname, newname); | |
| 1074 | |
| 1075 return (ret); | |
| 1 | 1076 } |
| 1077 | |
| 1078 | |
| 1079 int | |
| 499 | 1080 gftp_chmod (gftp_request * request, const char *file, mode_t mode) |
| 1 | 1081 { |
| 84 | 1082 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1083 |
| 1084 if (request->chmod == NULL) | |
| 84 | 1085 return (GFTP_EFATAL); |
| 504 | 1086 |
| 1087 mode &= S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX; | |
| 1 | 1088 return (request->chmod (request, file, mode)); |
| 1089 } | |
| 1090 | |
| 1091 | |
| 1092 int | |
| 1093 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
| 1094 { | |
| 84 | 1095 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1096 |
| 1097 if (request->set_file_time == NULL) | |
| 84 | 1098 return (GFTP_EFATAL); |
| 1 | 1099 return (request->set_file_time (request, file, datetime)); |
| 1100 } | |
| 1101 | |
| 1102 | |
| 1103 char | |
| 478 | 1104 gftp_site_cmd (gftp_request * request, int specify_site, const char *command) |
| 1 | 1105 { |
| 84 | 1106 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
| 1 | 1107 |
| 1108 if (request->site == NULL) | |
| 84 | 1109 return (GFTP_EFATAL); |
| 478 | 1110 return (request->site (request, specify_site, command)); |
| 1 | 1111 } |
| 1112 | |
| 1113 | |
| 58 | 1114 off_t |
| 1 | 1115 gftp_get_file_size (gftp_request * request, const char *filename) |
| 1116 { | |
| 1117 g_return_val_if_fail (request != NULL, 0); | |
| 1118 | |
| 1119 if (request->get_file_size == NULL) | |
| 1120 return (0); | |
| 1121 return (request->get_file_size (request, filename)); | |
| 1122 } | |
| 1123 | |
| 1124 | |
| 122 | 1125 static int |
| 1126 gftp_need_proxy (gftp_request * request, char *service, char *proxy_hostname, | |
| 518 | 1127 unsigned int proxy_port) |
| 1 | 1128 { |
| 122 | 1129 gftp_config_list_vars * proxy_hosts; |
| 1 | 1130 gftp_proxy_hosts * hostname; |
| 460 | 1131 size_t hostlen, domlen; |
| 1 | 1132 unsigned char addy[4]; |
| 1133 struct sockaddr *addr; | |
| 1134 GList * templist; | |
| 1135 gint32 netaddr; | |
| 1136 char *pos; | |
| 1137 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 1138 struct addrinfo hints; | |
| 516 | 1139 unsigned int port; |
| 1140 int errnum; | |
| 1 | 1141 char serv[8]; |
| 122 | 1142 #endif |
| 1143 | |
| 218 | 1144 gftp_lookup_global_option ("dont_use_proxy", &proxy_hosts); |
| 122 | 1145 |
| 1146 if (proxy_hostname == NULL || *proxy_hostname == '\0') | |
| 1147 return (0); | |
| 1148 else if (proxy_hosts->list == NULL) | |
| 1149 return (proxy_hostname != NULL && | |
| 1150 *proxy_hostname != '\0'); | |
| 1 | 1151 |
| 1152 request->hostp = NULL; | |
| 122 | 1153 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
| 151 | 1154 request->free_hostp = 1; |
| 1 | 1155 memset (&hints, 0, sizeof (hints)); |
| 1156 hints.ai_flags = AI_CANONNAME; | |
| 146 | 1157 hints.ai_family = PF_UNSPEC; |
| 1 | 1158 hints.ai_socktype = SOCK_STREAM; |
| 1159 | |
| 122 | 1160 port = request->use_proxy ? proxy_port : request->port; |
| 1 | 1161 if (port == 0) |
| 1162 strcpy (serv, service); | |
| 1163 else | |
| 1164 snprintf (serv, sizeof (serv), "%d", port); | |
| 1165 | |
| 186 | 1166 request->logging_function (gftp_logging_misc, request, |
| 1 | 1167 _("Looking up %s\n"), request->hostname); |
| 1168 | |
| 1169 if ((errnum = getaddrinfo (request->hostname, serv, &hints, | |
| 1170 &request->hostp)) != 0) | |
| 1171 { | |
| 186 | 1172 request->logging_function (gftp_logging_error, request, |
| 1 | 1173 _("Cannot look up hostname %s: %s\n"), |
| 1174 request->hostname, gai_strerror (errnum)); | |
| 84 | 1175 return (GFTP_ERETRYABLE); |
| 1 | 1176 } |
| 1177 | |
| 1178 addr = request->hostp->ai_addr; | |
| 1179 | |
| 1180 #else /* !HAVE_GETADDRINFO */ | |
| 186 | 1181 request->logging_function (gftp_logging_misc, request, |
| 1 | 1182 _("Looking up %s\n"), request->hostname); |
| 1183 | |
| 1184 if (!(request->hostp = r_gethostbyname (request->hostname, &request->host, | |
| 1185 NULL))) | |
| 1186 { | |
| 186 | 1187 request->logging_function (gftp_logging_error, request, |
| 1 | 1188 _("Cannot look up hostname %s: %s\n"), |
| 1189 request->hostname, g_strerror (errno)); | |
| 84 | 1190 return (GFTP_ERETRYABLE); |
| 1 | 1191 } |
| 1192 | |
| 1193 addr = (struct sockaddr *) request->host.h_addr_list[0]; | |
| 1194 | |
| 1195 #endif /* HAVE_GETADDRINFO */ | |
| 1196 | |
| 122 | 1197 templist = proxy_hosts->list; |
| 1 | 1198 while (templist != NULL) |
| 1199 { | |
| 1200 hostname = templist->data; | |
| 460 | 1201 if (hostname->domain != NULL) |
| 168 | 1202 { |
| 460 | 1203 hostlen = strlen (request->hostname); |
| 1204 domlen = strlen (hostname->domain); | |
| 1205 if (hostlen > domlen) | |
| 1206 { | |
| 1207 pos = request->hostname + hostlen - domlen; | |
| 1208 if (strcmp (hostname->domain, pos) == 0) | |
| 1209 return (0); | |
| 1210 } | |
| 168 | 1211 } |
| 1 | 1212 |
| 1213 if (hostname->ipv4_network_address != 0) | |
| 168 | 1214 { |
| 516 | 1215 memcpy (addy, addr, sizeof (*addy)); |
| 168 | 1216 netaddr = |
| 1217 (((addy[0] & 0xff) << 24) | ((addy[1] & 0xff) << 16) | | |
| 1218 ((addy[2] & 0xff) << 8) | (addy[3] & 0xff)) & | |
| 1 | 1219 hostname->ipv4_netmask; |
| 168 | 1220 if (netaddr == hostname->ipv4_network_address) |
| 1221 return (0); | |
| 1222 } | |
| 1 | 1223 templist = templist->next; |
| 1224 } | |
| 1225 | |
| 122 | 1226 return (proxy_hostname != NULL && *proxy_hostname != '\0'); |
| 1 | 1227 } |
| 1228 | |
| 1229 | |
| 48 | 1230 static char * |
| 1231 copy_token (char **dest, char *source) | |
| 1 | 1232 { |
| 48 | 1233 /* This function is used internally by gftp_parse_ls () */ |
| 1234 char *endpos, savepos; | |
| 1 | 1235 |
| 48 | 1236 endpos = source; |
| 1237 while (*endpos != ' ' && *endpos != '\t' && *endpos != '\0') | |
| 1238 endpos++; | |
| 1239 if (*endpos == '\0') | |
| 1240 return (NULL); | |
| 1 | 1241 |
| 48 | 1242 savepos = *endpos; |
| 1243 *endpos = '\0'; | |
| 1244 *dest = g_malloc (endpos - source + 1); | |
| 1245 strcpy (*dest, source); | |
| 1246 *endpos = savepos; | |
| 1 | 1247 |
| 48 | 1248 /* Skip the blanks till we get to the next entry */ |
| 1249 source = endpos + 1; | |
| 1250 while ((*source == ' ' || *source == '\t') && *source != '\0') | |
| 1251 source++; | |
| 1252 return (source); | |
| 1253 } | |
| 1 | 1254 |
| 1255 | |
| 48 | 1256 static char * |
| 1257 goto_next_token (char *pos) | |
| 1258 { | |
| 1259 while (*pos != ' ' && *pos != '\t' && *pos != '\0') | |
| 1260 pos++; | |
| 1 | 1261 |
| 516 | 1262 while (*pos == ' ' || *pos == '\t') |
| 48 | 1263 pos++; |
| 1264 | |
| 1265 return (pos); | |
| 1 | 1266 } |
| 1267 | |
| 1268 | |
| 485 | 1269 static time_t |
| 1270 parse_vms_time (char *str, char **endpos) | |
| 1271 { | |
| 1272 struct tm curtime; | |
| 1273 time_t ret; | |
| 1274 | |
| 1275 /* 8-JUN-2004 13:04:14 */ | |
| 1276 memset (&curtime, 0, sizeof (curtime)); | |
| 1277 | |
| 1278 *endpos = strptime (str, "%d-%b-%Y %H:%M:%S", &curtime); | |
| 1279 if (*endpos == NULL) | |
| 1280 *endpos = strptime (str, "%d-%b-%Y %H:%M", &curtime); | |
| 1281 | |
| 1282 if (*endpos != NULL) | |
| 1283 { | |
| 1284 ret = mktime (&curtime); | |
| 1285 for (; **endpos == ' ' || **endpos == '\t'; (*endpos)++); | |
| 1286 } | |
| 1287 else | |
| 1288 { | |
| 1289 ret = 0; | |
| 1290 *endpos = goto_next_token (str); | |
| 1291 if (*endpos != NULL) | |
| 1292 *endpos = goto_next_token (*endpos); | |
| 1293 } | |
| 1294 | |
| 1295 return (ret); | |
| 1296 } | |
| 1297 | |
| 1298 | |
| 102 | 1299 time_t |
| 1300 parse_time (char *str, char **endpos) | |
| 1 | 1301 { |
| 102 | 1302 struct tm curtime, *loctime; |
| 105 | 1303 time_t t, ret; |
| 102 | 1304 char *tmppos; |
| 460 | 1305 size_t slen; |
| 260 | 1306 int i, num; |
| 1 | 1307 |
| 460 | 1308 slen = strlen (str); |
| 102 | 1309 memset (&curtime, 0, sizeof (curtime)); |
| 1310 curtime.tm_isdst = -1; | |
| 460 | 1311 |
| 1312 if (slen > 4 && isdigit ((int) str[0]) && str[2] == '-' && | |
| 260 | 1313 isdigit ((int) str[3])) |
| 1 | 1314 { |
| 1315 /* This is how DOS will return the date/time */ | |
| 1316 /* 07-06-99 12:57PM */ | |
| 1317 | |
| 102 | 1318 tmppos = strptime (str, "%m-%d-%y %I:%M%p", &curtime); |
| 1 | 1319 } |
| 460 | 1320 else if (slen > 4 && isdigit ((int) str[0]) && str[2] == '-' && |
| 260 | 1321 isalpha (str[3])) |
| 105 | 1322 { |
| 1323 /* 10-Jan-2003 09:14 */ | |
| 1324 tmppos = strptime (str, "%d-%h-%Y %H:%M", &curtime); | |
| 1325 } | |
| 460 | 1326 else if (slen > 4 && isdigit ((int) str[0]) && str[4] == '/') |
| 358 | 1327 { |
| 1328 /* 2003/12/25 */ | |
| 1329 tmppos = strptime (str, "%Y/%m/%d", &curtime); | |
| 1330 } | |
| 1 | 1331 else |
| 1332 { | |
| 1333 /* This is how most UNIX, Novell, and MacOS ftp servers send their time */ | |
| 102 | 1334 /* Jul 06 12:57 or Jul 6 1999 */ |
| 1 | 1335 |
| 102 | 1336 if (strchr (str, ':') != NULL) |
| 1337 { | |
| 1338 tmppos = strptime (str, "%h %d %H:%M", &curtime); | |
| 1339 t = time (NULL); | |
| 1340 loctime = localtime (&t); | |
| 359 | 1341 |
| 1342 if (curtime.tm_mon > loctime->tm_mon) | |
| 1343 curtime.tm_year = loctime->tm_year - 1; | |
| 1344 else | |
| 1345 curtime.tm_year = loctime->tm_year; | |
| 102 | 1346 } |
| 1347 else | |
| 1348 tmppos = strptime (str, "%h %d %Y", &curtime); | |
| 1349 } | |
| 1 | 1350 |
| 105 | 1351 if (tmppos != NULL) |
| 1352 ret = mktime (&curtime); | |
| 1353 else | |
| 1354 ret = 0; | |
| 1355 | |
| 102 | 1356 if (endpos != NULL) |
| 105 | 1357 { |
| 1358 if (tmppos == NULL) | |
| 1359 { | |
| 260 | 1360 /* We cannot parse this date format. So, just skip this date field |
| 1361 and continue to the next token. This is mainly for the HTTP | |
| 1362 support */ | |
| 1363 | |
| 1364 *endpos = str; | |
| 1365 for (num = 0; num < 2 && **endpos != '\0'; num++) | |
| 1366 { | |
| 1367 for (i=0; | |
| 1368 (*endpos)[i] != ' ' && (*endpos)[i] != '\t' && | |
| 1369 (*endpos)[i] != '\0'; | |
| 1370 i++); | |
| 1371 *endpos += i; | |
| 1372 | |
| 1373 for (i=0; (*endpos)[i] == ' ' || (*endpos)[i] == '\t'; i++); | |
| 1374 *endpos += i; | |
| 1375 } | |
| 105 | 1376 } |
| 1377 else | |
| 1378 *endpos = tmppos; | |
| 1379 } | |
| 1 | 1380 |
| 281 | 1381 return (ret); |
| 1 | 1382 } |
| 1383 | |
| 1384 | |
| 499 | 1385 static mode_t |
| 1386 gftp_parse_vms_attribs (char **src, mode_t mask) | |
| 107 | 1387 { |
| 1388 char *endpos; | |
| 499 | 1389 mode_t ret; |
| 107 | 1390 |
| 1391 if (*src == NULL) | |
| 499 | 1392 return (0); |
| 107 | 1393 |
| 1394 if ((endpos = strchr (*src, ',')) != NULL) | |
| 1395 *endpos = '\0'; | |
| 1396 | |
| 499 | 1397 ret = 0; |
| 107 | 1398 if (strchr (*src, 'R') != NULL) |
| 499 | 1399 ret |= S_IRUSR | S_IRGRP | S_IROTH; |
| 107 | 1400 if (strchr (*src, 'W') != NULL) |
| 499 | 1401 ret |= S_IWUSR | S_IWGRP | S_IWOTH; |
| 107 | 1402 if (strchr (*src, 'E') != NULL) |
| 499 | 1403 ret |= S_IXUSR | S_IXGRP | S_IXOTH; |
| 107 | 1404 |
| 1405 *src = endpos + 1; | |
| 499 | 1406 |
| 1407 return (ret & mask); | |
| 107 | 1408 } |
| 1409 | |
| 1410 | |
| 1411 static int | |
| 485 | 1412 gftp_parse_ls_vms (gftp_request * request, int fd, char *str, gftp_file * fle) |
| 107 | 1413 { |
| 485 | 1414 char *curpos, *endpos, tempstr[1024]; |
| 1415 int multiline; | |
| 1416 ssize_t len; | |
| 107 | 1417 |
| 1418 /* .PINE-DEBUG1;1 9 21-AUG-2002 20:06 [MYERSRG] (RWED,RWED,,) */ | |
| 1419 /* WWW.DIR;1 1 23-NOV-1999 05:47 [MYERSRG] (RWE,RWE,RE,E) */ | |
| 1420 | |
| 485 | 1421 /* Multiline VMS |
| 1422 $MAIN.TPU$JOURNAL;1 | |
| 1423 1/18 8-JUN-2004 13:04:14 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
| 1424 TCPIP$FTP_SERVER.LOG;29 | |
| 1425 0/18 8-JUN-2004 14:42:04 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
| 1426 TCPIP$FTP_SERVER.LOG;28 | |
| 1427 5/18 8-JUN-2004 13:05:11 [NUCLEAR,FISSION] (RWED,RWED,RE,) | |
| 1428 TCPIP$FTP_SERVER.LOG;27 | |
| 1429 5/18 8-JUN-2004 13:03:51 [NUCLEAR,FISSION] (RWED,RWED,RE,) */ | |
| 1430 | |
| 107 | 1431 if ((curpos = strchr (str, ';')) == NULL) |
| 1432 return (GFTP_EFATAL); | |
| 1433 | |
| 485 | 1434 multiline = strchr (str, ' ') == NULL; |
| 1435 | |
| 107 | 1436 *curpos = '\0'; |
| 1437 if (strlen (str) > 4 && strcmp (curpos - 4, ".DIR") == 0) | |
| 1438 { | |
| 499 | 1439 fle->st_mode |= S_IFDIR; |
| 107 | 1440 *(curpos - 4) = '\0'; |
| 1441 } | |
| 1442 | |
| 1443 fle->file = g_strdup (str); | |
| 1444 | |
| 485 | 1445 if (multiline) |
| 1446 { | |
| 1447 if (request->get_next_dirlist_line == NULL) | |
| 1448 return (GFTP_EFATAL); | |
| 1449 | |
| 1450 len = request->get_next_dirlist_line (request, fd, tempstr, | |
| 1451 sizeof (tempstr)); | |
| 1452 if (len <= 0) | |
| 1453 return ((int) len); | |
| 1454 | |
| 1455 for (curpos = tempstr; *curpos == ' ' || *curpos == '\t'; curpos++); | |
| 1456 } | |
| 1457 else | |
| 1458 curpos = goto_next_token (curpos + 1); | |
| 1459 | |
| 244 | 1460 fle->size = gftp_parse_file_size (curpos) * 512; /* Is this correct? */ |
| 107 | 1461 |
| 1462 curpos = goto_next_token (curpos); | |
| 1463 | |
| 485 | 1464 fle->datetime = parse_vms_time (curpos, &curpos); |
| 1465 | |
| 107 | 1466 if (*curpos != '[') |
| 1467 return (GFTP_EFATAL); | |
| 485 | 1468 |
| 107 | 1469 if ((endpos = strchr (curpos, ']')) == NULL) |
| 1470 return (GFTP_EFATAL); | |
| 1471 | |
| 1472 curpos = goto_next_token (endpos + 1); | |
| 1473 if ((curpos = strchr (curpos, ',')) == NULL) | |
| 1474 return (0); | |
| 1475 curpos++; | |
| 1476 | |
| 499 | 1477 fle->st_mode = gftp_parse_vms_attribs (&curpos, S_IRWXU); |
| 1478 fle->st_mode |= gftp_parse_vms_attribs (&curpos, S_IRWXG); | |
| 1479 fle->st_mode |= gftp_parse_vms_attribs (&curpos, S_IRWXO); | |
| 107 | 1480 |
| 485 | 1481 fle->user = g_strdup (""); |
| 1482 fle->group = g_strdup (""); | |
| 1483 | |
| 107 | 1484 return (0); |
| 1485 } | |
| 358 | 1486 |
| 1487 | |
| 1488 static int | |
| 1489 gftp_parse_ls_mvs (char *str, gftp_file * fle) | |
| 1490 { | |
| 1491 char *curpos; | |
| 1492 | |
| 1493 /* Volume Unit Referred Ext Used Recfm Lrecl BlkSz Dsorg Dsname */ | |
| 1494 /* SVI52A 3390 2003/12/10 8 216 FB 80 27920 PS CARDS.DELETES */ | |
| 1495 /* SVI528 3390 2003/12/12 1 5 FB 80 24000 PO CLIST */ | |
| 1496 | |
| 1497 curpos = goto_next_token (str + 1); | |
| 1498 if (curpos == NULL) | |
| 1499 return (GFTP_EFATAL); | |
| 1500 | |
| 1501 curpos = goto_next_token (curpos + 1); | |
| 1502 if (curpos == NULL) | |
| 1503 return (GFTP_EFATAL); | |
| 1504 | |
| 479 | 1505 fle->datetime = parse_time (curpos, &curpos); |
| 1506 | |
| 1507 curpos = goto_next_token (curpos); | |
| 1508 if (curpos == NULL) | |
| 358 | 1509 return (GFTP_EFATAL); |
| 1510 | |
| 1511 curpos = goto_next_token (curpos + 1); | |
| 1512 if (curpos == NULL) | |
| 1513 return (GFTP_EFATAL); | |
| 1514 | |
| 1515 fle->size = gftp_parse_file_size (curpos) * 55996; | |
| 1516 curpos = goto_next_token (curpos + 1); | |
| 1517 if (curpos == NULL) | |
| 1518 return (GFTP_EFATAL); | |
| 1519 | |
| 1520 curpos = goto_next_token (curpos + 1); | |
| 1521 if (curpos == NULL) | |
| 1522 return (GFTP_EFATAL); | |
| 1523 | |
| 1524 curpos = goto_next_token (curpos + 1); | |
| 1525 if (curpos == NULL) | |
| 1526 return (GFTP_EFATAL); | |
| 1527 | |
| 1528 curpos = goto_next_token (curpos + 1); | |
| 1529 if (curpos == NULL) | |
| 1530 return (GFTP_EFATAL); | |
| 1531 | |
| 1532 if (strncmp (curpos, "PS", 2) == 0) | |
| 499 | 1533 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 358 | 1534 else if (strncmp (curpos, "PO", 2) == 0) |
| 499 | 1535 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
| 358 | 1536 else |
| 1537 return (GFTP_EFATAL); | |
| 1538 | |
| 1539 curpos = goto_next_token (curpos + 1); | |
| 1540 | |
| 1541 fle->user = g_strdup (_("unknown")); | |
| 1542 fle->group = g_strdup (_("unknown")); | |
| 1543 fle->file = g_strdup (curpos); | |
| 1544 | |
| 1545 return (0); | |
| 1546 } | |
| 107 | 1547 |
| 1548 | |
| 1 | 1549 static int |
| 1550 gftp_parse_ls_eplf (char *str, gftp_file * fle) | |
| 1551 { | |
| 1552 char *startpos; | |
| 358 | 1553 int isdir = 0; |
| 1 | 1554 |
| 1555 startpos = str; | |
| 1556 while (startpos) | |
| 1557 { | |
| 1558 startpos++; | |
| 1559 switch (*startpos) | |
| 168 | 1560 { |
| 516 | 1561 case '/': |
| 1562 isdir = 1; | |
| 1563 break; | |
| 1564 case 's': | |
| 1565 fle->size = gftp_parse_file_size (startpos + 1); | |
| 1566 break; | |
| 1567 case 'm': | |
| 1568 fle->datetime = strtol (startpos + 1, NULL, 10); | |
| 1569 break; | |
| 168 | 1570 } |
| 1 | 1571 startpos = strchr (startpos, ','); |
| 1572 } | |
| 358 | 1573 |
| 1 | 1574 if ((startpos = strchr (str, 9)) == NULL) |
| 84 | 1575 return (GFTP_EFATAL); |
| 358 | 1576 |
| 1577 if (isdir) | |
| 499 | 1578 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
| 358 | 1579 else |
| 499 | 1580 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 358 | 1581 |
| 124 | 1582 fle->file = g_strdup (startpos + 1); |
| 1583 fle->user = g_strdup (_("unknown")); | |
| 1584 fle->group = g_strdup (_("unknown")); | |
| 1 | 1585 return (0); |
| 1586 } | |
| 1587 | |
| 1588 | |
| 1589 static int | |
| 460 | 1590 gftp_parse_ls_unix (gftp_request * request, char *str, size_t slen, |
| 1591 gftp_file * fle) | |
| 1 | 1592 { |
| 499 | 1593 char *endpos, *startpos, *pos, *attribs; |
| 91 | 1594 int cols; |
| 1595 | |
| 1596 /* If there is no space between the attribs and links field, just make one */ | |
| 460 | 1597 if (slen > 10) |
| 91 | 1598 str[10] = ' '; |
| 1599 | |
| 1600 /* Determine the number of columns */ | |
| 1601 cols = 0; | |
| 1602 pos = str; | |
| 1603 while (*pos != '\0') | |
| 1604 { | |
| 1605 while (*pos != '\0' && *pos != ' ' && *pos != '\t') | |
| 1606 { | |
| 1607 if (*pos == ':') | |
| 1608 break; | |
| 1609 pos++; | |
| 1610 } | |
| 1611 | |
| 1612 cols++; | |
| 1613 | |
| 1614 if (*pos == ':') | |
| 1615 { | |
| 1616 cols++; | |
| 1617 break; | |
| 1618 } | |
| 1619 | |
| 1620 while (*pos == ' ' || *pos == '\t') | |
| 1621 pos++; | |
| 1622 } | |
| 1 | 1623 |
| 1624 startpos = str; | |
| 1625 /* Copy file attributes */ | |
| 499 | 1626 if ((startpos = copy_token (&attribs, startpos)) == NULL) |
| 84 | 1627 return (GFTP_EFATAL); |
| 1 | 1628 |
| 499 | 1629 fle->st_mode = gftp_convert_attributes_to_mode_t (attribs); |
| 1630 g_free (attribs); | |
| 1631 | |
| 1 | 1632 if (cols >= 9) |
| 1633 { | |
| 1634 /* Skip the number of links */ | |
| 1635 startpos = goto_next_token (startpos); | |
| 1636 | |
| 1637 /* Copy the user that owns this file */ | |
| 1638 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 168 | 1639 return (GFTP_EFATAL); |
| 1 | 1640 |
| 1641 /* Copy the group that owns this file */ | |
| 1642 if ((startpos = copy_token (&fle->group, startpos)) == NULL) | |
| 168 | 1643 return (GFTP_EFATAL); |
| 1 | 1644 } |
| 1645 else | |
| 1646 { | |
| 124 | 1647 fle->group = g_strdup (_("unknown")); |
| 1 | 1648 if (cols == 8) |
| 168 | 1649 { |
| 1650 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 1651 return (GFTP_EFATAL); | |
| 1652 } | |
| 1 | 1653 else |
| 124 | 1654 fle->user = g_strdup (_("unknown")); |
| 1 | 1655 startpos = goto_next_token (startpos); |
| 1656 } | |
| 1657 | |
| 281 | 1658 if (request->server_type == GFTP_DIRTYPE_CRAY) |
| 1 | 1659 { |
| 91 | 1660 /* See if this is a Cray directory listing. It has the following format: |
| 1661 drwx------ 2 feiliu g913 DK common 4096 Sep 24 2001 wv */ | |
| 1662 if (cols == 11 && strstr (str, "->") == NULL) | |
| 1663 { | |
| 1664 startpos = goto_next_token (startpos); | |
| 1665 startpos = goto_next_token (startpos); | |
| 1666 } | |
| 1 | 1667 } |
| 1668 | |
| 1669 /* See if this is a block or character device. We will store the major number | |
| 1670 in the high word and the minor number in the low word. */ | |
| 499 | 1671 if (GFTP_IS_SPECIAL_DEVICE (fle->st_mode) && |
| 1672 (endpos = strchr (startpos, ',')) != NULL) | |
| 1 | 1673 { |
| 1674 fle->size = strtol (startpos, NULL, 10) << 16; | |
| 1675 | |
| 1676 startpos = endpos + 1; | |
| 1677 while (*startpos == ' ') | |
| 168 | 1678 startpos++; |
| 1 | 1679 |
| 1680 /* Get the minor number */ | |
| 1681 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 168 | 1682 return (GFTP_EFATAL); |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1683 fle->size |= strtol (startpos, NULL, 10) & 0xFF; |
| 1 | 1684 } |
| 1685 else | |
| 1686 { | |
| 1687 /* This is a regular file */ | |
| 1688 if ((endpos = strchr (startpos, ' ')) == NULL) | |
| 168 | 1689 return (GFTP_EFATAL); |
| 244 | 1690 fle->size = gftp_parse_file_size (startpos); |
| 1 | 1691 } |
| 1692 | |
| 1693 /* Skip the blanks till we get to the next entry */ | |
| 1694 startpos = endpos + 1; | |
| 1695 while (*startpos == ' ') | |
| 1696 startpos++; | |
| 1697 | |
| 479 | 1698 fle->datetime = parse_time (startpos, &startpos); |
| 1 | 1699 |
| 1700 /* Skip the blanks till we get to the next entry */ | |
| 1701 startpos = goto_next_token (startpos); | |
| 1702 | |
| 1703 /* Parse the filename. If this file is a symbolic link, remove the -> part */ | |
| 499 | 1704 if (S_ISLNK (fle->st_mode) && ((endpos = strstr (startpos, "->")) != NULL)) |
| 1 | 1705 *(endpos - 1) = '\0'; |
| 1706 | |
| 124 | 1707 fle->file = g_strdup (startpos); |
| 1 | 1708 |
| 1709 /* Uncomment this if you want to strip the spaces off of the end of the file. | |
| 1710 I don't want to do this by default since there are valid filenames with | |
| 1711 spaces at the end of them. Some broken FTP servers like the Paradyne IPC | |
| 1712 DSLAMS append a bunch of spaces at the end of the file. | |
| 1713 for (endpos = fle->file + strlen (fle->file) - 1; | |
| 1714 *endpos == ' '; | |
| 1715 *endpos-- = '\0'); | |
| 1716 */ | |
| 1717 | |
| 1718 return (0); | |
| 1719 } | |
| 1720 | |
| 1721 | |
| 1722 static int | |
| 1723 gftp_parse_ls_nt (char *str, gftp_file * fle) | |
| 1724 { | |
| 1725 char *startpos; | |
| 1726 | |
| 1727 startpos = str; | |
| 479 | 1728 fle->datetime = parse_time (startpos, &startpos); |
| 1 | 1729 |
| 124 | 1730 fle->user = g_strdup (_("unknown")); |
| 1731 fle->group = g_strdup (_("unknown")); | |
| 1 | 1732 |
| 1733 startpos = goto_next_token (startpos); | |
| 499 | 1734 |
| 1 | 1735 if (startpos[0] == '<') |
| 499 | 1736 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
| 1 | 1737 else |
| 1738 { | |
| 499 | 1739 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 244 | 1740 fle->size = gftp_parse_file_size (startpos); |
| 1 | 1741 } |
| 124 | 1742 |
| 1 | 1743 startpos = goto_next_token (startpos); |
| 124 | 1744 fle->file = g_strdup (startpos); |
| 1 | 1745 return (0); |
| 1746 } | |
| 1747 | |
| 1748 | |
| 1749 static int | |
| 1750 gftp_parse_ls_novell (char *str, gftp_file * fle) | |
| 1751 { | |
| 1752 char *startpos; | |
| 1753 | |
| 1754 if (str[12] != ' ') | |
| 84 | 1755 return (GFTP_EFATAL); |
| 499 | 1756 |
| 1 | 1757 str[12] = '\0'; |
| 499 | 1758 fle->st_mode = gftp_convert_attributes_to_mode_t (str); |
| 1 | 1759 startpos = str + 13; |
| 1760 | |
| 1761 while ((*startpos == ' ' || *startpos == '\t') && *startpos != '\0') | |
| 1762 startpos++; | |
| 1763 | |
| 1764 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
| 84 | 1765 return (GFTP_EFATAL); |
| 1 | 1766 |
| 124 | 1767 fle->group = g_strdup (_("unknown")); |
| 1 | 1768 |
| 601 | 1769 while (*startpos != '\0' && !isdigit (*startpos)) |
| 1770 startpos++; | |
| 1771 | |
| 244 | 1772 fle->size = gftp_parse_file_size (startpos); |
| 1 | 1773 |
| 1774 startpos = goto_next_token (startpos); | |
| 479 | 1775 fle->datetime = parse_time (startpos, &startpos); |
| 1 | 1776 |
| 1777 startpos = goto_next_token (startpos); | |
| 124 | 1778 fle->file = g_strdup (startpos); |
| 1 | 1779 return (0); |
| 1780 } | |
| 1781 | |
| 1782 | |
| 48 | 1783 int |
| 485 | 1784 gftp_parse_ls (gftp_request * request, const char *lsoutput, gftp_file * fle, |
| 1785 int fd) | |
| 1 | 1786 { |
| 107 | 1787 char *str, *endpos, tmpchar; |
| 1788 int result, is_vms; | |
| 91 | 1789 size_t len; |
| 48 | 1790 |
| 84 | 1791 g_return_val_if_fail (lsoutput != NULL, GFTP_EFATAL); |
| 1792 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
| 48 | 1793 |
| 124 | 1794 str = g_strdup (lsoutput); |
| 48 | 1795 memset (fle, 0, sizeof (*fle)); |
| 1 | 1796 |
| 91 | 1797 len = strlen (str); |
| 107 | 1798 if (len > 0 && str[len - 1] == '\n') |
| 91 | 1799 str[--len] = '\0'; |
| 1800 if (len > 0 && str[len - 1] == '\r') | |
| 1801 str[--len] = '\0'; | |
| 39 | 1802 |
| 91 | 1803 switch (request->server_type) |
| 1804 { | |
| 122 | 1805 case GFTP_DIRTYPE_CRAY: |
| 1806 case GFTP_DIRTYPE_UNIX: | |
| 460 | 1807 result = gftp_parse_ls_unix (request, str, len, fle); |
| 91 | 1808 break; |
| 122 | 1809 case GFTP_DIRTYPE_EPLF: |
| 91 | 1810 result = gftp_parse_ls_eplf (str, fle); |
| 1811 break; | |
| 122 | 1812 case GFTP_DIRTYPE_NOVELL: |
| 91 | 1813 result = gftp_parse_ls_novell (str, fle); |
| 1814 break; | |
| 122 | 1815 case GFTP_DIRTYPE_DOS: |
| 91 | 1816 result = gftp_parse_ls_nt (str, fle); |
| 1817 break; | |
| 122 | 1818 case GFTP_DIRTYPE_VMS: |
| 485 | 1819 result = gftp_parse_ls_vms (request, fd, str, fle); |
| 107 | 1820 break; |
| 358 | 1821 case GFTP_DIRTYPE_MVS: |
| 1822 result = gftp_parse_ls_mvs (str, fle); | |
| 1823 break; | |
| 91 | 1824 default: /* autodetect */ |
| 1825 if (*lsoutput == '+') | |
| 1826 result = gftp_parse_ls_eplf (str, fle); | |
| 1827 else if (isdigit ((int) str[0]) && str[2] == '-') | |
| 1828 result = gftp_parse_ls_nt (str, fle); | |
| 1829 else if (str[1] == ' ' && str[2] == '[') | |
| 1830 result = gftp_parse_ls_novell (str, fle); | |
| 1831 else | |
| 107 | 1832 { |
| 1833 if ((endpos = strchr (str, ' ')) != NULL) | |
| 1834 { | |
| 1835 /* If the first token in the string has a ; in it, then */ | |
| 1836 /* we'll assume that this is a VMS directory listing */ | |
| 1837 tmpchar = *endpos; | |
| 1838 *endpos = '\0'; | |
| 1839 is_vms = strchr (str, ';') != NULL; | |
| 1840 *endpos = tmpchar; | |
| 1841 } | |
| 1842 else | |
| 1843 is_vms = 0; | |
| 48 | 1844 |
| 107 | 1845 if (is_vms) |
| 485 | 1846 result = gftp_parse_ls_vms (request, fd, str, fle); |
| 107 | 1847 else |
| 460 | 1848 result = gftp_parse_ls_unix (request, str, len, fle); |
| 107 | 1849 } |
| 91 | 1850 break; |
| 48 | 1851 } |
| 1852 g_free (str); | |
| 1853 | |
| 1854 return (result); | |
| 1 | 1855 } |
| 1856 | |
| 1857 | |
| 48 | 1858 static GHashTable * |
| 469 | 1859 gftp_gen_dir_hash (gftp_request * request, int *ret) |
| 1 | 1860 { |
| 48 | 1861 GHashTable * dirhash; |
| 1862 gftp_file * fle; | |
| 516 | 1863 off_t *newsize; |
| 48 | 1864 char * newname; |
| 1865 | |
| 1866 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); | |
| 469 | 1867 *ret = gftp_list_files (request); |
| 1868 if (*ret == 0) | |
| 48 | 1869 { |
| 1870 fle = g_malloc0 (sizeof (*fle)); | |
| 1871 while (gftp_get_next_file (request, NULL, fle) > 0) | |
| 1872 { | |
| 1873 newname = fle->file; | |
| 516 | 1874 newsize = g_malloc (sizeof (*newsize)); |
| 48 | 1875 *newsize = fle->size; |
| 1876 g_hash_table_insert (dirhash, newname, newsize); | |
| 1877 fle->file = NULL; | |
| 598 | 1878 gftp_file_destroy (fle, 0); |
| 48 | 1879 } |
| 1880 gftp_end_transfer (request); | |
| 1881 g_free (fle); | |
| 1882 } | |
| 1883 else | |
| 1884 { | |
| 1885 g_hash_table_destroy (dirhash); | |
| 1886 dirhash = NULL; | |
| 1887 } | |
| 509 | 1888 |
| 48 | 1889 return (dirhash); |
| 1890 } | |
| 39 | 1891 |
| 48 | 1892 |
| 1893 static void | |
| 1894 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
| 1895 { | |
| 39 | 1896 |
| 48 | 1897 g_free (key); |
| 1898 g_free (value); | |
| 1899 } | |
| 1900 | |
| 1901 | |
| 1902 static void | |
| 1903 gftp_destroy_dir_hash (GHashTable * dirhash) | |
| 1904 { | |
| 1905 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); | |
| 1906 g_hash_table_destroy (dirhash); | |
| 1 | 1907 } |
| 1908 | |
| 1909 | |
| 1910 static GList * | |
| 469 | 1911 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir, int *ret) |
| 1 | 1912 { |
| 1913 GHashTable * dirhash; | |
| 1914 GList * templist; | |
| 1915 gftp_file * fle; | |
| 516 | 1916 off_t *newsize; |
| 1 | 1917 char *newname; |
| 1918 | |
| 1919 if (getothdir && transfer->toreq) | |
| 469 | 1920 { |
| 1921 dirhash = gftp_gen_dir_hash (transfer->toreq, ret); | |
| 1922 if (*ret < 0) | |
| 1923 return (NULL); | |
| 1924 } | |
| 1 | 1925 else |
| 1926 dirhash = NULL; | |
| 1927 | |
| 509 | 1928 *ret = gftp_list_files (transfer->fromreq); |
| 1929 if (*ret < 0) | |
| 1 | 1930 return (NULL); |
| 1931 | |
| 1932 fle = g_malloc (sizeof (*fle)); | |
| 1933 templist = NULL; | |
| 1934 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
| 1935 { | |
| 1936 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) | |
| 1937 { | |
| 598 | 1938 gftp_file_destroy (fle, 0); |
| 1 | 1939 continue; |
| 1940 } | |
| 1941 | |
| 1942 if (dirhash && | |
| 1943 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
| 1944 fle->startsize = *newsize; | |
| 1945 | |
| 381 | 1946 if (transfer->toreq && fle->destfile == NULL) |
| 555 | 1947 fle->destfile = gftp_build_path (transfer->toreq, |
| 1948 transfer->toreq->directory, | |
| 245 | 1949 fle->file, NULL); |
| 1950 | |
| 381 | 1951 if (transfer->fromreq->directory != NULL && |
| 1952 *transfer->fromreq->directory != '\0' && | |
| 1953 *fle->file != '/') | |
| 1954 { | |
| 555 | 1955 newname = gftp_build_path (transfer->fromreq, |
| 1956 transfer->fromreq->directory, | |
| 381 | 1957 fle->file, NULL); |
| 1958 | |
| 1959 g_free (fle->file); | |
| 1960 fle->file = newname; | |
| 1961 } | |
| 1 | 1962 |
| 1963 templist = g_list_append (templist, fle); | |
| 1964 | |
| 1965 fle = g_malloc (sizeof (*fle)); | |
| 1966 } | |
| 1967 gftp_end_transfer (transfer->fromreq); | |
| 1968 | |
| 598 | 1969 gftp_file_destroy (fle, 1); |
| 1 | 1970 |
| 509 | 1971 if (dirhash != NULL) |
| 1 | 1972 gftp_destroy_dir_hash (dirhash); |
| 1973 | |
| 1974 return (templist); | |
| 1975 } | |
| 1976 | |
| 1977 | |
| 1978 int | |
| 1979 gftp_get_all_subdirs (gftp_transfer * transfer, | |
| 1980 void (*update_func) (gftp_transfer * transfer)) | |
| 1981 { | |
| 381 | 1982 char *oldfromdir, *oldtodir, *newname, *pos; |
| 469 | 1983 int forcecd, remotechanged, ret; |
| 1 | 1984 GList * templist, * lastlist; |
| 1985 GHashTable * dirhash; | |
| 1986 gftp_file * curfle; | |
| 516 | 1987 off_t *newsize; |
| 500 | 1988 mode_t st_mode; |
| 1 | 1989 |
| 84 | 1990 g_return_val_if_fail (transfer != NULL, GFTP_EFATAL); |
| 1991 g_return_val_if_fail (transfer->fromreq != NULL, GFTP_EFATAL); | |
| 1992 g_return_val_if_fail (transfer->files != NULL, GFTP_EFATAL); | |
| 1 | 1993 |
| 1994 if (transfer->toreq != NULL) | |
| 469 | 1995 { |
| 1996 ret = 0; | |
| 1997 dirhash = gftp_gen_dir_hash (transfer->toreq, &ret); | |
| 1998 if (ret < 0) | |
| 1999 return (ret); | |
| 2000 } | |
| 1 | 2001 else |
| 2002 dirhash = NULL; | |
| 2003 | |
| 2004 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
| 2005 { | |
| 2006 curfle = lastlist->data; | |
| 381 | 2007 |
| 2008 if ((pos = strrchr (curfle->file, '/')) != NULL) | |
| 2009 pos++; | |
| 2010 else | |
| 2011 pos = curfle->file; | |
| 2012 | |
| 2013 if (dirhash != NULL && | |
| 2014 (newsize = g_hash_table_lookup (dirhash, pos)) != NULL) | |
| 1 | 2015 curfle->startsize = *newsize; |
| 2016 | |
| 381 | 2017 if (curfle->size < 0 && GFTP_IS_CONNECTED (transfer->fromreq)) |
| 509 | 2018 { |
| 2019 curfle->size = gftp_get_file_size (transfer->fromreq, curfle->file); | |
| 2020 if (curfle->size < 0) | |
| 2021 return (curfle->size); | |
| 2022 } | |
| 381 | 2023 |
| 2024 if (transfer->toreq && curfle->destfile == NULL) | |
| 555 | 2025 curfle->destfile = gftp_build_path (transfer->toreq, |
| 2026 transfer->toreq->directory, | |
| 381 | 2027 curfle->file, NULL); |
| 2028 | |
| 2029 if (transfer->fromreq->directory != NULL && | |
| 509 | 2030 *transfer->fromreq->directory != '\0' && *curfle->file != '/') |
| 381 | 2031 { |
| 555 | 2032 newname = gftp_build_path (transfer->fromreq, |
| 2033 transfer->fromreq->directory, | |
| 381 | 2034 curfle->file, NULL); |
| 2035 g_free (curfle->file); | |
| 2036 curfle->file = newname; | |
| 2037 } | |
| 1 | 2038 |
| 2039 if (lastlist->next == NULL) | |
| 2040 break; | |
| 2041 } | |
| 2042 | |
| 509 | 2043 if (dirhash != NULL) |
| 1 | 2044 gftp_destroy_dir_hash (dirhash); |
| 2045 | |
| 2046 oldfromdir = oldtodir = NULL; | |
| 2047 remotechanged = 0; | |
| 2048 forcecd = 0; | |
| 2049 for (templist = transfer->files; templist != NULL; templist = templist->next) | |
| 2050 { | |
| 2051 curfle = templist->data; | |
| 2052 | |
| 500 | 2053 if (S_ISLNK (curfle->st_mode) && !S_ISDIR (curfle->st_mode)) |
| 2054 { | |
| 520 | 2055 st_mode = 0; |
| 2056 ret = gftp_stat_filename (transfer->fromreq, curfle->file, &st_mode); | |
| 2057 if (ret < 0) | |
| 2058 return (ret); | |
| 509 | 2059 else if (S_ISDIR (st_mode)) |
| 500 | 2060 curfle->st_mode = st_mode; |
| 2061 } | |
| 2062 | |
| 2063 if (curfle->st_mode & S_IFDIR) | |
| 1 | 2064 { |
| 2065 oldfromdir = transfer->fromreq->directory; | |
| 2066 transfer->fromreq->directory = curfle->file; | |
| 2067 | |
| 509 | 2068 if (transfer->toreq != NULL) |
| 1 | 2069 { |
| 2070 oldtodir = transfer->toreq->directory; | |
| 2071 transfer->toreq->directory = curfle->destfile; | |
| 2072 } | |
| 509 | 2073 |
| 1 | 2074 forcecd = 1; |
| 509 | 2075 ret = gftp_set_directory (transfer->fromreq, |
| 2076 transfer->fromreq->directory); | |
| 2077 if (ret < 0) | |
| 2078 return (ret); | |
| 2079 | |
| 2080 if (curfle->startsize > 0 && transfer->toreq != NULL) | |
| 1 | 2081 { |
| 509 | 2082 remotechanged = 1; |
| 2083 ret = gftp_set_directory (transfer->toreq, | |
| 2084 transfer->toreq->directory); | |
| 469 | 2085 if (ret < 0) |
| 509 | 2086 return (ret); |
| 2087 } | |
| 2088 | |
| 2089 ret = 0; | |
| 2090 lastlist->next = gftp_get_dir_listing (transfer, | |
| 2091 curfle->startsize > 0, | |
| 2092 &ret); | |
| 2093 if (ret < 0) | |
| 2094 return (ret); | |
| 2095 | |
| 2096 if (lastlist->next != NULL) | |
| 2097 { | |
| 2098 lastlist->next->prev = lastlist; | |
| 2099 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
| 1 | 2100 } |
| 509 | 2101 |
| 2102 transfer->numdirs++; | |
| 2103 if (update_func != NULL) | |
| 2104 update_func (transfer); | |
| 1 | 2105 |
| 2106 transfer->fromreq->directory = oldfromdir; | |
| 509 | 2107 if (transfer->toreq != NULL) |
| 1 | 2108 transfer->toreq->directory = oldtodir; |
| 2109 } | |
| 2110 else | |
| 122 | 2111 transfer->numfiles++; |
| 1 | 2112 } |
| 2113 | |
| 2114 if (forcecd) | |
| 509 | 2115 { |
| 2116 ret = gftp_set_directory (transfer->fromreq, | |
| 2117 transfer->fromreq->directory); | |
| 2118 if (ret < 0) | |
| 2119 return (ret); | |
| 2120 } | |
| 2121 | |
| 2122 if (remotechanged && transfer->toreq != NULL) | |
| 2123 { | |
| 2124 ret = gftp_set_directory (transfer->toreq, transfer->toreq->directory); | |
| 2125 if (ret < 0) | |
| 2126 return (ret); | |
| 2127 } | |
| 2128 | |
| 2129 if (update_func != NULL) | |
| 1 | 2130 { |
| 2131 transfer->numfiles = transfer->numdirs = -1; | |
| 2132 update_func (transfer); | |
| 2133 } | |
| 509 | 2134 |
| 1 | 2135 return (0); |
| 2136 } | |
| 2137 | |
| 2138 | |
| 2139 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
| 122 | 2140 static int |
| 1 | 2141 get_port (struct addrinfo *addr) |
| 2142 { | |
| 2143 struct sockaddr_in * saddr; | |
| 2144 int port; | |
| 2145 | |
| 2146 if (addr->ai_family == AF_INET) | |
| 2147 { | |
| 2148 saddr = (struct sockaddr_in *) addr->ai_addr; | |
| 2149 port = ntohs (saddr->sin_port); | |
| 2150 } | |
| 2151 else | |
| 2152 port = 0; | |
| 2153 | |
| 2154 return (port); | |
| 2155 } | |
| 2156 #endif | |
| 2157 | |
| 2158 | |
| 2159 int | |
| 122 | 2160 gftp_connect_server (gftp_request * request, char *service, |
| 516 | 2161 char *proxy_hostname, unsigned int proxy_port) |
| 1 | 2162 { |
| 2163 char *connect_host, *disphost; | |
| 516 | 2164 unsigned int port; |
| 2165 int sock = -1; | |
| 1 | 2166 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
| 2167 struct addrinfo hints, *res; | |
| 463 | 2168 intptr_t enable_ipv6; |
| 1 | 2169 char serv[8]; |
| 463 | 2170 int errnum; |
| 1 | 2171 |
| 516 | 2172 if ((errnum = gftp_need_proxy (request, service, proxy_hostname, |
| 2173 proxy_port)) < 0) | |
| 2174 return (errnum); | |
| 2175 else | |
| 2176 { | |
| 2177 request->use_proxy = errnum; | |
| 2178 if (request->use_proxy) | |
| 2179 request->hostp = NULL; | |
| 2180 } | |
| 1 | 2181 |
| 313 | 2182 gftp_lookup_request_option (request, "enable_ipv6", &enable_ipv6); |
| 2183 | |
| 151 | 2184 request->free_hostp = 1; |
| 1 | 2185 memset (&hints, 0, sizeof (hints)); |
| 2186 hints.ai_flags = AI_CANONNAME; | |
| 313 | 2187 |
| 2188 if (enable_ipv6) | |
| 2189 hints.ai_family = PF_UNSPEC; | |
| 2190 else | |
| 2191 hints.ai_family = AF_INET; | |
| 2192 | |
| 1 | 2193 hints.ai_socktype = SOCK_STREAM; |
| 2194 | |
| 122 | 2195 if (request->use_proxy) |
| 2196 { | |
| 2197 connect_host = proxy_hostname; | |
| 2198 port = proxy_port; | |
| 2199 } | |
| 2200 else | |
| 2201 { | |
| 2202 connect_host = request->hostname; | |
| 2203 port = request->port; | |
| 2204 } | |
| 1 | 2205 |
| 2206 if (request->hostp == NULL) | |
| 2207 { | |
| 2208 if (port == 0) | |
| 2209 strcpy (serv, service); | |
| 2210 else | |
| 2211 snprintf (serv, sizeof (serv), "%d", port); | |
| 2212 | |
| 186 | 2213 request->logging_function (gftp_logging_misc, request, |
| 168 | 2214 _("Looking up %s\n"), connect_host); |
| 1 | 2215 if ((errnum = getaddrinfo (connect_host, serv, &hints, |
| 2216 &request->hostp)) != 0) | |
| 168 | 2217 { |
| 186 | 2218 request->logging_function (gftp_logging_error, request, |
| 168 | 2219 _("Cannot look up hostname %s: %s\n"), |
| 2220 connect_host, gai_strerror (errnum)); | |
| 2221 return (GFTP_ERETRYABLE); | |
| 2222 } | |
| 1 | 2223 } |
| 2224 | |
| 2225 disphost = connect_host; | |
| 2226 for (res = request->hostp; res != NULL; res = res->ai_next) | |
| 2227 { | |
| 2228 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
| 2229 port = get_port (res); | |
| 56 | 2230 if (!request->use_proxy) |
| 2231 request->port = port; | |
| 2232 | |
| 1 | 2233 if ((sock = socket (res->ai_family, res->ai_socktype, |
| 2234 res->ai_protocol)) < 0) | |
| 2235 { | |
| 186 | 2236 request->logging_function (gftp_logging_error, request, |
| 1 | 2237 _("Failed to create a socket: %s\n"), |
| 2238 g_strerror (errno)); | |
| 2239 continue; | |
| 66 | 2240 } |
| 1 | 2241 |
| 186 | 2242 request->logging_function (gftp_logging_misc, request, |
| 168 | 2243 _("Trying %s:%d\n"), disphost, port); |
| 1 | 2244 |
| 2245 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
| 168 | 2246 { |
| 186 | 2247 request->logging_function (gftp_logging_error, request, |
| 168 | 2248 _("Cannot connect to %s: %s\n"), |
| 2249 disphost, g_strerror (errno)); | |
| 1 | 2250 close (sock); |
| 2251 continue; | |
| 168 | 2252 } |
| 547 | 2253 |
| 572 | 2254 request->current_hostp = res; |
| 547 | 2255 request->ai_family = res->ai_family; |
| 1 | 2256 break; |
| 2257 } | |
| 2258 | |
| 2259 if (res == NULL) | |
| 2260 { | |
| 2261 if (request->hostp != NULL) | |
| 2262 { | |
| 2263 freeaddrinfo (request->hostp); | |
| 2264 request->hostp = NULL; | |
| 2265 } | |
| 84 | 2266 return (GFTP_ERETRYABLE); |
| 1 | 2267 } |
| 2268 | |
| 2269 #else /* !HAVE_GETADDRINFO */ | |
| 2270 struct sockaddr_in remote_address; | |
| 2271 struct servent serv_struct; | |
| 2272 | |
| 122 | 2273 if ((request->use_proxy = gftp_need_proxy (request, service, |
| 2274 proxy_hostname, proxy_port)) < 0) | |
| 84 | 2275 return (request->use_proxy); |
| 1 | 2276 else if (request->use_proxy == 1) |
| 2277 request->hostp = NULL; | |
| 2278 | |
| 547 | 2279 request->ai_family = AF_INET; |
| 1 | 2280 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) |
| 2281 { | |
| 186 | 2282 request->logging_function (gftp_logging_error, request, |
| 313 | 2283 _("Failed to create a IPv4 socket: %s\n"), |
| 1 | 2284 g_strerror (errno)); |
| 84 | 2285 return (GFTP_ERETRYABLE); |
| 1 | 2286 } |
| 2287 | |
| 2288 memset (&remote_address, 0, sizeof (remote_address)); | |
| 2289 remote_address.sin_family = AF_INET; | |
| 2290 | |
| 122 | 2291 if (request->use_proxy) |
| 2292 { | |
| 2293 connect_host = proxy_hostname; | |
| 2294 port = proxy_port; | |
| 2295 } | |
| 2296 else | |
| 2297 { | |
| 2298 connect_host = request->hostname; | |
| 2299 port = request->port; | |
| 2300 } | |
| 1 | 2301 |
| 2302 if (port == 0) | |
| 2303 { | |
| 2304 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
| 2305 { | |
| 186 | 2306 request->logging_function (gftp_logging_error, request, |
| 122 | 2307 _("Cannot look up service name %s/tcp. Please check your services file\n"), |
| 2308 service); | |
| 2309 close (sock); | |
| 2310 return (GFTP_EFATAL); | |
| 1 | 2311 } |
| 451 | 2312 |
| 2313 port = ntohs (serv_struct.s_port); | |
| 56 | 2314 |
| 2315 if (!request->use_proxy) | |
| 451 | 2316 request->port = port; |
| 1 | 2317 } |
| 451 | 2318 |
| 2319 remote_address.sin_port = htons (port); | |
| 1 | 2320 |
| 2321 if (request->hostp == NULL) | |
| 2322 { | |
| 186 | 2323 request->logging_function (gftp_logging_misc, request, |
| 168 | 2324 _("Looking up %s\n"), connect_host); |
| 1 | 2325 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, |
| 2326 NULL))) | |
| 2327 { | |
| 186 | 2328 request->logging_function (gftp_logging_error, request, |
| 1 | 2329 _("Cannot look up hostname %s: %s\n"), |
| 2330 connect_host, g_strerror (errno)); | |
| 2331 close (sock); | |
| 84 | 2332 return (GFTP_ERETRYABLE); |
| 1 | 2333 } |
| 2334 } | |
| 2335 | |
| 2336 disphost = NULL; | |
| 641 | 2337 for (request->curhost = 0; |
| 2338 request->host.h_addr_list[request->curhost] != NULL; | |
| 2339 request->curhost++) | |
| 1 | 2340 { |
| 2341 disphost = request->host.h_name; | |
| 641 | 2342 memcpy (&remote_address.sin_addr, |
| 2343 request->host.h_addr_list[request->curhost], | |
| 1 | 2344 request->host.h_length); |
| 186 | 2345 request->logging_function (gftp_logging_misc, request, |
| 1 | 2346 _("Trying %s:%d\n"), |
| 451 | 2347 request->host.h_name, port); |
| 1 | 2348 |
| 2349 if (connect (sock, (struct sockaddr *) &remote_address, | |
| 2350 sizeof (remote_address)) == -1) | |
| 2351 { | |
| 186 | 2352 request->logging_function (gftp_logging_error, request, |
| 1 | 2353 _("Cannot connect to %s: %s\n"), |
| 2354 connect_host, g_strerror (errno)); | |
| 2355 } | |
| 2356 break; | |
| 2357 } | |
| 2358 | |
| 641 | 2359 if (request->host.h_addr_list[request->curhost] == NULL) |
| 1 | 2360 { |
| 2361 close (sock); | |
| 84 | 2362 return (GFTP_ERETRYABLE); |
| 1 | 2363 } |
| 2364 #endif /* HAVE_GETADDRINFO */ | |
| 2365 | |
| 182 | 2366 if (fcntl (sock, F_SETFD, 1) == -1) |
| 2367 { | |
| 186 | 2368 request->logging_function (gftp_logging_error, request, |
| 182 | 2369 _("Error: Cannot set close on exec flag: %s\n"), |
| 2370 g_strerror (errno)); | |
| 2371 | |
| 2372 return (GFTP_ERETRYABLE); | |
| 2373 } | |
| 2374 | |
| 186 | 2375 request->logging_function (gftp_logging_misc, request, |
| 168 | 2376 _("Connected to %s:%d\n"), connect_host, port); |
| 58 | 2377 |
| 168 | 2378 if (gftp_fd_set_sockblocking (request, sock, 1) < 0) |
| 58 | 2379 { |
| 2380 close (sock); | |
| 84 | 2381 return (GFTP_ERETRYABLE); |
| 58 | 2382 } |
| 2383 | |
| 169 | 2384 request->datafd = sock; |
| 168 | 2385 |
| 2386 if (request->post_connect != NULL) | |
| 2387 return (request->post_connect (request)); | |
| 2388 | |
| 2389 return (0); | |
| 1 | 2390 } |
| 2391 | |
| 2392 | |
| 177 | 2393 int |
| 1 | 2394 gftp_set_config_options (gftp_request * request) |
| 2395 { | |
| 58 | 2396 if (request->set_config_options != NULL) |
| 177 | 2397 return (request->set_config_options (request)); |
| 2398 else | |
| 2399 return (0); | |
| 1 | 2400 } |
| 2401 | |
| 2402 | |
| 2403 void | |
| 2404 print_file_list (GList * list) | |
| 2405 { | |
| 2406 gftp_file * tempfle; | |
| 2407 GList * templist; | |
| 499 | 2408 char *attribs; |
| 1 | 2409 |
| 2410 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
| 2411 for (templist = list; ; templist = templist->next) | |
| 2412 { | |
| 2413 tempfle = templist->data; | |
| 499 | 2414 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
| 2415 | |
| 532 | 2416 printf ("%s:%s:" GFTP_OFF_T_PRINTF_MOD ":" GFTP_OFF_T_PRINTF_MOD ":%s:%s:%s\n", |
| 372 | 2417 tempfle->file, tempfle->destfile, |
| 516 | 2418 tempfle->size, tempfle->startsize, |
| 499 | 2419 tempfle->user, tempfle->group, attribs); |
| 2420 | |
| 2421 g_free (attribs); | |
| 1 | 2422 if (templist->next == NULL) |
| 2423 break; | |
| 2424 } | |
| 2425 | |
| 2426 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
| 2427 for (; ; templist = templist->prev) | |
| 2428 { | |
| 2429 tempfle = templist->data; | |
| 499 | 2430 attribs = gftp_convert_attributes_from_mode_t (tempfle->st_mode); |
| 2431 | |
| 532 | 2432 printf ("%s:%s:" GFTP_OFF_T_PRINTF_MOD ":" GFTP_OFF_T_PRINTF_MOD ":%s:%s:%s\n", |
| 372 | 2433 tempfle->file, tempfle->destfile, |
| 516 | 2434 tempfle->size, tempfle->startsize, |
| 499 | 2435 tempfle->user, tempfle->group, attribs); |
| 2436 | |
| 2437 g_free (attribs); | |
| 1 | 2438 if (templist == list) |
| 2439 break; | |
| 2440 } | |
| 2441 printf ("--END OF FILE LISTING--\n"); | |
| 2442 } | |
| 2443 | |
| 41 | 2444 |
| 201 | 2445 void |
| 58 | 2446 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) |
| 41 | 2447 { |
| 58 | 2448 g_free ((*rbuf)->buffer); |
| 2449 g_free (*rbuf); | |
| 2450 *rbuf = NULL; | |
| 41 | 2451 } |
| 2452 | |
| 2453 | |
| 58 | 2454 ssize_t |
| 2455 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
| 2456 char * str, size_t len, int fd) | |
| 41 | 2457 { |
| 168 | 2458 ssize_t (*read_function) (gftp_request * request, void *ptr, size_t size, |
| 2459 int fd); | |
| 516 | 2460 char *pos, *nextpos; |
| 2461 size_t rlen, nslen; | |
| 179 | 2462 int end_of_buffer; |
| 516 | 2463 ssize_t ret; |
| 168 | 2464 |
| 2465 if (request == NULL || request->read_function == NULL) | |
| 2466 read_function = gftp_fd_read; | |
| 2467 else | |
| 2468 read_function = request->read_function; | |
| 58 | 2469 |
| 2470 if (*rbuf == NULL) | |
| 2471 { | |
| 2472 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
| 2473 (*rbuf)->max_bufsize = len; | |
| 249 | 2474 (*rbuf)->buffer = g_malloc0 ((*rbuf)->max_bufsize + 1); |
| 168 | 2475 |
| 2476 if ((ret = read_function (request, (*rbuf)->buffer, | |
| 2477 (*rbuf)->max_bufsize, fd)) <= 0) | |
| 58 | 2478 { |
| 2479 gftp_free_getline_buffer (rbuf); | |
| 2480 return (ret); | |
| 2481 } | |
| 60 | 2482 (*rbuf)->buffer[ret] = '\0'; |
| 58 | 2483 (*rbuf)->cur_bufsize = ret; |
| 2484 (*rbuf)->curpos = (*rbuf)->buffer; | |
| 2485 } | |
| 2486 | |
| 516 | 2487 ret = 0; |
| 2488 while (1) | |
| 58 | 2489 { |
| 179 | 2490 pos = strchr ((*rbuf)->curpos, '\n'); |
| 2491 end_of_buffer = (*rbuf)->curpos == (*rbuf)->buffer && | |
| 2492 ((*rbuf)->max_bufsize == (*rbuf)->cur_bufsize || (*rbuf)->eof); | |
| 2493 | |
| 2494 if ((*rbuf)->cur_bufsize > 0 && (pos != NULL || end_of_buffer)) | |
| 58 | 2495 { |
| 2496 if (pos != NULL) | |
| 2497 { | |
| 516 | 2498 nslen = pos - (*rbuf)->curpos + 1; |
| 58 | 2499 nextpos = pos + 1; |
| 2500 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
| 2501 pos--; | |
| 2502 *pos = '\0'; | |
| 2503 } | |
| 2504 else | |
| 249 | 2505 { |
| 516 | 2506 nslen = (*rbuf)->cur_bufsize; |
| 249 | 2507 nextpos = NULL; |
| 2508 | |
| 2509 /* This is not an overflow since we allocated one extra byte to | |
| 2510 buffer above */ | |
| 516 | 2511 ((*rbuf)->curpos)[nslen] = '\0'; |
| 249 | 2512 } |
| 58 | 2513 |
| 2514 strncpy (str, (*rbuf)->curpos, len); | |
| 249 | 2515 str[len - 1] = '\0'; |
| 516 | 2516 (*rbuf)->cur_bufsize -= nslen; |
| 58 | 2517 |
| 168 | 2518 if (nextpos != NULL) |
| 2519 (*rbuf)->curpos = nextpos; | |
| 2520 else | |
| 2521 (*rbuf)->cur_bufsize = 0; | |
| 516 | 2522 |
| 2523 ret = nslen; | |
| 58 | 2524 break; |
| 2525 } | |
| 2526 else | |
| 2527 { | |
| 2528 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
| 2529 { | |
| 2530 rlen = (*rbuf)->max_bufsize; | |
| 2531 pos = (*rbuf)->buffer; | |
| 2532 } | |
| 2533 else | |
| 2534 { | |
| 168 | 2535 memmove ((*rbuf)->buffer, (*rbuf)->curpos, (*rbuf)->cur_bufsize); |
| 2536 pos = (*rbuf)->buffer + (*rbuf)->cur_bufsize; | |
| 2537 rlen = (*rbuf)->max_bufsize - (*rbuf)->cur_bufsize; | |
| 58 | 2538 } |
| 168 | 2539 |
| 58 | 2540 (*rbuf)->curpos = (*rbuf)->buffer; |
| 2541 | |
| 209 | 2542 if ((*rbuf)->eof) |
| 2543 ret = 0; | |
| 2544 else | |
| 2545 ret = read_function (request, pos, rlen, fd); | |
| 2546 | |
| 2547 if (ret < 0) | |
| 58 | 2548 { |
| 2549 gftp_free_getline_buffer (rbuf); | |
| 2550 return (ret); | |
| 2551 } | |
| 179 | 2552 else if (ret == 0) |
| 168 | 2553 { |
| 179 | 2554 if ((*rbuf)->cur_bufsize == 0) |
| 2555 { | |
| 2556 gftp_free_getline_buffer (rbuf); | |
| 2557 return (ret); | |
| 2558 } | |
| 2559 | |
| 2560 (*rbuf)->eof = 1; | |
| 168 | 2561 } |
| 2562 | |
| 2563 (*rbuf)->cur_bufsize += ret; | |
| 215 | 2564 (*rbuf)->curpos[(*rbuf)->cur_bufsize] = '\0'; |
| 58 | 2565 } |
| 2566 } | |
| 516 | 2567 |
| 2568 return (ret); | |
| 58 | 2569 } |
| 2570 | |
| 2571 | |
| 2572 ssize_t | |
| 168 | 2573 gftp_fd_read (gftp_request * request, void *ptr, size_t size, int fd) |
| 58 | 2574 { |
| 325 | 2575 intptr_t network_timeout; |
| 58 | 2576 struct timeval tv; |
| 2577 fd_set fset; | |
| 2578 ssize_t ret; | |
| 41 | 2579 |
| 122 | 2580 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
| 2581 | |
| 41 | 2582 errno = 0; |
| 2583 ret = 0; | |
| 518 | 2584 |
| 546 | 2585 do |
| 41 | 2586 { |
| 58 | 2587 FD_ZERO (&fset); |
| 2588 FD_SET (fd, &fset); | |
| 122 | 2589 tv.tv_sec = network_timeout; |
| 58 | 2590 tv.tv_usec = 0; |
| 2591 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
| 546 | 2592 if (ret == -1 && (errno == EINTR || errno == EAGAIN)) |
| 41 | 2593 { |
| 546 | 2594 if (request != NULL && request->cancel) |
| 2595 { | |
| 2596 gftp_disconnect (request); | |
| 2597 return (GFTP_ERETRYABLE); | |
| 2598 } | |
| 2599 | |
| 2600 continue; | |
| 58 | 2601 } |
| 2602 else if (ret <= 0) | |
| 2603 { | |
| 2604 if (request != NULL) | |
| 41 | 2605 { |
| 186 | 2606 request->logging_function (gftp_logging_error, request, |
| 58 | 2607 _("Connection to %s timed out\n"), |
| 2608 request->hostname); | |
| 2609 gftp_disconnect (request); | |
| 2610 } | |
| 546 | 2611 |
| 84 | 2612 return (GFTP_ERETRYABLE); |
| 41 | 2613 } |
| 2614 | |
| 58 | 2615 if ((ret = read (fd, ptr, size)) < 0) |
| 41 | 2616 { |
| 546 | 2617 if (errno == EINTR || errno == EAGAIN) |
| 41 | 2618 { |
| 58 | 2619 if (request != NULL && request->cancel) |
| 546 | 2620 { |
| 2621 gftp_disconnect (request); | |
| 2622 return (GFTP_ERETRYABLE); | |
| 2623 } | |
| 2624 | |
| 2625 continue; | |
| 518 | 2626 } |
| 41 | 2627 |
| 58 | 2628 if (request != NULL) |
| 2629 { | |
| 186 | 2630 request->logging_function (gftp_logging_error, request, |
| 58 | 2631 _("Error: Could not read from socket: %s\n"), |
| 41 | 2632 g_strerror (errno)); |
| 58 | 2633 gftp_disconnect (request); |
| 2634 } | |
| 546 | 2635 |
| 84 | 2636 return (GFTP_ERETRYABLE); |
| 41 | 2637 } |
| 546 | 2638 |
| 518 | 2639 break; |
| 41 | 2640 } |
| 546 | 2641 while (1); |
| 41 | 2642 |
| 2643 return (ret); | |
| 2644 } | |
| 2645 | |
| 58 | 2646 |
| 2647 ssize_t | |
| 168 | 2648 gftp_fd_write (gftp_request * request, const char *ptr, size_t size, int fd) |
| 58 | 2649 { |
| 325 | 2650 intptr_t network_timeout; |
| 58 | 2651 struct timeval tv; |
| 248 | 2652 ssize_t w_ret; |
| 58 | 2653 fd_set fset; |
| 516 | 2654 int ret; |
| 58 | 2655 |
| 122 | 2656 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
| 2657 | |
| 58 | 2658 errno = 0; |
| 2659 ret = 0; | |
| 2660 do | |
| 2661 { | |
| 2662 FD_ZERO (&fset); | |
| 2663 FD_SET (fd, &fset); | |
| 122 | 2664 tv.tv_sec = network_timeout; |
| 58 | 2665 tv.tv_usec = 0; |
| 2666 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
| 546 | 2667 if (ret == -1 && (errno == EINTR || errno == EAGAIN)) |
| 58 | 2668 { |
| 2669 if (request != NULL && request->cancel) | |
| 546 | 2670 { |
| 2671 gftp_disconnect (request); | |
| 2672 return (GFTP_ERETRYABLE); | |
| 2673 } | |
| 2674 | |
| 2675 continue; | |
| 58 | 2676 } |
| 2677 else if (ret <= 0) | |
| 2678 { | |
| 2679 if (request != NULL) | |
| 2680 { | |
| 186 | 2681 request->logging_function (gftp_logging_error, request, |
| 58 | 2682 _("Connection to %s timed out\n"), |
| 2683 request->hostname); | |
| 2684 gftp_disconnect (request); | |
| 2685 } | |
| 546 | 2686 |
| 84 | 2687 return (GFTP_ERETRYABLE); |
| 58 | 2688 } |
| 2689 | |
| 248 | 2690 w_ret = write (fd, ptr, size); |
| 2691 if (w_ret < 0) | |
| 58 | 2692 { |
| 546 | 2693 if (errno == EINTR || errno == EAGAIN) |
| 58 | 2694 { |
| 2695 if (request != NULL && request->cancel) | |
| 546 | 2696 { |
| 2697 gftp_disconnect (request); | |
| 2698 return (GFTP_ERETRYABLE); | |
| 2699 } | |
| 2700 | |
| 2701 continue; | |
| 58 | 2702 } |
| 2703 | |
| 2704 if (request != NULL) | |
| 2705 { | |
| 186 | 2706 request->logging_function (gftp_logging_error, request, |
| 58 | 2707 _("Error: Could not write to socket: %s\n"), |
| 2708 g_strerror (errno)); | |
| 2709 gftp_disconnect (request); | |
| 2710 } | |
| 546 | 2711 |
| 84 | 2712 return (GFTP_ERETRYABLE); |
| 58 | 2713 } |
| 2714 | |
| 2715 ptr += w_ret; | |
| 2716 size -= w_ret; | |
| 2717 ret += w_ret; | |
| 2718 } | |
| 2719 while (size > 0); | |
| 2720 | |
| 2721 return (ret); | |
| 2722 } | |
| 2723 | |
| 2724 | |
| 2725 ssize_t | |
| 2726 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
| 2727 { | |
| 2728 char *tempstr; | |
| 2729 va_list argp; | |
| 2730 ssize_t ret; | |
| 2731 | |
| 2732 va_start (argp, fmt); | |
| 2733 tempstr = g_strdup_vprintf (fmt, argp); | |
| 2734 va_end (argp); | |
| 2735 | |
| 168 | 2736 ret = request->write_function (request, tempstr, strlen (tempstr), fd); |
| 58 | 2737 g_free (tempstr); |
| 2738 return (ret); | |
| 2739 } | |
| 2740 | |
| 2741 | |
| 2742 int | |
| 168 | 2743 gftp_fd_set_sockblocking (gftp_request * request, int fd, int non_blocking) |
| 58 | 2744 { |
| 2745 int flags; | |
| 2746 | |
| 84 | 2747 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) |
| 58 | 2748 { |
| 186 | 2749 request->logging_function (gftp_logging_error, request, |
| 58 | 2750 _("Cannot get socket flags: %s\n"), |
| 2751 g_strerror (errno)); | |
| 2752 gftp_disconnect (request); | |
| 84 | 2753 return (GFTP_ERETRYABLE); |
| 58 | 2754 } |
| 2755 | |
| 2756 if (non_blocking) | |
| 2757 flags |= O_NONBLOCK; | |
| 2758 else | |
| 2759 flags &= ~O_NONBLOCK; | |
| 2760 | |
| 84 | 2761 if (fcntl (fd, F_SETFL, flags) < 0) |
| 58 | 2762 { |
| 186 | 2763 request->logging_function (gftp_logging_error, request, |
| 58 | 2764 _("Cannot set socket to non-blocking: %s\n"), |
| 2765 g_strerror (errno)); | |
| 2766 gftp_disconnect (request); | |
| 84 | 2767 return (GFTP_ERETRYABLE); |
| 58 | 2768 } |
| 2769 | |
| 2770 return (0); | |
| 2771 } | |
| 2772 | |
| 2773 | |
| 63 | 2774 void |
| 2775 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
| 2776 { | |
| 2777 g_return_if_fail (dest != NULL); | |
| 2778 g_return_if_fail (source != NULL); | |
| 2779 g_return_if_fail (dest->protonum == source->protonum); | |
| 2780 | |
| 2781 dest->datafd = source->datafd; | |
| 2782 dest->cached = 0; | |
| 397 | 2783 #ifdef USE_SSL |
| 2784 dest->ssl = source->ssl; | |
| 2785 #endif | |
| 2786 | |
| 63 | 2787 if (!source->always_connected) |
| 2788 { | |
| 2789 source->datafd = -1; | |
| 2790 source->cached = 1; | |
| 397 | 2791 #ifdef USE_SSL |
| 2792 source->ssl = NULL; | |
| 2793 #endif | |
| 63 | 2794 } |
| 2795 | |
| 516 | 2796 if (dest->swap_socks != NULL) |
| 63 | 2797 dest->swap_socks (dest, source); |
| 2798 } | |
| 2799 | |
| 122 | 2800 |
| 2801 void | |
| 2802 gftp_calc_kbs (gftp_transfer * tdata, ssize_t num_read) | |
| 2803 { | |
| 469 | 2804 /* Needed for systems that size(float) < size(void *) */ |
| 2805 union { intptr_t i; float f; } maxkbs; | |
| 122 | 2806 unsigned long waitusecs; |
| 220 | 2807 double start_difftime; |
| 122 | 2808 struct timeval tv; |
| 222 | 2809 int waited; |
| 122 | 2810 |
| 469 | 2811 gftp_lookup_request_option (tdata->fromreq, "maxkbs", &maxkbs.f); |
| 122 | 2812 |
| 2813 if (g_thread_supported ()) | |
| 2814 g_static_mutex_lock (&tdata->statmutex); | |
| 2815 | |
| 220 | 2816 gettimeofday (&tv, NULL); |
| 2817 | |
| 122 | 2818 tdata->trans_bytes += num_read; |
| 2819 tdata->curtrans += num_read; | |
| 2820 tdata->stalled = 0; | |
| 2821 | |
| 220 | 2822 start_difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0); |
| 2823 | |
| 2824 if (start_difftime <= 0) | |
| 2825 tdata->kbs = tdata->trans_bytes / 1024.0; | |
| 122 | 2826 else |
| 220 | 2827 tdata->kbs = tdata->trans_bytes / 1024.0 / start_difftime; |
| 2828 | |
| 222 | 2829 waited = 0; |
| 469 | 2830 if (maxkbs.f > 0 && tdata->kbs > maxkbs.f) |
| 122 | 2831 { |
| 469 | 2832 waitusecs = num_read / 1024.0 / maxkbs.f * 1000000.0 - start_difftime; |
| 122 | 2833 |
| 2834 if (waitusecs > 0) | |
| 2835 { | |
| 2836 if (g_thread_supported ()) | |
| 2837 g_static_mutex_unlock (&tdata->statmutex); | |
| 2838 | |
| 222 | 2839 waited = 1; |
| 122 | 2840 usleep (waitusecs); |
| 2841 | |
| 2842 if (g_thread_supported ()) | |
| 2843 g_static_mutex_lock (&tdata->statmutex); | |
| 2844 } | |
| 222 | 2845 |
| 122 | 2846 } |
| 2847 | |
| 222 | 2848 if (waited) |
| 2849 gettimeofday (&tdata->lasttime, NULL); | |
| 2850 else | |
| 2851 memcpy (&tdata->lasttime, &tv, sizeof (tdata->lasttime)); | |
| 122 | 2852 |
| 2853 if (g_thread_supported ()) | |
| 2854 g_static_mutex_unlock (&tdata->statmutex); | |
| 2855 } | |
| 2856 | |
| 125 | 2857 |
| 764 | 2858 static int |
| 2859 _do_sleep (int sleep_time) | |
| 2860 { | |
| 2861 struct timeval tv; | |
| 2862 int ret; | |
| 2863 | |
| 2864 tv.tv_sec = sleep_time; | |
| 2865 tv.tv_usec = 0; | |
| 2866 | |
| 2867 /* FIXME - check for user aborted connection */ | |
| 2868 do | |
| 2869 { | |
| 2870 ret = select (0, NULL, NULL, NULL, &tv); | |
| 2871 } | |
| 2872 while (ret == -1 && (errno == EINTR || errno == EAGAIN)); | |
| 2873 | |
| 2874 return (ret); | |
| 2875 } | |
| 2876 | |
| 2877 | |
| 125 | 2878 int |
| 2879 gftp_get_transfer_status (gftp_transfer * tdata, ssize_t num_read) | |
| 2880 { | |
| 325 | 2881 intptr_t retries, sleep_time; |
| 125 | 2882 gftp_file * tempfle; |
| 498 | 2883 int ret1, ret2; |
| 125 | 2884 |
| 2885 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
| 2886 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
| 2887 | |
| 2888 if (g_thread_supported ()) | |
| 2889 g_static_mutex_lock (&tdata->structmutex); | |
| 2890 | |
| 2891 if (tdata->curfle == NULL) | |
| 2892 { | |
| 2893 if (g_thread_supported ()) | |
| 2894 g_static_mutex_unlock (&tdata->structmutex); | |
| 2895 | |
| 2896 return (GFTP_EFATAL); | |
| 2897 } | |
| 2898 | |
| 2899 tempfle = tdata->curfle->data; | |
| 2900 | |
| 2901 if (g_thread_supported ()) | |
| 2902 g_static_mutex_unlock (&tdata->structmutex); | |
| 2903 | |
| 2904 gftp_disconnect (tdata->fromreq); | |
| 2905 gftp_disconnect (tdata->toreq); | |
| 2906 | |
| 764 | 2907 if (tdata->cancel || num_read == GFTP_EFATAL) |
| 2908 return (GFTP_EFATAL); | |
| 2909 else if (num_read >= 0 && !tdata->skip_file) | |
| 2910 return (0); | |
| 2911 | |
| 2912 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout) | |
| 125 | 2913 { |
| 764 | 2914 if (retries != 0 && |
| 2915 tdata->current_file_retries >= retries) | |
| 2916 { | |
| 2917 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, | |
| 2918 _("Error: Remote site %s disconnected. Max retries reached...giving up\n"), | |
| 2919 tdata->fromreq->hostname != NULL ? | |
| 2920 tdata->fromreq->hostname : tdata->toreq->hostname); | |
| 2921 return (GFTP_EFATAL); | |
| 2922 } | |
| 2923 else | |
| 125 | 2924 { |
| 764 | 2925 tdata->fromreq->logging_function (gftp_logging_error, tdata->fromreq, |
| 2926 _("Error: Remote site %s disconnected. Will reconnect in %d seconds\n"), | |
| 2927 tdata->fromreq->hostname != NULL ? | |
| 2928 tdata->fromreq->hostname : tdata->toreq->hostname, | |
| 2929 sleep_time); | |
| 2930 } | |
| 2931 } | |
| 2932 | |
| 2933 while (retries == 0 || | |
| 2934 tdata->current_file_retries <= retries) | |
| 2935 { | |
| 2936 /* Look up the options in case the user changes them... */ | |
| 2937 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
| 2938 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
| 2939 | |
| 2940 if (num_read != GFTP_ETIMEDOUT && !tdata->conn_error_no_timeout && | |
| 2941 !tdata->skip_file) | |
| 2942 _do_sleep (sleep_time); | |
| 2943 | |
| 2944 tdata->current_file_retries++; | |
| 2945 | |
| 2946 ret1 = ret2 = 0; | |
| 2947 if ((ret1 = gftp_connect (tdata->fromreq)) == 0 && | |
| 2948 (ret2 = gftp_connect (tdata->toreq)) == 0) | |
| 2949 { | |
| 2950 if (g_thread_supported ()) | |
| 2951 g_static_mutex_lock (&tdata->structmutex); | |
| 2952 | |
| 2953 tdata->resumed_bytes = tdata->resumed_bytes + tdata->trans_bytes - tdata->curresumed - tdata->curtrans; | |
| 2954 tdata->trans_bytes = 0; | |
| 2955 if (tdata->skip_file) | |
| 303 | 2956 { |
| 764 | 2957 tdata->total_bytes -= tempfle->size; |
| 2958 tdata->curtrans = 0; | |
| 2959 | |
| 2960 tdata->curfle = tdata->curfle->next; | |
| 2961 tdata->next_file = 1; | |
| 2962 tdata->skip_file = 0; | |
| 2963 tdata->cancel = 0; | |
| 2964 tdata->fromreq->cancel = 0; | |
| 2965 tdata->toreq->cancel = 0; | |
| 303 | 2966 } |
| 2967 else | |
| 2968 { | |
| 764 | 2969 tempfle->transfer_action = GFTP_TRANS_ACTION_RESUME; |
| 2970 tempfle->startsize = tdata->curtrans + tdata->curresumed; | |
| 2971 /* We decrement this here because it will be incremented in | |
| 2972 the loop again */ | |
| 2973 tdata->curresumed = 0; | |
| 2974 tdata->current_file_number--; /* Decrement this because it | |
| 2975 will be incremented when we | |
| 2976 continue in the loop */ | |
| 125 | 2977 } |
| 2978 | |
| 764 | 2979 gettimeofday (&tdata->starttime, NULL); |
| 2980 | |
| 2981 if (g_thread_supported ()) | |
| 2982 g_static_mutex_unlock (&tdata->structmutex); | |
| 2983 | |
| 2984 return (GFTP_ERETRYABLE); | |
| 2985 } | |
| 2986 else if (ret1 == GFTP_EFATAL || ret2 == GFTP_EFATAL) | |
| 2987 { | |
| 2988 gftp_disconnect (tdata->fromreq); | |
| 2989 gftp_disconnect (tdata->toreq); | |
| 2990 return (GFTP_EFATAL); | |
| 125 | 2991 } |
| 2992 } | |
| 2993 | |
| 2994 return (0); | |
| 2995 } | |
| 2996 | |
| 182 | 2997 |
| 2998 int | |
| 2999 gftp_fd_open (gftp_request * request, const char *pathname, int flags, mode_t mode) | |
| 3000 { | |
| 3001 int fd; | |
| 3002 | |
| 227 | 3003 if (mode == 0) |
| 3004 fd = open (pathname, flags); | |
| 3005 else | |
| 3006 fd = open (pathname, flags, mode); | |
| 3007 | |
| 3008 if (fd < 0) | |
| 182 | 3009 { |
| 3010 if (request != NULL) | |
| 186 | 3011 request->logging_function (gftp_logging_error, request, |
| 182 | 3012 _("Error: Cannot open local file %s: %s\n"), |
| 3013 pathname, g_strerror (errno)); | |
| 3014 return (GFTP_ERETRYABLE); | |
| 3015 } | |
| 3016 | |
| 3017 if (fcntl (fd, F_SETFD, 1) == -1) | |
| 3018 { | |
| 3019 if (request != NULL) | |
| 186 | 3020 request->logging_function (gftp_logging_error, request, |
| 182 | 3021 _("Error: Cannot set close on exec flag: %s\n"), |
| 3022 g_strerror (errno)); | |
| 3023 | |
| 3024 return (-1); | |
| 3025 } | |
| 3026 | |
| 3027 return (fd); | |
| 3028 } | |
| 422 | 3029 |
| 3030 | |
| 3031 void | |
| 3032 gftp_setup_startup_directory (gftp_request * request) | |
| 3033 { | |
| 3034 char *startup_directory, *tempstr; | |
| 3035 | |
| 3036 gftp_lookup_request_option (request, "startup_directory", &startup_directory); | |
| 3037 | |
| 3038 if (*startup_directory != '\0' && | |
| 555 | 3039 (tempstr = gftp_expand_path (request, startup_directory)) != NULL) |
| 422 | 3040 { |
| 3041 gftp_set_directory (request, tempstr); | |
| 3042 g_free (tempstr); | |
| 3043 } | |
| 3044 } | |
| 3045 | |
| 499 | 3046 |
| 3047 char * | |
| 3048 gftp_convert_attributes_from_mode_t (mode_t mode) | |
| 3049 { | |
| 3050 char *str; | |
| 3051 | |
| 3052 str = g_malloc0 (11); | |
| 3053 | |
| 3054 str[0] = '?'; | |
| 3055 if (S_ISREG (mode)) | |
| 3056 str[0] = '-'; | |
| 3057 | |
| 3058 if (S_ISLNK (mode)) | |
| 3059 str[0] = 'l'; | |
| 3060 | |
| 3061 if (S_ISBLK (mode)) | |
| 3062 str[0] = 'b'; | |
| 3063 | |
| 3064 if (S_ISCHR (mode)) | |
| 3065 str[0] = 'c'; | |
| 3066 | |
| 3067 if (S_ISFIFO (mode)) | |
| 3068 str[0] = 'p'; | |
| 3069 | |
| 3070 if (S_ISSOCK (mode)) | |
| 3071 str[0] = 's'; | |
| 3072 | |
| 3073 if (S_ISDIR (mode)) | |
| 3074 str[0] = 'd'; | |
| 3075 | |
| 3076 str[1] = mode & S_IRUSR ? 'r' : '-'; | |
| 3077 str[2] = mode & S_IWUSR ? 'w' : '-'; | |
| 3078 | |
| 3079 if ((mode & S_ISUID) && (mode & S_IXUSR)) | |
| 3080 str[3] = 's'; | |
| 3081 else if (mode & S_ISUID) | |
| 3082 str[3] = 'S'; | |
| 3083 else if (mode & S_IXUSR) | |
| 3084 str[3] = 'x'; | |
| 3085 else | |
| 3086 str[3] = '-'; | |
| 3087 | |
| 3088 str[4] = mode & S_IRGRP ? 'r' : '-'; | |
| 3089 str[5] = mode & S_IWGRP ? 'w' : '-'; | |
| 3090 | |
| 3091 if ((mode & S_ISGID) && (mode & S_IXGRP)) | |
| 3092 str[6] = 's'; | |
| 3093 else if (mode & S_ISGID) | |
| 3094 str[6] = 'S'; | |
| 3095 else if (mode & S_IXGRP) | |
| 3096 str[6] = 'x'; | |
| 3097 else | |
| 3098 str[6] = '-'; | |
| 3099 | |
| 3100 str[7] = mode & S_IROTH ? 'r' : '-'; | |
| 3101 str[8] = mode & S_IWOTH ? 'w' : '-'; | |
| 3102 | |
| 3103 if ((mode & S_ISVTX) && (mode & S_IXOTH)) | |
| 3104 str[9] = 't'; | |
| 3105 else if (mode & S_ISVTX) | |
| 3106 str[9] = 'T'; | |
| 3107 else if (mode & S_IXOTH) | |
| 3108 str[9] = 'x'; | |
| 3109 else | |
| 3110 str[9] = '-'; | |
| 3111 | |
| 3112 return (str); | |
| 3113 } | |
| 3114 | |
| 3115 | |
| 3116 mode_t | |
| 3117 gftp_convert_attributes_to_mode_t (char *attribs) | |
| 3118 { | |
| 3119 mode_t mode; | |
| 3120 | |
| 3121 if (attribs[0] == 'd') | |
| 3122 mode = S_IFDIR; | |
| 3123 else if (attribs[0] == 'l') | |
| 3124 mode = S_IFLNK; | |
| 3125 else if (attribs[0] == 's') | |
| 3126 mode = S_IFSOCK; | |
| 3127 else if (attribs[0] == 'b') | |
| 3128 mode = S_IFBLK; | |
| 3129 else if (attribs[0] == 'c') | |
| 3130 mode = S_IFCHR; | |
| 3131 else | |
| 3132 mode = S_IFREG; | |
| 3133 | |
| 3134 if (attribs[1] == 'r') | |
| 3135 mode |= S_IRUSR; | |
| 3136 if (attribs[2] == 'w') | |
| 3137 mode |= S_IWUSR; | |
| 3138 if (attribs[3] == 'x' || attribs[3] == 's') | |
| 3139 mode |= S_IXUSR; | |
| 3140 if (attribs[3] == 's' || attribs[3] == 'S') | |
| 3141 mode |= S_ISUID; | |
| 3142 | |
| 3143 if (attribs[4] == 'r') | |
| 3144 mode |= S_IRGRP; | |
| 3145 if (attribs[5] == 'w') | |
| 3146 mode |= S_IWGRP; | |
| 3147 if (attribs[6] == 'x' || | |
| 3148 attribs[6] == 's') | |
| 3149 mode |= S_IXGRP; | |
| 3150 if (attribs[6] == 's' || attribs[6] == 'S') | |
| 3151 mode |= S_ISGID; | |
| 3152 | |
| 3153 if (attribs[7] == 'r') | |
| 3154 mode |= S_IROTH; | |
| 3155 if (attribs[8] == 'w') | |
| 3156 mode |= S_IWOTH; | |
| 3157 if (attribs[9] == 'x' || | |
| 3158 attribs[9] == 's') | |
| 3159 mode |= S_IXOTH; | |
| 3160 if (attribs[9] == 't' || attribs[9] == 'T') | |
| 3161 mode |= S_ISVTX; | |
| 3162 | |
| 3163 return (mode); | |
| 3164 } | |
| 3165 | |
| 542 | 3166 |
| 3167 unsigned int | |
| 3168 gftp_protocol_default_port (gftp_request * request) | |
| 3169 { | |
| 3170 struct servent serv_struct; | |
| 3171 | |
| 3172 if (r_getservbyname (gftp_protocols[request->protonum].url_prefix, "tcp", | |
| 3173 &serv_struct, NULL) == NULL) | |
| 3174 return (gftp_protocols[request->protonum].default_port); | |
| 3175 else | |
| 3176 return (ntohs (serv_struct.s_port)); | |
| 3177 } | |
| 3178 |
