| 1 | /* |
| 2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <assert.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <common/common.h> |
| 16 | #include <common/defaults.h> |
| 17 | #include <common/utils.h> |
| 18 | #include <common/path.h> |
| 19 | |
| 20 | #include "lttng-relayd.h" |
| 21 | #include "utils.h" |
| 22 | |
| 23 | static char *create_output_path_auto(const char *path_name) |
| 24 | { |
| 25 | int ret; |
| 26 | char *traces_path = NULL; |
| 27 | const char *default_path; |
| 28 | |
| 29 | default_path = utils_get_home_dir(); |
| 30 | if (default_path == NULL) { |
| 31 | ERR("Home path not found.\n \ |
| 32 | Please specify an output path using -o, --output PATH"); |
| 33 | goto exit; |
| 34 | } |
| 35 | ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME |
| 36 | "/%s", default_path, path_name); |
| 37 | if (ret < 0) { |
| 38 | PERROR("asprintf trace dir name"); |
| 39 | goto exit; |
| 40 | } |
| 41 | exit: |
| 42 | return traces_path; |
| 43 | } |
| 44 | |
| 45 | static char *create_output_path_noauto(const char *path_name) |
| 46 | { |
| 47 | int ret; |
| 48 | char *traces_path = NULL; |
| 49 | char *full_path; |
| 50 | |
| 51 | full_path = utils_expand_path(opt_output_path); |
| 52 | if (!full_path) { |
| 53 | goto exit; |
| 54 | } |
| 55 | |
| 56 | ret = asprintf(&traces_path, "%s/%s", full_path, path_name); |
| 57 | if (ret < 0) { |
| 58 | PERROR("asprintf trace dir name"); |
| 59 | goto exit; |
| 60 | } |
| 61 | exit: |
| 62 | free(full_path); |
| 63 | return traces_path; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Create the output trace directory path name string. |
| 68 | * |
| 69 | * Return the allocated string containing the path name or else NULL. |
| 70 | */ |
| 71 | char *create_output_path(const char *path_name) |
| 72 | { |
| 73 | assert(path_name); |
| 74 | |
| 75 | if (opt_output_path == NULL) { |
| 76 | return create_output_path_auto(path_name); |
| 77 | } else { |
| 78 | return create_output_path_noauto(path_name); |
| 79 | } |
| 80 | } |