Merging serial model from branch; converting to the hideous indentation style of...
[lwes-journaller.git] / src / rename_journal.c
blob8b2090e62c6b61f1d582551f9dc966ad533abd99
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"
28 #include "opt.h"
30 #include <limits.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <time.h>
35 #include <unistd.h>
37 int rename_journal(const char* path, time_t* last_rotate)
39 char empty[1] = "";
40 char* ext;
41 char base[PATH_MAX];
42 char newpath[PATH_MAX];
43 char timebfr[100]; /* Needs to be big enough for strftime below */
44 time_t now = time(NULL); /* get current time */
45 struct tm tm_now;
47 #ifndef WIN32 //TODO: someday figure out localtime_r() on WIN32, but not needed now
48 if ( strftime(timebfr, sizeof(timebfr), "%Y%m%d%H%M%S",
49 localtime_r(&now, &tm_now)) == 0 )
51 LOG_ER("strftime failed in rename_journal(\"%s\", ...)\n", path);
52 return -1;
54 #endif
56 if ( 0 != (ext = strrchr(path, '.')) )
58 strncpy(base, path, ext - path);
59 base[ext - path] = '\0';
61 else
63 strcpy(base, path);
64 ext = empty;
67 snprintf(newpath, sizeof(newpath), "%s.%s.%ld.%ld%s",
68 base, timebfr, *last_rotate, now, ext);
69 *last_rotate = now;
71 if ( gbl_rotate )
73 LOG_INF("Naming new journal file \"%s\".\n", newpath);
76 if ( rename(path, newpath) < 0 )
78 char buf[100] ;
79 LOG_ER("rename: %s: - '%s' -> '%s'\n",
80 strerror_r(errno,buf,sizeof(buf)), path, newpath);
81 return -1;
84 if ( chown(newpath, arg_journal_uid, -1) < 0 )
86 char buf[100] ;
87 LOG_ER("rename: %s: - '%s' could not be granted to uid %d\n",
88 strerror_r(errno,buf,sizeof(buf)), newpath, arg_journal_uid);
89 return -1;
92 return 0;