- /*
- * If base path is set, it overrides the session name for the
- * session relative base path. No timestamp is appended if the
- * base path is overridden.
- *
- * If the session name already contains the creation time (e.g.
- * auto-<timestamp>, don't append yet another timestamp after
- * the session name in the generated path.
- *
- * Otherwise, generate the path with session_name-<timestamp>.
- */
- if (session->base_path[0] != '\0') {
- pthread_mutex_lock(&session->lock);
- ret = asprintf(&session_directory, "%s/%s", session->hostname,
- session->base_path);
- pthread_mutex_unlock(&session->lock);
- } else if (session->session_name_contains_creation_time) {
- pthread_mutex_lock(&session->lock);
- ret = asprintf(&session_directory, "%s/%s", session->hostname,
- session->session_name);
- pthread_mutex_unlock(&session->lock);
- } else {
- char session_creation_datetime[16];
- size_t strftime_ret;
- struct tm *timeinfo;
- time_t creation_time;
-
- /*
- * The 2.11+ protocol guarantees that a creation time
- * is provided for a session. This would indicate a
- * protocol error or an improper use of this util.
- */
- if (!session->creation_time.is_set) {
- ERR("Creation time missing for session \"%s\" (protocol error)",
- session->session_name);
- ret = -1;
- goto end;
- }
- creation_time = LTTNG_OPTIONAL_GET(session->creation_time);
-
- timeinfo = localtime(&creation_time);
- if (!timeinfo) {
- ERR("Failed to get timeinfo while initializing session output directory handle");
- ret = -1;
- goto end;
- }
- strftime_ret = strftime(session_creation_datetime,
- sizeof(session_creation_datetime),
- "%Y%m%d-%H%M%S", timeinfo);
- if (strftime_ret == 0) {
- ERR("Failed to format session creation timestamp while initializing session output directory handle");
- ret = -1;
- goto end;
- }
- pthread_mutex_lock(&session->lock);
- ret = asprintf(&session_directory, "%s/%s-%s",
- session->hostname, session->session_name,
- session_creation_datetime);
- pthread_mutex_unlock(&session->lock);
- }
- if (ret < 0) {
- PERROR("Failed to format session directory name");
- goto end;
- }
-
- full_session_path = create_output_path(session_directory);
- if (!full_session_path) {
- ret = -1;
- goto end;
- }
-
- ret = utils_mkdir_recursive(
- full_session_path, S_IRWXU | S_IRWXG, -1, -1);
- if (ret) {
- ERR("Failed to create session output path \"%s\"",
- full_session_path);
- goto end;
- }
-
- ret = lttng_directory_handle_init(handle, full_session_path);
- if (ret) {
- goto end;
- }
-end:
- free(session_directory);
- free(full_session_path);
- return ret;
-}