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