Must use (void) instead of () for function declarations.
[lwes-journaller.git] / src / rename_journal.c
blob340b11bdf50b819c95d84b83246310cf2032d305
1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
4 * *
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. *
9 * *
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. *
14 * *
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 *======================================================================*/
21 #include "config.h"
23 #include "rename_journal.h"
25 #include "log.h"
26 #include "sig.h"
27 #include "perror.h"
29 #include <limits.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <time.h>
35 int rename_journal(const char* path, time_t* last_rotate)
37 char empty[1] = "";
38 char* ext;
39 char base[PATH_MAX];
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 */
43 struct tm tm_now;
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);
50 return -1;
52 #endif
54 if ( 0 != (ext = strrchr(path, '.')) )
56 strncpy(base, path, ext - path);
57 base[ext - path] = '\0';
59 else
61 strcpy(base, path);
62 ext = empty;
65 if ( ! gbl_rotate ) // sink-ram rotate ?
67 ext = empty ;
70 snprintf(newpath, sizeof(newpath), "%s.%s.%ld.%ld%s",
71 base, timebfr, *last_rotate, now, ext);
72 *last_rotate = now;
74 if ( gbl_rotate )
76 LOG_INF("Naming new journal file \"%s\".\n", newpath);
79 if ( rename(path, newpath) < 0 )
81 char buf[100] ;
82 LOG_ER("rename: %s: - '%s' -> '%s'\n",
83 strerror_r(errno,buf,sizeof(buf)), path, newpath);
84 return -1;
87 return 0;