Traverse a path recursively to add all traces within
authormdenis <mathieu.denis@polymtl.ca>
Wed, 4 Apr 2012 16:45:12 +0000 (12:45 -0400)
committerYannick Brosseau <yannick.brosseau@gmail.com>
Mon, 11 Jun 2012 21:34:34 +0000 (17:34 -0400)
Signed-off-by: Mathieu Denis <mathieu.denis@polymtl.ca>
Signed-off-by: Yannick Brosseau <yannick.brosseau@gmail.com>
lttv/lttv/traceset.c
lttv/lttv/traceset.h
lttv/modules/text/batchAnalysis.c

index 16487a117ff90b86bf7e3675e7d93762e3a480f6..35c84395af6678aa54356876ec5c3aef9097ffb9 100644 (file)
 #include <babeltrace/context.h>
 #include <babeltrace/iterator.h>
 #include <babeltrace/ctf/events.h>
+
+/* To traverse a tree recursively */
+#include <fcntl.h>
+#include <fts.h>
+
 /* A trace is a sequence of events gathered in the same tracing session. The
    events may be stored in several tracefiles in the same directory. 
    A trace set is defined when several traces are to be analyzed together,
@@ -232,11 +237,73 @@ void lttv_traceset_add(LttvTraceset *s, LttvTrace *t)
        g_ptr_array_add(s->traces, t);
 }
 
-int lttv_traceset_add_path(LttvTraceset *ts, const char *trace_path)
+int lttv_traceset_add_path(LttvTraceset *ts, char *trace_path)
 {
-  // todo mdenis 2012-03-27: add trace recursively and update comment
-  int ret = lttv_traceset_create_trace(ts, trace_path);
-  return ret;
+       FTS *tree;
+       FTSENT *node;
+       char * const paths[2] = { trace_path, NULL };
+       int ret = -1;
+
+       tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
+       if (tree == NULL) {
+               g_warning("Cannot traverse \"%s\" for reading.\n",
+                               trace_path);
+               return ret;
+       }
+
+       int dirfd, metafd;
+       while ((node = fts_read(tree))) {
+
+               if (!(node->fts_info & FTS_D))
+                       continue;
+
+               dirfd = open(node->fts_accpath, 0);
+               if (dirfd < 0) {
+                       g_warning("Unable to open trace "
+                                       "directory file descriptor : %s.", node->fts_accpath);
+                       ret = dirfd;
+                       goto error;
+               }
+
+               // Check if a metadata file exists in the current directory
+               metafd = openat(dirfd, "metadata", O_RDONLY);
+               if (metafd < 0) {
+                       ret = close(dirfd);
+                       if (ret < 0) {
+                               g_warning("Unable to open metadata "
+                                               "file descriptor : %s.", node->fts_accpath);
+                               goto error;
+                       }
+               } else {
+                       ret = close(metafd);
+                       if (ret < 0) {
+                               g_warning("Unable to close metadata "
+                                               "file descriptor : %s.", node->fts_accpath);
+                               goto error;
+                       }
+                       ret = close(dirfd);
+                       if (ret < 0) {
+                               g_warning("Unable to close trace "
+                                               "directory file descriptor : %s.", node->fts_accpath);
+                               goto error;
+                       }
+
+                       ret = lttv_traceset_create_trace(ts, node->fts_accpath);
+                       if (ret < 0) {
+                               g_warning("Opening trace \"%s\" from %s "
+                                               "for reading.", node->fts_accpath, trace_path);
+                               goto error;
+                       }
+               }
+       }
+
+error:
+       ret = fts_close(tree);
+       if (ret < 0) {
+               g_warning("Unable to close tree  "
+                               "file descriptor : %s.", trace_path);
+       }
+       return ret;
 }
 
 unsigned lttv_traceset_number(LttvTraceset *s) 
index 9d62687686e0b19a4cc8691379d24de88ea75722..63b1df97522d3d1a5ec5747ec4b2f8e4f03e2260 100644 (file)
@@ -85,18 +85,16 @@ void lttv_traceset_destroy(LttvTraceset *s);
 void lttv_traceset_add(LttvTraceset *s, LttvTrace *t);
 
 /*
- * lttv_traceset_add_path : Add all traces recursively to a traceset
+ * lttv_trace_create : Add all traces recursively to a traceset from a path
  *
  * ts is the traceset in which will be contained the traces
  *
- * path is a path to a trace(s). It cannot be NULL and it is not parse
- * recursively.
- * todo mdenis: implement algorithm to go through all folders recursively to
- * find all traces in the path
+ * trace_path is the path where to find a set of trace.
+ * Traverse the path recursively to add all traces within.
  *
- * @return 0 on success, -1 on failure
+ * return 0 on success or a negative integer on failure
  */
-int lttv_traceset_add_path(LttvTraceset *ts, const char *path);
+int lttv_traceset_add_path(LttvTraceset *ts, char *path);
 
 unsigned lttv_traceset_number(LttvTraceset *s);
 
index c830509cce01ebb777469f180b5f17375e8db3c8..cfd2bab72bd4fbb994f60258e8f9caed3779bd39 100644 (file)
@@ -78,7 +78,7 @@ void lttv_trace_option(void *hook_data)
   lttv_traceset_add(traceset, lttv_trace_new(trace));*/
 
   if(lttv_traceset_add_path(traceset, trace_path) < 0) {
-    printf("Cannot add trace %s", trace_path);
+    g_error("Cannot add trace %s", trace_path);
   }
 }
 
This page took 0.026026 seconds and 4 git commands to generate.