Nodelist processing cleanup

This commit is contained in:
Michiel Broek 2004-07-10 15:03:49 +00:00
parent 74e6c0db1b
commit 8802d940c7
4 changed files with 151 additions and 63 deletions

View File

@ -28,6 +28,7 @@ v0.61.1 20-Jun-2004.
Code cleanup for nodelist processing. Code cleanup for nodelist processing.
Added a extra check to see if nodelist datalines are valid to Added a extra check to see if nodelist datalines are valid to
prevent segfaults on Skynet nodelists. prevent segfaults on Skynet nodelists.
Does finally compile a username index.
mbnntp: mbnntp:
If compiled in full newsmode, this program acts like a dummy. If compiled in full newsmode, this program acts like a dummy.

3
TODO
View File

@ -139,9 +139,6 @@ mbfile:
mbaff: mbaff:
L: Rewrite filefind search algorithm. L: Rewrite filefind search algorithm.
mbindex:
X: Add usernames index.
mbsetup: mbsetup:
N: Add a check for double areatag names. N: Add a check for double areatag names.

View File

@ -40,14 +40,21 @@ typedef struct _nl_list {
} nl_list; } nl_list;
typedef struct _nl_user {
struct _nl_user *next;
struct _nlusr udx;
} nl_user;
#include "mbindex.h" #include "mbindex.h"
FILE *ifp, *ufp, *ffp; FILE *ifp, *ufp, *ffp;
long total = 0, entries = 0; long total = 0, entries = 0, users = 0;
int filenr = 0; int filenr = 0;
unsigned short regio; unsigned short regio;
nl_list *nll = NULL; nl_list *nll = NULL;
nl_user *nlu = NULL;
extern int do_quiet; /* Quiet flag */ extern int do_quiet; /* Quiet flag */
@ -289,6 +296,68 @@ void sort_nllist(nl_list **fap)
void tidy_nluser(nl_user **fap)
{
nl_user *tmp, *old;
for (tmp = *fap; tmp; tmp = old) {
old = tmp->next;
free(tmp);
}
*fap = NULL;
}
void fill_nluser(struct _nlusr udx, nl_user **fap)
{
nl_user *tmp;
tmp = (nl_user *)malloc(sizeof(nl_user));
tmp->next = *fap;
tmp->udx = udx;
*fap = tmp;
users++;
}
void sort_nluser(nl_user **fap)
{
nl_user *ta, **vector;
size_t n = 0, i;
if (*fap == NULL)
return;
for (ta = *fap; ta; ta = ta->next)
n++;
vector = (nl_user **)malloc(n * sizeof(nl_user *));
i = 0;
for (ta = *fap; ta; ta = ta->next) {
vector[i++] = ta;
}
qsort(vector, n, sizeof(nl_user *), (int(*)(const void*, const void *))comp_user);
(*fap) = vector[0];
i = 1;
for (ta = *fap; ta; ta = ta->next) {
if (i < n)
ta->next = vector[i++];
else
ta->next = NULL;
}
free(vector);
return;
}
int comp_node(nl_list **fap1, nl_list **fap2) int comp_node(nl_list **fap1, nl_list **fap2)
{ {
if ((*fap1)->idx.zone != (*fap2)->idx.zone) if ((*fap1)->idx.zone != (*fap2)->idx.zone)
@ -303,6 +372,13 @@ int comp_node(nl_list **fap1, nl_list **fap2)
int comp_user(nl_user **fap1, nl_user **fap2)
{
return strcmp((*fap1)->udx.user, (*fap2)->udx.user);
}
int compile(char *nlname, unsigned short zo, unsigned short ne, unsigned short no) int compile(char *nlname, unsigned short zo, unsigned short ne, unsigned short no)
{ {
int num, i, lineno, boss = FALSE, bossvalid = FALSE; int num, i, lineno, boss = FALSE, bossvalid = FALSE;
@ -525,8 +601,10 @@ int compile(char *nlname, unsigned short zo, unsigned short ne, unsigned short n
*/ */
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
p = q; p = q;
if (p == NULL) if (p == NULL) {
WriteError("%s(%u): invalid dataline", nlname,lineno);
continue; continue;
}
if ((q = strchr(p, ','))) if ((q = strchr(p, ',')))
*q++ = '\0'; *q++ = '\0';
if (q == NULL) if (q == NULL)
@ -539,15 +617,16 @@ int compile(char *nlname, unsigned short zo, unsigned short ne, unsigned short n
udx.net = ndx.net; udx.net = ndx.net;
udx.node = ndx.node; udx.node = ndx.node;
udx.point = ndx.point; udx.point = ndx.point;
// FIXME: the record is filled, now do something with it!
/* /*
* Now search for the baudrate field, just to check if it's there. * Now search for the baudrate field, just to check if it's there.
*/ */
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
p = q; p = q;
if (p == NULL) if (p == NULL) {
WriteError("%s(%u): invalid dataline", nlname,lineno);
continue; continue;
}
if ((q = strchr(p, ','))) if ((q = strchr(p, ',')))
*q++ = '\0'; *q++ = '\0';
if (q == NULL) if (q == NULL)
@ -570,6 +649,7 @@ int compile(char *nlname, unsigned short zo, unsigned short ne, unsigned short n
} }
} else } else
fill_nllist(ndx, &nll); fill_nllist(ndx, &nll);
fill_nluser(udx, &nlu);
} }
fclose(nl); fclose(nl);
@ -710,16 +790,15 @@ int makelist(char *base, unsigned short zo, unsigned short ne, unsigned short no
if ((dp = opendir(CFG.nodelists)) == NULL) { if ((dp = opendir(CFG.nodelists)) == NULL) {
WriteError("$Can't open dir %s", CFG.nodelists); WriteError("$Can't open dir %s", CFG.nodelists);
rc = 103; rc = MBERR_GENERAL;
} else { } else {
while ((de = readdir(dp))) while ((de = readdir(dp))) {
if (strncmp(de->d_name, base, strlen(base)) == 0) { if (strncmp(de->d_name, base, strlen(base)) == 0) {
/* /*
* Extension must be at least 2 digits * Extension must be at least 2 digits
*/ */
q = (de->d_name) + strlen(base); q = (de->d_name) + strlen(base);
if ((*q == '.') && (strlen(q) > 2) && if ((*q == '.') && (strlen(q) > 2) && (strspn(q+1,"0123456789") == (strlen(q)-1))) {
(strspn(q+1,"0123456789") == (strlen(q)-1))) {
/* /*
* Add matched filenames to the array * Add matched filenames to the array
*/ */
@ -727,11 +806,12 @@ int makelist(char *base, unsigned short zo, unsigned short ne, unsigned short no
files++; files++;
} }
} }
}
closedir(dp); closedir(dp);
if (files == 0) { if (files == 0) {
Syslog('+', "No nodelist found for %s", base); Syslog('+', "No nodelist found for %s", base);
return 103; return MBERR_GENERAL;
} }
/* /*
@ -770,6 +850,7 @@ int nodebld(void)
struct _nlfil fdx; struct _nlfil fdx;
FILE *fp; FILE *fp;
nl_list *tmp; nl_list *tmp;
nl_user *tmu;
memset(&fdx, 0, sizeof(fdx)); memset(&fdx, 0, sizeof(fdx));
im = xstrcpy(fullpath((char *)"temp.index")); im = xstrcpy(fullpath((char *)"temp.index"));
@ -827,18 +908,24 @@ int nodebld(void)
fclose(fp); fclose(fp);
} }
fclose(ufp);
fclose(ffp); fclose(ffp);
if (rc == 0) { if (rc == 0) {
IsDoing("Sorting nodes"); IsDoing("Sorting nodes");
sort_nllist(&nll); sort_nllist(&nll);
IsDoing("Sorting users");
sort_nluser(&nlu);
IsDoing("Writing files"); IsDoing("Writing files");
for (tmp = nll; tmp; tmp = tmp->next) for (tmp = nll; tmp; tmp = tmp->next)
fwrite(&tmp->idx, sizeof(struct _nlidx), 1, ifp); fwrite(&tmp->idx, sizeof(struct _nlidx), 1, ifp);
fclose(ifp); fclose(ifp);
for (tmu = nlu; tmu; tmu = tmu->next)
fwrite(&tmu->udx, sizeof(struct _nlusr), 1, ufp);
fclose(ufp);
Syslog('+', "Compiled %d entries", total); Syslog('+', "Compiled %d entries", total);
/* /*

View File

@ -25,6 +25,9 @@ int makelist(char *, unsigned short, unsigned short, unsigned short);
void tidy_nllist(nl_list **); void tidy_nllist(nl_list **);
int in_nllist(struct _nlidx, nl_list **, int); int in_nllist(struct _nlidx, nl_list **, int);
void fill_nllist(struct _nlidx, nl_list **); void fill_nllist(struct _nlidx, nl_list **);
void tidy_nluser(nl_user **);
void fill_nluser(struct _nlusr, nl_user **);
int comp_user(nl_user **, nl_user **);
int comp_node(nl_list **, nl_list **); int comp_node(nl_list **, nl_list **);
int nodebld(void); int nodebld(void);