X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcommon%2Fcommon.c;h=c8890cd2cab5748690d8951fb4fea9aab3ce795d;hb=87c882849d73c8567c517d630d4fba6d48b555cb;hp=ed1d1c277f687d614d3795698e5e29044888d8b6;hpb=2c4f022eddb11b0fd4e164a6fdae169b008f2371;p=babeltrace.git diff --git a/src/common/common.c b/src/common/common.c index ed1d1c27..c8890cd2 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -49,7 +49,7 @@ #include #endif -#define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace2/plugins" +#define SYSTEM_PLUGIN_PATH BABELTRACE_PLUGINS_DIR #define HOME_ENV_VAR "HOME" #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace2/plugins" @@ -512,6 +512,7 @@ set_end_pos: error: if (output) { g_string_free(output, TRUE); + output = NULL; } end: @@ -672,7 +673,7 @@ struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url( at += end_pos; - /* :// */ + /* `://` */ if (strncmp(at, "://", 3) != 0) { if (error_buf) { snprintf(error_buf, error_buf_size, @@ -682,6 +683,7 @@ struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url( goto error; } + /* Skip `://` */ at += 3; /* Hostname */ @@ -731,12 +733,13 @@ struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url( } if (at[end_pos] == '\0') { + /* Relay daemon hostname and ports provided only */ goto end; } at += end_pos; - /* /host/ */ + /* `/host/` */ if (strncmp(at, "/host/", 6) != 0) { if (error_buf) { snprintf(error_buf, error_buf_size, @@ -760,9 +763,16 @@ struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url( } if (at[end_pos] == '\0') { - goto end; + if (error_buf) { + snprintf(error_buf, error_buf_size, + "Missing `/` after target hostname (`%s`)", + parts.target_hostname->str); + } + + goto error; } + /* Skip `/` */ at += end_pos + 1; /* Session name */ @@ -1115,9 +1125,7 @@ error: norm_path = NULL; } end: - if (tmp) { - free(tmp); - } + free(tmp); return norm_path; } #else @@ -1782,9 +1790,7 @@ end: g_strfreev(lines); } - if (line_words) { - g_strfreev(line_words); - } + BT_ASSERT(!line_words); if (tmp_line) { g_string_free(tmp_line, TRUE); @@ -1824,3 +1830,76 @@ end: return ret; } #endif /* __MINGW32__ */ + +BT_HIDDEN +int bt_common_g_string_append_printf(GString *str, const char *fmt, ...) +{ + va_list ap; + gsize len, allocated_len, available_len; + int print_len; + + /* str->len excludes \0. */ + len = str->len; + /* Explicitly exclude \0. */ + allocated_len = str->allocated_len - 1; + available_len = allocated_len - len; + + str->len = allocated_len; + va_start(ap, fmt); + print_len = vsnprintf(str->str + len, available_len + 1, fmt, ap); + va_end(ap); + if (print_len < 0) { + return print_len; + } + if (G_UNLIKELY(available_len < print_len)) { + /* Resize. */ + g_string_set_size(str, len + print_len); + va_start(ap, fmt); + print_len = vsprintf(str->str + len, fmt, ap); + va_end(ap); + } else { + str->len = len + print_len; + } + return print_len; +} + +BT_HIDDEN +int bt_common_append_file_content_to_g_string(GString *str, FILE *fp) +{ + const size_t chunk_size = 4096; + int ret = 0; + char *buf; + size_t read_len; + gsize orig_len = str->len; + + BT_ASSERT(str); + BT_ASSERT(fp); + buf = g_malloc(chunk_size); + if (!buf) { + ret = -1; + goto end; + } + + while (true) { + if (ferror(fp)) { + ret = -1; + goto end; + } + + if (feof(fp)) { + break; + } + + read_len = fread(buf, 1, chunk_size, fp); + g_string_append_len(str, buf, read_len); + } + +end: + if (ret) { + /* Remove what was appended */ + g_string_truncate(str, orig_len); + } + + g_free(buf); + return ret; +}