File: keepers.c

package info (click to toggle)
debfoster 2.7-2.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,188 kB
  • sloc: sh: 4,243; ansic: 2,397; perl: 39; makefile: 26; sed: 16
file content (111 lines) | stat: -rw-r--r-- 2,061 bytes parent folder | download | duplicates (8)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/file.h>

#include "AVLTree.h"
#include "error.h"
#include "conffile.h"
#include "symbol.h"
#include "status.h"
#include "keepers.h"

#define READBUF 4096

AVLTree *keepers = NULL, *nokeepers = NULL;
static int keeperfd = -1;

void openkeepers(int readonly) {
	if(readonly) {
		keeperfd = open(KeeperFile, O_RDONLY);
		if(keeperfd<0 && errno != ENOENT)
			perror_exit(ERROR_CONFIG, KeeperFile);
		return;
	}

	keeperfd = open(KeeperFile, O_RDWR|O_CREAT, 0666);
	if(keeperfd < 0)
		perror_exit(ERROR_CONFIG, KeeperFile);

	if(flock(keeperfd, LOCK_EX|LOCK_NB)) {
		if(getenv("DEBFOSTER_LOCK_SOFTFAIL"))
			exit(0);
		fprintf(stderr, "debfoster is unable to acquire a lock on the keeper file.\nAnother debfoster process probably has it.\n");
		perror_exit(ERROR_USER, KeeperFile);
	}

	if(setenv("DEBFOSTER_LOCK_SOFTFAIL", "", 0))
		perror_exit(ERROR_SYSTEM, "setenv()");

}

void closekeepers(void) {
	if(keeperfd < 0)
		return;

	flock(keeperfd, LOCK_UN);
	close(keeperfd);
	keeperfd = -1;
}

void writekeepers(void) {
	FILE *f;
	AVLNode *c;

	if(keeperfd < 0)
		return;

	ftruncate(keeperfd, 0);
	f = fdopen(dup(keeperfd), "w");
	if(!f)
		perror_exit(ERROR_SYSTEM, KeeperFile);
	rewind(f);

	for(c = keepers->head; c; c = c->next)
		fprintf(f, "%s\n", (char *)c->item);
	for(c = nokeepers->head; c; c = c->next)
		fprintf(f, "-%s\n", (char *)c->item);

	fclose(f);
}

void readkeepers(void) {
	FILE *f;
	char buf[READBUF], *s;
	symbol_t name;
	int keep;

	if(keeperfd < 0)
		return;

	f = fdopen(dup(keeperfd), "r");
	if(!f)
		perror_exit(ERROR_SYSTEM, KeeperFile);
	rewind(f);

	while(fgets(buf, READBUF, f)) {
		s = chop(buf);

		switch(*s) {
			case '-':
				s++;
				keep = 0;
			break;
			case '+':
				s++;
			default:
				keep = 1;
		}
		if (*s) {
			name = symbol(s);
			if(keep)
				AVLInsert(keepers, name);
			else if (NegativeKeepers && !AVLSearch(keepers, name)) /* sanity check */
				AVLInsert(nokeepers, name);
		}
	}

	fclose(f);
}