]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
Update the Microsoft Visual Studio build to match the MinGW32 build.
[bacula/bacula] / bacula / src / lib / res.c
1 /*
2  *  This file handles locking and seaching resources
3  *
4  *     Kern Sibbald, January MM
5  *       Split from parse_conf.c April MMV
6  *
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2006 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
20    the file LICENSE for additional details.
21
22  */
23
24 #include "bacula.h"
25
26 /* Each daemon has a slightly different set of
27  * resources, so it will define the following
28  * global values.
29  */
30 extern int r_first;
31 extern int r_last;
32 extern RES_TABLE resources[];
33 extern RES **res_head;
34
35 brwlock_t res_lock;                   /* resource lock */
36 static int res_locked = 0;            /* set when resource chains locked -- for debug */
37
38
39 /* #define TRACE_RES */
40
41 void b_LockRes(const char *file, int line)
42 {
43    int errstat;
44 #ifdef TRACE_RES
45    Pmsg4(000, "LockRes  locked=%d w_active=%d at %s:%d\n", 
46          res_locked, res_lock.w_active, file, line);
47     if (res_locked) {
48        Pmsg2(000, "LockRes writerid=%d myid=%d\n", res_lock.writer_id,
49           pthread_self());
50      }
51 #endif
52    if ((errstat=rwl_writelock(&res_lock)) != 0) {
53       Emsg3(M_ABORT, 0, _("rwl_writelock failure at %s:%d:  ERR=%s\n"),
54            file, line, strerror(errstat));
55    }
56    res_locked++;
57 }
58
59 void b_UnlockRes(const char *file, int line)
60 {
61    int errstat;
62    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
63       Emsg3(M_ABORT, 0, _("rwl_writeunlock failure at %s:%d:. ERR=%s\n"),
64            file, line, strerror(errstat));
65    }
66    res_locked--;
67 #ifdef TRACE_RES
68    Pmsg4(000, "UnLockRes locked=%d wactive=%d at %s:%d\n", 
69          res_locked, res_lock.w_active, file, line);
70 #endif
71 }
72
73 /*
74  * Return resource of type rcode that matches name
75  */
76 RES *
77 GetResWithName(int rcode, char *name)
78 {
79    RES *res;
80    int rindex = rcode - r_first;
81
82    LockRes();
83    res = res_head[rindex];
84    while (res) {
85       if (strcmp(res->name, name) == 0) {
86          break;
87       }
88       res = res->next;
89    }
90    UnlockRes();
91    return res;
92
93 }
94
95 /*
96  * Return next resource of type rcode. On first
97  * call second arg (res) is NULL, on subsequent
98  * calls, it is called with previous value.
99  */
100 RES *
101 GetNextRes(int rcode, RES *res)
102 {
103    RES *nres;
104    int rindex = rcode - r_first;
105
106    if (res == NULL) {
107       nres = res_head[rindex];
108    } else {
109       nres = res->next;
110    }
111    return nres;
112 }
113
114
115 /* Parser state */
116 enum parse_state {
117    p_none,
118    p_resource
119 };