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