Browse Source

* source/files : refactoring

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

61
src/sources/files.c

@ -14,12 +14,15 @@
#define MODNAME "files"
#define FREE(x) { free(x); x = NULL; }
#define FCLOSE(x) { fclose(x); x = NULL; }
typedef struct f2b_file_t {
struct f2b_file_t *next;
uint32_t lines;
uint32_t stag;
FILE *fd;
char path[PATH_MAX];
char *path;
struct stat st;
} f2b_file_t;
@ -34,34 +37,32 @@ struct _config {
#include "source.c"
static bool
file_open(f2b_file_t *file, const char *path) {
file_open(const cfg_t *cfg, f2b_file_t *file) {
FILE *fd;
struct stat st;
char buf[PATH_MAX] = "";
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(buf, &st) != 0)
if (stat(file->path, &st) != 0)
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;
}
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;
}
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;
}
memcpy(&file->st, &st, sizeof(st));
strlcpy(file->path, buf, sizeof(file->path));
file->fd = fd;
file->stag = fnv_32a_str(file->path, FNV1_32A_INIT);
@ -72,11 +73,8 @@ static void
file_close(f2b_file_t *file) {
assert(file != NULL);
if (file->fd == NULL)
return;
fclose(file->fd);
file->fd = NULL;
FCLOSE(file->fd);
FREE(file->path);
file->lines = 0;
memset(&file->st, 0, sizeof(struct stat));
}
@ -169,17 +167,23 @@ start(cfg_t *cfg) {
return NULL;
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;
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));
free(file);
}
if ((file->path = strndup(globbuf.gl_pathv[i], PATH_MAX)) == NULL) {
FREE(file);
continue;
}
if (!file_open(cfg, file)) {
FREE(file->path);
FREE(file);
continue;
}
if (cfg->files == NULL) {
if (cfg->files) {
file->next = cfg->files;
cfg->files = file;
} else {
file->next = cfg->files;
cfg->files = file;
}
}
@ -197,7 +201,7 @@ stop(cfg_t *cfg) {
for (; cfg->files != NULL; cfg->files = next) {
next = cfg->files->next;
file_close(cfg->files);
free(cfg->files);
FREE(cfg->files);
}
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) {
if (file_rotated(cfg, file))
file_close(file);
if (file->fd == NULL) {
if (!file_open(file, NULL))
log_msg(cfg, error, "can't open file: %s", file->path);
if (file->fd == NULL && !file_open(cfg, file))
continue;
}
if (file_getline(cfg, file, buf, bufsize))
return file->stag;
}
@ -260,5 +261,5 @@ void
destroy(cfg_t *cfg) {
assert(cfg != NULL);
free(cfg);
FREE(cfg);
}

Loading…
Cancel
Save