File: ImportFilter.cpp

package info (click to toggle)
gcvs 1.0final-12
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,248 kB
  • ctags: 10,631
  • sloc: ansic: 71,709; cpp: 39,780; sh: 18,434; makefile: 1,915; yacc: 1,299; tcl: 1,283; perl: 910; lex: 249; csh: 185; lisp: 7
file content (646 lines) | stat: -rwxr-xr-x 14,078 bytes parent folder | download | duplicates (3)
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
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- March 1998
 */

/*
 * ImportFilter.cpp : filter the imported files before importing
 */

#include "stdafx.h"

#ifdef WIN32
#	include "resource.h"
#endif /* WIN32 */

#ifdef macintosh
#	include "GUSIInternal.h"
#	include "GUSIFileSpec.h"
#	include "MacMisc.h"
#endif /* macintosh */

#include "ImportFilter.h"
#include "AppConsole.h"

//
// MN - Apply $HOME/.cvsignore to import filter
//
// list_add_type function uses ignore filter to decided whether
// a file should be ignored in importing (user can change this
// in import dialogbox, but this is a default). If user imports
// a whole subdirectory instead of a file then this will
// eliminate ignored files.
//
#include "CvsIgnore.h"	

#if qCvsDebug
#	include "CvsAlert.h"
#endif /* qCvsDebug */

#ifdef macintosh
	static const char *lf = "\r";
#elif defined(WIN32)
	static const char *lf = "\r\n";
#else
	static const char *lf = "\n";
#endif

#ifdef WIN32
#	ifdef _DEBUG
#	define new DEBUG_NEW
#	undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
#	endif
#endif /* WIN32 */

#ifndef NEW
#define NEW new
#endif

ReportConflict::ReportConflict(int mac_type, int mac_creator)
{
	next = 0L;
	status = noConflict;
	ftype = mac_type;
	fcreator = mac_creator;
}

void ReportConflict::PrintOut(CStr & out) const
{
	out = "";
	if(HasConflict())
	{
		out << "Text/Binary conflict, at least these files :";
		out << lf;
		if(!sample1.empty() && !sample2.empty())
		{
			out << "    ";
			out << sample1;
			out << " and ";
			out << sample2;
			out << lf;
		}
	}
	else if(HasTypeCreatorConflict())
	{
		out << "Mac signature mismatch for this type, at least these files :";
		out << lf;
		if(!sample1.empty() && !sample2.empty())
		{
			out << "    ";
			out << sample1;
			out << " and ";
			out << sample2;
			out << lf;
		}
	}
}

#if qCvsDebug
void ReportConflict::PrintOut(void) const
{
	if(pattern.empty())
		return;

	cvs_out("  New type : %s%s (%s)\n",
		(status & hasExtension) ? "*" : "",
		(const char *)pattern,
		(status & isBinary) ? "BINARY" : "TEXT"
	);
	if(!sample1.empty() && !sample2.empty())
	{
		if(HasConflict())
			cvs_out("    Text/Binary CONFLICT ! %s and %s\n",
					(const char *)sample1, (const char *)sample2);
		else if(HasTypeCreatorConflict())
			cvs_out("    Mac signature mismatch ! %s and %s\n",
					(const char *)sample1, (const char *)sample2);
	}
}
#endif /* qCvsDebug */

ReportConflict::~ReportConflict()
{
}

void ReportConflict::AddPattern(const char *newpat, kFileType fileType, bool isExt, const char *sample)
{
	pattern = newpat;
	sample1 = sample;
	
	switch( fileType )
	{
	case kFileTypeBin:
		status |= isBinary;
		break;
	case kFileTypeUnicode:
		status |= isUnicode;
		break;
	default:
		// nothing to do
		break;
	}

	if(isExt)
		status |= hasExtension;
}

bool ReportConflict::Match(const char *pat)
{
	if(pat == 0L || pattern.empty())
		return false;

	int len = strlen(pat);
	int patlen = pattern.length();

	if(status & hasExtension)
	{
		if(len < patlen)
			return false;
		// since it is the server who compares against the pattern,
		// that might be a case compare
#if 0 //def WIN32
		return stricmp(pattern, pat + len - patlen) == 0;
#else /* !WIN32 */
		return strcmp(pattern, pat + len - patlen) == 0;
#endif /* !WIN32 */
	}

#if 0 //def WIN32
	return stricmp(pattern, pat) == 0;
#else /* !WIN32 */
	return strcmp(pattern, pat) == 0;
#endif /* !WIN32 */
}

void ReportConflict::SetConflict(const char *sample)
{
	// one is enough !
	if(HasConflict())
		return;

	sample2 = sample;
	status |= hasConflict;
}

void ReportConflict::SetTypeCreatorConflict(const char *sample)
{
	// one is enough !
	if(HasTypeCreatorConflict() || HasConflict())
		return;

	sample2 = sample;
	status |= hasTypeCreatorConflict;
}

const size_t ReportWarning::maxWarnings = 10;

ReportWarning::ReportWarning(kTextBinTYPE akind)
{
	kind = akind;
	next = 0L;
	tooMuchWarnings = false;
}

ReportWarning::~ReportWarning()
{
}

void ReportWarning::PrintOut(CStr & out) const
{
	out = "";

	if(samples.size() == 0)
		return;

	out = "Warning : ";
	switch(kind)
	{
	default:
	case kFileIsOK:
		out << "Files are OK";
		break;
	case kFileMissing:
		out << "Files are missing or unreadable";
		break;
	case kFileIsAlias:
		out << "Files are aliases";
		break;
	case kFileInvalidName:
		out << "Files have invalid name";
		break;
	case kTextWrongLF:
		out << "Files have the wrong line feed";
		break;
	case kTextIsBinary:
		out << "Files are binary";
		break;
	case kTextEscapeChar:
		out << "Files have escape characters in it";
		break;
	case kTextWrongSig:
		out << "Text files have the wrong signature";
		break;
	case kBinIsText:
		out << "Files are text, should be binary";
		break;
	case kBinWrongSig:
		out << "Binary files have the wrong signature";
		break;
	case kTextIsUnicode:
		out << "Files are unicode, should be text";
		break;
	case kBinIsUnicode:
		out << "Files are unicode, should be binary";
		break;
	}
	out << lf;
	
	std::vector<CStr>::const_iterator i;
	for(i = samples.begin(); i != samples.end(); ++i)
	{
		out << "    ";
		out << *i;
		out << lf;
	}
	
	if(tooMuchWarnings)
	{
		out << "    More files follow";
		out << lf;
	}
}

#if qCvsDebug
void ReportWarning::PrintOut(void) const
{
	if(samples.size() == 0)
		return;

	cvs_out("  Warning : ");
	switch(kind)
	{
	default:
	case kFileIsOK:
		cvs_out("Files are OK\n");
		break;
	case kFileMissing:
		cvs_out("Files are missing or unreadable\n");
		break;
	case kFileIsAlias:
		cvs_out("Files are aliases\n");
		break;
	case kFileInvalidName:
		cvs_out("Files have invalid name\n");
		break;
	case kTextWrongLF: 
		cvs_out("Files have the wrong line feed\n");
		break;
	case kTextIsBinary:
		cvs_out("Files are binary\n");
		break;
	case kTextEscapeChar:
		cvs_out("Files have escape characters in it\n");
		break;
	case kTextWrongSig:
		cvs_out("Text files have the wrong signature\n");
		break;
	case kBinIsText:
		cvs_out("Files are text, should be binary\n");
		break;
	case kBinWrongSig:
		cvs_out("Binary files have the wrong signature\n");
		break;
	case kTextIsUnicode:
		cvs_out("Files are unicode, should be text\n");
		break;
	case kBinIsUnicode:
		cvs_out("Files are unicode, should be binary\n");
		break;
	}

	std::vector<CStr>::const_iterator i;
	for(i = samples.begin(); i != samples.end(); ++i)
	{
		cvs_out("    %s\n", (const char *)(*i));
	}
	
	if(tooMuchWarnings)
		cvs_out("    More files follow\n");
}
#endif /* qCvsDebug */

void ReportWarning::AddWarning(const char *sample)
{
	// it's enough !
	if(samples.size() >= maxWarnings)
	{
		tooMuchWarnings = true;
		return;
	}

	samples.push_back(CStr(sample));
}

static void list_add_warning(ReportWarning *& warnings, kTextBinTYPE warn, const char *sample)
{
	// find if it exists already a same kind of warning
	ReportWarning *tmp = warnings;
	while(tmp != 0L)
	{
		if(tmp->Kind() == warn)
			break;
		tmp = tmp->next;
	}
	if(tmp != 0L && tmp->Kind() == warn)
	{
		tmp->AddWarning(sample);
		return;
	}

	// insert new one
	tmp = NEW ReportWarning(warn);
	if(tmp == 0L)
	{
		cvs_err("Impossible to allocate %d bytes !\n", sizeof(ReportWarning));
		return;
	}

	tmp->AddWarning(sample);
	if(warnings == 0L)
	{
		warnings = tmp;
	}
	else
	{
		tmp->next = warnings;
		warnings = tmp;
	}
}

class ImportReport;

static void list_add_type(ImportReport *impReport, ReportConflict *& conflicts, kFileType fileType,
						  const char *sample, const char *fullname,
						  int mac_type, int mac_creator);

class ImportReport : public TraversalReport
{
public:
	ImportReport(const char *path, ReportWarning *& w, ReportConflict *& c) : warnings(w), conflicts(c)
	{
		// MN - Apply ignore filter to import file list
		BuildIgnoredList(m_Ignlist, path);	
	}
	virtual ~ImportReport() {}

	ReportWarning *& warnings;
	ReportConflict *& conflicts;
	std::vector<CStr> m_Ignlist;
		// MN - Ignore list

	virtual kTraversal EnterDirectory(const char *fullpath, const char *dirname, const UFSSpec * macspec)
	{
		cvs_out("Filtering \'%s\'...\n", fullpath);
		return kContinueTraversal;
	}

	virtual kTraversal ExitDirectory(const char *fullpath)
	{
		return kContinueTraversal;
	}

	virtual kTraversal OnError(const char *err, int errcode)
	{
		list_add_warning(warnings, kFileMissing, err);
		return kContinueTraversal;
	}

	virtual kTraversal OnIdle(const char *fullpath)
	{
		return kContinueTraversal;
	}

	virtual kTraversal OnDirectory(const char *fullpath,
		const char *fullname,
		const char *name,
		const struct stat & dir, const UFSSpec * macspec)
	{
#if defined(WIN32) || defined(macintosh)
		if(stricmp(name, "CVS") == 0)
#else
		if(strcmp(name, "CVS") == 0)
#endif
			return kSkipFile;
		return kContinueTraversal;
	}

	virtual kTraversal OnFile(const char *fullpath,
		const char *fullname,
		const char *name,
		const struct stat & dir, const UFSSpec * macspec)
	{
		int mac_type = 0;
		int mac_creator = 0;
#ifdef macintosh
#	define MACSPEC , &fsSpec
		GUSIFileSpec inspec(fullname);
#	if TARGET_RT_MAC_CFM
		mac_type = inspec.CatInfo()->FileInfo().ioFlFndrInfo.fdType;
		mac_creator = inspec.CatInfo()->FileInfo().ioFlFndrInfo.fdCreator;
#	else
		FInfo finfo;
		memcpy(&finfo, inspec.CatInfo()->Info().finderInfo, 2 * sizeof(OSType));
		mac_type = finfo.fdType;
		mac_creator = finfo.fdCreator;
#	endif
		UFSSpec fsSpec = inspec;
#else /* !macintosh */
#	define MACSPEC
#endif /* !macintosh */

		kFileType newType = kFileTypeText;
		kTextBinTYPE filetype = FileIsText(name, fullpath MACSPEC);
		if( kTextIsBinary == filetype || kTextWrongSig == filetype )
		{
			kTextBinTYPE bintype = FileIsBinary(name, fullpath MACSPEC);
			if( kFileIsOK != bintype )
			{
				list_add_warning(warnings, bintype, fullname);
			}
			else if( kTextWrongSig == filetype )
			{
				list_add_warning(warnings, filetype, fullname);
				filetype = kTextIsBinary;
				newType = kFileTypeBin;
			}
			
			if( kFileIsOK == bintype )
			{
				newType = kFileTypeBin;
			}
		}
		else if( kTextIsUnicode == filetype )
		{
			newType = kFileTypeUnicode;
		}
		else if( kFileIsOK != filetype )
		{
			list_add_warning(warnings, filetype, fullname);
		}
		
		// add the type
		list_add_type(this, conflicts, newType, name, fullname, mac_type, mac_creator);

		return kContinueTraversal;
	}

	virtual kTraversal OnAlias(const char *fullpath,
		const char *fullname,
		const char *name,
		const struct stat & dir, const UFSSpec * macspec)
	{
		list_add_warning(warnings, kFileIsAlias, fullname);
		return kContinueTraversal;
	}
};

//
// MN - Had to move this function after class ImportReport, because
// this function uses a variable of that type. 
//
// When a file is added to warning/conflict list, it is also
// processed through ignore filter. If file should be ignored
// then the initial state is "IGNORE" in import dialogbox.
//
static void list_add_type(ImportReport *impReport, ReportConflict *& conflicts, kFileType fileType,
						  const char *sample, const char *fullname,
						  int mac_type, int mac_creator)
{
	// look for a matching entry
	ReportConflict *entry = conflicts;
	while(entry != 0L)
	{
		if(entry->Match(sample))
		{
			if(entry->HasConflict())
				return;
			
			if(entry->IsBinary() ^ (kFileTypeBin == fileType))
				entry->SetConflict(fullname);
			
			if(entry->IsUnicode() ^ (kFileTypeUnicode == fileType))
				entry->SetConflict(fullname);
			
			// mac only
			if(entry->GetMacType() != mac_type || entry->GetMacCreator() != mac_creator)
				entry->SetTypeCreatorConflict(fullname);
			
			// it matches perfectly
			return;
		}
		
		entry = entry->next;
	}

	// find the extension
	const char *tmp = sample, *ext = 0L;
	while((tmp = strchr(tmp, '.')) != 0L)
	{
		ext = tmp++;
	}

	// add a new entry
	ReportConflict *newc = NEW ReportConflict(mac_type, mac_creator);
	if(newc == 0L)
	{
		cvs_err("Impossible to allocate %d bytes !\n", sizeof(ReportConflict));
		return;
	}

	newc->AddPattern(ext != 0L ? ext : sample, fileType, ext != 0L, fullname);
	if(conflicts == 0L)
		conflicts = newc;
	else
	{
		newc->next = conflicts;
		conflicts = newc;
	}

	// MN - Apply ignore filter. If $HOME/.cvsignore tells that
	// file should be ignored then set object to ignored state.
	if(MatchIgnoredList(sample, impReport->m_Ignlist))
		newc->Ignore();
}

kTraversal list_all_types_recur(const char *path, ReportWarning *& warnings,
								ReportConflict *& conflicts)
{
	ImportReport report(path, warnings, conflicts);
	return FileTraverse(path, report);
}

void free_list_types(ReportWarning * warnings, ReportConflict * conflicts)
{
	ReportWarning *tmpwarn;
	ReportWarning *warn = warnings;
	while(warn != 0L)
	{
		tmpwarn = warn;
		warn = warn->next;
		delete tmpwarn;
	}

	ReportConflict *tmpconf;
	ReportConflict *conf = conflicts;
	while(conf != 0L)
	{
		tmpconf = conf;
		conf = conf->next;
		delete tmpconf;
	}
}

#if qCvsDebug
bool list_all_types(const char *path)
{
	ReportWarning * warnings = 0L;
	ReportConflict * conflicts = 0L;
	list_all_types_recur(path, warnings, conflicts);
	
	cvs_out("Reporting warnings :\n");
	ReportWarning *warn = warnings;
	while(warn != 0L)
	{
		warn->PrintOut();
		warn = warn->next;
	}

	cvs_out("Reporting types :\n");
	ReportConflict *conf = conflicts;
	while(conf != 0L)
	{
		conf->PrintOut();
		conf = conf->next;
	}

	free_list_types(warnings, conflicts);

	return !CvsAlert("Continue importing ?", "Cancel", "Continue");
}
#endif /* qCvsDebug */