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