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