+ trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
+
+ while ((node = fts_read(tree))) {
+ int dirfd, metafd;
+ int closeret;
+
+ if (!(node->fts_info & FTS_D))
+ continue;
+
+ dirfd = open(node->fts_accpath, 0);
+ if (dirfd < 0) {
+ fprintf(stderr, "[error] [Context] Unable to open trace "
+ "directory file descriptor.\n");
+ ret = 1; /* partial error */
+ goto error;
+ }
+ metafd = openat(dirfd, "metadata", O_RDONLY);
+ if (metafd < 0) {
+ closeret = close(dirfd);
+ if (closeret < 0) {
+ perror("close");
+ ret = -1; /* failure */
+ goto error;
+ }
+ continue;
+ } else {
+ int trace_id;
+
+ closeret = close(metafd);
+ if (closeret < 0) {
+ perror("close");
+ ret = -1; /* failure */
+ goto error;
+ }
+ closeret = close(dirfd);
+ if (closeret < 0) {
+ perror("close");
+ ret = -1; /* failure */
+ goto error;
+ }
+
+ trace_id = bt_context_add_trace(ctx,
+ node->fts_accpath, format_str,
+ packet_seek, NULL, NULL);
+ if (trace_id < 0) {
+ fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
+ "for reading.\n", node->fts_accpath, path);
+ /* Allow to skip erroneous traces. */
+ ret = 1; /* partial error */
+ continue;
+ }
+ g_array_append_val(trace_ids, trace_id);
+ }
+ }
+
+error:
+ /*
+ * Return an error if no trace can be opened.
+ */
+ if (trace_ids->len == 0) {
+ fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
+ ret = -ENOENT; /* failure */
+ }
+ g_array_free(trace_ids, TRUE);
+ return ret;