Don't report an error when the last section of the path exist in mkdir_recursive
[lttng-tools.git] / ltt-sessiond / utils.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; only version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28
29 #include <lttngerr.h>
30
31 #include "utils.h"
32
33 /*
34 * Write to writable pipe used to notify a thread.
35 */
36 int notify_thread_pipe(int wpipe)
37 {
38 int ret;
39
40 ret = write(wpipe, "!", 1);
41 if (ret < 0) {
42 PERROR("write poll pipe");
43 }
44
45 return ret;
46 }
47
48 /*
49 * Return pointer to home directory path using the env variable HOME.
50 *
51 * No home, NULL is returned.
52 */
53 const char *get_home_dir(void)
54 {
55 return ((const char *) getenv("HOME"));
56 }
57
58 /*
59 * Create recursively directory using the FULL path.
60 */
61 int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
62 {
63 int ret;
64 char *p, tmp[PATH_MAX];
65 size_t len;
66 mode_t old_umask;
67
68 ret = snprintf(tmp, sizeof(tmp), "%s", path);
69 if (ret < 0) {
70 PERROR("snprintf mkdir");
71 goto error;
72 }
73
74 len = ret;
75 if (tmp[len - 1] == '/') {
76 tmp[len - 1] = 0;
77 }
78
79 old_umask = umask(0);
80 for (p = tmp + 1; *p; p++) {
81 if (*p == '/') {
82 *p = 0;
83 ret = mkdir(tmp, mode);
84 if (ret < 0) {
85 if (!(errno == EEXIST)) {
86 PERROR("mkdir recursive");
87 ret = -errno;
88 goto umask_error;
89 }
90 } else if (ret == 0) {
91 /*
92 * We created the directory. Set its ownership to the
93 * user/group specified.
94 */
95 ret = chown(tmp, uid, gid);
96 if (ret < 0) {
97 PERROR("chown in mkdir recursive");
98 ret = -errno;
99 goto umask_error;
100 }
101 }
102 *p = '/';
103 }
104 }
105
106 ret = mkdir(tmp, mode);
107 if (ret < 0) {
108 if (!(errno == EEXIST)) {
109 PERROR("mkdir recursive last piece");
110 ret = -errno;
111 } else {
112 ret = 0;
113 }
114 } else if (ret == 0) {
115 /*
116 * We created the directory. Set its ownership to the user/group
117 * specified.
118 */
119 ret = chown(tmp, uid, gid);
120 if (ret < 0) {
121 PERROR("chown in mkdir recursive");
122 ret = -errno;
123 goto umask_error;
124 }
125 }
126
127 umask_error:
128 umask(old_umask);
129 error:
130 return ret;
131 }
This page took 0.045029 seconds and 4 git commands to generate.