summaryrefslogtreecommitdiffstats
path: root/contrib/cvs/src/fileattr.c
blob: bae03a661d9d84ccd9df6815b26112ac755a7ec3 (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
/* Implementation for file attribute munging features.

   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, 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.  */

#include "cvs.h"
#include "getline.h"
#include "fileattr.h"
#include <assert.h>

static void fileattr_read PROTO ((void));
static int writeattr_proc PROTO ((Node *, void *));

/* Where to look for CVSREP_FILEATTR.  */
static char *fileattr_stored_repos;

/* The in-memory attributes.  */
static List *attrlist;
static char *fileattr_default_attrs;
/* We have already tried to read attributes and failed in this directory
   (for example, there is no CVSREP_FILEATTR file).  */
static int attr_read_attempted;

/* Have the in-memory attributes been modified since we read them?  */
static int attrs_modified;

/* More in-memory attributes: linked list of unrecognized
   fileattr lines.  We pass these on unchanged.  */
struct unrecog {
    char *line;
    struct unrecog *next;
};
static struct unrecog *unrecog_head;

/* Note that if noone calls fileattr_get, this is very cheap.  No stat(),
   no open(), no nothing.  */
void
fileattr_startdir (repos)
    char *repos;
{
    assert (fileattr_stored_repos == NULL);
    fileattr_stored_repos = xstrdup (repos);
    assert (attrlist == NULL);
    attr_read_attempted = 0;
    assert (unrecog_head == NULL);
}

static void
fileattr_delproc (node)
    Node *node;
{
    assert (node->data != NULL);
    free (node->data);
    node->data = NULL;
}

/* Read all the attributes for the current directory into memory.  */
static void
fileattr_read ()
{
    char *fname;
    FILE *fp;
    char *line = NULL;
    size_t line_len = 0;

    /* If there are no attributes, don't waste time repeatedly looking
       for the CVSREP_FILEATTR file.  */
    if (attr_read_attempted)
	return;

    /* If NULL was passed to fileattr_startdir, then it isn't kosher to look
       at attributes.  */
    assert (fileattr_stored_repos != NULL);

    fname = xmalloc (strlen (fileattr_stored_repos)
		     + 1
		     + sizeof (CVSREP_FILEATTR)
		     + 1);

    strcpy (fname, fileattr_stored_repos);
    strcat (fname, "/");
    strcat (fname, CVSREP_FILEATTR);

    attr_read_attempted = 1;
    fp = CVS_FOPEN (fname, FOPEN_BINARY_READ);
    if (fp == NULL)
    {
	if (!existence_error (errno))
	    error (0, errno, "cannot read %s", fname);
	free (fname);
	return;
    }
    attrlist = getlist ();
    while (1) {
	int nread;
	nread = getline (&line, &line_len, fp);
	if (nread < 0)
	    break;
	/* Remove trailing newline.  */
	line[nread - 1] = '\0';
	if (line[0] == 'F')
	{
	    char *p;
	    Node *newnode;

	    p = strchr (line, '\t');
	    if (p == NULL)
		error (1, 0,
		       "file attribute database corruption: tab missing in %s",
		       fname);
	    *p++ = '\0';
	    newnode = getnode ();
	    newnode->type = FILEATTR;
	    newnode->delproc = fileattr_delproc;
	    newnode->key = xstrdup (line + 1);
	    newnode->data = xstrdup (p);
	    if (addnode (attrlist, newnode) != 0)
		/* If the same filename appears twice in the file, discard
		   any line other than the first for that filename.  This
		   is the way that CVS has behaved since file attributes
		   were first introduced.  */
		freenode (newnode);
	}
	else if (line[0] == 'D')
	{
	    char *p;
	    /* Currently nothing to skip here, but for future expansion,
	       ignore anything located here.  */
	    p = strchr (line, '\t');
	    if (p == NULL)
		error (1, 0,
		       "file attribute database corruption: tab missing in %s",
		       fname);
	    ++p;
	    fileattr_default_attrs = xstrdup (p);
	}
	else
	{
	    /* Unrecognized type, we want to just preserve the line without
	       changing it, for future expansion.  */
	    struct unrecog *new;

	    new = (struct unrecog *) xmalloc (sizeof (struct unrecog));
	    new->line = xstrdup (line);
	    new->next = unrecog_head;
	    unrecog_head = new;
	}
    }
    if (ferror (fp))
	error (0, errno, "cannot read %s", fname);
    if (line != NULL)
	free (line);
    if (fclose (fp) < 0)
	error (0, errno, "cannot close %s", fname);
    attrs_modified = 0;
    free (fname);
}

char *
fileattr_get (filename, attrname)
    const char *filename;
    const char *attrname;
{
    Node *node;
    size_t attrname_len = strlen (attrname);
    char *p;

    if (attrlist == NULL)
	fileattr_read ();
    if (attrlist == NULL)
	/* Either nothing has any attributes, or fileattr_read already printed
	   an error message.  */
	return NULL;

    if (filename == NULL)
	p = fileattr_default_attrs;
    else
    {
	node = findnode (attrlist, filename);
	if (node == NULL)
	    /* A file not mentioned has no attributes.  */
	    return NULL;
	p = node->data;
    }
    while (p)
    {
	if (strncmp (attrname, p, attrname_len) == 0
	    && p[attrname_len] == '=')
	{
	    /* Found it.  */
	    return p + attrname_len + 1;
	}
	p = strchr (p, ';');
	if (p == NULL)
	    break;
	++p;
    }
    /* The file doesn't have this attribute.  */
    return NULL;
}

char *
fileattr_get0 (filename, attrname)
    const char *filename;
    const char *attrname;
{
    char *cp;
    char *cpend;
    char *retval;

    cp = fileattr_get (filename, attrname);
    if (cp == NULL)
	return NULL;
    cpend = strchr (cp, ';');
    if (cpend == NULL)
	cpend = cp + strlen (cp);
    retval = xmalloc (cpend - cp + 1);
    strncpy (retval, cp, cpend - cp);
    retval[cpend - cp] = '\0';
    return retval;
}

char *
fileattr_modify (list, attrname, attrval, namevalsep, entsep)
    char *list;
    const char *attrname;
    const char *attrval;
    int namevalsep;
    int entsep;
{
    char *retval;
    char *rp;
    size_t attrname_len = strlen (attrname);

    /* Portion of list before the attribute to be replaced.  */
    char *pre;
    char *preend;
    /* Portion of list after the attribute to be replaced.  */
    char *post;

    char *p;
    char *p2;

    p = list;
    pre = list;
    preend = NULL;
    /* post is NULL unless set otherwise.  */
    post = NULL;
    p2 = NULL;
    if (list != NULL)
    {
	while (1) {
	    p2 = strchr (p, entsep);
	    if (p2 == NULL)
	    {
		p2 = p + strlen (p);
		if (preend == NULL)
		    preend = p2;
	    }
	    else
		++p2;
	    if (strncmp (attrname, p, attrname_len) == 0
		&& p[attrname_len] == namevalsep)
	    {
		/* Found it.  */
		preend = p;
		if (preend > list)
		    /* Don't include the preceding entsep.  */
		    --preend;

		post = p2;
	    }
	    if (p2[0] == '\0')
		break;
	    p = p2;
	}
    }
    if (post == NULL)
	post = p2;

    if (preend == pre && attrval == NULL && post == p2)
	return NULL;

    retval = xmalloc ((preend - pre)
		      + 1
		      + (attrval == NULL ? 0 : (attrname_len + 1
						+ strlen (attrval)))
		      + 1
		      + (p2 - post)
		      + 1);
    if (preend != pre)
    {
	strncpy (retval, pre, preend - pre);
	rp = retval + (preend - pre);
	if (attrval != NULL)
	    *rp++ = entsep;
	*rp = '\0';
    }
    else
	retval[0] = '\0';
    if (attrval != NULL)
    {
	strcat (retval, attrname);
	rp = retval + strlen (retval);
	*rp++ = namevalsep;
	strcpy (rp, attrval);
    }
    if (post != p2)
    {
	rp = retval + strlen (retval);
	if (preend != pre || attrval != NULL)
	    *rp++ = entsep;
	strncpy (rp, post, p2 - post);
	rp += p2 - post;
	*rp = '\0';
    }
    return retval;
}

void
fileattr_set (filename, attrname, attrval)
    const char *filename;
    const char *attrname;
    const char *attrval;
{
    Node *node;
    char *p;

    if (filename == NULL)
    {
	p = fileattr_modify (fileattr_default_attrs, attrname, attrval,
			     '=', ';');
	if (fileattr_default_attrs != NULL)
	    free (fileattr_default_attrs);
	fileattr_default_attrs = p;
	attrs_modified = 1;
	return;
    }
    if (attrlist == NULL)
	fileattr_read ();
    if (attrlist == NULL)
    {
	/* Not sure this is a graceful way to handle things
	   in the case where fileattr_read was unable to read the file.  */
        /* No attributes existed previously.  */
	attrlist = getlist ();
    }

    node = findnode (attrlist, filename);
    if (node == NULL)
    {
	if (attrval == NULL)
	    /* Attempt to remove an attribute which wasn't there.  */
	    return;

	/* First attribute for this file.  */
	node = getnode ();
	node->type = FILEATTR;
	node->delproc = fileattr_delproc;
	node->key = xstrdup (filename);
	node->data = xmalloc (strlen (attrname) + 1 + strlen (attrval) + 1);
	strcpy (node->data, attrname);
	strcat (node->data, "=");
	strcat (node->data, attrval);
	addnode (attrlist, node);
    }

    p = fileattr_modify (node->data, attrname, attrval, '=', ';');
    if (p == NULL)
	delnode (node);
    else
    {
	free (node->data);
	node->data = p;
    }

    attrs_modified = 1;
}

char *
fileattr_getall (filename)
    const char *filename;
{
    Node *node;
    char *p;

    if (attrlist == NULL)
	fileattr_read ();
    if (attrlist == NULL)
	/* Either nothing has any attributes, or fileattr_read already printed
	   an error message.  */
	return NULL;

    if (filename == NULL)
	p = fileattr_default_attrs;
    else
    {
	node = findnode (attrlist, filename);
	if (node == NULL)
	    /* A file not mentioned has no attributes.  */
	    return NULL;
	p = node->data;
    }
    return xstrdup (p);
}

void
fileattr_setall (filename, attrs)
    const char *filename;
    const char *attrs;
{
    Node *node;

    if (filename == NULL)
    {
	if (fileattr_default_attrs != NULL)
	    free (fileattr_default_attrs);
	fileattr_default_attrs = xstrdup (attrs);
	attrs_modified = 1;
	return;
    }
    if (attrlist == NULL)
	fileattr_read ();
    if (attrlist == NULL)
    {
	/* Not sure this is a graceful way to handle things
	   in the case where fileattr_read was unable to read the file.  */
        /* No attributes existed previously.  */
	attrlist = getlist ();
    }

    node = findnode (attrlist, filename);
    if (node == NULL)
    {
	/* The file had no attributes.  Add them if we have any to add.  */
	if (attrs != NULL)
	{
	    node = getnode ();
	    node->type = FILEATTR;
	    node->delproc = fileattr_delproc;
	    node->key = xstrdup (filename);
	    node->data = xstrdup (attrs);
	    addnode (attrlist, node);
	}
    }
    else
    {
	if (attrs == NULL)
	    delnode (node);
	else
	{
	    free (node->data);
	    node->data = xstrdup (attrs);
	}
    }

    attrs_modified = 1;
}

void
fileattr_newfile (filename)
    const char *filename;
{
    Node *node;

    if (attrlist == NULL)
	fileattr_read ();

    if (fileattr_default_attrs == NULL)
	return;

    if (attrlist == NULL)
    {
	/* Not sure this is a graceful way to handle things
	   in the case where fileattr_read was unable to read the file.  */
        /* No attributes existed previously.  */
	attrlist = getlist ();
    }

    node = getnode ();
    node->type = FILEATTR;
    node->delproc = fileattr_delproc;
    node->key = xstrdup (filename);
    node->data = xstrdup (fileattr_default_attrs);
    addnode (attrlist, node);
    attrs_modified = 1;
}

static int
writeattr_proc (node, data)
    Node *node;
    void *data;
{
    FILE *fp = (FILE *)data;
    fputs ("F", fp);
    fputs (node->key, fp);
    fputs ("\t", fp);
    fputs (node->data, fp);
    fputs ("\012", fp);
    return 0;
}

void
fileattr_write ()
{
    FILE *fp;
    char *fname;
    mode_t omask;
    struct unrecog *p;

    if (!attrs_modified)
	return;

    if (noexec)
	return;

    /* If NULL was passed to fileattr_startdir, then it isn't kosher to set
       attributes.  */
    assert (fileattr_stored_repos != NULL);

    fname = xmalloc (strlen (fileattr_stored_repos)
		     + 1
		     + sizeof (CVSREP_FILEATTR)
		     + 1);

    strcpy (fname, fileattr_stored_repos);
    strcat (fname, "/");
    strcat (fname, CVSREP_FILEATTR);

    if (list_isempty (attrlist)
	&& fileattr_default_attrs == NULL
	&& unrecog_head == NULL)
    {
	/* There are no attributes.  */
	if (unlink_file (fname) < 0)
	{
	    if (!existence_error (errno))
	    {
		error (0, errno, "cannot remove %s", fname);
	    }
	}

	/* Now remove CVSREP directory, if empty.  The main reason we bother
	   is that CVS 1.6 and earlier will choke if a CVSREP directory
	   exists, so provide the user a graceful way to remove it.  */
	strcpy (fname, fileattr_stored_repos);
	strcat (fname, "/");
	strcat (fname, CVSREP);
	if (CVS_RMDIR (fname) < 0)
	{
	    if (errno != ENOTEMPTY

		/* Don't know why we would be here if there is no CVSREP
		   directory, but it seemed to be happening anyway, so
		   check for it.  */
		&& !existence_error (errno))
		error (0, errno, "cannot remove %s", fname);
	}

	free (fname);
	return;
    }

    omask = umask (cvsumask);
    fp = CVS_FOPEN (fname, FOPEN_BINARY_WRITE);
    if (fp == NULL)
    {
	if (existence_error (errno))
	{
	    /* Maybe the CVSREP directory doesn't exist.  Try creating it.  */
	    char *repname;

	    repname = xmalloc (strlen (fileattr_stored_repos)
			       + 1
			       + sizeof (CVSREP)
			       + 1);
	    strcpy (repname, fileattr_stored_repos);
	    strcat (repname, "/");
	    strcat (repname, CVSREP);

	    if (CVS_MKDIR (repname, 0777) < 0 && errno != EEXIST)
	    {
		error (0, errno, "cannot make directory %s", repname);
		(void) umask (omask);
		free (repname);
		return;
	    }
	    free (repname);

	    fp = CVS_FOPEN (fname, FOPEN_BINARY_WRITE);
	}
	if (fp == NULL)
	{
	    error (0, errno, "cannot write %s", fname);
	    (void) umask (omask);
	    return;
	}
    }
    (void) umask (omask);

    /* First write the "F" attributes.  */
    walklist (attrlist, writeattr_proc, fp);

    /* Then the "D" attribute.  */
    if (fileattr_default_attrs != NULL)
    {
	fputs ("D\t", fp);
	fputs (fileattr_default_attrs, fp);
	fputs ("\012", fp);
    }

    /* Then any other attributes.  */
    for (p = unrecog_head; p != NULL; p = p->next)
    {
	fputs (p->line, fp);
	fputs ("\012", fp);
    }

    if (fclose (fp) < 0)
	error (0, errno, "cannot close %s", fname);
    attrs_modified = 0;
    free (fname);
}

void
fileattr_free ()
{
    /* Note that attrs_modified will ordinarily be zero, but there are
       a few cases in which fileattr_write will fail to zero it (if
       noexec is set, or error conditions).  This probably is the way
       it should be.  */
    dellist (&attrlist);
    if (fileattr_stored_repos != NULL)
	free (fileattr_stored_repos);
    fileattr_stored_repos = NULL;
    if (fileattr_default_attrs != NULL)
	free (fileattr_default_attrs);
    fileattr_default_attrs = NULL;
    while (unrecog_head)
    {
	struct unrecog *p = unrecog_head;
	unrecog_head = p->next;
	free (p->line);
	free (p);
    }
}
OpenPOWER on IntegriCloud