Fix purging old log files with absolute file path

Removing old timestamp or increment versions of log backup files did
not work when the file is an absolute path: only the entry name was
provided to the file remove function.

The dirname was also bogus, since the file separater was put back too
soon.

Fix these issues to make log file rotation work when the file is
configured to be an absolute path.
This commit is contained in:
Matthijs Mekking 2023-04-05 17:50:12 +02:00
parent 25a3874e1f
commit 70629d73da

View file

@ -1036,18 +1036,11 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
isc_dir_init(&dir);
result = isc_dir_open(&dir, dirname);
/*
* Replace the file separator if it was taken out.
*/
if (bname != file->name) {
*(bname - 1) = sep;
}
/*
* Return if the directory open failed.
*/
if (result != ISC_R_SUCCESS) {
return (result);
goto out;
}
while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
@ -1061,14 +1054,25 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
* Remove any backup files that exceed versions.
*/
if (*digit_end == '\0' && version >= versions) {
result = isc_file_remove(dir.entry.name);
char rmfile[PATH_MAX + 1];
int n = snprintf(rmfile, sizeof(rmfile),
"%s/%s", dirname,
dir.entry.name);
if (n >= (int)sizeof(rmfile) || n < 0) {
result = ISC_R_NOSPACE;
syslog(LOG_ERR,
"unable to remove log files: %s",
isc_result_totext(result));
break;
}
result = isc_file_remove(rmfile);
if (result != ISC_R_SUCCESS &&
result != ISC_R_FILENOTFOUND)
result != ISC_R_NOTFOUND)
{
syslog(LOG_ERR,
"unable to remove "
"log file '%s': %s",
dir.entry.name,
"unable to remove log file "
"'%s': %s",
rmfile,
isc_result_totext(result));
}
} else if (*digit_end == '\0' && version > greatest) {
@ -1079,8 +1083,16 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
isc_dir_close(&dir);
*greatestp = greatest;
result = ISC_R_SUCCESS;
return (ISC_R_SUCCESS);
out:
/*
* Replace the file separator if it was taken out.
*/
if (bname != file->name) {
*(bname - 1) = sep;
}
return (result);
}
static void
@ -1168,18 +1180,11 @@ remove_old_tsversions(isc_logfile_t *file, int versions) {
isc_dir_init(&dir);
result = isc_dir_open(&dir, dirname);
/*
* Replace the file separator if it was taken out.
*/
if (bname != file->name) {
*(bname - 1) = sep;
}
/*
* Return if the directory open failed.
*/
if (result != ISC_R_SUCCESS) {
return (result);
goto out;
}
last = last_to_keep(versions, &dir, bname, bnamelen);
@ -1198,14 +1203,25 @@ remove_old_tsversions(isc_logfile_t *file, int versions) {
* Remove any backup files that exceed versions.
*/
if (*digit_end == '\0' && version < last) {
result = isc_file_remove(dir.entry.name);
char rmfile[PATH_MAX + 1];
int n = snprintf(rmfile, sizeof(rmfile),
"%s/%s", dirname,
dir.entry.name);
if (n >= (int)sizeof(rmfile) || n < 0) {
result = ISC_R_NOSPACE;
syslog(LOG_ERR,
"unable to remove log files: %s",
isc_result_totext(result));
break;
}
result = isc_file_remove(rmfile);
if (result != ISC_R_SUCCESS &&
result != ISC_R_FILENOTFOUND)
result != ISC_R_NOTFOUND)
{
syslog(LOG_ERR,
"unable to remove "
"log file '%s': %s",
dir.entry.name,
"unable to remove log file "
"'%s': %s",
rmfile,
isc_result_totext(result));
}
}
@ -1213,8 +1229,16 @@ remove_old_tsversions(isc_logfile_t *file, int versions) {
}
isc_dir_close(&dir);
result = ISC_R_SUCCESS;
return (ISC_R_SUCCESS);
out:
/*
* Replace the file separator if it was taken out.
*/
if (bname != file->name) {
*(bname - 1) = sep;
}
return (result);
}
static isc_result_t