-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsy.c
286 lines (256 loc) · 6.05 KB
/
bsy.c
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
/*
* You must be VERY CAREFUL with this module. Note, this
* code is working in VERY diff. ways in forking vs. threading versions!!
*/
/*
* $Id: bsy.c,v 2.12 2013/01/24 17:25:35 gul Exp $
*
* $Log: bsy.c,v $
* Revision 2.12 2013/01/24 17:25:35 gul
* Support "-pipe" option on Win32
*
* Revision 2.11 2010/06/15 20:24:48 gul
* Improve diagnostics
*
* Revision 2.10 2003/11/20 17:56:53 gul
* Delete empty zone outbound directories with "deletedirs"
*
* Revision 2.9 2003/10/29 21:08:38 gul
* Change include-files structure, relax dependences
*
* Revision 2.8 2003/08/26 22:18:47 gul
* Fix compilation under w32-mingw and os2-emx
*
* Revision 2.7 2003/08/26 21:01:09 gul
* Fix compilation under unix
*
* Revision 2.6 2003/08/26 16:06:26 stream
* Reload configuration on-the fly.
*
* Warning! Lot of code can be broken (Perl for sure).
* Compilation checked only under OS/2-Watcom and NT-MSVC (without Perl)
*
* Revision 2.5 2003/06/11 13:10:34 gul
* Do not try to remove bsy for 0:0/0 at exitlist
*
* Revision 2.4 2003/03/11 09:21:29 gul
* Fixed OS/2 Watcom compilation
*
* Revision 2.3 2003/03/03 22:11:27 gul
* Fix compilation by msvc/2
*
* Revision 2.2 2003/02/28 20:39:08 gul
* Code cleanup:
* change "()" to "(void)" in function declarations;
* change C++-style comments to C-style
*
* Revision 2.1 2002/05/11 08:37:32 gul
* Added token deletedirs
*
* Revision 2.0 2001/01/10 12:12:37 gul
* Binkd is under CVS again
*
*
*/
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "readcfg.h"
#include "bsy.h"
#include "ftnaddr.h"
#include "ftndom.h"
#include "sem.h"
#include "tools.h"
#include "iphdr.h"
#include "assert.h"
#include "readdir.h" /* for rmdir() */
#if defined(HAVE_THREADS) || defined(AMIGA)
static MUTEXSEM sem; /* =0 initializer fails for amiga. removed. useless anyway? */
#endif
typedef struct _BSY_ADDR BSY_ADDR;
struct _BSY_ADDR
{
BSY_ADDR *next;
FTN_ADDR fa;
bsy_t bt;
#ifndef UNIX
int h;
#endif
};
BSY_ADDR *bsy_list = 0;
void bsy_init (void)
{
InitSem (&sem);
}
void bsy_deinit (void)
{
CleanSem (&sem);
}
static BSY_ADDR *bsy_get_free_cell (void)
{
BSY_ADDR *lst;
for (lst = bsy_list; lst; lst = lst->next)
if (FA_ISNULL (&lst->fa))
break;
if (!lst)
{
lst = xalloc (sizeof (BSY_ADDR));
FA_ZERO (&lst->fa);
lst->next = bsy_list;
bsy_list = lst;
}
return lst;
}
int bsy_add (FTN_ADDR *fa0, bsy_t bt, BINKD_CONFIG *config)
{
char buf[MAXPATHLEN + 1];
int ok = 0;
ftnaddress_to_filename (buf, fa0, config);
LockSem (&sem);
if (*buf)
{
strnzcat (buf, bt == F_CSY ? ".csy" : ".bsy", sizeof (buf));
if (mkpath (buf) == -1)
Log (1, "mkpath('%s'): %s", buf, strerror (errno));
if (create_sem_file (buf, 5))
{
BSY_ADDR *new_bsy = bsy_get_free_cell ();
memcpy (&new_bsy->fa, fa0, sizeof (FTN_ADDR));
new_bsy->bt = bt;
#ifndef UNIX
new_bsy->h = open(buf, O_RDONLY|O_NOINHERIT);
if (new_bsy->h == -1)
Log (2, "Can't open %s: %s!", buf, strerror(errno));
#if defined(OS2)
else
DosSetFHState(new_bsy->h, OPEN_FLAGS_NOINHERIT);
#elif defined(EMX)
else
fcntl(new_bsy->h, F_SETFD, FD_CLOEXEC);
#endif
#endif
ok = 1;
}
}
ReleaseSem (&sem);
return ok;
}
/*
* Test a busy-flag. 1 -- free, 0 -- busy
*/
int bsy_test (FTN_ADDR *fa0, bsy_t bt, BINKD_CONFIG *config)
{
char buf[MAXPATHLEN + 1];
ftnaddress_to_filename (buf, fa0, config);
if (*buf)
{
strnzcat (buf, bt == F_CSY ? ".csy" : ".bsy", sizeof (buf));
if (mkpath (buf) == -1)
Log (1, "mkpath('%s'): %s", buf, strerror (errno));
if (access (buf, F_OK) == -1)
return 1;
}
return 0;
}
void bsy_remove (FTN_ADDR *fa0, bsy_t bt, BINKD_CONFIG *config)
{
char buf[MAXPATHLEN + 1], *p;
BSY_ADDR *bsy;
ftnaddress_to_filename (buf, fa0, config);
if (*buf)
{
strnzcat (buf, bt == F_CSY ? ".csy" : ".bsy", sizeof (buf));
LockSem (&sem);
for (bsy = bsy_list; bsy; bsy = bsy->next)
{
if (!ftnaddress_cmp (&bsy->fa, fa0) && bsy->bt == bt)
{
#ifndef UNIX
if (bsy->h != -1)
if (close(bsy->h))
Log (2, "Can't close %s (handle %d): %s!", buf, bsy->h, strerror(errno));
#endif
delete (buf);
/* remove empty point directory */
if (config->deletedirs)
{
FTN_DOMAIN *d;
if (fa0->p != 0 && (p = last_slash(buf)) != NULL)
{
*p = '\0';
rmdir(buf);
}
/* remove empty zone directory */
d = get_domain_info (fa0->domain, config->pDomains.first);
if (d && (fa0->z != d->z[0]) && (p = last_slash(buf)) != NULL)
{
*p = '\0';
rmdir(buf);
}
}
FA_ZERO (&bsy->fa);
break;
}
}
ReleaseSem (&sem);
}
}
/*
* For exitlist...
*/
void bsy_remove_all (BINKD_CONFIG *config)
{
char buf[MAXPATHLEN + 1], *p;
BSY_ADDR *bsy;
for (bsy = bsy_list; bsy; bsy = bsy->next)
{
if (FA_ISNULL (&bsy->fa)) continue; /* free cell */
ftnaddress_to_filename (buf, &bsy->fa, config);
if (*buf)
{
strnzcat (buf, bsy->bt == F_CSY ? ".csy" : ".bsy", sizeof (buf));
#ifndef UNIX
if (bsy->h != -1)
if (close(bsy->h))
Log (2, "Can't close %s (handle %d): %s!", buf, bsy->h, strerror(errno));
#endif
delete (buf);
/* remove empty point directory */
if (config->deletedirs && bsy->fa.p != 0 && (p = last_slash(buf)) != NULL)
{
*p = '\0';
rmdir(buf);
}
FA_ZERO (&bsy->fa);
}
}
Log (6, "bsy_remove_all: done");
bsy_deinit ();
}
/*
* Touchs all our .bsy's if needed
*/
void bsy_touch (BINKD_CONFIG *config)
{
static time_t last_touch = 0;
LockSem (&sem);
if (time (0) - last_touch > BSY_TOUCH_DELAY)
{
BSY_ADDR *bsy;
char buf[MAXPATHLEN + 1];
for (bsy = bsy_list; bsy; bsy = bsy->next)
{
ftnaddress_to_filename (buf, &bsy->fa, config);
if (*buf)
{
strnzcat (buf, bsy->bt == F_CSY ? ".csy" : ".bsy", sizeof (buf));
if (touch (buf, time (0)) == -1)
Log (1, "touch %s: %s", buf, strerror (errno));
else
Log (6, "touched %s", buf);
}
}
last_touch = time (0);
}
ReleaseSem (&sem);
}