Add strbuf_local_buf() macro

This commit is contained in:
Andrew Bettison 2015-10-13 18:42:22 +10:30
parent 27d98a29fa
commit a060642fdb
18 changed files with 39 additions and 25 deletions

View File

@ -253,7 +253,7 @@ static int app_config_paths(const struct cli_parsed *parsed, struct cli_context
cli_field_name(context, "SERVAL_CACHE_PATH", ":"); cli_field_name(context, "SERVAL_CACHE_PATH", ":");
cli_put_string(context, path, "\n"); cli_put_string(context, path, "\n");
} }
strbuf sb = strbuf_local(path, sizeof path); strbuf sb = strbuf_local_buf(path);
strbuf_system_log_path(sb); strbuf_system_log_path(sb);
if (!strbuf_overrun(sb)) { if (!strbuf_overrun(sb)) {
cli_field_name(context, "SYSTEM_LOG_PATH", ":"); cli_field_name(context, "SYSTEM_LOG_PATH", ":");

View File

@ -598,7 +598,7 @@ dna_helper_enqueue(struct subscriber *source, mdp_port_t source_port, const char
return 0; return 0;
} }
char buffer[sizeof request_buffer]; char buffer[sizeof request_buffer];
strbuf b = strbuf_local(request_bufptr == request_buffer ? buffer : request_buffer, sizeof buffer); strbuf b = request_bufptr == request_buffer ? strbuf_local_buf(buffer) : strbuf_local_buf(request_buffer);
strbuf_tohex(b, SID_STRLEN, source->sid.binary); strbuf_tohex(b, SID_STRLEN, source->sid.binary);
strbuf_putc(b, '|'); strbuf_putc(b, '|');
strbuf_puts(b, did); strbuf_puts(b, did);

View File

@ -206,7 +206,7 @@ int create_serval_instance_dir()
char path[PATH_MAX]; char path[PATH_MAX];
// emkdire_info can log if paths don't exist, which will also try to create paths... // emkdire_info can log if paths don't exist, which will also try to create paths...
// so try to create logging folders first // so try to create logging folders first
strbuf sb = strbuf_local(path, sizeof path); strbuf sb = strbuf_local_buf(path);
strbuf_system_log_path(sb); strbuf_system_log_path(sb);
if (!strbuf_overrun(sb) && emkdirs_info(path, 0700) == -1) if (!strbuf_overrun(sb) && emkdirs_info(path, 0700) == -1)
ret = -1; ret = -1;

2
log.c
View File

@ -450,7 +450,7 @@ static void _open_log_file(_log_iterator *it)
if (_log_file_path == NULL) if (_log_file_path == NULL)
_log_file_path = getenv("SERVALD_LOG_FILE"); _log_file_path = getenv("SERVALD_LOG_FILE");
if (_log_file_path == NULL && !cf_limbo) { if (_log_file_path == NULL && !cf_limbo) {
strbuf sbfile = strbuf_local(_log_file_path_buf, sizeof _log_file_path_buf); strbuf sbfile = strbuf_local_buf(_log_file_path_buf);
strbuf_serval_log_path(sbfile); strbuf_serval_log_path(sbfile);
strbuf_path_join(sbfile, config.log.file.directory_path, "", NULL); // with trailing '/' strbuf_path_join(sbfile, config.log.file.directory_path, "", NULL); // with trailing '/'
_compute_file_start_time(it); _compute_file_start_time(it);

View File

@ -37,7 +37,7 @@ int logDump(int level, struct __sourceloc whence, char *name, const unsigned cha
if (name) if (name)
logMessage(level, whence, "Dump of %s", name); logMessage(level, whence, "Dump of %s", name);
for(i = 0; i < len; i += 16) { for(i = 0; i < len; i += 16) {
strbuf b = strbuf_local(buf, sizeof buf); strbuf b = strbuf_local_buf(buf);
strbuf_sprintf(b, " %04zx :", i); strbuf_sprintf(b, " %04zx :", i);
int j; int j;
for (j = 0; j < 16 && i + j < len; j++) for (j = 0; j < 16 && i + j < len; j++)
@ -208,7 +208,7 @@ int logBacktrace(int level, struct __sourceloc whence)
int status = 0; int status = 0;
if (waitpid(child_pid, &status, 0) == -1) if (waitpid(child_pid, &status, 0) == -1)
WHY_perror("waitpid"); WHY_perror("waitpid");
strbuf b = strbuf_local(buf, sizeof buf); strbuf b = strbuf_local_buf(buf);
strbuf_append_exit_status(b, status); strbuf_append_exit_status(b, status);
logMessage(level, __NOWHERE__, "gdb %s", buf); logMessage(level, __NOWHERE__, "gdb %s", buf);
unlink(tempfile); unlink(tempfile);

View File

@ -470,7 +470,7 @@ static int app_route_print(const struct cli_parsed *parsed, struct cli_context *
cli_put_string(context, alloca_tohex_sid_t(p->sid), ":"); cli_put_string(context, alloca_tohex_sid_t(p->sid), ":");
char flags[32]; char flags[32];
strbuf b = strbuf_local(flags, sizeof flags); strbuf b = strbuf_local_buf(flags);
switch (p->reachable){ switch (p->reachable){
case REACHABLE_SELF: case REACHABLE_SELF:

View File

@ -912,7 +912,7 @@ static int send_local_packet(int fd, const uint8_t *bytes, size_t len, const cha
{ {
struct socket_address addr; struct socket_address addr;
strbuf d = strbuf_local(addr.local.sun_path, sizeof addr.local.sun_path); strbuf d = strbuf_local_buf(addr.local.sun_path);
strbuf_path_join(d, folder, file, NULL); strbuf_path_join(d, folder, file, NULL);
if (strbuf_overrun(d)) if (strbuf_overrun(d))
return WHYF("interface file name overrun: %s", alloca_str_toprint(strbuf_str(d))); return WHYF("interface file name overrun: %s", alloca_str_toprint(strbuf_str(d)));

View File

@ -698,7 +698,7 @@ int overlay_mdp_dnalookup_reply(struct subscriber *dest, mdp_port_t dest_port,
/* build reply as TOKEN|URI|DID|NAME|<NUL> */ /* build reply as TOKEN|URI|DID|NAME|<NUL> */
char buff[256]; char buff[256];
strbuf b = strbuf_local(buff, sizeof buff); strbuf b = strbuf_local_buf(buff);
strbuf_tohex(b, SID_STRLEN, resolved_sid->sid.binary); strbuf_tohex(b, SID_STRLEN, resolved_sid->sid.binary);
strbuf_sprintf(b, "|%s|%s|%s|", uri, did, name?name:""); strbuf_sprintf(b, "|%s|%s|%s|", uri, did, name?name:"");
if (strbuf_overrun(b)) if (strbuf_overrun(b))

View File

@ -1325,7 +1325,7 @@ void _rhizome_manifest_free(struct __sourceloc __whence, rhizome_manifest *m)
static int rhizome_manifest_pack_variables(rhizome_manifest *m) static int rhizome_manifest_pack_variables(rhizome_manifest *m)
{ {
assert(m->var_count <= NELS(m->vars)); assert(m->var_count <= NELS(m->vars));
strbuf sb = strbuf_local((char*)m->manifestdata, sizeof m->manifestdata); strbuf sb = strbuf_local_buf(m->manifestdata);
unsigned i; unsigned i;
for (i = 0; i < m->var_count; ++i) { for (i = 0; i < m->var_count; ++i) {
strbuf_puts(sb, m->vars[i]); strbuf_puts(sb, m->vars[i]);

View File

@ -318,7 +318,7 @@ int rhizome_opendb()
All changes should attempt to preserve all existing interesting data */ All changes should attempt to preserve all existing interesting data */
char buf[UUID_STRLEN + 1]; char buf[UUID_STRLEN + 1];
int r = sqlite_exec_strbuf_retry(&retry, strbuf_local(buf, sizeof buf), "SELECT uuid from IDENTITY LIMIT 1;", END); int r = sqlite_exec_strbuf_retry(&retry, strbuf_local_buf(buf), "SELECT uuid from IDENTITY LIMIT 1;", END);
if (r == -1) if (r == -1)
RETURN(-1); RETURN(-1);
if (r) { if (r) {
@ -1579,7 +1579,7 @@ enum rhizome_bundle_status rhizome_find_duplicate(const rhizome_manifest *m, rhi
if (m->service == NULL) if (m->service == NULL)
return WHY("Manifest has no service"); return WHY("Manifest has no service");
char sqlcmd[1024]; char sqlcmd[1024];
strbuf b = strbuf_local(sqlcmd, sizeof sqlcmd); strbuf b = strbuf_local_buf(sqlcmd);
strbuf_puts(b, "SELECT id, manifest, author FROM manifests WHERE filesize = ? AND service = ?"); strbuf_puts(b, "SELECT id, manifest, author FROM manifests WHERE filesize = ? AND service = ?");
assert(m->filesize != RHIZOME_SIZE_UNSET); assert(m->filesize != RHIZOME_SIZE_UNSET);
if (m->filesize > 0) if (m->filesize > 0)

View File

@ -488,7 +488,7 @@ static int rhizome_sync_with_peers(int mode, int peer_count, const struct config
const struct config_rhizome_peer *peer = peers[peer_number]; const struct config_rhizome_peer *peer = peers[peer_number];
if (strcasecmp(peer->protocol, "http") != 0) if (strcasecmp(peer->protocol, "http") != 0)
return WHYF("Unsupported Rhizome Direct protocol %s", alloca_str_toprint(peer->protocol)); return WHYF("Unsupported Rhizome Direct protocol %s", alloca_str_toprint(peer->protocol));
strbuf h = strbuf_local(state->host, sizeof state->host); strbuf h = strbuf_local_buf(state->host);
strbuf_puts(h, peer->host); strbuf_puts(h, peer->host);
if (strbuf_overrun(h)) if (strbuf_overrun(h))
return WHYF("Rhizome Direct host name too long: %s", alloca_str_toprint(peer->host)); return WHYF("Rhizome Direct host name too long: %s", alloca_str_toprint(peer->host));

View File

@ -503,7 +503,7 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r)
char boundary[20]; char boundary[20];
char buffer[8192]; char buffer[8192];
strbuf bb = strbuf_local(boundary, sizeof boundary); strbuf bb = strbuf_local_buf(boundary);
strbuf_sprintf(bb, "%08lx%08lx", random(), random()); strbuf_sprintf(bb, "%08lx%08lx", random(), random());
assert(!strbuf_overrun(bb)); assert(!strbuf_overrun(bb));
strbuf content_preamble = strbuf_alloca(200); strbuf content_preamble = strbuf_alloca(200);
@ -522,7 +522,7 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r)
+ r->cursor->buffer_offset_bytes + r->cursor->buffer_offset_bytes
+ r->cursor->buffer_used + r->cursor->buffer_used
+ strbuf_len(content_postamble); + strbuf_len(content_postamble);
strbuf request = strbuf_local(buffer, sizeof buffer); strbuf request = strbuf_local_buf(buffer);
strbuf_sprintf(request, strbuf_sprintf(request,
"POST /rhizome/enquiry HTTP/1.0\r\n" "POST /rhizome/enquiry HTTP/1.0\r\n"
"Content-Length: %d\r\n" "Content-Length: %d\r\n"

View File

@ -511,7 +511,7 @@ schedule_fetch(struct rhizome_fetch_slot *slot)
slot->manifest->dataFileName = NULL; slot->manifest->dataFileName = NULL;
slot->manifest->dataFileUnlinkOnFree = 0; slot->manifest->dataFileUnlinkOnFree = 0;
strbuf r = strbuf_local(slot->request, sizeof slot->request); strbuf r = strbuf_local_buf(slot->request);
strbuf_sprintf(r, "GET /rhizome/file/%s HTTP/1.0\r\n", alloca_tohex_rhizome_filehash_t(slot->manifest->filehash)); strbuf_sprintf(r, "GET /rhizome/file/%s HTTP/1.0\r\n", alloca_tohex_rhizome_filehash_t(slot->manifest->filehash));
if (slot->manifest->is_journal){ if (slot->manifest->is_journal){
@ -569,7 +569,7 @@ schedule_fetch(struct rhizome_fetch_slot *slot)
status_ok: status_ok:
; ;
} else { } else {
strbuf r = strbuf_local(slot->request, sizeof slot->request); strbuf r = strbuf_local_buf(slot->request);
strbuf_sprintf(r, "GET /rhizome/manifestbyprefix/%s HTTP/1.0\r\n\r\n", alloca_tohex(slot->bid.binary, slot->prefix_length)); strbuf_sprintf(r, "GET /rhizome/manifestbyprefix/%s HTTP/1.0\r\n\r\n", alloca_tohex(slot->bid.binary, slot->prefix_length));
if (strbuf_overrun(r)) if (strbuf_overrun(r))
RETURN(WHY("request overrun")); RETURN(WHY("request overrun"));

View File

@ -83,7 +83,7 @@ static int rhizome_status_page(httpd_request *r, const char *remainder)
if (r->http.verb != HTTP_VERB_GET) if (r->http.verb != HTTP_VERB_GET)
return 405; return 405;
char buf[32*1024]; char buf[32*1024];
strbuf b = strbuf_local(buf, sizeof buf); strbuf b = strbuf_local_buf(buf);
strbuf_puts(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>"); strbuf_puts(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>");
strbuf_sprintf(b, "%d HTTP requests<br>", current_httpd_request_count); strbuf_sprintf(b, "%d HTTP requests<br>", current_httpd_request_count);
strbuf_sprintf(b, "%d Bundles transferring via MDP<br>", rhizome_cache_count()); strbuf_sprintf(b, "%d Bundles transferring via MDP<br>", rhizome_cache_count());

View File

@ -21,7 +21,7 @@ static int root_page(httpd_request *r, const char *remainder)
if (r->http.verb != HTTP_VERB_GET) if (r->http.verb != HTTP_VERB_GET)
return 405; return 405;
char temp[8192]; char temp[8192];
strbuf b = strbuf_local(temp, sizeof temp); strbuf b = strbuf_local_buf(temp);
strbuf_sprintf(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>" strbuf_sprintf(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>"
"<h1>Hello, I'm %s*</h1>", "<h1>Hello, I'm %s*</h1>",
alloca_tohex_sid_t_trunc(my_subscriber->sid, 16)); alloca_tohex_sid_t_trunc(my_subscriber->sid, 16));
@ -64,7 +64,7 @@ static int neighbour_page(httpd_request *r, const char *remainder)
if (r->http.verb != HTTP_VERB_GET) if (r->http.verb != HTTP_VERB_GET)
return 405; return 405;
char buf[8*1024]; char buf[8*1024];
strbuf b = strbuf_local(buf, sizeof buf); strbuf b = strbuf_local_buf(buf);
sid_t neighbour_sid; sid_t neighbour_sid;
if (str_to_sid_t(&neighbour_sid, remainder) == -1) if (str_to_sid_t(&neighbour_sid, remainder) == -1)
return 404; return 404;
@ -85,7 +85,7 @@ static int interface_page(httpd_request *r, const char *remainder)
if (r->http.verb != HTTP_VERB_GET) if (r->http.verb != HTTP_VERB_GET)
return 405; return 405;
char buf[8*1024]; char buf[8*1024];
strbuf b=strbuf_local(buf, sizeof buf); strbuf b=strbuf_local_buf(buf);
int index=atoi(remainder); int index=atoi(remainder);
if (index<0 || index>=OVERLAY_MAX_INTERFACES) if (index<0 || index>=OVERLAY_MAX_INTERFACES)
return 404; return 404;

View File

@ -155,7 +155,7 @@ static void recv_packet(int fd, struct network *network, struct peer *destinatio
DEBUGF(verbose, "New peer %s", alloca_socket_address(&addr)); DEBUGF(verbose, "New peer %s", alloca_socket_address(&addr));
struct socket_address unicast_addr; struct socket_address unicast_addr;
unicast_addr.local.sun_family=AF_UNIX; unicast_addr.local.sun_family=AF_UNIX;
strbuf d = strbuf_local(unicast_addr.local.sun_path, sizeof unicast_addr.local.sun_path); strbuf d = strbuf_local_buf(unicast_addr.local.sun_path);
static unsigned peerid=0; static unsigned peerid=0;
strbuf_sprintf(d, "%s/peer%d", network->path, peerid++); strbuf_sprintf(d, "%s/peer%d", network->path, peerid++);
if (strbuf_overrun(d)) { if (strbuf_overrun(d)) {
@ -413,7 +413,7 @@ static int console_create(const struct cli_parsed *parsed, struct cli_context *U
struct socket_address addr; struct socket_address addr;
addr.local.sun_family=AF_UNIX; addr.local.sun_family=AF_UNIX;
strbuf b = strbuf_local(addr.local.sun_path, sizeof addr.local.sun_path); strbuf b = strbuf_local_buf(addr.local.sun_path);
strbuf_path_join(b, path, "broadcast", NULL); strbuf_path_join(b, path, "broadcast", NULL);
if (strbuf_overrun(b)) if (strbuf_overrun(b))
return WHY("Path too long"); return WHY("Path too long");

View File

@ -220,13 +220,27 @@ typedef const struct strbuf *const_strbuf;
* strbuf b = strbuf_local(buf, len); * strbuf b = strbuf_local(buf, len);
* strbuf_puts(b, "some text"); * strbuf_puts(b, "some text");
* strbuf_puts(b, " some more text"); * strbuf_puts(b, " some more text");
* printf("%s\n", strbuf_str(b));
* } * }
* *
* @author Andrew Bettison <andrew@servalproject.com> * @author Andrew Bettison <andrew@servalproject.com>
*/ */
#define strbuf_local(buf,len) strbuf_init(alloca(SIZEOF_STRBUF), (buf), (len)) #define strbuf_local(buf,len) strbuf_init(alloca(SIZEOF_STRBUF), (buf), (len))
/** Convenience variant of the strbuf_local() macro that computes the 'len'
* parameter from 'sizeof buf'.
*
* void print_integer(int value) {
* char temp[20];
* strbuf b = strbuf_local_buf(temp);
* strbuf_puts(b, "[");
* strbuf_sprintf(b, "%d", value);
* strbuf_puts(b, "]");
* printf("%s\n", temp);
* }
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
#define strbuf_local_buf(buf) strbuf_local((char*)(buf), sizeof (buf))
/** Initialise a strbuf with a caller-supplied backing buffer. The current /** Initialise a strbuf with a caller-supplied backing buffer. The current
* backing buffer and its contents are forgotten, and all strbuf operations * backing buffer and its contents are forgotten, and all strbuf operations

2
vomp.c
View File

@ -618,7 +618,7 @@ static int vomp_update_local_state(struct vomp_call_state *call, int new_state){
unsigned char our_codecs[CODEC_FLAGS_LENGTH]; unsigned char our_codecs[CODEC_FLAGS_LENGTH];
char msg[256]; char msg[256];
monitor_get_all_supported_codecs(our_codecs); monitor_get_all_supported_codecs(our_codecs);
strbuf b = strbuf_local(msg, sizeof msg); strbuf b = strbuf_local_buf(msg);
strbuf_sprintf(b, "\nCODECS:%06x", call->local.session); strbuf_sprintf(b, "\nCODECS:%06x", call->local.session);
for (i = 0; i < 256; ++i){ for (i = 0; i < 256; ++i){