Port: Replace dirent->d_type by stat
[lttng-tools.git] / src / common / utils.c
index 9fcceab1f3149c2c5e17a3e68d4ee1b60fc4932c..ca4eb44481c7d3e7fc90500c91112f50aaacef09 100644 (file)
@@ -24,7 +24,6 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <stdlib.h>
-#include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -37,6 +36,7 @@
 #include <common/common.h>
 #include <common/runas.h>
 #include <common/compat/getenv.h>
+#include <common/compat/string.h>
 
 #include "utils.h"
 #include "defaults.h"
@@ -92,9 +92,9 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
                }
 
                /* Cut the part we will be trying to resolve */
-               cut_path = strndup(path, next - path);
+               cut_path = lttng_strndup(path, next - path);
                if (cut_path == NULL) {
-                       PERROR("strndup");
+                       PERROR("lttng_strndup");
                        goto error;
                }
 
@@ -125,7 +125,7 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
                /* Free the allocated memory */
                free(cut_path);
                cut_path = NULL;
-       };
+       }
 
        /* Allocate memory for the resolved path if necessary */
        if (resolved_path == NULL) {
@@ -162,6 +162,8 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
                /* 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
@@ -228,9 +230,9 @@ char *utils_expand_path(const char *path)
        while ((next = strstr(absolute_path, "/./"))) {
 
                /* We prepare the start_path not containing it */
-               start_path = strndup(absolute_path, next - absolute_path);
+               start_path = lttng_strndup(absolute_path, next - absolute_path);
                if (!start_path) {
-                       PERROR("strndup");
+                       PERROR("lttng_strndup");
                        goto error;
                }
                /* And we concatenate it with the part after this string */
@@ -248,9 +250,9 @@ char *utils_expand_path(const char *path)
                }
 
                /* Then we prepare the start_path not containing it */
-               start_path = strndup(absolute_path, previous - absolute_path);
+               start_path = lttng_strndup(absolute_path, previous - absolute_path);
                if (!start_path) {
-                       PERROR("strndup");
+                       PERROR("lttng_strndup");
                        goto error;
                }
 
@@ -1195,6 +1197,7 @@ 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;
 
@@ -1210,13 +1213,34 @@ int utils_recursive_rmdir(const char *path)
                return -1;
        }
 
+       path_len = strlen(path);
        while ((entry = readdir(dir))) {
                if (!strcmp(entry->d_name, ".")
                                || !strcmp(entry->d_name, ".."))
                        continue;
-               switch (entry->d_type) {
-               case DT_DIR:
-               {
+
+               struct stat st;
+               size_t name_len;
+               char filename[PATH_MAX];
+
+               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;
+               }
+
+               if (S_ISDIR(st.st_mode)) {
                        char subpath[PATH_MAX];
 
                        strncpy(subpath, path, PATH_MAX);
@@ -1228,12 +1252,9 @@ int utils_recursive_rmdir(const char *path)
                        if (utils_recursive_rmdir(subpath)) {
                                is_empty = 0;
                        }
-                       break;
-               }
-               case DT_REG:
+               } else if (S_ISREG(st.st_mode)) {
                        is_empty = 0;
-                       break;
-               default:
+               } else {
                        ret = -EINVAL;
                        goto end;
                }
This page took 0.024416 seconds and 4 git commands to generate.