File: put.c

package info (click to toggle)
xdir 2.1-3
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 2,376 kB
  • ctags: 2,449
  • sloc: ansic: 30,140; makefile: 481; sh: 64
file content (511 lines) | stat: -rw-r--r-- 14,532 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/***************************************************************************/
/***************************************************************************/
/*                                                                         */
/*   (c) 1995-1998.  The Regents of the University of California.  All     */
/*   rights reserved.                                                      */
/*                                                                         */
/*   This work was produced at the University of California, Lawrence      */
/*   Livermore National Laboratory (UC LLNL) under contract no.            */
/*   W-7405-ENG-48 (Contract 48) between the U.S. Department of Energy     */
/*   (DOE) and The Regents of the University of California (University)    */
/*   for the operation of UC LLNL.  Copyright is reserved to the           */
/*   University for purposes of controlled dissemination,                  */
/*   commercialization through formal licensing, or other disposition      */
/*   under terms of Contract 48; DOE policies, regulations and orders;     */
/*   and U.S. statutes.  The rights of the Federal Government are          */
/*   reserved under Contract 48 subject to the restrictions agreed upon    */
/*   by the DOE and University.                                            */
/*                                                                         */
/*                                                                         */
/*                              DISCLAIMER                                 */
/*                                                                         */
/*   This software was prepared as an account of work sponsored by an      */
/*   agency of the United States Government.  Neither the United States    */
/*   Government nor the University of California nor any of their          */
/*   employees, makes any warranty, express or implied, or assumes any     */
/*   liability or responsibility for the accuracy, completeness, or        */
/*   usefulness of any information, apparatus, product, or process         */
/*   disclosed, or represents that its specific commercial products,       */
/*   process, or service by trade name, trademark, manufacturer, or        */
/*   otherwise, does not necessarily constitute or imply its               */
/*   endorsement, recommendation, or favoring by the United States         */
/*   Government or the University of California. The views and opinions    */
/*   of the authors expressed herein do not necessarily state or reflect   */
/*   those of the United States Government or the University of            */
/*   California, and shall not be used for advertising or product          */
/*   endorsement purposes.                                                 */
/*                                                                         */
/*   Permission to use, copy, modify and distribute this software and its  */
/*   documentation for any non-commercial purpose, without fee, is         */
/*   hereby granted, provided that the above copyright notice and this     */
/*   permission notice appear in all copies of the software and            */
/*   supporting documentation, and that all UC LLNL identification in      */
/*   the user interface remain unchanged.  The title to copyright LLNL     */
/*   XDIR shall at all times remain with The Regents of the University     */
/*   of California and users agree to preserve same. Users seeking the     */
/*   right to make derivative works with LLNL XDIR for commercial          */
/*   purposes may obtain a license from the Lawrence Livermore National    */
/*   Laboratory's Technology Transfer Office, P.O. Box 808, L-795,         */
/*   Livermore, CA 94550.                                                  */
/*                                                                         */
/***************************************************************************/
/***************************************************************************/

#include <sys/types.h>
#include <sys/socket.h>
#ifdef HIPPI
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <Xm/Xm.h>
#include "xdir.h"
#include "xfer.h"

static char *tcpbuf = NULL;
static char *filebuf = NULL;

extern struct xfer_ctrl_block xc;
extern int max_ftp_retries;
extern int diagnostics;
extern int store_unique;
extern int maxt;
extern int passive_mode;
extern struct st_host_info hinfo[];


/*
 * init_put - Initialize transfer of file from local host to remote host.
 *            "xc" is used for input and output.  Returns 0 if successful,
 *            -4 for lost sink connection, -6 for stop button pushed, and
 *            -1 for other errors.
 */
init_put()
{
	char *src_path;
	char *snk_dir;
	char *snk_file;
	int file_fd;
	int data_fd;
	long file_len;
	struct stat status;
	int code;
	char reply[MAXFTPREPLY];
	int sock;
	char cmd[MAXPATHLEN+5];
	int retval;
	char msg[MAXPATHLEN+30];
	int retries = 0;
	int scode;
	int errval;
    int sys_tcpbuflen = XFER_SYS_TCPBUFLEN;
	char *snk_path;
#ifdef HIPPI
    int winshift = XFER_TCPWINSHIFT;
#endif

retry:

	/* Open source file and verify that it is a "real" file */
	src_paths_of_xfer_node(xc.current_node, NULL, &src_path);
	if ((file_fd = iopen2(src_path, O_RDONLY)) < 0) {
		report_perror(src_path);
		XtFree(src_path);
		return -1;
	}
	if (fstat(file_fd, &status) < 0 || !S_ISREG(status.st_mode)) {
		sprintf(msg, "%s is not a \"real\" file", src_path);
		XtFree(src_path);
		record_warning(msg);
		close(file_fd);
		return -1;
	}
	XtFree(src_path);

	/* Get length of file */
	file_len = status.st_size;

	/* "cd" into sink directory */
	snk_paths_of_xfer_node(xc.current_node, NULL, &snk_path);
	parse_path(xc.snk_host_system, snk_path, &snk_dir,
		&snk_file);
	XtFree(snk_path);
	retval = remote_cd(xc.snk_dirwin->host, snk_dir, True);
	XtFree(snk_dir);
	switch (retval) {
	case 0:
		break;
	case -3:
		XtFree(snk_file);
		close(file_fd);
		return -4;
	case -6:
		XtFree(snk_file);
		close(file_fd);
		return -6;
	default:
		XtFree(snk_file);
		close(file_fd);
		return -1;
	}

	/* Initialize data connection */
	if (passive_mode) {
		if ((data_fd = passive_data_conn(xc.snk_dirwin->host)) < 0) {
			close(file_fd);
			return data_fd;
		}
	} else if ((sock = init_data_conn(xc.snk_dirwin->host)) < 0) {
		XtFree(snk_file);
		close(file_fd);
		if (sock == -3)
			return -4;
		else
			return sock;
	}

	/* Send ALLO command to UniTree FTP server */
	if (((hinfo[xc.snk_dirwin->host].server == SERVER_UNIX_UNITREE) ||
			(hinfo[xc.snk_dirwin->host].server == SERVER_UNIX_NSL_UNITREE)) &&
			(xc.mode == BINARY)) {
		if ((retval = ftp_allocate(xc.snk_dirwin->host, file_len)) < 0) {
			if (retval == -3)
				errval = -4;
			else
				errval = retval;
			goto error;
		}
	}

	/* Send STOR command to FTP server */
	if (store_unique)
		sprintf(cmd, "STOU %s", snk_file);
	else
		sprintf(cmd, "STOR %s", snk_file);
	XtFree(snk_file);
	if ((retval = send_ftp_cmd(xc.snk_dirwin->host, cmd)) < 0) {
		switch (retval) {
		case -6:
			errval = -6;
			goto error;
		case -3:
			errval = -4;
			goto error;
		case -2:
			report_client_timeout(xc.snk_dirwin->host);
		default:
			errval = -1;
			goto error;
		}
	}

	/* Get initial response from FTP server */
	scode = get_ftp_reply(xc.snk_dirwin->host, reply, MAXFTPREPLY, &code, maxt);
	switch (scode) {
	case 1:
		break;
    case 4:
		close(file_fd);
		if (passive_mode)
			close(data_fd);
		else
			close(sock);
        if (code >= 450 && code < 460 && retries++ < max_ftp_retries) {
            if (diagnostics >= VERBOSE)
                write_log("*** Unable to initialize put.  Will retry.\n");
            goto retry;
        }
		if (diagnostics < VERBOSE)
			report_ftp_reply(xc.snk_dirwin->host, reply);
        return -1;
	case -3:
		errval = -4;
		goto error;
	case -6:
		abort_ftp_cmd(xc.snk_dirwin->host);
		errval = -6;
		goto error;
	case -2:
		report_client_timeout(xc.snk_dirwin->host);
	default:
		if (scode > 0 && diagnostics < VERBOSE)
			report_ftp_reply(xc.snk_dirwin->host, reply);
		errval = -1;
		goto error;
	}

	/* Establish data connection */
	if (!passive_mode) {
		data_fd = accept_data_conn(sock);
		close(sock);
		if (data_fd < 0) {
			close(file_fd);
			return -1;
		}
	}

	/* Attempt to increase TCP send buffer size to improve performance */
#if defined(SOL_SOCKET) && defined(SO_SNDBUF)
    setsockopt(data_fd, SOL_SOCKET, SO_SNDBUF, (char *)&sys_tcpbuflen,
        sizeof(sys_tcpbuflen));
#endif

    /* Optimize tranfer rate for HIPPI channel on UNICOS Cray */
#ifdef HIPPI
    setsockopt(data_fd, IPPROTO_TCP, TCP_WINSHIFT, (char *)&winshift,
        sizeof(winshift));
#endif

	/* Allocate user buffers (but first perform sanity check) */
	if (tcpbuf)
		fatal_error("Programming bug in init_put() involving tcpbuf");
	tcpbuf = XtMalloc(XFER_USER_TCPBUFLEN);
	if (filebuf)
		fatal_error("Programming bug in init_put() involving filebuf");
	filebuf = XtMalloc(XFER_FILEBUFLEN);

	xc.data_fd = data_fd;
	xc.src_file_fd = file_fd;
	xc.file_len = file_len;
	xc.file_index = 0;
	return 0;

error:

	close(file_fd);
	if (passive_mode)
		close(data_fd);
	else
		close(sock);
	return errval;
}


/*
 * do_put - Transfer next part of file from local host to remote host.
 *          "xc" is used for input and output.  Returns 0 if file
 *          transfer complete, 1 if file transfer not complete,
 *          -4 for lost sink connection, -7 for a "552" reply (for
 *          UniTree retry mechanism), -6 for stop button pressed,
 *          and -1 for other errors.
 */
do_put()
{
	int tcp_index = 0;
	int nbytes;
	int code;
	char reply[MAXFTPREPLY];
	int i;
	int retval;
	int scode;
	char *src_path;

	if (xc.mode == ASCII) {
		if (xc.file_index < xc.file_len) {
			nbytes = MIN(xc.file_len-xc.file_index, XFER_FILEBUFLEN);
			if (iread(xc.src_file_fd, filebuf, nbytes) != nbytes) {
				src_paths_of_xfer_node(xc.current_node, NULL, &src_path);
				report_perror(src_path);
				XtFree(src_path);
				return abort_put();
			}
			for (i=0; i<nbytes; i++)
				if (filebuf[i] == '\n') {
					if (tcp_index+2 >= XFER_USER_TCPBUFLEN) {
						retval = write_tcp(xc.snk_dirwin->host, xc.data_fd,
							tcpbuf, tcp_index, maxt);
						if (retval != tcp_index) {
							switch (retval) {
							case -3:
								close(xc.src_file_fd);
								close(xc.data_fd);
								XtFree(tcpbuf);
								tcpbuf = NULL;
								XtFree(filebuf);
								filebuf = NULL;
								return -4;
							case -6:
								abort_put();
								return -6;
							case -2:
								report_client_timeout(xc.snk_dirwin->host);
							default:
								return abort_put();
							}
						}
						tcp_index = 0;
					}
					tcpbuf[tcp_index++] = '\r';
					tcpbuf[tcp_index++] = '\n';
				} else {
					if (tcp_index+1 >= XFER_USER_TCPBUFLEN) {
						retval = write_tcp(xc.snk_dirwin->host, xc.data_fd,
							tcpbuf, tcp_index, maxt);
						if (retval != tcp_index) {
							switch (retval) {
							case -3:
								close(xc.src_file_fd);
								close(xc.data_fd);
								XtFree(tcpbuf);
								tcpbuf = NULL;
								XtFree(filebuf);
								filebuf = NULL;
								return -4;
							case -6:
								abort_put();
								return -6;
							case -2:
								report_client_timeout(xc.snk_dirwin->host);
							default:
								return abort_put();
							}
						}
						tcp_index = 0;
					}
					tcpbuf[tcp_index++] = filebuf[i];
				}
			xc.file_index += nbytes;
		}
		if (tcp_index) {
			retval = write_tcp(xc.snk_dirwin->host, xc.data_fd, tcpbuf,
				tcp_index, maxt);
			if (retval != tcp_index) {
				switch (retval) {
				case -3:
					close(xc.src_file_fd);
					close(xc.data_fd);
					XtFree(tcpbuf);
					tcpbuf = NULL;
					XtFree(filebuf);
					filebuf = NULL;
					return -4;
				case -6:
					abort_put();
					return -6;
				case -2:
					report_client_timeout(xc.snk_dirwin->host);
				default:
					return abort_put();
				}
			}
		}
	} else      /* Send binary file to FTP server */
		if (xc.file_index < xc.file_len) {
			nbytes = MIN(xc.file_len-xc.file_index, XFER_FILEBUFLEN);
			if (iread(xc.src_file_fd, filebuf, nbytes) != nbytes) {
				src_paths_of_xfer_node(xc.current_node, NULL, &src_path);
				report_perror(src_path);
				XtFree(src_path);
				return abort_put();
			}
			retval = write_tcp(xc.snk_dirwin->host, xc.data_fd, filebuf,
				nbytes, maxt);
			if (retval != nbytes) {
				switch (retval) {
				case -3:
					close(xc.src_file_fd);
					close(xc.data_fd);
					XtFree(tcpbuf);
					tcpbuf = NULL;
					XtFree(filebuf);
					filebuf = NULL;
					return -4;
				case -6:
					abort_put();
					return -6;
				case -2:
					report_client_timeout(xc.snk_dirwin->host);
				default:
					return abort_put();
				}
			}
			xc.file_index += nbytes;
		}

	/* Have all the bytes been transferred? */
	if (xc.file_index < xc.file_len)
		return 1;

	/* Close source file */
	close(xc.src_file_fd);

	/* Close data connection */
	close(xc.data_fd);

	/* Free up user buffers */
	XtFree(tcpbuf);
	tcpbuf = NULL;
	XtFree(filebuf);
	filebuf = NULL;

	/* Get final response from FTP server */
	scode = get_ftp_reply(xc.snk_dirwin->host, reply, MAXFTPREPLY, &code, maxt);
	switch (scode) {
	case 2:
		return 0;
	case -3:
		return -4;
	case -6:
		abort_ftp_cmd(xc.snk_dirwin->host);
		return -6;
	case -2:
		report_client_timeout(xc.snk_dirwin->host);
	default:
		if (scode > 0 && diagnostics < VERBOSE)
			report_ftp_reply(xc.snk_dirwin->host, reply);
		if (code == 552)        /* UniTree retry logic */
			return -7;
		else
			return -1;
	}
}


/*
 * abort_put - Abort transfer of file from local host to remote host.
 *             "xc" is used for input and output.  Returns -4 for lost
 *             sink connection, -6 for stop button pushed, else -1.
 */
abort_put()
{
	int code;
	char reply[MAXFTPREPLY];
	int retval;

	/* Pop up abort dialog */
	show_abort_dialog();

	/* Close source file */
	close(xc.src_file_fd);

	/* Close data connection */
	close(xc.data_fd);

	/* Make sure that user buffers have been freed */
	XtFree(tcpbuf);
	tcpbuf = NULL;
	XtFree(filebuf);
	filebuf = NULL;

	/* Get final response from FTP server */
	retval = get_ftp_reply(xc.snk_dirwin->host, reply, MAXFTPREPLY, &code,
		maxt);

	/* Hide abort dialog */
	hide_abort_dialog();

	/* Finish up */
	switch (retval) {
	case -3:
		return -4;
	case -6:
		abort_ftp_cmd(xc.snk_dirwin->host);
		return -6;
	case -2:
		report_client_timeout(xc.snk_dirwin->host);
	default:
		return -1;
	}
}