File: cvs.cxx

package info (click to toggle)
curves 0.8.7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 704 kB
  • ctags: 1,001
  • sloc: cpp: 6,197; ansic: 519; makefile: 270; sh: 192; fortran: 149
file content (487 lines) | stat: -rw-r--r-- 13,053 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
/* cvs.cxx
     $Id: cvs.cxx,v 1.24 1998/09/11 00:03:55 elf Exp $

   written by Marc Singer
   20 September 1996

   This file is part of the project CurVeS.  See the file README for
   more information.

   Copyright (C) 1996 Marc Singer

   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 2 of the
   License, 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
   in a file called COPYING along with this program; if not, write to
   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
   02139, USA.

   -----------
   DESCRIPTION
   -----------

   CVS specific control functions.

   --------------------
   IMPLEMENTATION NOTES
   --------------------

   m_rgFlags:			File and directory flags
   
   This array maps one to one to directory listing in
   m_pDir->m_rgInfo.  Each item there is examined and the flags are
   set appropriately.

   *** This has been superceeded by a user field in the directory
   structures.  We need this because the maintainence of parallel data
   structures prohibits sorting.

*/


#include "std.h"
#include "preferences.h"	// Everyone MUST get into the pref scene

#include <sys/stat.h>
#include <fnmatch.h>
#include <time.h>
#include "cvs.h"
#include "path.h"
#include "file.h"
#include "mman.h"

static char g_pchIgnoreDefault[] = "core RCSLOG tags TAGS RCS SCCS .make.state"
	" .nse_depinfo #* .#* cvslog.* ,* CVS* .del-* *.a *.o *.so *.Z"
	" *~ *.old *.elc *.ln *.bak *.BAK *.orig *.rej";
char* LCVS::g_pchIgnore;


void LCVS::analyze (void)
{
  if (!m_fControlled)
    return;

  assert_ (m_pDir);
  char sz[256];
  strcpy (sz, m_pDir->path ());
  int cchPath = strlen (sz);
  strcat (sz, "/");
  strcat (sz, "CVS/Entries");
  struct stat stat;
  if (::stat (sz, &stat) || !S_ISREG (stat.st_mode))
    return;

				// --- Check for changes to CVS/Entries
  int fh = -1;			// Done for the compiler and it's warnings
  char* pbEntries = NULL;
  if (stat.st_mtime != m_timeEntries) {
    m_timeEntries = stat.st_mtime;
  
    int cbEntries = stat.st_size;
    fh = open (sz, O_RDONLY);
    pbEntries = (char*) malloc (cbEntries + 1);
    pbEntries[cbEntries] = 0;
    assert_ (fh != -1);
    read (fh, pbEntries, cbEntries);
    close (fh);
  }

				// --- Read and parse ignore data
  TRACE((T_CVS_TRACE, "parsing .cvsignore"));
  strcpy (sz + cchPath, "/.cvsignore");
  if (!::stat (sz, &stat) && S_ISREG (stat.st_mode)
      && (fh = open (sz, O_RDONLY))) {
    char* pch = (char*) malloc (stat.st_size + 1);
    read (fh, pch, stat.st_size);
    pch[stat.st_size] = 0;
    close (fh);
    m_pchIgnore = _parse_ignore (pch);
    free (pch);
  }
  if (!g_pchIgnore) 
    g_pchIgnore = _parse_ignore (g_pchIgnoreDefault);

				// -- Parse the CVS/Entries file
  if (pbEntries) {
    TRACE((T_CVS_TRACE, "parsing CVS/Entries"));
    char* pb = pbEntries;
    bool fDirectory;
    while (*pb) {
      fDirectory = (*pb == 'D');
      assert_ (fDirectory || *pb == '/');
      if (!fDirectory) {
	++pb;
	int cch = strcspn (pb, "/");
	int iFile = m_pDir->find (pb, cch);
	if (iFile >= 0)
	  set_flag (iFile, flagControlled);
	pb += cch + 1;
	cch = strcspn (pb, "/");
	bool fVersion = set_version (pb, cch);
	pb += cch + 1;
	cch = strcspn (pb, "/");
	time_t time = fVersion ? recover_time (pb, cch) : 0;
//  const char* szNewTime = asctime (gmtime (&time));
//  fprintf (stderr, "From %*.*s\n  to %s\n", cch, cch, pb, szNewTime);
	if (m_pDir->time (iFile) != time)
	  set_flag (iFile, flagEdited);
      }
      pb += strcspn (pb, "\n");
      if (*pb)
	++pb;
    }
    free (pbEntries);
  }

  bool fSenseBinary = g_preferences.as_bool ("SenseBinaryFiles");

  TRACE((T_CVS_TRACE, "stat'ing files"));
  for (int iFile = 0; iFile < m_pDir->count_files (); ++iFile) {
    if (!m_pDir->is_file (iFile))
      continue;
    if (flag (iFile) & flagControlled)
      continue;
    LPath path (m_pDir->path ());
    path += m_pDir->file (iFile);
    if (fSenseBinary && file_guess_binary (path))
      set_flag (iFile, flagBinary);
    if (!_is_ignored (m_pDir->file (iFile)))
      continue;
    set_flag (iFile, flagIgnored);
  }

  TRACE((T_CVS_TRACE, "parsing status data"));
  parse_status ();
  TRACE((T_CVS_TRACE, "cvs.analyze complete"));
}

/* LCVS::fetch_status

   requests file status information from CVS.  If the directory is not
   controlled, it will not call cvs.  If the szFiles parameter is not
   NULL, it will be appended to the end of the status request command
   thus allowing the status of specific files to be requested.

   FIXME: presently, szFiles can be only one file.

   If the UseStatusHash preference is true, we will try to use the -H
   switch for cvs status which can improve the performance of the call
   dramatically.

*/

void LCVS::fetch_status (const char* szFiles)
{
  release_filestatus ();
  if (!m_fControlled)
    return;
  
  char* szFileTemp = tmpnam (NULL);
  m_szFileStatus = (char*) malloc (strlen (szFileTemp) + 1);
  strcpy (m_szFileStatus, szFileTemp);

  char* sz = (char*) malloc ((szFiles ? strlen (szFiles) : 0) + CB_COMMAND*2
			     + strlen (m_pDir->path ()));
  bool fOK = false;
  if (g_preferences.as_bool ("UseStatusHash")) {
    sprintf (sz, "cd %s ; %s status -l -H %s > %s 2>&1", 
	     m_pDir->path (), g_preferences.fetch ("cvs"), 
	     szFiles ? szFiles : "", m_szFileStatus);
    TRACE((T_CVS_TRACE, "Executing: '%s'\n", sz));
    
    fOK = (system (sz) == 0);
  }
  if (!fOK) {
    sprintf (sz, "cd %s ; %s status -l %s > %s 2>&1", 
	     m_pDir->path (), g_preferences.fetch ("cvs"),
	     szFiles ? szFiles : "", m_szFileStatus);
    TRACE((T_CVS_TRACE, "Executing: '%s'\n", sz));
    system (sz);
  }
  free (sz);
}

void LCVS::init (LDirectory* pDir)
{
  release_this ();

  m_pDir = pDir;

  m_fControlled = false;	// Default state

  assert_ (m_pDir);
  char sz[256];
  strcpy (sz, m_pDir->path ());
  strcat (sz, "/");
  strcat (sz, "CVS");
  struct stat stat;
  if (::stat (sz, &stat) || !S_ISDIR (stat.st_mode))
    return;
  m_fControlled = true;
}


/* LCurves::_is_ignored

   attempts to match the given filename with one of the patterns in
   the list of ignored files.

*/

bool LCVS::_is_ignored (const char* szFile)
{
  if (m_pchIgnore) {
    char* pch = m_pchIgnore;
    while (pch && *((short*) pch)) {
      if (fnmatch (pch + sizeof (short), szFile, 
		   FNM_NOESCAPE | FNM_PERIOD) == 0)
	return true;
      pch += sizeof (short) + *((short*) pch);
    }
  }

  if (g_pchIgnore) {
    char* pch = g_pchIgnore;
    while (pch && *((short*) pch)) {
      if (fnmatch (pch + sizeof (short), szFile, 
		   FNM_NOESCAPE | FNM_PERIOD) == 0)
	return true;
      pch += sizeof (short) + *((short*) pch);
    }
  }

  return false;
}


/* LCVS::parse_status

   parses the fetched filestatus information.  Note this this
   information may be a subset of all of the files in the controlled
   directory.

   FIXME: This parse routine may be prone to failure because of the
   inability of many string manipulation routines to accept byte
   limits.  We memory map the status file, but we do not prevent
   reading past the end of file.  However, more to the point, we
   should avoid scanning the file more than once.  The memory mapping
   makes it easy to do the kind of parsing we're used to, but we want
   to be able to parse in one pass.  For speed.

*/

void LCVS::parse_status (void) 
{
  int fh = -1;
  char* pbMap = NULL;
  struct stat stat;
  if (!::stat (m_szFileStatus, &stat) && stat.st_size
      && ((fh = open (m_szFileStatus, O_RDONLY)) != -1)
      && (pbMap = (char*) mmap (NULL, stat.st_size + 1, PROT_READ, 
				MAP_FILE | MAP_PRIVATE, fh, 0))) {
    int cchEnd;
    for (char* pb = pbMap; pb < pbMap + stat.st_size; pb += cchEnd +
	   ((pb[cchEnd] == '\n') ? 1 : 0)) {
      cchEnd = strcspn (pb, "\n");
      if (cchEnd < 5)
	continue;
      if (strncasecmp (pb, "File: ", 6) != 0)
	continue;

      char* pbFile = pb + 6;
      if (strncasecmp (pbFile, "no file ", 8) == 0)
	pbFile += 8;
      int iFile = m_pDir->find (pbFile, strcspn (pbFile, "\n \t"));
      if (iFile == -1)
	iFile = m_pDir->add_file (pbFile, strcspn (pbFile, "\n \t"));
      assert_ (iFile != -1);
      char* pbStatus = strstr (pbFile, "Status: ");
      assert_ (pbStatus < pb + cchEnd);
      pbStatus += 8;
      clear_flag (iFile, flagTransient);
      set_flag (iFile, flagControlled);
      if (     strncasecmp (pbStatus, "Up-to-date", 10) == 0)
	continue;
      else if (strncasecmp (pbStatus, "Needs Checkout", 14) == 0)
	set_flag (iFile, flagStatusCheckout);
      else if (strncasecmp (pbStatus, "Needs Patch", 11) == 0)
	set_flag (iFile, flagStatusPatch);
      else if (strncasecmp (pbStatus, "Needs Merge", 11) == 0)
	set_flag (iFile, flagStatusMerge);
      else if (strncasecmp (pbStatus, "File had conflicts on merge", 27) == 0)
	set_flag (iFile, flagStatusConflict);
      else if (strncasecmp (pbStatus, "Locally Modified", 16) == 0)
	set_flag (iFile, flagEdited);
      else if (strncasecmp (pbStatus, "Locally Added", 13) == 0)
	set_flag (iFile, flagAdded);
      else if (strncasecmp (pbStatus, "Locally Removed", 15) == 0)
	set_flag (iFile, flagRemoved);
      else if (strncasecmp (pbStatus, "Unknown", 7) == 0)
	continue;
      else if (strncasecmp (pbStatus, "Entry Invalid", 13) == 0)
	continue;
      else if (strncasecmp (pbStatus, "Unresolved Conflict", 19) == 0)
	continue;
      else
	assert_ (0);
    }
  }
  if (pbMap)
    munmap (pbMap, stat.st_size);
  if (fh != -1)
    close (fh);
}


/* LCVS::_parse_ignore

   processes the data from either a .cvsignore file or from the
   default list of ignored patterns.  The malloc'd buffer returned can
   be used to efficiently scan the patterns for matches.

   The output is a buffer of data structured with short (2 byte)
   counts of characters that offset to the next pattern.  If the count
   is zero, the end of the list has been achieved.

*/

char* LCVS::_parse_ignore (char* pch)
{
  int cbIgnore = 0;
  int cIgnore = 0;
  int ich = 0;
  do {
    int cch = strcspn (pch + ich, "\n ");
    if (cch) {
      cbIgnore += cch + 1;
      if (!(cch & 1))
	++cbIgnore;		// Permit aligned references to shorts
      ++cIgnore;
    }
    ich += cch;
    if (pch[ich])
      ++ich;
  } while (pch[ich]);
  char* pchIgnoreOrg
    = (char*) malloc (cbIgnore + sizeof (short)*(cIgnore + 1));
  ich = 0;
  char* pchIgnore = pchIgnoreOrg;
  do {
    int cch = strcspn (pch + ich, "\n ");
    if (cch) {
      *((short*) pchIgnore) = (short) cch + 1 + ((cch & 1) ? 0 : 1);
      pchIgnore += sizeof (short);
      strncpy (pchIgnore, pch + ich, cch);
      pchIgnore[cch] = 0;
      pchIgnore += cch + 1;
      if (!(cch & 1))
	++pchIgnore;
    }
    ich += cch;
    if (pch[ich])
      ++ich;
  } while (pch[ich]);
  *((short*) pchIgnore) = 0;	// Terminate list

  return pchIgnoreOrg;
}


/* LCVS::recover_time
   
   recovers the time_t value from a string describing the time in the
   form: Wed Sep 27 01:57:06 1995.

*/

time_t LCVS::recover_time (char* pch, int cch)
{
  //  static const char* m_rgszDay[] = { "...
  struct tm tm;
  memset (&tm, 0, sizeof (tm));
  char sz[80];

  if (cch > (int) sizeof (sz) - 1)
    return 0;
  strncpy (sz, pch, cch);
  sz[cch] = 0;
  pch = sz;
  int ich;
				// Month
  ich = strcspn (pch, " ");
  if (!*pch)
    return 0;
  pch += ich + 1;
  pch[3] = 0;
  static const char* m_szMonths = "JanFebMarAprMayJunJulAugSepOctNovDec";
  tm.tm_mon = ((char*) strstr (m_szMonths, pch) - m_szMonths)/3;

				// Date
  pch += 4;
  while (*pch == ' ')
    ++pch;
  tm.tm_mday = atoi (pch);

				// Hour
  pch += strcspn (pch, " ") + 1;
  tm.tm_hour = atoi (pch);

				// Minute
  pch += strcspn (pch, ":") + 1;
  tm.tm_min = atoi (pch);

				// Second
  pch += strcspn (pch, ":") + 1;
  tm.tm_sec = atoi (pch);

				// Year
  pch += strcspn (pch, " ") + 1;
  tm.tm_year = atoi (pch) - 1900;

  return mktime (&tm);
}


void LCVS::release_filestatus (void)
{
  if (m_szFileStatus) {
    unlink (m_szFileStatus);
    free (m_szFileStatus);
    m_szFileStatus = NULL;
  }
}

void LCVS::release_this (void)
{
  release_filestatus ();

  if (m_pchIgnore) {
    free (m_pchIgnore);
    m_pchIgnore = NULL;
  }
}

/* LCVS::set_version

   stores the file version with the rest of the file status
   information.  FIXME: this is not yet implemented.  We need to
   attach a structure to the directory entry instead of using the
   void* as a bitmask.  Ho hum.  At least we check for new entries so
   we can avoid the time-consuming search for an invalid date.

*/

int LCVS::set_version (char* pch, int cch)
{
  if (cch == 1 && *pch == '0')
    return 0;
  return 1;
}