1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 *======================================================================*/
23 #include "rename_journal.h"
35 int rename_journal(const char* path
, time_t* last_rotate
)
40 char newpath
[PATH_MAX
];
41 char timebfr
[100]; /* Needs to be big enough for strftime below */
42 time_t now
= time(NULL
); /* get current time */
45 #ifndef WIN32 //TODO: someday figure out localtime_r() on WIN32, but not needed now
46 if ( strftime(timebfr
, sizeof(timebfr
), "%Y%m%d%H%M%S",
47 localtime_r(&now
, &tm_now
)) == 0 )
49 LOG_ER("strftime failed in rename_journal(\"%s\", ...)\n", path
);
54 if ( 0 != (ext
= strrchr(path
, '.')) )
56 strncpy(base
, path
, ext
- path
);
57 base
[ext
- path
] = '\0';
65 if ( ! gbl_rotate
) // sink-ram rotate ?
70 snprintf(newpath
, sizeof(newpath
), "%s.%s.%ld.%ld%s",
71 base
, timebfr
, *last_rotate
, now
, ext
);
76 LOG_INF("Naming new journal file \"%s\".\n", newpath
);
79 if ( rename(path
, newpath
) < 0 )
82 LOG_ER("rename: %s: - '%s' -> '%s'\n",
83 strerror_r(errno
,buf
,sizeof(buf
)), path
, newpath
);