File: CvsEntries.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 (600 lines) | stat: -rwxr-xr-x 13,420 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
/*
** 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> --- April 1998
 */

/*
 * CvsEntries.cpp --- adaptation from cvs/src/entries.c
 */

#include "stdafx.h"

#ifdef macintosh
#	include "GUSIInternal.h"
#	include "GUSIFileSpec.h"
#	define S_IWRITE S_IWUSR
#endif

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "CvsEntries.h"
#include "AppConsole.h"
#include "getline.h"
#include "FileTraversal.h"

#ifdef WIN32
#	ifdef _DEBUG
#		define new DEBUG_NEW
#		undef THIS_FILE
		static char THIS_FILE[] = __FILE__;
#	endif
#	include "WinCvsDebug.h"
#endif /* WIN32 */

#if TIME_WITH_SYS_TIME
#	include <sys/time.h>
#	include <time.h>
#elif HAVE_SYS_TIME_H
#	include <sys/time.h>
#else
#	include <time.h>
#endif

#ifndef NEW
#define NEW new
#endif

static const char* cloneStr(const char* cp)
{
	if(cp && *cp)
	{
		cp = strdup(cp);
		if (!cp)
		{
			throw std::bad_alloc();
		}
	}
	else
	{
		cp = 0L;
	}
	return cp;
}

static void disposeStr(const char* cp) 
{
	if (cp)
	{
		free((void*)cp);
	}
}

EntnodePath::EntnodePath(const char* path) : fullpathname(0L), ref(1)
{
	fullpathname = cloneStr(path);
}

EntnodePath::~EntnodePath()
{
	disposeStr(fullpathname);
}

const char* EntnodePath::GetFullPathName(UStr & resPath, const char* name) const
{
	resPath = fullpathname;
	if(!resPath.endsWith(kPathDelimiter))
		resPath << kPathDelimiter;
	resPath << name;

	return resPath;
}

EntnodeData::EntnodeData(const char* name, EntnodePath *path)
	: ref(1), missing(false), visited(false), unknown(false),
	  unmodified(true), ignored(false), locked(false), removed(false),
	  user(0L), desc(0L), fullpathname(0L)
{
#if TARGET_RT_MAC_CFM
	macspec.vRefNum = 0;
	macspec.parID = 0;
	macspec.name[0] = '\0';
#endif
#if TARGET_RT_MAC_MACHO
	memset(&macspec, 0, sizeof(macspec));
#endif
	user = cloneStr(name);
	fullpathname = path->Ref();
}

EntnodeData::~EntnodeData()
{
	disposeStr(user);
	if(fullpathname)
		fullpathname->UnRef();
}

void EntnodeData::SetDesc(const char *newdesc)
{
	desc = newdesc;
}

UStr EntnodeData::sbuffer;

EntnodeFile::~EntnodeFile()
{
	disposeStr(vn);
	disposeStr(ts);
	disposeStr(option);
	disposeStr(tag);
	disposeStr(date);
	disposeStr(ts_conflict);
}

EntnodeFile::EntnodeFile(const char* name, EntnodePath *path, const char *newvn,
	const char *newts, const char *newoptions, const char *newtag,
	const char *newdate, const char *newts_conflict) : EntnodeData(name, path)
{
	vn = 0L;
	ts = 0L;
	option = 0L;
	tag = 0L;
	date = 0L;
	ts_conflict = 0L;

	vn = cloneStr(newvn);
	ts = cloneStr(newts);
	option = cloneStr(newoptions);
	tag = cloneStr(newtag);
	date = cloneStr(newdate);
	ts_conflict = cloneStr(newts_conflict);

	if(newvn != 0L && newvn[0] == '-')
		SetRemoved(true);
}

EntnodeFolder::~EntnodeFolder()
{
}

EntnodeFolder::EntnodeFolder(const char* name, EntnodePath *path, const char *newvn,
	const char *newts, const char *newoptions, const char *newtag,
	const char *newdate, const char *newts_conflict) : EntnodeData(name, path)
{
}

/* Return the next real Entries line.  On end of file, returns NULL.
   On error, prints an error message and returns NULL.	*/

static EntnodeData * fgetentent(FILE *fpin, const char* path, char *cmd = 0L)
{
	EntnodeData *ent;
	char *line = 0L;
	size_t line_chars_allocated = 0;
	register char *cp;
	ent_type type;
	char *l, *user, *vn, *ts, *options;
	char *tag_or_date, *tag, *date, *ts_conflict;
	int line_length;

	EntnodePath *thePath = new EntnodePath(path);

	ent = 0L;
	while ((line_length = getline (&line, &line_chars_allocated, fpin)) > 0)
	{
		l = line;

		/* If CMD is not NULL, we are reading an Entries.Log file.
		Each line in the Entries.Log file starts with a single
		character command followed by a space.	For backward
		compatibility, the absence of a space indicates an add
		command.  */
		if (cmd != 0L)
		{
			if (l[1] != ' ')
				*cmd = 'A';
			else
			{
				*cmd = l[0];
				l += 2;
			}
		}
		type = ENT_FILE;

		if (l[0] == 'D')
		{
			type = ENT_SUBDIR;
			++l;
			/* An empty D line is permitted; it is a signal that this
			Entries file lists all known subdirectories.  */
		}

		if (l[0] != '/')
			continue;

		user = l + 1;
		if ((cp = strchr (user, '/')) == 0L)
			continue;
		*cp++ = '\0';
		vn = cp;
		if ((cp = strchr (vn, '/')) == 0L)
			continue;
		*cp++ = '\0';
		ts = cp;
		if ((cp = strchr (ts, '/')) == 0L)
			continue;
		*cp++ = '\0';
		options = cp;
		if ((cp = strchr (options, '/')) == 0L)
			continue;
		*cp++ = '\0';
		tag_or_date = cp;
		if ((cp = strchr (tag_or_date, '\n')) == 0L
#ifdef macintosh
			&& (cp = strchr (tag_or_date, '\r')) == 0L
#endif
		)
			continue;
		*cp = '\0';
		tag = (char *) 0L;
		date = (char *) 0L;
		if (*tag_or_date == 'T')
			tag = tag_or_date + 1;
		else if (*tag_or_date == 'D')
			date = tag_or_date + 1;

		if ((ts_conflict = strchr (ts, '+')))
			*ts_conflict++ = '\0';

		if(type == ENT_SUBDIR)
			ent = NEW EntnodeFolder(user, thePath);
		else
			ent = NEW EntnodeFile(user, thePath, vn, ts, options,
				tag, date, ts_conflict);
		break;
	}

	if (line_length < 0 && !feof (fpin))
		cvs_err("Cannot read entries file (error %d)\n", errno);

	free (line);

	thePath->UnRef();

	return ent;
}

/* Read the entries file into a list, hashing on the file name.

   UPDATE_DIR is the name of the current directory, for use in error
   messages, or NULL if not known (that is, noone has gotten around
   to updating the caller to pass in the information).	*/
bool Entries_Open (CSortList<ENTNODE> & entries, const char *fullpath)
{
	EntnodeData *ent;

	UStr adminPath(fullpath);
	if(!adminPath.endsWith(kPathDelimiter))
		adminPath << kPathDelimiter;
	adminPath << "CVS";
	adminPath << kPathDelimiter;
	
	entries.Reset();
	UStr filename = adminPath;
	filename << "Entries";
	FILE *fpin = fopen(filename, "r");
	if (fpin == 0L)
		return false;

	while ((ent = fgetentent (fpin, fullpath)) != 0L)
	{
		ENTNODE newnode(ent);
		ent->UnRef();

		if(entries.InsideAndInsert(newnode))
		{
			cvs_err("Warning : duplicated entry in the 'CVS/Entries' file in directory '%s'\n",
				fullpath);
		}
	}

	fclose (fpin);

	filename = adminPath;
	filename << "Entries.log";
	fpin = fopen(filename, "r");
	if (fpin != 0L)
	{
		char cmd;

		while ((ent = fgetentent (fpin, &cmd)) != 0L)
		{
			ENTNODE newnode(ent);
			ent->UnRef();

			switch (cmd)
			{
			case 'A':
				if(entries.InsideAndInsert(newnode))
				{
					// dont care
				}
				break;
			case 'R':
				entries.Delete(newnode);
				break;
			default:
				/* Ignore unrecognized commands.  */
				break;
			}
		}
		fclose (fpin);
	}
	return true;
}

// mostly the cvs one...
static bool unmodified(const struct stat & sb, const char *ts)
{
	char *cp;
	struct tm *tm_p;
	struct tm local_tm;
	/* We want to use the same timestamp format as is stored in the
	st_mtime.  For unix (and NT I think) this *must* be universal
	time (UT), so that files don't appear to be modified merely
	because the timezone has changed.  For VMS, or hopefully other
	systems where gmtime returns NULL, the modification time is
	stored in local time, and therefore it is not possible to cause
	st_mtime to be out of sync by changing the timezone.  */
	tm_p = gmtime (&sb.st_mtime);
	if (tm_p)
	{
		memcpy (&local_tm, tm_p, sizeof (local_tm));
		cp = asctime (&local_tm);	/* copy in the modify time */
	}
	else
		cp = ctime(&sb.st_mtime);

	cp[24] = 0;

// [6/8/2001] Jerzy K. - the code has been replaced by the version from cvsnt 
//which seems simpler and giving the same effect
//keep it here for a while and remove eventually 
#if 0	
// Bug 432
	if( cp[8]=='0') cp[8]=' ';
//  The C Standard Library and the Unix/cygwin versions of CVS
//  write a padding space before days 1-9; Visual C++ libraries use
//  a leading 0.  This affects ctime() and asctime() calls.  Bug 432
//  adopts the padding space standard.  The following allows for backward
//  compatibility with a WinCVS working area checked out prior to the
//  Bug 432 fix.  (cp is the timestamp from the stat of the working file;
//  ts is the timestamp as read from CVS/Entries).
	char *ns = (char *)malloc( (strlen(ts) + 1) * sizeof( char ) );
	if ( ns == NULL ) {
		cvs_err("Could not allocate memory for string ns\n");
		return 1;
	}
	strcpy( ns, ts );
	if( ns[8]=='0') ns[8]=' ';
	int compare_result = strcmp(ns, cp);
	free( ns );
	return compare_result == 0;
// Bug 432
#else

	/* Deal with the differences in timestamps - posix uses space filled, NT uses zero filled */
	if(cp[8]=='0' && ts[8]==' ') 
		cp[8]=' ';
	if(cp[8]==' ' && ts[8]=='0') 
		cp[8]='0';

	return strcmp(cp, ts) == 0;
#endif
}

EntnodeData *Entries_SetVisited(const char *path, CSortList<ENTNODE> & entries, const char *name,
	const struct stat & finfo, bool isFolder, const std::vector<CStr> * ignlist)
{
	int index;
	bool isCvs = false;

	EntnodePath *thePath = new EntnodePath(path);

	if(isFolder)
	{
		EntnodeFolder *adata = NEW EntnodeFolder(name, thePath);
		ENTNODE anode(adata);
		adata->UnRef();
		isCvs = entries.InsideAndInsert(anode, &index);
	}
	else
	{
		EntnodeFile *adata = NEW EntnodeFile(name, thePath);
		ENTNODE anode(adata);
		adata->UnRef();
		isCvs = entries.InsideAndInsert(anode, &index);
	}

	thePath->UnRef();

	const ENTNODE & theNode = entries.Get(index);
	EntnodeData *data = theNode.Data();
	data->SetVisited(true);
	if(!isCvs)
	{
		data->SetUnknown(true);
		if(ignlist != 0L && MatchIgnoredList(name, *ignlist))
			data->SetIgnored(true);

		// the folder may have some cvs informations in it, despite the fact
		// that it is not referenced by the parent directory, so try
		// to figure it.

		if(!data->IsIgnored())
		{
			CStr cvsFile(path);
			if(!cvsFile.endsWith(kPathDelimiter))
				cvsFile << kPathDelimiter;
			cvsFile << name;
			if(!cvsFile.endsWith(kPathDelimiter))
				cvsFile << kPathDelimiter;
			cvsFile << "CVS";
			struct stat sb;
			if (stat(cvsFile, &sb) != -1 && S_ISDIR(sb.st_mode))
			{
				data->SetUnknown(false);
			}
		}
	}

	if(isFolder)
	{
		if(data->IsIgnored())
		{
			data->SetDesc("Ignored Folder");
		}
		else if(data->IsUnknown())
		{
			data->SetDesc("NonCvs Folder");
		}
		else
		{
			data->SetDesc("Folder");
		}
	}
	else
	{
		const char *ts = (*data)[EntnodeFile::kTS];
		if(ts == 0L)
			data->SetUnmodified(true);
		else
			data->SetUnmodified(unmodified(finfo, ts));

#if defined(WIN32)
		if (GetWinCvsDebugMaskBit(wcvsdbg_trace_modified_checks_bit))
		{
			char scratch[128];

			/* copied from unmodified... */
			char *cp;
			struct tm *tm_p;
			struct tm local_tm;
			tm_p = gmtime (& finfo.st_mtime);
			if (tm_p)
			{
				memcpy (&local_tm, tm_p, sizeof (local_tm));
				cp = asctime (&local_tm);	/* copy in the modify time */
			}
			else
			{
				cp = "NULL";
			}

			_snprintf(scratch, 128, 
				"Checking timestamps for %s: Entries: %s, file: %s --> %s\n",
                name, ts, cp, 
                data->IsUnmodified() ? "unmodified" : "modified");
			cvs_err(scratch);
			OutputDebugString(scratch);
        }
#endif

		data->SetLocked((finfo.st_mode & S_IWRITE) == 0);
		
		const char *info = 0L;
		if(data->IsIgnored())
		{
			data->SetDesc("Ignored");
		}
		else if(data->IsUnknown())
		{
			data->SetDesc("NonCvs file");
		}
		else if(data->GetConflict() != 0L)
		{
			data->SetDesc("Conflict");
		}
		else if((info = (*data)[EntnodeFile::kOption]) != 0L && strcmp(info, "-kb") == 0)
		{
			data->SetDesc(data->IsUnmodified() ? "Binary" : "Mod. Binary");
		}
		else if((info = (*data)[EntnodeFile::kOption]) != 0L && strcmp(info, "-ku") == 0)
		{
			data->SetDesc(data->IsUnmodified() ? "Unicode" : "Mod. Unicode");
		}
		else
		{
			data->SetDesc(data->IsUnmodified() ? "File" : "Mod. File");
		}
	}

	return data;
}

void Entries_SetMissing(CSortList<ENTNODE> & entries)
{
	int numEntries = entries.NumOfElements();
	for(int i = 0; i < numEntries; i++)
	{
		const ENTNODE & theNode = entries.Get(i);
		EntnodeData *data = theNode.Data();
		if(data->IsVisited())
			continue;

		data->SetMissing(true);
		data->SetDesc(data->IsRemoved() ? "Removed" : "Missing");
	}
}

bool Tag_Open(CStr & tag, const char *fullpath)
{
	tag = "";
	CStr tagFile(fullpath);
	if(!tagFile.endsWith(kPathDelimiter))
		tagFile << kPathDelimiter;
	tagFile << "Tag";

	FILE *fpin = fopen(tagFile, "r");
	if (fpin == 0L)
		return false;

	char *line = 0L;
	size_t line_chars_allocated = 0;
	int line_length;
	if((line_length = getline (&line, &line_chars_allocated, fpin)) > 0)
	{
		char *tmp = strchr(line, '\n');
		if(tmp != 0L)
			*tmp = '\0';
		if(line[0] == 'T')
			tag = line + 1;
	}
	if(line != 0L)
		free(line);
	if (line_length < 0 && !feof (fpin))
	{
		cvs_err("Cannot open Tag file in '%s' (error %d)\n", fullpath, errno);
		return false;
	}

	fclose (fpin);
	return !tag.empty();
}