summaryrefslogtreecommitdiffstats
path: root/usr.sbin/sup/lib/scan.c
blob: edb42ea6c465a6ce23833a96b5a27e2645bfd99a (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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
/*
 * Copyright (c) 1992 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 * scan.c - sup list file scanner
 *
 **********************************************************************
 * HISTORY
 * $Log: scan.c,v $
 * Revision 1.2  1995/12/26 05:02:48  peter
 * Apply ports/net/sup/patches/patch-aa...
 *
 * Revision 1.1.1.1  1995/12/26 04:54:47  peter
 * Import the unmodified version of the sup that we are using.
 * The heritage of this version is not clear.  It appears to be NetBSD
 * derived from some time ago.
 *
 * Revision 1.1.1.1  1993/08/21  00:46:33  jkh
 * Current sup with compression support.
 *
 * Revision 1.1.1.1  1993/05/21  14:52:17  cgd
 * initial import of CMU's SUP to NetBSD
 *
 * Revision 1.8  92/08/11  12:04:28  mrt
 * 	Brad's changes: delinted, added forward declarations of static
 * 	functions.Added Copyright.
 * 	[92/07/24            mrt]
 * 
 * 18-Mar-88  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added host=<hostfile> support to releases file.
 *
 * 11-Mar-88  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added "rsymlink" recursive symbolic link quoting directive.
 *
 * 28-Jun-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added code for "release" support.
 *
 * 26-May-87  Doug Philips (dwp) at Carnegie-Mellon University
 *	Lets see if we'll be able to write the scan file BEFORE
 *	we collect the data for it.  Include sys/file.h and use
 *	new definitions for access check codes.
 *
 * 20-May-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added type casting information for lint.
 *
 * 21-Jan-86  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added check for newonly upgrade when lasttime is the same as
 *	scantime.  This will save us the trouble of parsing the scanfile
 *	when the client has successfully received everything in the
 *	scanfile already.
 *
 * 16-Jan-86  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Clear Texec pointers in execT so that Tfree of execT will not
 *	free command trees associated with files in listT.
 *
 * 06-Jan-86  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added code to omit scanned files from list if we want new files
 *	only and they are old.
 *
 * 29-Dec-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Major rewrite for protocol version 4.  Added version numbers to
 *	scan file.  Also added mode of file in addition to flags.
 *	Execute commands are now immediately after file information.
 *
 * 13-Dec-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added comments to list file format.
 *
 * 08-Dec-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added code to implement omitany.  Currently doesn't know about
 *	{a,b,c} patterns.
 *
 * 07-Oct-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Created.
 *
 **********************************************************************
 */

#include <libc.h>
#include <c.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/file.h>
#include "sup.h"

/*************************
 ***    M A C R O S    ***
 *************************/

#define SPECNUMBER 1000
	/* number of filenames produced by a single spec in the list file */

/*******************************************
 ***    D A T A   S T R U C T U R E S    ***
 *******************************************/

typedef enum {			/* release options */
	ONEXT,		OPREFIX,	OLIST,		OSCAN,
	OHOST
} OPTION;

static char *options[] = {
	"next",		"prefix",	"list",		"scan",
	"host",
	0
};

typedef enum {			/* <collection>/list file lines */
	LUPGRADE,	LOMIT,		LBACKUP,	LEXECUTE,
	LINCLUDE,	LNOACCT,	LOMITANY,	LALWAYS,
	LSYMLINK,	LRSYMLINK,	LRENAME
} LISTTYPE;

static char *ltname[] = {
	"upgrade",	"omit",		"backup",	"execute",
	"include",	"noaccount",	"omitany",	"always",
	"symlink",	"rsymlink",	"rename",
	0
};

#define FALWAYS		FUPDATE

/* list file lines */
static TREE *upgT;			/* files to upgrade */
static TREE *flagsT;			/* special flags: BACKUP NOACCT */
static TREE *omitT;			/* recursize file omition list */
static TREE *omanyT;			/* non-recursize file omition list */
static TREE *symT;			/* symbolic links to quote */
static TREE *rsymT;			/* recursive symbolic links to quote */
static TREE *execT;			/* execute command list */

/*************************
 ***    E X T E R N    ***
 *************************/

#ifdef	lint
static char _argbreak;
#else
extern char _argbreak;			/* break character from nxtarg */
#endif

extern TREELIST *listTL;		/* list of trees for scanning */
extern TREE *listT;			/* final list of files in collection */
extern TREE *refuseT;			/* files refused by client */

extern char *collname;			/* collection name */
extern char *basedir;			/* base directory name */
extern char *prefix;			/* collection pathname prefix */
extern long lasttime;			/* time of last upgrade */
extern long scantime;			/* time of this scan */
extern int trace;			/* trace directories */
extern int newonly;			/* new files only */

extern long time();

/*************************************************
 ***   STATIC   R O U T I N E S    ***
 *************************************************/

static makescan();
static getscan();
static doscan();
static readlistfile();
static expTinsert();
static listone();
static listentry();
static listname();
static listdir();
static omitanyone();
static anyglob();
static int getscanfile();
static int chkscanfile();
static makescanfile();
static recordone();
static recordexec();

/*************************************************
 ***    L I S T   S C A N   R O U T I N E S    ***
 *************************************************/

static
passdelim (ptr,delim)		/* skip over delimiter */
char **ptr,delim;
{
	*ptr = skipover (*ptr, " \t");
	if (_argbreak != delim && **ptr == delim) {
		(*ptr)++;
		*ptr = skipover (*ptr, " \t");
	}
}

static
char *parserelease(tlp,relname,args)
TREELIST **tlp;
char *relname,*args;
{
	register TREELIST *tl;
	register char *arg;
	register OPTION option;
	int opno;
	char *nextrel;

	tl = (TREELIST *) malloc (sizeof(TREELIST));
	if ((*tlp = tl) == NULL)
		goaway ("Couldn't allocate TREELIST");
	tl->TLnext = NULL;
	tl->TLname = salloc (relname);
	tl->TLprefix = NULL;
	tl->TLlist = NULL;
	tl->TLscan = NULL;
	tl->TLhost = NULL;
	nextrel = NULL;
	args = skipover (args," \t");
	while (*(arg=nxtarg(&args," \t="))) {
		for (opno = 0; options[opno] != NULL; opno++)
			if (strcmp (arg,options[opno]) == 0)
				break;
		if (options[opno] == NULL)
			goaway ("Invalid release option %s for release %s",
				arg,relname);
		option = (OPTION) opno;
		switch (option) {
		case ONEXT:
			passdelim (&args,'=');
			arg = nxtarg (&args," \t");
			nextrel = salloc (arg);
			break;
		case OPREFIX:
			passdelim (&args,'=');
			arg = nxtarg (&args," \t");
			tl->TLprefix = salloc (arg);
			break;
		case OLIST:
			passdelim (&args,'=');
			arg = nxtarg (&args," \t");
			tl->TLlist = salloc (arg);
			break;
		case OSCAN:
			passdelim (&args,'=');
			arg = nxtarg (&args," \t");
			tl->TLscan = salloc (arg);
			break;
		case OHOST:
			passdelim (&args,'=');
			arg = nxtarg (&args," \t");
			tl->TLhost = salloc (arg);
			break;
		}
	}
	return (nextrel);
}

getrelease (release)
char *release;
{
	TREELIST *tl;
	char buf[STRINGLENGTH];
	char *p,*q;
	int rewound;
	FILE *f;

	if (release == NULL)
		release = salloc (DEFRELEASE);
	listTL = NULL;

	(void) sprintf (buf,FILERELEASES,collname);
	f = fopen (buf,"r");
	if (f != NULL) {
		rewound = TRUE;
		for (;;) {
			p = fgets (buf,STRINGLENGTH,f);
			if (p == NULL) {
				if (rewound)
					break;
				rewind (f);
				rewound = TRUE;
				continue;
			}
			q = index (p,'\n');
			if (q)  *q = 0;
			if (index ("#;:",*p))  continue;
			q = nxtarg (&p," \t");
			if (strcmp (q,release) != 0)
				continue;
			release = parserelease (&tl,release,p);
			if (tl->TLprefix == NULL)
				tl->TLprefix = prefix;
			else if (chdir (tl->TLprefix) < 0)
				return (FALSE);
			else
				(void) chdir (basedir);
			tl->TLnext = listTL;
			listTL = tl;
			if (release == NULL)
				break;
			rewound = FALSE;
		}
		(void) fclose (f);
	}
	if (release == NULL)
		return (TRUE);
	if (strcmp (release,DEFRELEASE) != 0)
		return (FALSE);
	(void) parserelease (&tl,release,"");
	tl->TLprefix = prefix;
	tl->TLnext = listTL;
	listTL = tl;
	return (TRUE);
}

makescanlists (releases)
	char **releases;
{
	TREELIST *tl;
	char buf[STRINGLENGTH];
	char *p,*q;
	FILE *f;
	char *saveprefix = prefix;
	char **rel_index;
	int count = 0;

	(void) sprintf (buf,FILERELEASES,collname);
	f = fopen (buf,"r");
	if (f != NULL) {
		while (p = fgets (buf,STRINGLENGTH,f)) {
			q = index (p,'\n');
			if (q)  *q = 0;
			if (index ("#;:",*p))  continue;
			q = nxtarg (&p," \t");
			(void) parserelease (&tl,q,p);
			if ((prefix = tl->TLprefix) == NULL)
				prefix = saveprefix;
			if (prefix != NULL) {
				if (chdir (prefix) < 0)
					goaway ("Can't chdir to %s",prefix);
				(void) chdir (basedir);
			}
			if (releases)
			{
				rel_index = releases;
				while (*rel_index) {
					if (!strcmp (*rel_index, tl->TLname)) {
						makescan (tl->TLlist,tl->TLscan);
						break;
					}
					rel_index++;
				}
			}
			else
				makescan (tl->TLlist,tl->TLscan);
			free ((char *)tl);
			count++;
		}
		(void) fclose (f);
	}
	if (count == 0)
		makescan ((char *)NULL,FILESCANDEF);
}

static
scanone (t)
register TREE *t;
{
	register TREE *newt;

	if (newonly && (t->Tflags&FNEW) == 0)
		return (SCMOK);
	newt = Tinsert (&listT,t->Tname,t->Tflags&FRENAME ? TRUE : FALSE);
	if (newt == NULL)
		return (SCMOK);
	if(t->Tnewname)
	  newt->Tnewname = salloc(t->Tnewname);
	newt->Tmode = t->Tmode;
	newt->Tflags = t->Tflags;
	newt->Tmtime = t->Tmtime;
	return (SCMOK);
}

getscanlists ()
{
	TREELIST *tl,*stl;

	stl = listTL;
	listTL = NULL;
	while ((tl = stl) != NULL) {
		prefix = tl->TLprefix;
		getscan (tl->TLlist,tl->TLscan);
		tl->TLtree = listT;
		stl = tl->TLnext;
		tl->TLnext = listTL;
		listTL = tl;
	}
	listT = NULL;
	for (tl = listTL; tl != NULL; tl = tl->TLnext)
		(void) Tprocess (tl->TLtree,scanone);
}

static
makescan (listfile,scanfile)
char *listfile,*scanfile;
{
	listT = NULL;
	if(chkscanfile (scanfile)) {	/* can we can write a scan file? */
		doscan (listfile);	/* read list file and scan disk */
		makescanfile (scanfile);/* record names in scan file */
		Tfree (&listT);		/* free file list tree */
	}
}

static
getscan (listfile,scanfile)
char *listfile,*scanfile;
{
	listT = NULL;
	if (!getscanfile(scanfile)) {	/* check for pre-scanned file list */
		scantime = time ((long *)NULL);
		doscan (listfile);	/* read list file and scan disk */
	}
}

static
doscan (listfile)
	char *listfile;
{
	char buf[STRINGLENGTH];
	int listone ();

	upgT = NULL;
	flagsT = NULL;
	omitT = NULL;
	omanyT = NULL;
	execT = NULL;
	symT = NULL;
	rsymT = NULL;
	if (listfile == NULL)
		listfile = FILELISTDEF;
	(void) sprintf (buf,FILELIST,collname,listfile);
	readlistfile (buf);		/* get contents of list file */
	(void) Tprocess (upgT,listone); /* build list of files specified */
	cdprefix ((char *)NULL);
	Tfree (&upgT);
	Tfree (&flagsT);
	Tfree (&omitT);
	Tfree (&omanyT);
	Tfree (&execT);
	Tfree (&symT);
	Tfree (&rsymT);
}

static
readlistfile (fname)
char *fname;
{
	char buf[STRINGLENGTH],*p;
	register char *q,*r;
	register FILE *f;
	register int ltn,n,i,flags;
	register TREE **t;
	register LISTTYPE lt;
	char *speclist[SPECNUMBER];

	f = fopen (fname,"r");
	if (f == NULL)  goaway ("Can't read list file %s",fname);
	cdprefix (prefix);
	while (p = fgets (buf,STRINGLENGTH,f)) {
		if (q = index (p,'\n'))  *q = '\0';
		if (index ("#;:",*p))  continue;
		q = nxtarg (&p," \t");
		if (*q == '\0') continue;
		for (ltn = 0; ltname[ltn] && strcmp(q,ltname[ltn]) != 0; ltn++);
		if (ltname[ltn] == NULL)
			goaway ("Invalid list file keyword %s",q);
		lt = (LISTTYPE) ltn;
		flags = 0;
		switch (lt) {
		case LUPGRADE:
			t = &upgT;
			break;
		case LRENAME:
			t = &flagsT;
			flags = FRENAME;
			break;
		case LBACKUP:
			t = &flagsT;
			flags = FBACKUP;
			break;
		case LNOACCT:
			t = &flagsT;
			flags = FNOACCT;
			break;
		case LSYMLINK:
			t = &symT;
			break;
		case LRSYMLINK:
			t = &rsymT;
			break;
		case LOMIT:
			t = &omitT;
			break;
		case LOMITANY:
			t = &omanyT;
			break;
		case LALWAYS:
			t = &upgT;
			flags = FALWAYS;
			break;
		case LINCLUDE:
			while (*(q=nxtarg(&p," \t"))) {
				cdprefix ((char *)NULL);
				n = expand (q,speclist,SPECNUMBER);
				for (i = 0; i < n && i < SPECNUMBER; i++) {
					readlistfile (speclist[i]);
					cdprefix ((char *)NULL);
					free (speclist[i]);
				}
				cdprefix (prefix);
			}
			continue;
		case LEXECUTE:
			r = p = q = skipover (p," \t");
			do {
				q = p = skipto (p," \t(");
				p = skipover (p," \t");
			} while (*p != '(' && *p != '\0');
			if (*p++ == '(') {
				*q = '\0';
				do {
					q = nxtarg (&p," \t)");
					if (*q == 0)
						_argbreak = ')';
					else
						expTinsert (q,&execT,0,r,NULL);
				} while (_argbreak != ')');
				continue;
			}
			/* fall through */
		default:
			goaway ("Error in handling list file keyword %d",ltn);
		}
		while (*(q=nxtarg(&p," \t"))) {
			if (lt == LOMITANY)
				(void) Tinsert (t,q,FALSE);
			else if( lt == LRENAME )
				if(*(r=nxtarg(&p," \t")))
				{
					expTinsert (q,t,flags,(char *)NULL,r);
					/*
					 * Omit the file it is being
					 * renamed to, to avoid confusion
					 */
					expTinsert (r,&omitT,0,
						   (char *)NULL, (char *)NULL);
				}
				else
					printf("Rename %s without destination "
					       "file.  Skipping...\n", q);
			else
				expTinsert (q,t,flags,(char *)NULL,(char *)NULL);
		}
	}
	(void) fclose (f);
}

static
expTinsert (p,t,flags,exec, q)
char *p;
char *q;
TREE **t;
int flags;
char *exec;
{
	register int n, i;
	register TREE *newt, *ts;
	char *speclist[SPECNUMBER];
	char buf[STRINGLENGTH];

	n = expand (p,speclist,SPECNUMBER);
	for (i = 0; i < n && i < SPECNUMBER; i++) {
		newt = Tinsert (t,speclist[i],TRUE);
		newt->Tflags |= flags;
		if (exec) {
			if((ts = Tsearch(flagsT, speclist[i])) 
			   && ts->Tflags&FRENAME)
				(void) sprintf (buf,exec,ts->Tnewname);
			else
				(void) sprintf (buf,exec,speclist[i]);
			(void) Tinsert (&newt->Texec,buf,FALSE);
		}
		if (q)
			newt->Tnewname = salloc(q);
		free (speclist[i]);
	}
}

static
listone(t)		/* expand and add one name from upgrade list */
TREE *t;
{
	listentry(t->Tname,t->Tname,(char *)NULL,
		  (t->Tflags&FALWAYS) != 0);
	return (SCMOK);
}

static
listentry(name,fullname,updir,always)
register char *name, *fullname, *updir;
int always;
{
	struct stat statbuf;
	int link = 0;
	int omitanyone ();

	if (Tlookup (refuseT,fullname))  return;
	if (!always) {
		if (Tsearch (omitT,fullname))  return;
		if (Tprocess (omanyT,omitanyone,fullname) != SCMOK)
			return;
	}
	if (lstat(name,&statbuf) < 0)
		return;
	if ((statbuf.st_mode&S_IFMT) == S_IFLNK) {
		if (Tsearch (symT,fullname)) {
			listname (fullname,&statbuf);
			return;
		}
		if (Tlookup (rsymT,fullname)) {
			listname (fullname,&statbuf);
			return;
		}
		if (updir) link++;
		if (stat(name,&statbuf) < 0) return;
	}
	if ((statbuf.st_mode&S_IFMT) == S_IFDIR) {
		if (access(name,R_OK|X_OK) < 0) return;
		if (chdir(name) < 0) return;
		listname (fullname,&statbuf);
		if (trace) {
			printf ("Scanning directory %s\n",fullname);
			(void) fflush (stdout);
		}
		listdir (fullname,always);
		if (updir == 0 || link) {
			(void) chdir (basedir);
			if (prefix) (void) chdir (prefix);
			if (updir && *updir) (void) chdir (updir);
		} else
			(void) chdir ("..");
		return;
	}
	if (access(name,R_OK) < 0) return;
	listname (fullname,&statbuf);
}

static
listname (name,st)
register char *name;
register struct stat *st;
{
	register TREE *t,*ts;
	register int new;
	register TREELIST *tl;

	new = st->st_ctime > lasttime;
	if (newonly && !new) {
		for (tl = listTL; tl != NULL; tl = tl->TLnext)
			if (ts = Tsearch (tl->TLtree,name))
				ts->Tflags &= ~FNEW;
		return;
	}
	t = Tinsert (&listT,name,FALSE);
	if (t == NULL)  return;
	t->Tmode = st->st_mode;
	t->Tctime = st->st_ctime;
	t->Tmtime = st->st_mtime;
	if (new)  t->Tflags |= FNEW;
	if (ts = Tsearch (flagsT,name)){
		t->Tflags |= ts->Tflags;
		if(t->Tflags&FRENAME)
			t->Tnewname = salloc(ts->Tnewname);
	}
	if (ts = Tsearch (execT,name)) {
		t->Texec = ts->Texec;
		ts->Texec = NULL;
	}
}

static
listdir (name,always)		/* expand directory */
char *name;
int always;
{
	struct direct *dentry;
	register DIR *dirp;
	char ename[STRINGLENGTH],newname[STRINGLENGTH],filename[STRINGLENGTH];
	register char *p,*newp;
	register int i;

	dirp = opendir (".");
	if (dirp == 0) return;	/* unreadable: probably protected */

	p = name;		/* punt leading ./ and trailing / */
	newp = newname;
	if (p[0] == '.' && p[1] == '/') {
		p += 2;
		while (*p == '/') p++;
	}
	while (*newp++ = *p++) ;	/* copy string */
	--newp;				/* trailing null */
	while (newp > newname && newp[-1] == '/') --newp; /* trailing / */
	*newp = 0;
	if (strcmp (newname,".") == 0) newname[0] = 0;	/* "." ==> "" */

	while (dentry=readdir(dirp)) {
		if (dentry->d_ino == 0) continue;
		if (strcmp(dentry->d_name,".") == 0) continue;
		if (strcmp(dentry->d_name,"..") == 0) continue;
		for (i=0; i<=MAXNAMLEN && dentry->d_name[i]; i++)
			ename[i] = dentry->d_name[i];
		ename[i] = 0;
		if (*newname)
			(void) sprintf (filename,"%s/%s",newname,ename);
		else
			(void) strcpy (filename,ename);
		listentry(ename,filename,newname,always);
	}
	closedir (dirp);
}

static
omitanyone (t,filename)
TREE *t;
char **filename;
{
	if (anyglob (t->Tname,*filename))
		return (SCMERR);
	return (SCMOK);
}

static
anyglob (pattern,match)
char *pattern,*match;
{
	register char *p,*m;
	register char *pb,*pe;

	p = pattern; 
	m = match;
	while (*m && *p == *m ) { 
		p++; 
		m++; 
	}
	if (*p == '\0' && *m == '\0')
		return (TRUE);
	switch (*p++) {
	case '*':
		for (;;) {
			if (*p == '\0')
				return (TRUE);
			if (*m == '\0')
				return (*p == '\0');
			if (anyglob (p,++m))
				return (TRUE);
		}
	case '?':
		return (anyglob (p,++m));
	case '[':
		pb = p;
		while (*(++p) != ']')
			if (*p == '\0')
				return (FALSE);
		pe = p;
		for (p = pb + 1; p != pe; p++) {
			switch (*p) {
			case '-':
				if (p == pb && *m == '-') {
					p = pe + 1;
					return (anyglob (p,++m));
				}
				if (p == pb)
					continue;
				if ((p + 1) == pe)
					return (FALSE);
				if (*m > *(p - 1) &&
				    *m <= *(p + 1)) {
					p = pe + 1;
					return (anyglob (p,++m));
				}
				continue;
			default:
				if (*m == *p) {
					p = pe + 1;
					return (anyglob (p,++m));
				}
			}
		}
		return (FALSE);
	default:
		return (FALSE);
	}
}

/*****************************************
 ***    R E A D   S C A N   F I L E    ***
 *****************************************/

static
int getscanfile (scanfile)
char *scanfile;
{
	char buf[STRINGLENGTH];
	struct stat sbuf;
	register FILE *f;
	TREE ts;
	register char *p,*q;
	register TREE *tmp, *t = NULL;
	register notwanted;
	register TREELIST *tl;

	if (scanfile == NULL)
		return(FALSE);
	(void) sprintf (buf,FILESCAN,collname,scanfile);
	if (stat(buf,&sbuf) < 0)
		return (FALSE);
	if ((f = fopen (buf,"r")) == NULL)
		return (FALSE);
	if ((p = fgets (buf,STRINGLENGTH,f)) == NULL) {
		(void) fclose (f);
		return (FALSE);
	}
	if (q = index (p,'\n'))  *q = '\0';
	if (*p++ != 'V') {
		(void) fclose (f);
		return (FALSE);
	}
	if (atoi (p) != SCANVERSION) {
		(void) fclose (f);
		return (FALSE);
	}
	scantime = sbuf.st_mtime;	/* upgrade time is time of supscan,
					 * i.e. time of creation of scanfile */
	if (newonly && scantime == lasttime) {
		(void) fclose (f);
		return (TRUE);
	}
	notwanted = FALSE;
	while (p = fgets (buf,STRINGLENGTH,f)) {
		q = index (p,'\n');
		if (q)  *q = 0;
		ts.Tflags = 0;
		if (*p == 'X') {
			if (notwanted)  continue;
			if (t == NULL)
				goaway ("scanfile format inconsistant");
			(void) Tinsert (&t->Texec,++p,FALSE);
			continue;
		}
		notwanted = FALSE;
		if (*p == 'B') {
			p++;
			ts.Tflags |= FBACKUP;
		}
		if (*p == 'N') {
			p++;
			ts.Tflags |= FNOACCT;
		}
		if (*p == 'R') {
			p++;
			ts.Tflags |= FRENAME;
		}
		if ((q = index (p,' ')) == NULL)
			goaway ("scanfile format inconsistant");
		*q++ = '\0';
		ts.Tmode = atoo (p);
		p = q;
		if ((q = index (p,' ')) == NULL)
			goaway ("scanfile format inconsistant");
		*q++ = '\0';
		ts.Tctime = atoi (p);
		p = q;
		if ((q = index (p,' ')) == NULL)
			goaway ("scanfile format inconsistant");
		*q++ = 0;
		ts.Tmtime = atoi (p);
		p = q;
		ts.Tnewname = NULL;
		if (ts.Tflags & FRENAME){
			if ((q = index (p,' ')) == NULL)
				goaway ("scanfile format inconsistant");
			*q++ = '\0';
			ts.Tnewname = salloc(q);
			q = p;
		}
		if (ts.Tctime > lasttime)
			ts.Tflags |= FNEW;
		else if (newonly) {
			for (tl = listTL; tl != NULL; tl = tl->TLnext)
				if (tmp = Tsearch (tl->TLtree,q))
					tmp->Tflags &= ~FNEW;
			notwanted = TRUE;
			continue;
		}
		if (Tlookup (refuseT,q)) {
			notwanted = TRUE;
			continue;
		}
		t = Tinsert (&listT,q,TRUE);
		t->Tmode = ts.Tmode;
		t->Tflags = ts.Tflags;
		t->Tctime = ts.Tctime;
		t->Tmtime = ts.Tmtime;
		t->Tnewname = ts.Tnewname;
	}
	(void) fclose (f);
	return (TRUE);
}

/*******************************************
 ***    W R I T E   S C A N   F I L E    ***
 *******************************************/

static chkscanfile (scanfile)
char *scanfile;
{
	char tname[STRINGLENGTH], fname[STRINGLENGTH];
	FILE *f;

	if (scanfile == NULL)
		return(FALSE);
	(void) sprintf (fname,FILESCAN,collname,scanfile);
	(void) sprintf (tname,"%s.temp",fname);
	if (NULL == (f = fopen (tname, "w")))
		goaway ("Can't test scan file temp %s for %s",tname,collname);
	else {
		(void) unlink (tname);
		(void) fclose (f);
	}
	return(TRUE);
}

static makescanfile (scanfile)
char *scanfile;
{
	char tname[STRINGLENGTH],fname[STRINGLENGTH];
	struct timeval tbuf[2];
	FILE *scanF;			/* output file for scanned file list */
	int recordone ();

	(void) sprintf (fname,FILESCAN,collname,scanfile);
	(void) sprintf (tname,"%s.temp",fname);
	scanF = fopen (tname,"w");
	if (scanF == NULL)
		goaway ("Can't write scan file temp %s for %s",tname,collname);
	fprintf (scanF,"V%d\n",SCANVERSION);
	(void) Tprocess (listT,recordone,scanF);
	(void) fclose (scanF);
	if (rename (tname,fname) < 0)
		goaway ("Can't change %s to %s",tname,fname);
	(void) unlink (tname);
	tbuf[0].tv_sec = time((long *)NULL);  tbuf[0].tv_usec = 0;
	tbuf[1].tv_sec = scantime;  tbuf[1].tv_usec = 0;
	(void) utimes (fname,tbuf);
}

static
recordone (t,scanF)
TREE *t;
FILE **scanF;
{
	int recordexec ();

	if (t->Tflags&FBACKUP)  fprintf (*scanF,"B");
	if (t->Tflags&FNOACCT)  fprintf (*scanF,"N");
	if (t->Tflags&FRENAME)  fprintf (*scanF,"R");

	fprintf (*scanF,"%o %d %d",
		t->Tmode,t->Tctime,t->Tmtime,t->Tname);
	if ( t->Tflags&FRENAME)
		fprintf (*scanF," %s %s\n",t->Tname, t->Tnewname);
	else
		fprintf (*scanF," %s\n", t->Tname);
	(void) Tprocess (t->Texec,recordexec,*scanF);
	return (SCMOK);
}

static
recordexec (t,scanF)
TREE *t;
FILE **scanF;
{
	fprintf(*scanF,"X%s\n",t->Tname);
	return (SCMOK);
}

cdprefix (prefix)
char *prefix;
{
	static char *curprefix = NULL;

	if (curprefix == NULL) {
		if (prefix == NULL)
			return;
		(void) chdir (prefix);
		curprefix = prefix;
		return;
	}
	if (prefix == NULL) {
		(void) chdir (basedir);
		curprefix = NULL;
		return;
	}
	if (prefix == curprefix)
		return;
	if (strcmp (prefix, curprefix) == 0) {
		curprefix = prefix;
		return;
	}
	(void) chdir (basedir);
	(void) chdir (prefix);
	curprefix = prefix;
}
OpenPOWER on IntegriCloud