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