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"
37 int rename_journal(const char* path
, time_t* last_rotate
)
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 */
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
);
56 if ( 0 != (ext
= strrchr(path
, '.')) )
58 strncpy(base
, path
, ext
- path
);
59 base
[ext
- path
] = '\0';
67 snprintf(newpath
, sizeof(newpath
), "%s.%s.%ld.%ld%s",
68 base
, timebfr
, *last_rotate
, now
, ext
);
73 LOG_INF("Naming new journal file \"%s\".\n", newpath
);
76 if ( rename(path
, newpath
) < 0 )
79 LOG_ER("rename: %s: - '%s' -> '%s'\n",
80 strerror_r(errno
,buf
,sizeof(buf
)), path
, newpath
);
84 if ( chown(newpath
, arg_journal_uid
, -1) < 0 )
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
);