+/*
+ * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
+ * it receives the path of the current entry (file, dir, link..etc) with
+ * a flag to indicate the type of the entry.
+ * if the entry being visited is a directory and contains a metadata file,
+ * then add the path to a global list to be processed later in
+ * add_traces_recursive.
+ */
+static int traverse_trace_dir(const char *fpath, const struct stat *sb,
+ int tflag, struct FTW *ftwbuf)
+{
+ int dirfd, metafd;
+ int closeret;
+
+ if (tflag != FTW_D)
+ return 0;
+
+ dirfd = open(fpath, 0);
+ if (dirfd < 0) {
+ fprintf(stderr, "[error] [Context] Unable to open trace "
+ "directory file descriptor.\n");
+ return 0; /* partial error */
+ }
+ metafd = openat(dirfd, "metadata", O_RDONLY);
+ if (metafd < 0) {
+ closeret = close(dirfd);
+ if (closeret < 0) {
+ perror("close");
+ return -1;
+ }
+ /* No meta data, just return */
+ return 0;
+ } else {
+ closeret = close(metafd);
+ if (closeret < 0) {
+ perror("close");
+ return -1; /* failure */
+ }
+ closeret = close(dirfd);
+ if (closeret < 0) {
+ perror("close");
+ return -1; /* failure */
+ }
+
+ /* Add path to the global list */
+ if (traversed_paths == NULL) {
+ fprintf(stderr, "[error] [Context] Invalid open path array.\n");
+ return -1;
+ }
+ g_ptr_array_add(traversed_paths, g_string_new(fpath));
+ }
+
+ return 0;
+}