Fix: Tests: Segfault in `test_utils_expand_path()`
[lttng-tools.git] / src / common / utils.c
index 2f132a0603c8d9891d1280ef1912709b3b3c9013..17a313ee16f66efe8c574ddfddc8f94f156b9e11 100644 (file)
@@ -379,6 +379,9 @@ char *_utils_expand_path(const char *path, bool keep_symlink)
                /* Resolve partially our path */
                absolute_path = utils_partial_realpath(absolute_path,
                                absolute_path, LTTNG_PATH_MAX);
+               if (!absolute_path) {
+                       goto error;
+               }
        }
 
        ret = expand_double_slashes_dot_and_dotdot(absolute_path);
@@ -765,142 +768,6 @@ int utils_stream_file_path(const char *path_name, const char *file_name,
        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;
-}
-
-/*
- * Change the output tracefile according to the given size and count The
- * new_count pointer is set during this operation.
- *
- * From the consumer, the stream lock MUST be held before calling this function
- * because we are modifying the stream status.
- *
- * Return 0 on success or else a negative value.
- */
-LTTNG_HIDDEN
-int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
-               uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count,
-               int *stream_fd)
-{
-       int ret;
-
-       assert(stream_fd);
-
-       ret = close(out_fd);
-       if (ret < 0) {
-               PERROR("Closing tracefile");
-               goto error;
-       }
-       *stream_fd = -1;
-
-       if (count > 0) {
-               /*
-                * In tracefile rotation, for the relay daemon we need
-                * to unlink the old file if present, because it may
-                * still be open in reading by the live thread, and we
-                * need to ensure that we do not overwrite the content
-                * between get_index and get_packet. Since we have no
-                * way to verify integrity of the data content compared
-                * to the associated index, we need to ensure the reader
-                * has exclusive access to the file content, and that
-                * the open of the data file is performed in get_index.
-                * Unlinking the old file rather than overwriting it
-                * achieves this.
-                */
-               if (new_count) {
-                       *new_count = (*new_count + 1) % count;
-               }
-               ret = utils_unlink_stream_file(path_name, file_name, size,
-                               new_count ? *new_count : 0, uid, gid, 0);
-               if (ret < 0 && errno != ENOENT) {
-                       goto error;
-               }
-       } else {
-               if (new_count) {
-                       (*new_count)++;
-               }
-       }
-
-       ret = utils_create_stream_file(path_name, file_name, size,
-                       new_count ? *new_count : 0, uid, gid, 0);
-       if (ret < 0) {
-               goto error;
-       }
-       *stream_fd = ret;
-
-       /* Success. */
-       ret = 0;
-
-error:
-       return ret;
-}
-
-
 /**
  * Parse a string that represents a size in human readable format. It
  * supports decimal integers suffixed by 'k', 'K', 'M' or 'G'.
@@ -1265,7 +1132,7 @@ int utils_get_count_order_u64(uint64_t x)
  * 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;
@@ -1485,79 +1352,16 @@ end:
 LTTNG_HIDDEN
 int utils_recursive_rmdir(const char *path)
 {
-       DIR *dir;
-       size_t path_len;
-       int dir_fd, ret = 0, closeret, is_empty = 1;
-       struct dirent *entry;
-
-       /* Open directory */
-       dir = opendir(path);
-       if (!dir) {
-               PERROR("Cannot open '%s' path", path);
-               return -1;
-       }
-       dir_fd = lttng_dirfd(dir);
-       if (dir_fd < 0) {
-               PERROR("lttng_dirfd");
-               return -1;
-       }
-
-       path_len = strlen(path);
-       while ((entry = readdir(dir))) {
-               struct stat st;
-               size_t name_len;
-               char filename[PATH_MAX];
-
-               if (!strcmp(entry->d_name, ".")
-                               || !strcmp(entry->d_name, "..")) {
-                       continue;
-               }
-
-               name_len = strlen(entry->d_name);
-               if (path_len + name_len + 2 > sizeof(filename)) {
-                       ERR("Failed to remove file: path name too long (%s/%s)",
-                               path, entry->d_name);
-                       continue;
-               }
-               if (snprintf(filename, sizeof(filename), "%s/%s",
-                               path, entry->d_name) < 0) {
-                       ERR("Failed to format path.");
-                       continue;
-               }
-
-               if (stat(filename, &st)) {
-                       PERROR("stat");
-                       continue;
-               }
+       int ret;
+       struct lttng_directory_handle handle;
 
-               if (S_ISDIR(st.st_mode)) {
-                       char subpath[PATH_MAX];
-
-                       strncpy(subpath, path, PATH_MAX);
-                       subpath[PATH_MAX - 1] = '\0';
-                       strncat(subpath, "/",
-                               PATH_MAX - strlen(subpath) - 1);
-                       strncat(subpath, entry->d_name,
-                               PATH_MAX - strlen(subpath) - 1);
-                       if (utils_recursive_rmdir(subpath)) {
-                               is_empty = 0;
-                       }
-               } else if (S_ISREG(st.st_mode)) {
-                       is_empty = 0;
-               } else {
-                       ret = -EINVAL;
-                       goto end;
-               }
+       ret = lttng_directory_handle_init(&handle, NULL);
+       if (ret) {
+               goto end;
        }
+       ret = lttng_directory_handle_remove_subdirectory(&handle, path);
+       lttng_directory_handle_fini(&handle);
 end:
-       closeret = closedir(dir);
-       if (closeret) {
-               PERROR("closedir");
-       }
-       if (is_empty) {
-               DBG3("Attempting rmdir %s", path);
-               ret = rmdir(path);
-       }
        return ret;
 }
 
This page took 0.02542 seconds and 4 git commands to generate.