fs-sink: add the single-trace option
[babeltrace.git] / plugins / ctf / fs-sink / write.c
index dd380f5b667cd062f9c28a53cede8e360398f553..0b0cea42494caa9e28104d49670199f53c851328 100644 (file)
@@ -181,6 +181,48 @@ enum fs_writer_stream_state *insert_new_stream_state(
        return v;
 }
 
+/*
+ * Make sure the output path is valid for a single trace: either it does
+ * not exists or it is empty.
+ *
+ * Return 0 if the path is valid, -1 otherwise.
+ */
+static
+bool valid_single_trace_path(const char *path)
+{
+       int n = 0;
+       struct dirent *d;
+       DIR *dir = opendir(path);
+       int ret;
+
+       /* non-existant directory */
+       if (!dir) {
+               ret = 0;
+               goto end;
+       }
+
+       while ((d = readdir(dir)) != NULL) {
+               if (++n > 2) {
+                       break;
+               }
+       }
+
+       ret = closedir(dir);
+       if (ret) {
+               perror("closedir");
+               goto end;
+       }
+
+       if (n <= 2) {
+               ret = 0;
+       } else {
+               ret = -1;
+       }
+
+end:
+       return ret;
+}
+
 static
 int make_trace_path(struct writer_component *writer_component,
                struct bt_ctf_trace *trace, char *trace_path)
@@ -188,11 +230,14 @@ int make_trace_path(struct writer_component *writer_component,
        int ret;
        const char *trace_name;
 
-       trace_name = bt_ctf_trace_get_name(trace);
-       if (!trace_name) {
-               trace_name = writer_component->trace_name_base->str;
+       if (writer_component->single_trace) {
+               trace_name = "\0";
+       } else {
+               trace_name = bt_ctf_trace_get_name(trace);
+               if (!trace_name) {
+                       trace_name = writer_component->trace_name_base->str;
+               }
        }
-       /* XXX: we might have to skip the first level, TBD. */
 
        /* Sanitize the trace name. */
        if (strlen(trace_name) == 2 && !strcmp(trace_name, "..")) {
@@ -212,22 +257,34 @@ int make_trace_path(struct writer_component *writer_component,
        snprintf(trace_path, PATH_MAX, "%s/%s",
                        writer_component->base_path->str,
                        trace_name);
-       if (g_file_test(trace_path, G_FILE_TEST_EXISTS)) {
-               int i = 0;
-               do {
-                       snprintf(trace_path, PATH_MAX, "%s/%s-%d",
-                                       writer_component->base_path->str,
-                                       trace_name, ++i);
-               } while (g_file_test(trace_path, G_FILE_TEST_EXISTS) && i < INT_MAX);
-               if (i == INT_MAX) {
-                       fprintf(writer_component->err, "[error] Unable to find "
-                                       "a unique trace path\n");
+       /*
+        * Append a suffix if the trace_path exists and we are not in
+        * single-trace mode.
+        */
+       if (writer_component->single_trace) {
+               if (valid_single_trace_path(trace_path) != 0) {
+                       fprintf(writer_component->err,
+                                       "[error] Invalid output directory\n");
                        goto error;
                }
+       } else {
+               if (g_file_test(trace_path, G_FILE_TEST_EXISTS)) {
+                       int i = 0;
+
+                       do {
+                               snprintf(trace_path, PATH_MAX, "%s/%s-%d",
+                                               writer_component->base_path->str,
+                                               trace_name, ++i);
+                       } while (g_file_test(trace_path, G_FILE_TEST_EXISTS) && i < INT_MAX);
+                       if (i == INT_MAX) {
+                               fprintf(writer_component->err, "[error] Unable to find "
+                                               "a unique trace path\n");
+                               goto error;
+                       }
+               }
        }
 
        ret = 0;
-
        goto end;
 
 error:
@@ -249,6 +306,13 @@ struct fs_writer *insert_new_writer(
        struct fs_writer *fs_writer = NULL;
        int nr_stream, i;
 
+       if (writer_component->single_trace && writer_component->nr_traces > 0) {
+               fprintf(writer_component->err, "[error] Trying to process more "
+                               "than one trace but --single-trace mode "
+                               "enabled\n");
+               goto error;
+       }
+
        ret = make_trace_path(writer_component, trace, trace_path);
        if (ret) {
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
@@ -331,6 +395,7 @@ struct fs_writer *insert_new_writer(
                fs_writer->static_listener_id = ret;
        }
 
+       writer_component->nr_traces++;
        g_hash_table_insert(writer_component->trace_map, (gpointer) trace,
                        fs_writer);
 
This page took 0.023911 seconds and 4 git commands to generate.