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