2007-08-10 19:24:45 +00:00
|
|
|
#include "log-internal.h"
|
2007-08-10 21:15:17 +00:00
|
|
|
#include "memory.h"
|
2007-08-10 19:24:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-08-10 21:15:17 +00:00
|
|
|
int tokulogger_find_next_unused_log_file(const char *directory, long long *result) {
|
2007-08-10 19:24:45 +00:00
|
|
|
DIR *d=opendir(directory);
|
|
|
|
long long max=-1;
|
|
|
|
struct dirent *de;
|
|
|
|
if (d==0) return errno;
|
|
|
|
while ((de=readdir(d))) {
|
|
|
|
if (de==0) return -errno;
|
|
|
|
long long thisl;
|
|
|
|
int r = sscanf(de->d_name, "log%llu.tokulog", &thisl);
|
|
|
|
if (r==1 && thisl>max) max=thisl;
|
|
|
|
}
|
|
|
|
*result=max+1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-10 21:15:17 +00:00
|
|
|
int tokulogger_create_and_open_logger (const char *directory) {
|
2007-08-10 19:24:45 +00:00
|
|
|
TAGMALLOC(TOKULOGGER, result);
|
|
|
|
if (result==0) return -1;
|
|
|
|
int r;
|
|
|
|
long long nexti;
|
2007-08-10 21:15:17 +00:00
|
|
|
r = tokulogger_find_next_unused_log_file(directory, &nexti);
|
2007-08-10 19:24:45 +00:00
|
|
|
if (r!=0) {
|
|
|
|
died0:
|
|
|
|
toku_free(result);
|
|
|
|
return nexti;
|
|
|
|
}
|
|
|
|
result->directory = toku_strdup(directory);
|
2007-08-10 21:15:17 +00:00
|
|
|
if (result->directory!=0) goto died0;
|
2007-08-10 19:24:45 +00:00
|
|
|
result->f = 0;
|
|
|
|
result->next_log_file_number = nexti;
|
|
|
|
result->n_in_buf = 0;
|
|
|
|
return 0;
|
|
|
|
}
|