- FTS *tree;
- FTSENT *node;
- GArray *trace_ids;
- char lpath[PATH_MAX];
- char * const paths[2] = { lpath, NULL };
- int ret = 0;
-
- /*
- * Need to copy path, because fts_open can change it.
- * It is the pointer array, not the strings, that are constant.
- */
- strncpy(lpath, path, PATH_MAX);
- lpath[PATH_MAX - 1] = '\0';
-
- tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
- if (tree == NULL) {
- fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
- path);
- return -EINVAL;
- }
-
- 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);
+ int ret = 0, trace_ids = 0;
+
+ /* Should lock traversed_paths mutex here if used in multithread */
+
+ traversed_paths = g_ptr_array_new();
+ ret = nftw(path, traverse_trace_dir, 10, 0);
+
+ /* Process the array if ntfw did not return a fatal error */
+ if (ret >= 0) {
+ int i;
+
+ for (i = 0; i < traversed_paths->len; i++) {
+ GString *trace_path = g_ptr_array_index(traversed_paths,
+ i);
+ int trace_id = bt_context_add_trace(ctx,
+ trace_path->str,
+ format_str,
+ packet_seek,
+ NULL,
+ NULL);