+static
+struct ctf_fs_trace *ctf_fs_trace_create(struct ctf_fs_component *ctf_fs,
+ const char *path, const char *name)
+{
+ struct ctf_fs_trace *ctf_fs_trace;
+ int ret;
+
+ ctf_fs_trace = g_new0(struct ctf_fs_trace, 1);
+ if (!ctf_fs_trace) {
+ goto end;
+ }
+
+ ctf_fs_trace->ctf_fs = ctf_fs;
+ ctf_fs_trace->path = g_string_new(path);
+ if (!ctf_fs_trace->path) {
+ goto error;
+ }
+
+ ctf_fs_trace->name = g_string_new(name);
+ if (!ctf_fs_trace->name) {
+ goto error;
+ }
+
+ ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1);
+ if (!ctf_fs_trace->metadata) {
+ goto error;
+ }
+
+ ret = ctf_fs_metadata_set_trace(ctf_fs_trace);
+ if (ret) {
+ goto error;
+ }
+
+ ret = create_cc_prio_map(ctf_fs_trace);
+ if (ret) {
+ goto error;
+ }
+
+ ret = create_ports_for_trace(ctf_fs_trace);
+ if (ret) {
+ goto error;
+ }
+
+ goto end;
+
+error:
+ ctf_fs_trace_destroy(ctf_fs_trace);
+ ctf_fs_trace = NULL;
+end:
+ return ctf_fs_trace;
+}
+
+static
+int path_is_ctf_trace(const char *path)
+{
+ GString *metadata_path = g_string_new(NULL);
+ int ret = 0;
+
+ if (!metadata_path) {
+ ret = -1;
+ goto end;
+ }
+
+ g_string_printf(metadata_path, "%s/%s", path, CTF_FS_METADATA_FILENAME);
+
+ if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
+ ret = 1;
+ goto end;
+ }
+
+end:
+ g_string_free(metadata_path, TRUE);
+ return ret;
+}
+
+static
+int add_trace_path(struct ctf_fs_component *ctf_fs, GList **trace_paths,
+ const char *path)
+{
+ GString *path_str = g_string_new(NULL);
+ int ret = 0;
+ char *rp = NULL;
+
+ if (!path_str) {
+ ret = -1;
+ goto end;
+ }
+
+ /*
+ * Find the real path so that we don't have relative components
+ * in the trace name. This also squashes consecutive slashes and
+ * removes any slash at the end.
+ */
+ rp = realpath(path, NULL);
+ if (!rp) {
+ PERR("realpath() failed: %s (%d)\n", strerror(errno), errno);
+ ret = -1;
+ goto end;
+ }
+
+ if (strcmp(rp, "/") == 0) {
+ PERR("Opening a trace in `/` is not supported.\n");
+ ret = -1;
+ goto end;
+ }
+
+ g_string_assign(path_str, rp);
+ *trace_paths = g_list_prepend(*trace_paths, path_str);
+ assert(*trace_paths);
+
+end:
+ free(rp);
+ return ret;
+}
+
+static
+int find_ctf_traces(struct ctf_fs_component *ctf_fs,
+ GList **trace_paths, const char *start_path)
+{
+ int ret;
+ GError *error = NULL;
+ GDir *dir = NULL;
+ const char *basename = NULL;
+
+ /* Check if the starting path is a CTF trace itself */
+ ret = path_is_ctf_trace(start_path);
+ if (ret < 0) {
+ goto end;
+ }
+
+ if (ret) {
+ /*
+ * Do not even recurse: a CTF trace cannot contain
+ * another CTF trace.
+ */
+ ret = add_trace_path(ctf_fs, trace_paths, start_path);
+ goto end;
+ }
+
+ /* Look for subdirectories */
+ if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) {
+ /* Starting path is not a directory: end of recursion */
+ goto end;
+ }
+
+ dir = g_dir_open(start_path, 0, &error);
+ if (!dir) {
+ if (error->code == G_FILE_ERROR_ACCES) {
+ PDBG("Cannot open directory `%s`: %s (code %d): continuing\n",
+ start_path, error->message, error->code);
+ goto end;
+ }
+
+ PERR("Cannot open directory `%s`: %s (code %d)\n",
+ start_path, error->message, error->code);
+ ret = -1;
+ goto end;
+ }
+
+ while ((basename = g_dir_read_name(dir))) {
+ GString *sub_path = g_string_new(NULL);
+
+ if (!sub_path) {
+ ret = -1;
+ goto end;
+ }
+
+ g_string_printf(sub_path, "%s/%s", start_path, basename);
+ ret = find_ctf_traces(ctf_fs, trace_paths, sub_path->str);
+ g_string_free(sub_path, TRUE);
+ if (ret) {
+ goto end;
+ }
+ }
+
+end:
+ if (dir) {
+ g_dir_close(dir);
+ }
+
+ if (error) {
+ g_error_free(error);
+ }
+
+ return ret;
+}
+
+static
+GList *create_trace_names(GList *trace_paths) {
+ GList *trace_names = NULL;
+ size_t chars_to_strip = 0;
+ size_t at = 0;
+ GList *node;
+ bool done = false;
+
+ /*
+ * Find the number of characters to strip from the beginning,
+ * that is, the longest prefix until a common slash (also
+ * stripped).
+ */
+ while (true) {
+ gchar common_ch = '\0';
+
+ for (node = trace_paths; node; node = g_list_next(node)) {
+ GString *gstr = node->data;
+ gchar this_ch = gstr->str[at];
+
+ if (this_ch == '\0') {
+ done = true;
+ break;
+ }
+
+ if (common_ch == '\0') {
+ /*
+ * Establish the expected common
+ * character at this position.
+ */
+ common_ch = this_ch;
+ continue;
+ }
+
+ if (this_ch != common_ch) {
+ done = true;
+ break;
+ }
+ }
+
+ if (done) {
+ break;
+ }
+
+ if (common_ch == '/') {
+ /*
+ * Common character is a slash: safe to include
+ * this slash in the number of characters to
+ * strip because the paths are guaranteed not to
+ * end with slash.
+ */
+ chars_to_strip = at + 1;
+ }
+
+ at++;
+ }
+
+ /* Create the trace names */
+ for (node = trace_paths; node; node = g_list_next(node)) {
+ GString *trace_name = g_string_new(NULL);
+ GString *trace_path = node->data;
+
+ g_string_assign(trace_name, &trace_path->str[chars_to_strip]);
+ trace_names = g_list_append(trace_names, trace_name);
+ }
+
+ return trace_names;
+}
+
+static
+int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs,
+ const char *path_param)
+{
+ struct ctf_fs_trace *ctf_fs_trace = NULL;
+ int ret = 0;
+ GList *trace_paths = NULL;
+ GList *trace_names = NULL;
+ GList *tp_node;
+ GList *tn_node;
+
+ ret = find_ctf_traces(ctf_fs, &trace_paths, path_param);
+ if (ret) {
+ goto error;
+ }
+
+ if (!trace_paths) {
+ PERR("No CTF traces recursively found in `%s`.\n",
+ path_param);
+ goto error;
+ }
+
+ trace_names = create_trace_names(trace_paths);
+ if (!trace_names) {
+ PERR("Cannot create trace names from trace paths.\n");
+ goto error;
+ }
+
+ for (tp_node = trace_paths, tn_node = trace_names; tp_node;
+ tp_node = g_list_next(tp_node),
+ tn_node = g_list_next(tn_node)) {
+ GString *trace_path = tp_node->data;
+ GString *trace_name = tn_node->data;
+
+ ctf_fs_trace = ctf_fs_trace_create(ctf_fs, trace_path->str,
+ trace_name->str);
+ if (!ctf_fs_trace) {
+ PERR("Cannot create trace for `%s`.\n",
+ trace_path->str);
+ goto error;
+ }
+ }
+
+ g_ptr_array_add(ctf_fs->traces, ctf_fs_trace);
+ ctf_fs_trace = NULL;
+ goto end;
+
+error:
+ ret = -1;
+ ctf_fs_trace_destroy(ctf_fs_trace);
+
+end:
+ for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) {
+ if (tp_node->data) {
+ g_string_free(tp_node->data, TRUE);
+ }
+ }
+
+ for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) {
+ if (tn_node->data) {
+ g_string_free(tn_node->data, TRUE);
+ }
+ }
+
+ if (trace_paths) {
+ g_list_free(trace_paths);
+ }
+
+ if (trace_names) {
+ g_list_free(trace_names);
+ }
+
+ return ret;
+}
+