HomeClusterLabs Projects

Refactor: libcrmcommon: pcmk__epoch2str() accepts flags and is non-const

Description

Refactor: libcrmcommon: pcmk__epoch2str() accepts flags and is non-const

This allows us an easy way to use crm_time_as_string() with epoch time
inputs.

The existing pcmkepoch2str() returns a const char *. That's not
possible if the return value comes from crm_time_as_string(). So here we
change the return value to (non-const) char *. This gives the caller
ownership over the buffer, so that we don't have to worry about a second
call to pcmk
epoch2str() or ctime() overwriting a static buffer before
we're finished with the first call's return value.

An alternative would be to use a static string buffer in
pcmk__epoch2str() and return a const char * that points there. We could
also add an optional buffer argument, giving the caller the option to
pass their own buffer of size >= DATE_MAX instead of using the shared
static buffer. That way the caller has control over overwrites. We could
also make crm_time_as_string() a wrapper for an internal function that
behaves the same way.

That alternative is all very doable, and it would save some dynamic
memory overhead and a few store-and-free lines at the caller side.
However, pcmk__epoch2str() is not used in any performance-critical
paths, so it's not clear that that's worth doing.

As a final note, we remove the check for time(NULL) == (time_t) -1. It's
unclear whether this condition is possible -- the man page says it can't
happen on Linux but that POSIX permits it. However, we don't check the
time(NULL) return code anywhere else in Pacemaker that I can see.

Signed-off-by: Reid Wahl <nrwahl@protonmail.com>

Details

Provenance
nrwahl2Authored on Nov 1 2022, 3:15 PM
Parents
rP864ec8cb7466: Refactor: libcrmcommon: New pcmk__copy_timet() function
Branches
Unknown
Tags
Unknown