You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.6 KiB

9 years ago
/* Copyright 2016 Alex 'AdUser' Z (ad_user@runbox.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
9 years ago
#include "common.h"
#include "logfile.h"
bool
f2b_logfile_open(f2b_logfile_t *file, const char *path) {
9 years ago
struct stat st;
char buf[PATH_MAX] = "";
9 years ago
assert(file != NULL);
assert(path != NULL || file->path[0] != '\0');
strlcpy(buf, path ? path : file->path, sizeof(buf));
9 years ago
memset(file, 0x0, sizeof(f2b_logfile_t));
if (stat(buf, &st) != 0)
9 years ago
return false;
if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode)))
return false;
if ((file->fd = fopen(buf, "r")) == NULL)
9 years ago
return false;
if (S_ISREG(st.st_mode) && fseek(file->fd, 0, SEEK_END) < 0)
9 years ago
return false;
memcpy(&file->st, &st, sizeof(st));
strlcpy(file->path, buf, sizeof(file->path));
file->opened = true;
9 years ago
return true;
}
void
f2b_logfile_close(f2b_logfile_t *file) {
assert(file != NULL);
if (file->fd)
fclose(file->fd);
file->opened = false;
file->fd = NULL;
}
9 years ago
bool
f2b_logfile_rotated(const f2b_logfile_t *file) {
struct stat st;
assert(file != NULL);
if (!file->opened)
return true;
if (stat(file->path, &st) != 0)
return true;
if (file->st.st_dev != st.st_dev ||
file->st.st_ino != st.st_ino ||
file->st.st_size > st.st_size)
return true;
return false;
}
9 years ago
bool
f2b_logfile_getline(const f2b_logfile_t *file, char *buf, size_t bufsize) {
if (fgets(buf, bufsize, file->fd) != NULL)
return true;
return false;
9 years ago
}