Improve Serval instance path operations

Replace form_serval_instance_path(), which takes a single 'path'
argument, with formf_serval_instance_path() and
vformf_serval_instance_path(), which take printf(3)-like arguments.
This supports more flexible instance path file names.

FORM_SERVAL_INSTANCE_PATH() macro works as before, but reimplemented
This commit is contained in:
Andrew Bettison 2013-09-19 17:19:37 +09:30
parent f3cd0d8718
commit 4937757d36
2 changed files with 20 additions and 4 deletions

View File

@ -43,10 +43,25 @@ void serval_setinstancepath(const char *instancepath)
thisinstancepath = strdup(instancepath);
}
int form_serval_instance_path(char *buf, size_t bufsiz, const char *path)
int formf_serval_instance_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vformf_serval_instance_path(__whence, buf, bufsiz, fmt, ap);
va_end(ap);
return ret;
}
int vformf_serval_instance_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, va_list ap)
{
strbuf b = strbuf_local(buf, bufsiz);
strbuf_path_join(b, serval_instancepath(), path, NULL);
strbuf_va_vprintf(b, fmt, ap);
if (!strbuf_overrun(b) && strbuf_len(b) && buf[0] != '/') {
strbuf_reset(b);
strbuf_puts(b, serval_instancepath());
strbuf_putc(b, '/');
strbuf_va_vprintf(b, fmt, ap);
}
if (!strbuf_overrun(b))
return 1;
WHYF("instance path overflow (strlen %lu, sizeof buffer %lu): %s",

View File

@ -161,11 +161,12 @@ extern const char version_servald[];
* is in scope (so that sizeof(buf) will work). Evaluates to true if the pathname fitted into
* the provided buffer, false (0) otherwise (after logging an error).
*/
#define FORM_SERVAL_INSTANCE_PATH(buf, path) (form_serval_instance_path(buf, sizeof(buf), (path)))
#define FORM_SERVAL_INSTANCE_PATH(buf, path) (formf_serval_instance_path(__WHENCE__, buf, sizeof(buf), "%s", (path)))
const char *serval_instancepath();
int create_serval_instance_dir();
int form_serval_instance_path(char *buf, size_t bufsiz, const char *path);
int formf_serval_instance_path(struct __sourceloc, char *buf, size_t bufsiz, const char *fmt, ...) __attribute__((format(printf,4,5)));
int vformf_serval_instance_path(struct __sourceloc, char *buf, size_t bufsiz, const char *fmt, va_list);
void serval_setinstancepath(const char *instancepath);
/* Basic socket operations.