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