Port: Add Solaris dirent compat
[lttng-tools.git] / src / common / utils.c
index aaf577bcfd1a47e172f9c49be4657e3c15d319c5..e3f1bf7e62ad8109db5cbefd211f81484f02d737 100644 (file)
@@ -17,7 +17,6 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
 #define _LGPL_SOURCE
 #include <assert.h>
 #include <ctype.h>
 #include <grp.h>
 #include <pwd.h>
 #include <sys/file.h>
-#include <dirent.h>
 
 #include <common/common.h>
 #include <common/runas.h>
 #include <common/compat/getenv.h>
 #include <common/compat/string.h>
+#include <common/compat/dirent.h>
 
 #include "utils.h"
 #include "defaults.h"
@@ -504,11 +503,13 @@ int utils_create_lock_file(const char *filepath)
 {
        int ret;
        int fd;
+       struct flock lock;
 
        assert(filepath);
 
-       fd = open(filepath, O_CREAT,
-               O_WRONLY | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+       memset(&lock, 0, sizeof(lock));
+       fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR |
+               S_IRGRP | S_IWGRP);
        if (fd < 0) {
                PERROR("open lock file %s", filepath);
                ret = -1;
@@ -520,8 +521,12 @@ int utils_create_lock_file(const char *filepath)
         * already a process using the same lock file running
         * and we should exit.
         */
-       ret = flock(fd, LOCK_EX | LOCK_NB);
-       if (ret) {
+       lock.l_whence = SEEK_SET;
+       lock.l_type = F_WRLCK;
+
+       ret = fcntl(fd, F_SETLK, &lock);
+       if (ret == -1) {
+               PERROR("fcntl lock file");
                ERR("Could not get lock file %s, another instance is running.",
                        filepath);
                if (close(fd)) {
@@ -1197,6 +1202,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;
 
@@ -1206,19 +1212,40 @@ int utils_recursive_rmdir(const char *path)
                PERROR("Cannot open '%s' path", path);
                return -1;
        }
-       dir_fd = dirfd(dir);
+       dir_fd = lttng_dirfd(dir);
        if (dir_fd < 0) {
-               PERROR("dirfd");
+               PERROR("lttng_dirfd");
                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);
@@ -1230,12 +1257,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.025324 seconds and 4 git commands to generate.