mirror of
https://github.com/troglobit/finit.git
synced 2026-11-20 19:41:51 +00:00
Followup to #66: Gzip log files >= .2
This patch to the logrotate() function extends the log rotattion of a file with a gzip of .2 and older log files. If gzip is not available in $PATH then @num files are left uncompressed. Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
9f7604848c
commit
5ea1ac7582
1 file changed
+31
-5
+31
-5
@@ -36,7 +36,7 @@
|
|||||||
#define _PATH_BTMP "/var/log/btmp"
|
#define _PATH_BTMP "/var/log/btmp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MAX_NO 2
|
#define MAX_NO 5
|
||||||
#define MAX_SZ 100 * 1024
|
#define MAX_SZ 100 * 1024
|
||||||
|
|
||||||
static void utmp_strncpy(char *dst, const char *src, size_t dlen)
|
static void utmp_strncpy(char *dst, const char *src, size_t dlen)
|
||||||
@@ -51,8 +51,15 @@ static void utmp_strncpy(char *dst, const char *src, size_t dlen)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef LOGROTATE_ENABLED
|
#ifdef LOGROTATE_ENABLED
|
||||||
|
/*
|
||||||
|
* This function triggers a log rotates of @file when size >= @sz bytes
|
||||||
|
* At most @num old versions are kept and by default it starts gzipping
|
||||||
|
* .2 and older log files. If gzip is not available in $PATH then @num
|
||||||
|
* files are kept uncompressed.
|
||||||
|
*/
|
||||||
static int logrotate(char *file, int num, off_t sz)
|
static int logrotate(char *file, int num, off_t sz)
|
||||||
{
|
{
|
||||||
|
int cnt;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (stat(file, &st))
|
if (stat(file, &st))
|
||||||
@@ -60,18 +67,37 @@ static int logrotate(char *file, int num, off_t sz)
|
|||||||
|
|
||||||
if (sz > 0 && S_ISREG(st.st_mode) && st.st_size > sz) {
|
if (sz > 0 && S_ISREG(st.st_mode) && st.st_size > sz) {
|
||||||
if (num > 0) {
|
if (num > 0) {
|
||||||
size_t len = strlen(file) + 3 + 1;
|
size_t len = strlen(file) + 10 + 1;
|
||||||
char ofile[len];
|
char ofile[len];
|
||||||
char nfile[len];
|
char nfile[len];
|
||||||
|
|
||||||
while (num) {
|
/* First age zipped log files */
|
||||||
snprintf(ofile, len, "%s.%d", file, num - 1);
|
for (cnt = num; cnt > 2; cnt--) {
|
||||||
snprintf(nfile, len, "%s.%d", file, num--);
|
snprintf(ofile, len, "%s.%d.gz", file, cnt - 1);
|
||||||
|
snprintf(nfile, len, "%s.%d.gz", file, cnt);
|
||||||
|
|
||||||
/* May fail because ofile doesn't exist yet, ignore. */
|
/* May fail because ofile doesn't exist yet, ignore. */
|
||||||
(void)rename(ofile, nfile);
|
(void)rename(ofile, nfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (cnt = num; cnt > 0; cnt--) {
|
||||||
|
snprintf(ofile, len, "%s.%d", file, cnt - 1);
|
||||||
|
snprintf(nfile, len, "%s.%d", file, cnt);
|
||||||
|
|
||||||
|
/* May fail because ofile doesn't exist yet, ignore. */
|
||||||
|
(void)rename(ofile, nfile);
|
||||||
|
|
||||||
|
if (cnt == 2 && fexist(nfile)) {
|
||||||
|
size_t len = 5 + strlen(nfile) + 1;
|
||||||
|
char cmd[len];
|
||||||
|
|
||||||
|
snprintf(cmd, len, "gzip %s", nfile);
|
||||||
|
run(cmd);
|
||||||
|
|
||||||
|
remove(nfile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rename(file, nfile);
|
rename(file, nfile);
|
||||||
create(file, st.st_mode, st.st_uid, st.st_gid);
|
create(file, st.st_mode, st.st_uid, st.st_gid);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in new issue
Block a user