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