+ return;
+}
+
+/* Search for something like : .*_.*
+ *
+ * The left side is the name, the right side is the number.
+ */
+
+int get_tracefile_name_number(const gchar *raw_name,
+ GQuark *name,
+ guint *num)
+{
+ guint raw_name_len = strlen(raw_name);
+ gchar char_name[PATH_MAX];
+ int i;
+ int underscore_pos;
+ long int cpu_num;
+ gchar *endptr;
+
+ for(i=raw_name_len-1;i>=0;i--) {
+ if(raw_name[i] == '_') break;
+ }
+ if(i==0) /* Either not found or name length is 0 */
+ return -1;
+ underscore_pos = i;
+
+ cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
+
+ if(endptr == raw_name+underscore_pos+1)
+ return -1; /* No digit */
+ if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
+ return -1; /* underflow / overflow */
+
+ strncpy(char_name, raw_name, underscore_pos);
+
+ char_name[underscore_pos] = '\0';
+
+ *name = g_quark_from_string(char_name);
+ *num = cpu_num;
+
+ return 0;
+}
+
+
+GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
+{
+ return &trace->tracefiles;
+}
+
+
+void compute_tracefile_group(GQuark key_id,
+ GArray *group,
+ struct compute_tracefile_group_args *args)
+{
+ int i;
+ LttTracefile *tf;
+
+ for(i=0; i<group->len; i++) {
+ tf = &g_array_index (group, LttTracefile, i);
+ if(tf->cpu_online)
+ args->func(tf, args->func_args);
+ }
+}
+
+
+void ltt_tracefile_group_destroy(gpointer data)
+{
+ GArray *group = (GArray *)data;
+ int i;
+ LttTracefile *tf;
+
+ for(i=0; i<group->len; i++) {
+ tf = &g_array_index (group, LttTracefile, i);
+ if(tf->cpu_online)
+ ltt_tracefile_close(tf);
+ }
+ g_array_free(group, TRUE);
+}
+
+gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
+{
+ GArray *group = (GArray *)data;
+ int i;
+ LttTracefile *tf;
+
+ for(i=0; i<group->len; i++) {
+ tf = &g_array_index (group, LttTracefile, i);
+ if(tf->cpu_online) return 1;
+ }
+ return 0;
+}
+
+
+/* Open each tracefile under a specific directory. Put them in a
+ * GData : permits to access them using their tracefile group pathname.
+ * i.e. access control/modules tracefile group by index :
+ * "control/module".
+ *
+ * relative path is the path relative to the trace root
+ * root path is the full path
+ *
+ * A tracefile group is simply an array where all the per cpu tracefiles sits.
+ */
+
+static int open_tracefiles(LttTrace *trace, gchar *root_path,
+ gchar *relative_path)
+{
+ DIR *dir = opendir(root_path);
+ struct dirent *entry;
+ struct stat stat_buf;
+ int ret;
+
+ gchar path[PATH_MAX];
+ int path_len;
+ gchar *path_ptr;
+
+ int rel_path_len;
+ gchar rel_path[PATH_MAX];
+ gchar *rel_path_ptr;
+ LttTracefile tmp_tf;
+
+ if(dir == NULL) {
+ perror(root_path);
+ return ENOENT;
+ }
+
+ strncpy(path, root_path, PATH_MAX-1);
+ path_len = strlen(path);
+ path[path_len] = '/';
+ path_len++;
+ path_ptr = path + path_len;
+
+ strncpy(rel_path, relative_path, PATH_MAX-1);
+ rel_path_len = strlen(rel_path);
+ rel_path[rel_path_len] = '/';
+ rel_path_len++;
+ rel_path_ptr = rel_path + rel_path_len;
+
+ while((entry = readdir(dir)) != NULL) {
+
+ if(entry->d_name[0] == '.') continue;
+
+ strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
+ strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
+
+ ret = stat(path, &stat_buf);
+ if(ret == -1) {
+ perror(path);
+ continue;
+ }
+
+ g_debug("Tracefile file or directory : %s\n", path);
+
+ if(strcmp(rel_path, "/eventdefs") == 0) continue;
+
+ if(S_ISDIR(stat_buf.st_mode)) {
+
+ g_debug("Entering subdirectory...\n");
+ ret = open_tracefiles(trace, path, rel_path);
+ if(ret < 0) continue;
+ } else if(S_ISREG(stat_buf.st_mode)) {
+ GQuark name;
+ guint num;
+ GArray *group;
+
+ if(get_tracefile_name_number(rel_path, &name, &num))
+ continue; /* invalid name */
+
+ g_debug("Opening file.\n");
+ if(ltt_tracefile_open(trace, path, &tmp_tf)) {
+ g_info("Error opening tracefile %s", path);
+
+ continue; /* error opening the tracefile : bad magic number ? */
+ }
+
+ g_debug("Tracefile name is %s and number is %u",
+ g_quark_to_string(name), num);
+
+ tmp_tf.cpu_online = 1;
+ tmp_tf.cpu_num = num;
+ tmp_tf.name = name;
+
+ group = g_datalist_id_get_data(&trace->tracefiles, name);
+ if(group == NULL) {
+ /* Elements are automatically cleared when the array is allocated.
+ * It makes the cpu_online variable set to 0 : cpu offline, by default.
+ */
+ group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
+ g_datalist_id_set_data_full(&trace->tracefiles, name,
+ group, ltt_tracefile_group_destroy);
+ }
+
+ /* Add the per cpu tracefile to the named group */
+ unsigned int old_len = group->len;
+ if(num+1 > old_len)
+ group = g_array_set_size(group, num+1);
+ g_array_index (group, LttTracefile, num) = tmp_tf;
+
+ }
+ }
+
+ closedir(dir);
+
+ return 0;
+}
+
+/* ltt_get_facility_description
+ *
+ * Opens the trace corresponding to the requested facility (identified by fac_id
+ * and checksum).
+ *
+ * The name searched is : %trace root%/eventdefs/facname_checksum.xml
+ *
+ * Returns 0 on success, or 1 on failure.
+ */
+
+static int ltt_get_facility_description(LttFacility *f,
+ LttTrace *t,
+ LttTracefile *fac_tf)
+{
+ char desc_file_name[PATH_MAX];
+ const gchar *text;
+ guint textlen;
+ gint err;
+
+ text = g_quark_to_string(t->pathname);
+ textlen = strlen(text);
+
+ if(textlen >= PATH_MAX) goto name_error;
+ strcpy(desc_file_name, text);
+
+ text = "/eventdefs/";
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ text = g_quark_to_string(f->name);
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ text = "_";
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
+ "%u", f->checksum);
+ if(err < 0) goto name_error;
+
+ textlen=strlen(desc_file_name);
+
+ text = ".xml";
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ err = ltt_facility_open(f, t, desc_file_name);
+ if(err) goto facility_error;