~ubuntu-branches/debian/stretch/debfoster/stretch

« back to all changes in this revision

Viewing changes to src/keepers.c

  • Committer: Bazaar Package Importer
  • Author(s): Ivo Timmermans
  • Date: 2002-01-17 23:08:39 UTC
  • Revision ID: james.westby@ubuntu.com-20020117230839-s22xl5hew1z4s3r5
Tags: upstream-2.5
ImportĀ upstreamĀ versionĀ 2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <string.h>
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include <getopt.h>
 
6
#include <sys/file.h>
 
7
 
 
8
#include "AVLTree.h"
 
9
#include "error.h"
 
10
#include "conffile.h"
 
11
#include "symbol.h"
 
12
#include "status.h"
 
13
#include "keepers.h"
 
14
 
 
15
#define READBUF 4096
 
16
 
 
17
AVLTree *keepers = NULL, *nokeepers = NULL;
 
18
static int keeperfd = -1;
 
19
 
 
20
void openkeepers(int readonly) {
 
21
        if(readonly) {
 
22
                keeperfd = open(KeeperFile, O_RDONLY);
 
23
                if(keeperfd<0 && errno != ENOENT)
 
24
                        perror_exit(ERROR_CONFIG, KeeperFile);
 
25
                return;
 
26
        }
 
27
 
 
28
        keeperfd = open(KeeperFile, O_RDWR|O_CREAT, 0666);
 
29
        if(keeperfd < 0)
 
30
                perror_exit(ERROR_CONFIG, KeeperFile);
 
31
 
 
32
        if(flock(keeperfd, LOCK_EX|LOCK_NB)) {
 
33
                if(getenv("DEBFOSTER_LOCK_SOFTFAIL"))
 
34
                        exit(0);
 
35
                fprintf(stderr, "debfoster is unable to acquire a lock on the keeper file.\nAnother debfoster process probably has it.\n");
 
36
                perror_exit(ERROR_USER, KeeperFile);
 
37
        }
 
38
 
 
39
        if(setenv("DEBFOSTER_LOCK_SOFTFAIL", "", 0))
 
40
                perror_exit(ERROR_SYSTEM, "setenv()");
 
41
 
 
42
}
 
43
 
 
44
void closekeepers(void) {
 
45
        if(keeperfd < 0)
 
46
                return;
 
47
 
 
48
        flock(keeperfd, LOCK_UN);
 
49
        close(keeperfd);
 
50
        keeperfd = -1;
 
51
}
 
52
 
 
53
void writekeepers(void) {
 
54
        FILE *f;
 
55
        AVLNode *c;
 
56
 
 
57
        if(keeperfd < 0)
 
58
                return;
 
59
 
 
60
        ftruncate(keeperfd, 0);
 
61
        f = fdopen(dup(keeperfd), "w");
 
62
        if(!f)
 
63
                perror_exit(ERROR_SYSTEM, KeeperFile);
 
64
        rewind(f);
 
65
 
 
66
        for(c = keepers->head; c; c = c->next)
 
67
                fprintf(f, "%s\n", (char *)c->item);
 
68
        for(c = nokeepers->head; c; c = c->next)
 
69
                fprintf(f, "-%s\n", (char *)c->item);
 
70
 
 
71
        fclose(f);
 
72
}
 
73
 
 
74
void readkeepers(void) {
 
75
        FILE *f;
 
76
        char buf[READBUF], *s;
 
77
        symbol_t name;
 
78
        int keep;
 
79
 
 
80
        if(keeperfd < 0)
 
81
                return;
 
82
 
 
83
        f = fdopen(dup(keeperfd), "r");
 
84
        if(!f)
 
85
                perror_exit(ERROR_SYSTEM, KeeperFile);
 
86
        rewind(f);
 
87
 
 
88
        while(fgets(buf, READBUF, f)) {
 
89
                s = chop(buf);
 
90
 
 
91
                switch(*s) {
 
92
                        case '-':
 
93
                                s++;
 
94
                                keep = 0;
 
95
                        break;
 
96
                        case '+':
 
97
                                s++;
 
98
                        default:
 
99
                                keep = 1;
 
100
                }
 
101
                if (*s) {
 
102
                        name = symbol(s);
 
103
                        if(keep)
 
104
                                AVLInsert(keepers, name);
 
105
                        else if (NegativeKeepers && !AVLSearch(keepers, name)) /* sanity check */
 
106
                                AVLInsert(nokeepers, name);
 
107
                }
 
108
        }
 
109
 
 
110
        fclose(f);
 
111
}