1
0
forked from bip/bip

Fixup file name allocation

This commit is contained in:
Arnaud Cornet 2008-12-20 20:26:38 +01:00
parent e8bb841e5f
commit fa78df6482
3 changed files with 49 additions and 13 deletions

View File

@ -29,7 +29,7 @@ int yywrap()
return 1; return 1;
} }
int yydebug = 1; //int yydebug = 1;
list_t *root_list; list_t *root_list;

View File

@ -14,7 +14,6 @@
#include "conf.h" #include "conf.h"
int linec; int linec;
#define YY_NO_UNPUT
#include "util.h" #include "util.h"
extern list_t *root_list; extern list_t *root_list;
void yyparse(void); void yyparse(void);
@ -23,14 +22,6 @@ int conf_error;
typedef struct bip bip_t; typedef struct bip bip_t;
extern bip_t *_bip; extern bip_t *_bip;
#if 0
/* SPANK ME WITH A SHOVEL */
static void yyunput(int c, char *buf);
void dummy_lex_FFS(void)
{
yyunput(0, NULL);
}
#endif
void conf_die(bip_t *, char *, ...); void conf_die(bip_t *, char *, ...);
int yyerror(char *err) int yyerror(char *err)

View File

@ -211,24 +211,66 @@ void log_reinit(logstore_t *store)
log_reset(store); log_reset(store);
} }
static char *filename_uniq(const char *filename)
{
struct stat filestat;
int i;
if (stat(filename, &filestat) != -1) {
char *buf = bip_malloc(strlen(filename) + 4 + 1);
for (i = 0; i < 256; i++) {
sprintf(buf, "%s.%d", filename, i);
if (stat(buf, &filestat) == -1)
return buf;
}
free(buf);
}
return bip_strdup(filename);
}
static int log_has_file(log_t *logdata, const char *fname)
{
hash_iterator_t hi;
list_iterator_t li;
logstore_t *store;
for (hash_it_init(&logdata->logfgs, &hi); hash_it_item(&hi);
hash_it_next(&hi)) {
store = hash_it_item(&hi);
for (list_it_init(&store->file_group, &li); list_it_item(&li);
list_it_next(&li)) {
struct logfile *lf = list_it_item(&li);
if (strcmp(fname, lf->filename) == 0)
return 1;
}
}
return 0;
}
static int log_add_file(log_t *logdata, const char *destination, static int log_add_file(log_t *logdata, const char *destination,
const char *filename) const char *filename)
{ {
FILE *f; FILE *f;
logfile_t *lf = NULL; logfile_t *lf = NULL;
logstore_t *store; logstore_t *store;
char *uniq_fname;
if (conf_log) { if (conf_log) {
f = fopen(filename, "a+"); if (log_has_file(logdata, filename))
uniq_fname = filename_uniq(filename);
else
uniq_fname = bip_strdup(filename);
f = fopen(uniq_fname, "a+");
if (!f) { if (!f) {
mylog(LOG_ERROR, "fopen(%s) %s", filename, mylog(LOG_ERROR, "fopen(%s) %s", uniq_fname,
strerror(errno)); strerror(errno));
free(uniq_fname);
return 0; return 0;
} }
lf = bip_malloc(sizeof(logfile_t)); lf = bip_malloc(sizeof(logfile_t));
lf->file = f; lf->file = f;
lf->filename = bip_strdup(filename); lf->filename = uniq_fname;
fseek(lf->file, 0, SEEK_END); fseek(lf->file, 0, SEEK_END);
if (ftell(f) < 0) if (ftell(f) < 0)
@ -416,8 +458,11 @@ void log_nick(log_t *logdata, const char *ircmask, const char *channel,
const char *newnick) const char *newnick)
{ {
char *oldnick = nick_from_ircmask(ircmask); char *oldnick = nick_from_ircmask(ircmask);
if (hash_includes(&logdata->logfgs, oldnick)) if (hash_includes(&logdata->logfgs, oldnick))
hash_rename_key(&logdata->logfgs, oldnick, newnick); hash_rename_key(&logdata->logfgs, oldnick, newnick);
free(oldnick);
snprintf(logdata->buffer, LOGLINE_MAXLEN, snprintf(logdata->buffer, LOGLINE_MAXLEN,
"%s -!- %s is now known as %s", "%s -!- %s is now known as %s",
timestamp(), ircmask, newnick); timestamp(), ircmask, newnick);