Command metadata regenerate
[lttng-tools.git] / src / common / utils.c
index ee53477de559fedaf43eabfa194ee3255e58b293..d71d92446ce71e14177e26005797211c4f7f0730 100644 (file)
@@ -30,6 +30,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include <sys/file.h>
+#include <unistd.h>
 
 #include <common/common.h>
 #include <common/runas.h>
@@ -551,6 +552,44 @@ error:
        return fd;
 }
 
+/*
+ * On some filesystems (e.g. nfs), mkdir will validate access rights before
+ * checking for the existence of the path element. This means that on a setup
+ * where "/home/" is a mounted NFS share, and running as an unpriviledged user,
+ * recursively creating a path of the form "/home/my_user/trace/" will fail with
+ * EACCES on mkdir("/home", ...).
+ *
+ * Performing a stat(...) on the path to check for existence allows us to
+ * work around this behaviour.
+ */
+static
+int mkdir_check_exists(const char *path, mode_t mode)
+{
+       int ret = 0;
+       struct stat st;
+
+       ret = stat(path, &st);
+       if (ret == 0) {
+               if (S_ISDIR(st.st_mode)) {
+                       /* Directory exists, skip. */
+                       goto end;
+               } else {
+                       /* Exists, but is not a directory. */
+                       errno = ENOTDIR;
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       /*
+        * Let mkdir handle other errors as the caller expects mkdir
+        * semantics.
+        */
+       ret = mkdir(path, mode);
+end:
+       return ret;
+}
+
 /*
  * Create directory using the given path and mode.
  *
@@ -562,7 +601,7 @@ int utils_mkdir(const char *path, mode_t mode, int uid, int gid)
        int ret;
 
        if (uid < 0 || gid < 0) {
-               ret = mkdir(path, mode);
+               ret = mkdir_check_exists(path, mode);
        } else {
                ret = run_as_mkdir(path, mode, uid, gid);
        }
@@ -617,9 +656,9 @@ int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode)
                                ret = -1;
                                goto error;
                        }
-                       ret = mkdir(tmp, mode);
+                       ret = mkdir_check_exists(tmp, mode);
                        if (ret < 0) {
-                               if (errno != EEXIST) {
+                               if (errno != EACCES) {
                                        PERROR("mkdir recursive");
                                        ret = -errno;
                                        goto error;
@@ -629,14 +668,10 @@ int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode)
                }
        }
 
-       ret = mkdir(tmp, mode);
+       ret = mkdir_check_exists(tmp, mode);
        if (ret < 0) {
-               if (errno != EEXIST) {
-                       PERROR("mkdir recursive last element");
-                       ret = -errno;
-               } else {
-                       ret = 0;
-               }
+               PERROR("mkdir recursive last element");
+               ret = -errno;
        }
 
 error:
@@ -808,7 +843,6 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
 {
        int ret;
 
-       assert(new_count);
        assert(stream_fd);
 
        ret = close(out_fd);
@@ -831,18 +865,22 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
                 * Unlinking the old file rather than overwriting it
                 * achieves this.
                 */
-               *new_count = (*new_count + 1) % count;
-               ret = utils_unlink_stream_file(path_name, file_name,
-                               size, *new_count, uid, gid, 0);
+               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 {
-               (*new_count)++;
+               if (new_count) {
+                       (*new_count)++;
+               }
        }
 
-       ret = utils_create_stream_file(path_name, file_name, size, *new_count,
-                       uid, gid, 0);
+       ret = utils_create_stream_file(path_name, file_name, size,
+                       new_count ? *new_count : 0, uid, gid, 0);
        if (ret < 0) {
                goto error;
        }
@@ -1287,3 +1325,23 @@ end:
        }
        return ret;
 }
+
+LTTNG_HIDDEN
+int utils_truncate_stream_file(int fd, off_t length)
+{
+       int ret;
+
+       ret = ftruncate(fd, length);
+       if (ret < 0) {
+               PERROR("ftruncate");
+               goto end;
+       }
+       ret = lseek(fd, length, SEEK_SET);
+       if (ret < 0) {
+               PERROR("lseek");
+               goto end;
+       }
+
+end:
+       return ret;
+}
This page took 0.026599 seconds and 5 git commands to generate.