summaryrefslogtreecommitdiff
path: root/src/tet3/tcc/copysave.c
blob: 727bf9e6024bb6af123d1921f2399af2b25d0be2 (plain)
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
 *	SCCS: @(#)copysave.c	1.8 (98/09/01)
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1996 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 * A copy of the end-user licence agreement is contained in the file
 * Licence which accompanies this distribution.
 * 
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifndef lint
static char sccsid[] = "@(#)copysave.c	1.8 (98/09/01) TET3 release 3.3";
#endif

/************************************************************************

SCCS:   	@(#)copysave.c	1.8 98/09/01 TETware release 3.3
NAME:		copysave.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	August 1996

DESCRIPTION:
	functions to deal with copying and saving files

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., June 1997
	don't update TET_TSROOT in the distributed configuration
	after processing a remote TET_RUN (rtrcopy());
	instead always use get_runtime_tsroot() to determine the location
	of the runtime test suite root directory on a particular system

	Andrew Dingwall, UniSoft Ltd., October 1997
	in tcc_rmtmpdir(), don't check for being in the tmpdir subtree
	if we haven't changed directory yet

************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <errno.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "error.h"
#include "ltoa.h"
#include "tet3_config.h"
#include "servlib.h"
#include "dtetlib.h"
#include "scentab.h"
#include "proctab.h"
#include "systab.h"
#include "tcc.h"
#include "tcclib.h"


#ifdef NEEDsrcFile
static char srcFile[] = __FILE__;	/* file name for error reporting */
#endif

/* static function declarations */
static void inittd2 PROTOLIST((struct systab *));
static void inittd3 PROTOLIST((int, char [], int));
#ifndef TET_LITE	/* -START-LITE-CUT- */
static int init1sfdir PROTOLIST((struct systab *));
static void rtrc2 PROTOLIST((int, char *));
#endif /* !TET_LITE */	/* -END-LITE-CUT- */


/*
**	rtlcopy() - copy the test suite to the runtime directory
**		on the local system if so required
*/

void rtlcopy()
{
	static char fmt[] = "can't copy test suite %.*s to runtime directory %.*s on";
	char msg[sizeof fmt + (MAXPATH * 2)];
	char dest[MAXPATH];

	TRACE3(tet_Ttcc, 1, "copying test suite %s to run-time directory %s on the local system",
		tet_tsroot, tet_run);

	/* determine the name of the destination directory */
	fullpath(tet_run, tet_basename(tet_tsroot), dest, sizeof dest, 0);

	/* do the copy */
	errno = 0;
	if (tet_fcopy(tet_tsroot, dest) < 0) {
		(void) sprintf(msg, fmt, MAXPATH, tet_tsroot, MAXPATH, dest);
		fatal(errno, msg, "the local system");
	}

	/* update tet_tsroot to refer to the new location */
	TRACE2(tet_Tbuf, 6, "free tet_tsroot = %s", tet_i2x(tet_tsroot));
	free(tet_tsroot);
	tet_tsroot = rstrstore(dest);
	TRACE2(tet_Ttcc, 1, "new tet_tsroot = %s", tet_tsroot);
}


#ifndef TET_LITE	/* -START-LITE-CUT- */

/*
**	rtrcopy() - copy the test suite to the runtime directory
**		on each of the remote systems if so required
*/

void rtrcopy()
{
	register int sysid, sysmax;
	char *rtdir;

	for (sysid = 1, sysmax = symax(); sysid <= sysmax; sysid++)
		if (syfind(sysid) != (struct systab *) 0 &&
			(rtdir = getdcfg("TET_RUN", sysid)) != (char *) 0)
				rtrc2(sysid, rtdir);
}

/*
**	rtrc2() - extend the rtrcopy() processing for a particular system
*/

static void rtrc2(sysid, rtdir)
int sysid;
char *rtdir;
{
	static char fmt[] = "can't copy test suite %.*s to runtime directory %.*s on system";
	char msg[sizeof fmt + (MAXPATH * 2)];
	char dest[MAXPATH];
	char *tsroot;

	/* determine the name of the destination directory */
	tsroot = getdcfg("TET_TSROOT", sysid);
	ASSERT(tsroot);
	fullpath(rtdir, tet_basename(tsroot), dest, sizeof dest, 1);

	TRACE4(tet_Ttcc, 1,
		"copying test suite %s to run-time directory %s on system %s",
		tsroot, rtdir, tet_i2a(sysid));

	/* do the copy */
	errno = 0;
	if (tet_tcrcopy(sysid, tsroot, dest) < 0) {
		(void) sprintf(msg, fmt, MAXPATH, tsroot, MAXPATH, dest);
		if (!IS_ER_ERRNO(tet_tcerrno))
			errno = 0;
		fatal(errno ? errno : tet_tcerrno, msg, tet_i2a(sysid));
	} 
}

#endif /* !TET_LITE */	/* -END-LITE-CUT- */


/*
**	inittmpdir() - create a temporary execution directory if necessary
**		on each system for use when TET_EXEC_IN_PLACE is false
*/

void inittmpdir()
{
	register int sysid, sysmax;
	register struct systab *sp;

	for (sysid = 0, sysmax = symax(); sysid <= sysmax; sysid++)
		if ((sp = syfind(sysid)) != (struct systab *) 0)
			inittd2(sp);
}

/*
**	inittd2() - extend the inittmpdir() processing for a particular system
*/

static void inittd2(sp)
struct systab *sp;
{
	static char tmpdirname[] = "TET_TMP_DIR";
	char buf[MAXPATH];
	char *tdir;

	TRACE2(tet_Ttcc, 4, "inittd2(%s)", tet_i2a(sp->sy_sysid));

	ASSERT_LITE(sp->sy_sysid == 0);

	/*
	** if no temporary directory has been specified on this system,
	** create it if necessary and install the name in the distributed
	** configuration for this system
	*/
	if ((tdir = getdcfg(tmpdirname, sp->sy_sysid)) == (char *) 0) {
		inittd3(sp->sy_sysid, buf, sizeof buf);
		tdir = buf;
		putdcfg(tmpdirname, sp->sy_sysid, tdir);
	}

	TRACE3(tet_Ttcc, 1, "TET_TMP_DIR on system %s = %s",
		tet_i2a(sp->sy_sysid), tdir);
}

/*
**	inittd3() - determine the default location for the temporary
**		directory and create it if necessary
**
**	return the name of the default location
*/

static void inittd3(sysid, tdir, tdirlen)
int sysid, tdirlen;
char tdir[];
{
	static char fmt[] = "can't create directory %.*s on system";
	char msg[sizeof fmt + MAXPATH];
	char *tsroot;

	TRACE2(tet_Ttcc, 4, "inittd3(): sysid = %s", tet_i2a(sysid));

	/* determine the name of the default tmpdir */
	tsroot = get_runtime_tsroot(sysid);
	ASSERT(tsroot);
	fullpath(tsroot, "tet_tmp_dir", tdir, tdirlen, sysid > 0 ? 1 : 0);

	/* return now if this directory exists already */
	if (tcc_access(sysid, tdir, 0) == 0)
		return;

	/* here to create the tmpdir */
	errno = 0;
	if (tcc_mkdir(sysid, tdir) < 0) {
		(void) sprintf(msg, fmt, MAXPATH, tdir);
		fatal(errno ? errno : tet_tcerrno, msg, tet_i2a(sysid));
	}

	/* all OK so return */
	TRACE3(tet_Ttcc, 2, "created directory %s on system %s",
		tdir, tet_i2a(sysid));
}

/*
**	tcc_mktmpdir() - create the temporary directory for use when
**		TET_EXEC_IN_PLACE is false
**
**	return 0 if successful or -1 on error
**
**	if successful, the name of the newly-created directory is returned
**	indirectly through *tdp
*/

int tcc_mktmpdir(prp, tmproot, tdp)
struct proctab *prp;
char *tmproot, **tdp;
{

	ASSERT_LITE(*prp->pr_sys == 0);

	/* create the temporary directory */
#ifdef TET_LITE	/* -LITE-CUT-LINE- */
	if ((tet_tcerrno = tcf_mktmpdir(tmproot, tdp)) != ER_OK)
		*tdp = (char *) 0;
#else	/* -START-LITE-CUT- */
	*tdp = tet_tcmktmpdir(*prp->pr_sys, tmproot);
#endif /* TET_LITE */	/* -END-LITE-CUT- */

	/* handle an error return */
	if (*tdp == (char *) 0) {
		if (!IS_ER_ERRNO(tet_tcerrno))
			errno = 0;
		prperror(prp, *prp->pr_sys, errno ? errno : tet_tcerrno,
			"can't create temporary directory below", tmproot);
		return(-1);
	}

	TRACE3(tet_Ttcc, 4,
		"created temporary execution directory %s on system %s",
		*tdp, tet_i2a(*prp->pr_sys));

	return(0);
}

/*
**	tcc_mkalldirs() - make directories recursively
**
**	return 0 if successful or -1 on error
*/

int tcc_mkalldirs(prp, dir)
struct proctab *prp;
char *dir;
{
	int rc;

	ASSERT_LITE(*prp->pr_sys == 0);

#ifdef TET_LITE	/* -LITE-CUT-LINE- */
	rc = tet_mkalldirs(dir);
	tet_tcerrno = rc < 0 ? tet_maperrno(errno) : ER_OK;
#else /* TET_LITE */	/* -START-LITE-CUT- */
	rc = tet_tcmkalldirs(*prp->pr_sys, dir);
#endif /* TET_LITE */	/* -END-LITE-CUT- */

	if (rc < 0) {
		if (!IS_ER_ERRNO(tet_tcerrno))
			errno = 0;
		prperror(prp, *prp->pr_sys, errno ? errno : tet_tcerrno,
			"can't recursively make directory", dir);
		return(-1);
	}

	return(0);
}

/*
**	tcc_rmtmpdir() - remove the temporary directory which is used when
**		TET_EXEC_IN_PLACE is false
**
**	return 0 if successful or -1 on error
*/

int tcc_rmtmpdir(prp, tmpdir)
struct proctab *prp;
char *tmpdir;
{
	struct systab *sp;
	char *tetroot;
	int err, rc;

	/*
	** if we are currently in the tmpdir subtree (or don't yet know where
	** we are), go back to TET_ROOT
	*/
	sp = syfind(*prp->pr_sys);
	ASSERT(sp);
	
	if (sp->sy_cwd) {
		rc = strncmp(tmpdir, sp->sy_cwd, strlen(tmpdir));
	}
	else
		rc = 0;

	if (!rc) {
		tetroot = getdcfg("TET_ROOT", *prp->pr_sys);
		ASSERT(tetroot && *tetroot);
		if (sychdir(sp, tetroot) < 0) {
			prperror(prp, *prp->pr_sys, errno ? errno : tet_tcerrno,
				"can't change directory to", tetroot);
			return(-1);
		}
	}

	/* remove the tmpdir */
#ifdef TET_LITE	/* -LITE-CUT-LINE- */
	rc = tcf_rmrf(tmpdir);
	err = errno;
#else	/* -START-LITE-CUT- */
	rc = tet_tcrmalldirs(*prp->pr_sys, tmpdir);
	err = tet_tcerrno;
#endif /* TET_LITE */	/* -END-LITE-CUT- */
	if (rc < 0) {
		prperror(prp, *prp->pr_sys, err,
			"can't remove temporary directory subtree", tmpdir);
		return(-1);
	}

	TRACE3(tet_Ttcc, 4,
		"removed temporary execution directory %s on system %s",
		tmpdir, tet_i2a(*prp->pr_sys));
	return(0);
}

/*
**	tccopy() - copy the test case directory to the temporary
**		execution directory when TET_EXEC_IN_PLACE is false
**
**	return 0 if successful or -1 on error
*/

int tccopy(prp, from, to)
struct proctab *prp;
char *from, *to;
{
	static char fmt[] = "can't copy test case directory %.*s to temporary directory";
	char msg[sizeof fmt + MAXPATH];
	int err, rc;

#ifdef TET_LITE	/* -LITE-CUT-LINE- */
	if ((rc = tet_fcopy(from, to)) < 0)
		err = errno;
#else	/* -START-LITE-CUT- */
	if ((rc = tet_tcrcopy(*prp->pr_sys, from, to)) < 0)
		err = IS_ER_ERRNO(tet_tcerrno) ? errno : tet_tcerrno;
#endif /* TET_LITE */	/* -END-LITE-CUT- */
	if (rc < 0) {
		(void) sprintf(msg, fmt, MAXPATH, from);
		prperror(prp, *prp->pr_sys, err, msg, to);
		return(-1);
	}

	TRACE3(tet_Ttcc, 4, "copied test case directory %s to %s", from, to);

	return(0);
}


#ifndef TET_LITE	/* -START-LITE-CUT- */

/*
**	initsfdir() - create saved files directories on each remote system
*/

void initsfdir()
{
	register int sysid, sysmax;
	register struct systab *sp;
	int rc = 0;

	for (sysid = 1, sysmax = symax(); sysid <= sysmax; sysid++)
		if ((sp = syfind(sysid)) != (struct systab *) 0 &&
			init1sfdir(sp) < 0)
				rc = -1;

	if (rc < 0)
		tcc_exit(1);
}

/*
**	init1sfdir() - create the saved files directory on a single
**		remote system
**
**	return 0 if successful or -1 on error
*/

static int init1sfdir(sp)
struct systab *sp;
{
	static char fmt[] =
		"can't create saved files directory %s%.*s on system";
	static char below[] = "below ";
	char msg[sizeof fmt + sizeof below + MAXPATH];
	char resroot[MAXPATH];
	char *sfdir, *tsroot;

	TRACE2(tet_Ttcc, 4, "initsfdir(%s)", tet_i2a(sp->sy_sysid));

	/*
	** determine the name of the results directory root on the
	** remote system
	*/
	tsroot = get_runtime_tsroot(sp->sy_sysid);
	ASSERT(tsroot);
	fullpath(tsroot, "results", resroot, sizeof resroot, 1);

	/* create the results directory on the remote system if necessary */
	if (tet_tcmkalldirs(sp->sy_sysid, resroot) < 0) {
		(void) sprintf(msg, fmt, "",
			sizeof msg - sizeof fmt, resroot);
		if (!IS_ER_ERRNO(tet_tcerrno))
			errno = 0;
		error(errno ? errno : tet_tcerrno, msg, tet_i2a(sp->sy_sysid));
		return(-1);
	}

	/* create the saved files directory on the remote system */
	if ((sfdir = tet_tcmksdir(sp->sy_sysid, resroot, resdirsuffix())) == (char *) 0) {
		(void) sprintf(msg, fmt, below,
			sizeof msg - sizeof fmt - sizeof below, resroot);
		error(tet_tcerrno, msg, tet_i2a(sp->sy_sysid));
		return(-1);
	}

	/* all OK so remember the directory name and return */
	sp->sy_sfdir = rstrstore(sfdir);
	return(0);
}

#endif /* !TET_LITE */	/* -END-LITE-CUT- */


/*
**	sfproc() - perform save files processing on a single system
**
**	return 0 if successful or -1 on error
*/

int sfproc(prp, sfiles, nsfiles)
register struct proctab *prp;
char **sfiles;
int nsfiles;
{
	static char fmt[] = "can't copy save files from %.*s on system %03d to";
	char msg[sizeof fmt + MAXPATH + LNUMSZ];
	struct systab *sp;
	int rc;
	int tsfiles = 0;
	char savedir[MAXPATH];
#ifndef TET_LITE	/* -START-LITE-CUT- */
	char path[MAXPATH];
	static char remote[] = "REMOTE%03d";
	char subdir[sizeof remote];
#endif /* !TET_LITE */	/* -END-LITE-CUT- */


	TRACE3(tet_Ttcc, 4, "sfproc(%s): sysid = %s",
		tet_i2x(prp), tet_i2a(*prp->pr_sys));

	ASSERT(prp->pr_nsys == 1);
	ASSERT_LITE(*prp->pr_sys == 0);

	/* change directory to the test case execution directory */
	sp = syfind(*prp->pr_sys);
	ASSERT(sp);
	if (sychdir(sp, prp->pr_tcedir) < 0) {
		prperror(prp, *prp->pr_sys, errno ? errno : tet_tcerrno,
			"can't change directory to", prp->pr_tcedir);
		return(-1);
	}

	/* then do the save */
#ifdef TET_LITE	/* -LITE-CUT-LINE- */

	tsfiles = 0;
	tcexecdir(prp, resdirname(), savedir, sizeof savedir);
	tet_tcerrno = tcf_procdir(".", savedir, sfiles, nsfiles, TCF_TS_LOCAL);
	rc = (tet_tcerrno == ER_OK) ? 0 : -1;

#else /* TET_LITE */	/* -START-LITE-CUT- */

	/*
	** if TET_TRANSFER_SAVE_FILES is true for this system, we must
	** copy the files to REMOTEnnn below the results directory on
	** the local system; if this is the local system, TCCD can do
	** this unaided (tet_tctslfiles()), otherwise TCCD must work with
	** XRESD to do this (tet_tctsmfiles())
	**
	** if TET_TRANSFER_SAVE_FILES is false for this system, we must
	** copy the files to the saved files directory on this system;
	** TCCD can always do this unaded
	*/

	tsfiles = getcflag("TET_TRANSFER_SAVE_FILES", *prp->pr_sys,
		prp->pr_currmode);

	if (tsfiles) {
		(void) sprintf(subdir, remote, *prp->pr_sys % 1000);
		fullpath(resdirname(), subdir, path, sizeof path,
			*prp->pr_sys ? 1 : 0);
		tcexecdir(prp, path, savedir, sizeof savedir);
		if (*prp->pr_sys > 0)
			rc = tet_tctsmfiles(*prp->pr_sys, sfiles, nsfiles,
				savedir + strlen(resdirname()));
		else
			rc = tet_tctslfiles(*prp->pr_sys, sfiles, nsfiles,
				(char *) 0, savedir);
	}
	else
	{
		tcexecdir(prp,
			(*prp->pr_sys > 0) ? sp->sy_sfdir : resdirname(),
			savedir, sizeof savedir);
		rc = tet_tctslfiles(*prp->pr_sys, sfiles, nsfiles,
			(char *) 0, savedir);
	}

#endif /* TET_LITE */	/* -END-LITE-CUT- */

	if (rc < 0) {
		(void) sprintf(msg, fmt, MAXPATH, prp->pr_tcedir, *prp->pr_sys);
		prperror(prp, tsfiles ? 0 : *prp->pr_sys, tet_tcerrno,
			msg, savedir);
	}

	return(rc);
}