X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=cfb6555a1d65fc061eb20c2c69326a91175f97ec;hp=38f78a7e8e7d40cd6fcebefd297e5bb240c52aaf;hb=ca6b395f1a6a30bc855fa566cae98ecde5950dfe;hpb=cfa9a5a2b4a96e0d6a9eeddd2622a6d7c173b7ac diff --git a/src/common/utils.c b/src/common/utils.c index 38f78a7e8..cfb6555a1 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -22,8 +22,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -32,6 +32,7 @@ #include #include "utils.h" +#include "defaults.h" /* * Return the realpath(3) of the path even if the last directory token does not @@ -248,7 +249,6 @@ LTTNG_HIDDEN int utils_mkdir_recursive(const char *path, mode_t mode) { char *p, tmp[PATH_MAX]; - struct stat statbuf; size_t len; int ret; @@ -276,15 +276,12 @@ int utils_mkdir_recursive(const char *path, mode_t mode) ret = -1; goto error; } - ret = stat(tmp, &statbuf); + ret = mkdir(tmp, mode); if (ret < 0) { - ret = mkdir(tmp, mode); - if (ret < 0) { - if (errno != EEXIST) { - PERROR("mkdir recursive"); - ret = -errno; - goto error; - } + if (errno != EEXIST) { + PERROR("mkdir recursive"); + ret = -errno; + goto error; } } *p = '/'; @@ -311,7 +308,7 @@ error: * Return 0 on success or else a negative value. */ LTTNG_HIDDEN -int utils_create_stream_file(char *path_name, char *file_name, uint64_t size, +int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, uint64_t count, int uid, int gid) { int ret, out_fd, flags, mode; @@ -444,6 +441,7 @@ static void regex_print_error(int errcode, regex_t *regex) * * @return 0 on success, -1 on failure. */ +LTTNG_HIDDEN int utils_parse_size_suffix(char *str, uint64_t *size) { regex_t regex; @@ -584,3 +582,45 @@ int utils_get_count_order_u32(uint32_t x) return fls_u32(x - 1); } + +/** + * Obtain the value of LTTNG_HOME environment variable, if exists. + * Otherwise returns the value of HOME. + */ +LTTNG_HIDDEN +char *utils_get_home_dir(void) +{ + char *val = NULL; + val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR); + if (val != NULL) { + return val; + } + return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); +} + +/* + * With the given format, fill dst with the time of len maximum siz. + * + * Return amount of bytes set in the buffer or else 0 on error. + */ +LTTNG_HIDDEN +size_t utils_get_current_time_str(const char *format, char *dst, size_t len) +{ + size_t ret; + time_t rawtime; + struct tm *timeinfo; + + assert(format); + assert(dst); + + /* Get date and time for session path */ + time(&rawtime); + timeinfo = localtime(&rawtime); + ret = strftime(dst, len, format, timeinfo); + if (ret == 0) { + ERR("Unable to strftime with format %s at dst %p of len %lu", format, + dst, len); + } + + return ret; +}