* but the /tmp/test1 does, the real path for /tmp/test1 is concatened with
* /test2/test3 then returned. In normal time, realpath(3) fails if the end
* point directory does not exist.
- * In case resolved_path is NULL, the string returned was allocated in the
- * function and thus need to be freed by the caller. The size argument allows
- * to specify the size of the resolved_path argument if given, or the size to
- * allocate.
+ *
+ * Return a newly-allocated string.
*/
-LTTNG_HIDDEN
-char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
+static
+char *utils_partial_realpath(const char *path)
{
char *cut_path = NULL, *try_path = NULL, *try_path_prev = NULL;
const char *next, *prev, *end;
+ char *resolved_path = NULL;
/* Safety net */
if (path == NULL) {
cut_path = NULL;
}
- /* Allocate memory for the resolved path if necessary */
+ /* Allocate memory for the resolved path. */
+ resolved_path = zmalloc(LTTNG_PATH_MAX);
if (resolved_path == NULL) {
- resolved_path = zmalloc(size);
- if (resolved_path == NULL) {
- PERROR("zmalloc resolved path");
- goto error;
- }
+ PERROR("zmalloc resolved path");
+ goto error;
}
/*
}
/* Concatenate the strings */
- snprintf(resolved_path, size, "%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);
* return it as is
*/
} else {
- strncpy(resolved_path, path, size);
+ strncpy(resolved_path, path, LTTNG_PATH_MAX);
}
/* Then we return the 'partially' resolved path */
* Copy the current token which is neither a '.' nor a '..'.
*/
path[expanded_path_len++] = '/';
- memcpy(&path[expanded_path_len], curr_char, curr_token_len);
+ memmove(&path[expanded_path_len], curr_char, curr_token_len);
expanded_path_len += curr_token_len;
}
if (keep_symlink) {
/* Resolve partially our path */
- absolute_path = utils_partial_realpath(absolute_path,
- absolute_path, LTTNG_PATH_MAX);
+ char *new_absolute_path = utils_partial_realpath(absolute_path);
+ if (!new_absolute_path) {
+ goto error;
+ }
+
+ free(absolute_path);
+ absolute_path = new_absolute_path;
}
ret = expand_double_slashes_dot_and_dotdot(absolute_path);
if (ret) {
PERROR("close pipe");
}
+ src[i] = -1;
}
}
char count_str[MAX_INT_DEC_LEN(count) + 1] = {};
const char *path_separator;
- if (path_name && path_name[strlen(path_name) - 1] == '/') {
+ if (path_name && (path_name[0] == '\0' ||
+ path_name[strlen(path_name) - 1] == '/')) {
path_separator = "";
} else {
path_separator = "/";
return ret;
}
-/*
- * Create the stream file on disk.
- *
- * Return 0 on success or else a negative value.
- */
-LTTNG_HIDDEN
-int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size,
- uint64_t count, int uid, int gid, char *suffix)
-{
- int ret, flags, mode;
- char path[LTTNG_PATH_MAX];
-
- ret = utils_stream_file_path(path_name, file_name,
- size, count, suffix, path, sizeof(path));
- if (ret < 0) {
- goto error;
- }
-
- /*
- * With the session rotation feature on the relay, we might need to seek
- * and truncate a tracefile, so we need read and write access.
- */
- flags = O_RDWR | O_CREAT | O_TRUNC;
- /* Open with 660 mode */
- mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
-
- if (uid < 0 || gid < 0) {
- ret = open(path, flags, mode);
- } else {
- ret = run_as_open(path, flags, mode, uid, gid);
- }
- if (ret < 0) {
- PERROR("open stream path %s", path);
- }
-error:
- return ret;
-}
-
-/*
- * Unlink the stream tracefile from disk.
- *
- * Return 0 on success or else a negative value.
- */
-LTTNG_HIDDEN
-int utils_unlink_stream_file(const char *path_name, char *file_name, uint64_t size,
- uint64_t count, int uid, int gid, char *suffix)
-{
- int ret;
- char path[LTTNG_PATH_MAX];
-
- ret = utils_stream_file_path(path_name, file_name, size, count, suffix,
- path, sizeof(path));
- if (ret < 0) {
- goto error;
- }
- if (uid < 0 || gid < 0) {
- ret = unlink(path);
- } else {
- ret = run_as_unlink(path, uid, gid);
- }
- if (ret < 0) {
- goto error;
- }
-error:
- DBG("utils_unlink_stream_file %s returns %d", path, ret);
- return ret;
-}
-
/**
* Parse a string that represents a size in human readable format. It
* supports decimal integers suffixed by 'k', 'K', 'M' or 'G'.
* Otherwise returns the value of HOME.
*/
LTTNG_HIDDEN
-char *utils_get_home_dir(void)
+const char *utils_get_home_dir(void)
{
char *val = NULL;
struct passwd *pwd;
}
}
if (ret) {
- PERROR("Failed to get group file entry for group name \"%s\"",
- name);
+ if (ret == ESRCH) {
+ DBG("Could not find group file entry for group name '%s'",
+ name);
+ } else {
+ PERROR("Failed to get group file entry for group name '%s'",
+ name);
+ }
+
ret = -1;
goto error;
}