Browse Source

* source/files : refactoring

master
Alex 'AdUser' Z 3 months ago
parent
commit
db19569b83
  1. 61
      src/sources/files.c

61
src/sources/files.c

@ -14,12 +14,15 @@
#define MODNAME "files" #define MODNAME "files"
#define FREE(x) { free(x); x = NULL; }
#define FCLOSE(x) { fclose(x); x = NULL; }
typedef struct f2b_file_t { typedef struct f2b_file_t {
struct f2b_file_t *next; struct f2b_file_t *next;
uint32_t lines; uint32_t lines;
uint32_t stag; uint32_t stag;
FILE *fd; FILE *fd;
char path[PATH_MAX]; char *path;
struct stat st; struct stat st;
} f2b_file_t; } f2b_file_t;
@ -34,34 +37,32 @@ struct _config {
#include "source.c" #include "source.c"
static bool static bool
file_open(f2b_file_t *file, const char *path) { file_open(const cfg_t *cfg, f2b_file_t *file) {
FILE *fd; FILE *fd;
struct stat st; struct stat st;
char buf[PATH_MAX] = "";
assert(file != NULL); assert(file != NULL);
assert(path != NULL || file->path[0] != '\0');
strlcpy(buf, path ? path : file->path, sizeof(buf));
memset(file, 0x0, sizeof(f2b_file_t)); if (stat(file->path, &st) != 0)
if (stat(buf, &st) != 0)
return false; return false;
if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) {
log_msg(cfg, error, "not a regular file/fifo: %s", file->path);
return false; return false;
}
if ((fd = fopen(buf, "r")) == NULL) if ((fd = fopen(file->path, "r")) == NULL) {
log_msg(cfg, error, "can't open file: %s -- %s", file->path, strerror(errno));
return false; return false;
}
if (S_ISREG(st.st_mode) && fseek(fd, 0, SEEK_END) < 0) { if (S_ISREG(st.st_mode) && fseek(fd, 0, SEEK_END) < 0) {
fclose(fd); log_msg(cfg, error, "fseek error: %s -- %s", file->path, strerror(errno));
FCLOSE(fd);
return false; return false;
} }
memcpy(&file->st, &st, sizeof(st)); memcpy(&file->st, &st, sizeof(st));
strlcpy(file->path, buf, sizeof(file->path));
file->fd = fd; file->fd = fd;
file->stag = fnv_32a_str(file->path, FNV1_32A_INIT); file->stag = fnv_32a_str(file->path, FNV1_32A_INIT);
@ -72,11 +73,8 @@ static void
file_close(f2b_file_t *file) { file_close(f2b_file_t *file) {
assert(file != NULL); assert(file != NULL);
if (file->fd == NULL) FCLOSE(file->fd);
return; FREE(file->path);
fclose(file->fd);
file->fd = NULL;
file->lines = 0; file->lines = 0;
memset(&file->st, 0, sizeof(struct stat)); memset(&file->st, 0, sizeof(struct stat));
} }
@ -169,17 +167,23 @@ start(cfg_t *cfg) {
return NULL; return NULL;
for (size_t i = 0; i < globbuf.gl_pathc; i++) { for (size_t i = 0; i < globbuf.gl_pathc; i++) {
if ((file = calloc(1, sizeof(f2b_file_t))) == NULL) if ((file = calloc(1, sizeof(f2b_file_t))) == NULL) {
log_msg(cfg, error, "can't allocate memory: %s", strerror(errno));
continue; continue;
if (file_open(file, globbuf.gl_pathv[i]) == false) { }
log_msg(cfg, error, "can't open file: %s -- %s", globbuf.gl_pathv[i], strerror(errno)); if ((file->path = strndup(globbuf.gl_pathv[i], PATH_MAX)) == NULL) {
free(file); FREE(file);
continue;
}
if (!file_open(cfg, file)) {
FREE(file->path);
FREE(file);
continue; continue;
} }
if (cfg->files == NULL) { if (cfg->files) {
file->next = cfg->files;
cfg->files = file; cfg->files = file;
} else { } else {
file->next = cfg->files;
cfg->files = file; cfg->files = file;
} }
} }
@ -197,7 +201,7 @@ stop(cfg_t *cfg) {
for (; cfg->files != NULL; cfg->files = next) { for (; cfg->files != NULL; cfg->files = next) {
next = cfg->files->next; next = cfg->files->next;
file_close(cfg->files); file_close(cfg->files);
free(cfg->files); FREE(cfg->files);
} }
return true; return true;
@ -215,11 +219,8 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
for (f2b_file_t *file = cfg->current; file != NULL; file = file->next) { for (f2b_file_t *file = cfg->current; file != NULL; file = file->next) {
if (file_rotated(cfg, file)) if (file_rotated(cfg, file))
file_close(file); file_close(file);
if (file->fd == NULL) { if (file->fd == NULL && !file_open(cfg, file))
if (!file_open(file, NULL))
log_msg(cfg, error, "can't open file: %s", file->path);
continue; continue;
}
if (file_getline(cfg, file, buf, bufsize)) if (file_getline(cfg, file, buf, bufsize))
return file->stag; return file->stag;
} }
@ -260,5 +261,5 @@ void
destroy(cfg_t *cfg) { destroy(cfg_t *cfg) {
assert(cfg != NULL); assert(cfg != NULL);
free(cfg); FREE(cfg);
} }

Loading…
Cancel
Save