summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rwxr-xr-xsrc/util.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/src/util.c b/src/util.c
index 34b3739..b69a166 100755
--- a/src/util.c
+++ b/src/util.c
@@ -891,7 +891,13 @@ BackupFile(CHAR *szFile)
Routine description:
Backup a file to file.000. If file.000 already exists, back it up
- to file.001 etc...
+ to file.001 etc... szFile may include a directory path; the
+ numeric prefix is inserted before the basename, not the whole
+ path (a bare "%03u%s" prepend onto a full path like
+ "/usr/local/tmp/typhoon.log" produces the bogus relative path
+ "000/usr/local/tmp/typhoon.log", whose "000" directory component
+ doesn't exist -- that's what used to make this silently fail
+ whenever the logfile path had a directory in it).
Note: this function is recursive and can require quite a lot of
stack space. Also it is full of race conditions and should not be
@@ -911,25 +917,32 @@ Return value:
ULONG u;
CHAR buf[SMALL_STRING_LEN_CHAR];
CHAR *p;
+ CHAR *szBase;
+ ULONG uDirLen;
if (TRUE == SystemDoesFileExist(szFile))
{
- if ((strlen(szFile) > 3) &&
- (isdigit(szFile[0])) &&
- (isdigit(szFile[1])) &&
- (isdigit(szFile[2])))
+ szBase = strrchr(szFile, '/');
+ szBase = (NULL != szBase) ? (szBase + 1) : szFile;
+ uDirLen = (ULONG)(szBase - szFile);
+
+ if ((strlen(szBase) > 3) &&
+ (isdigit(szBase[0])) &&
+ (isdigit(szBase[1])) &&
+ (isdigit(szBase[2])))
{
- u = (szFile[0] - '0') * 100;
- u += (szFile[1] - '0') * 10;
- u += szFile[2] - '0' + 1;
- p = &(szFile[3]);
+ u = (szBase[0] - '0') * 100;
+ u += (szBase[1] - '0') * 10;
+ u += szBase[2] - '0' + 1;
+ p = &(szBase[3]);
}
else
{
u = 0;
- p = szFile;
+ p = szBase;
}
- snprintf(buf, ARRAY_LENGTH(buf), "%03u%s", u, p);
+ snprintf(buf, ARRAY_LENGTH(buf), "%.*s%03u%s",
+ (int)uDirLen, szFile, u, p);
if (TRUE == SystemDoesFileExist(buf))
{
BackupFile(buf);