X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fpath.cpp;h=61e78a5c1d7a583f11b92ce5cbe2e9da20f3c375;hp=1699cc5a6de28d0075002ee0266220399fd8c79d;hb=HEAD;hpb=c9e313bc594f40a86eed237dce222c0fc99c957f diff --git a/src/common/path.cpp b/src/common/path.cpp index 1699cc5a6..14611c216 100644 --- a/src/common/path.cpp +++ b/src/common/path.cpp @@ -8,8 +8,8 @@ */ #define _LGPL_SOURCE -#include #include +#include #include /* @@ -21,15 +21,14 @@ * * Return a newly-allocated string. */ -static -char *utils_partial_realpath(const char *path) +static char *utils_partial_realpath(const char *path) { - char *cut_path = NULL, *try_path = NULL, *try_path_prev = NULL; + char *cut_path = nullptr, *try_path = nullptr, *try_path_prev = nullptr; const char *next, *prev, *end; - char *resolved_path = NULL; + char *resolved_path = nullptr; /* Safety net */ - if (path == NULL) { + if (path == nullptr) { goto error; } @@ -40,7 +39,7 @@ char *utils_partial_realpath(const char *path) * the path given as argument */ end = path + strlen(path); - if (*(end-1) == '/') { + if (*(end - 1) == '/') { end--; } @@ -48,29 +47,29 @@ char *utils_partial_realpath(const char *path) next = path; prev = next; /* Only to ensure try_path is not NULL to enter the while */ - try_path = (char *)next; + try_path = (char *) next; /* Resolve the canonical path of the first part of the path */ - while (try_path != NULL && next != end) { - char *try_path_buf = NULL; + while (try_path != nullptr && next != end) { + char *try_path_buf = nullptr; /* * If there is not any '/' left, we want to try with * the full path */ next = strpbrk(next + 1, "/"); - if (next == NULL) { + if (next == nullptr) { next = end; } /* Cut the part we will be trying to resolve */ cut_path = lttng_strndup(path, next - path); - if (cut_path == NULL) { + if (cut_path == nullptr) { PERROR("lttng_strndup"); goto error; } - try_path_buf = (char *) zmalloc(LTTNG_PATH_MAX); + try_path_buf = zmalloc(LTTNG_PATH_MAX); if (!try_path_buf) { PERROR("zmalloc"); goto error; @@ -78,7 +77,7 @@ char *utils_partial_realpath(const char *path) /* Try to resolve this part */ try_path = realpath((char *) cut_path, try_path_buf); - if (try_path == NULL) { + if (try_path == nullptr) { free(try_path_buf); /* * There was an error, we just want to be assured it @@ -96,7 +95,7 @@ char *utils_partial_realpath(const char *path) } } else { /* Save the place we are before trying the next step */ - try_path_buf = NULL; + try_path_buf = nullptr; free(try_path_prev); try_path_prev = try_path; prev = next; @@ -104,12 +103,12 @@ char *utils_partial_realpath(const char *path) /* Free the allocated memory */ free(cut_path); - cut_path = NULL; + cut_path = nullptr; } /* Allocate memory for the resolved path. */ - resolved_path = (char *) zmalloc(LTTNG_PATH_MAX); - if (resolved_path == NULL) { + resolved_path = zmalloc(LTTNG_PATH_MAX); + if (resolved_path == nullptr) { PERROR("zmalloc resolved path"); goto error; } @@ -118,7 +117,7 @@ char *utils_partial_realpath(const char *path) * If we were able to solve at least partially the path, we can concatenate * what worked and what didn't work */ - if (try_path_prev != NULL) { + if (try_path_prev != nullptr) { /* If we risk to concatenate two '/', we remove one of them */ if (try_path_prev[strlen(try_path_prev) - 1] == '/' && prev[0] == '/') { try_path_prev[strlen(try_path_prev) - 1] = '\0'; @@ -129,24 +128,23 @@ char *utils_partial_realpath(const char *path) * path are pointers for the same memory space */ cut_path = strdup(prev); - if (cut_path == NULL) { + if (cut_path == nullptr) { PERROR("strdup"); goto error; } /* Concatenate the strings */ - snprintf(resolved_path, LTTNG_PATH_MAX, "%s%s", - try_path_prev, cut_path); + snprintf(resolved_path, LTTNG_PATH_MAX, "%s%s", try_path_prev, cut_path); /* Free the allocated memory */ free(cut_path); free(try_path_prev); - cut_path = NULL; - try_path_prev = NULL; - /* - * Else, we just copy the path in our resolved_path to - * return it as is - */ + cut_path = nullptr; + try_path_prev = nullptr; + /* + * Else, we just copy the path in our resolved_path to + * return it as is + */ } else { strncpy(resolved_path, path, LTTNG_PATH_MAX); } @@ -161,11 +159,10 @@ error: if (try_path_prev != try_path) { free(try_path_prev); } - return NULL; + return nullptr; } -static -int expand_double_slashes_dot_and_dotdot(char *path) +static int expand_double_slashes_dot_and_dotdot(char *path) { size_t expanded_path_len, path_len; const char *curr_char, *path_last_char, *next_slash, *prev_slash; @@ -190,14 +187,14 @@ int expand_double_slashes_dot_and_dotdot(char *path) } next_slash = (const char *) memchr(curr_char, '/', path_last_char - curr_char); - if (next_slash == NULL) { + if (next_slash == nullptr) { /* Reached the end of the provided path. */ next_slash = path_last_char; } /* Compute how long is the previous token. */ curr_token_len = next_slash - curr_char; - switch(curr_token_len) { + switch (curr_token_len) { case 0: /* * The pointer has not move meaning that curr_char is @@ -230,13 +227,14 @@ int expand_double_slashes_dot_and_dotdot(char *path) * previous forward slash and substract that * len to the resulting path. */ - prev_slash = (const char *) lttng_memrchr(path, '/', expanded_path_len); + prev_slash = + (const char *) lttng_memrchr(path, '/', expanded_path_len); /* * If prev_slash is NULL, we reached the * beginning of the path. We can't go back any * further. */ - if (prev_slash != NULL) { + if (prev_slash != nullptr) { expanded_path_len = prev_slash - path; } continue; @@ -274,22 +272,21 @@ error: * The returned string was allocated in the function, it is thus of * the responsibility of the caller to free this memory. */ -static -char *_utils_expand_path(const char *path, bool keep_symlink) +static char *_utils_expand_path(const char *path, bool keep_symlink) { int ret; - char *absolute_path = NULL; + char *absolute_path = nullptr; char *last_token; bool is_dot, is_dotdot; /* Safety net */ - if (path == NULL) { + if (path == nullptr) { goto error; } /* Allocate memory for the absolute_path */ - absolute_path = (char *) zmalloc(LTTNG_PATH_MAX); - if (absolute_path == NULL) { + absolute_path = zmalloc(LTTNG_PATH_MAX); + if (absolute_path == nullptr) { PERROR("zmalloc expand path"); goto error; } @@ -316,11 +313,12 @@ char *_utils_expand_path(const char *path, bool keep_symlink) * Get the number of character in the CWD and allocate an array * to can hold it and the path provided by the caller. */ - ret = snprintf(absolute_path, LTTNG_PATH_MAX, "%s/%s", - current_working_dir, path); + ret = snprintf(absolute_path, LTTNG_PATH_MAX, "%s/%s", current_working_dir, path); if (ret >= LTTNG_PATH_MAX) { ERR("Concatenating current working directory %s and path %s exceeds maximal size of %i bytes", - current_working_dir, path, LTTNG_PATH_MAX); + current_working_dir, + path, + LTTNG_PATH_MAX); goto error; } } @@ -370,7 +368,7 @@ char *_utils_expand_path(const char *path, bool keep_symlink) error: free(absolute_path); - return NULL; + return nullptr; } char *utils_expand_path(const char *path) {