Fix comments spacing
[lttng-tools.git] / ltt-sessiond / utils.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple 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 "utils.h"
30
31 int mkdir_recursive(const char *path, mode_t mode)
32 {
33 int ret;
34 char *p, tmp[PATH_MAX];
35 size_t len;
36 mode_t old_umask;
37
38 ret = snprintf(tmp, sizeof(tmp), "%s", path);
39 if (ret < 0) {
40 perror("snprintf mkdir");
41 goto error;
42 }
43
44 len = ret;
45 if (tmp[len - 1] == '/') {
46 tmp[len - 1] = 0;
47 }
48
49 old_umask = umask(0);
50 for (p = tmp + 1; *p; p++) {
51 if (*p == '/') {
52 *p = 0;
53 ret = mkdir(tmp, mode);
54 if (ret < 0) {
55 if (!(errno == EEXIST)) {
56 perror("mkdir recursive");
57 ret = errno;
58 goto umask_error;
59 }
60 }
61 *p = '/';
62 }
63 }
64
65 ret = mkdir(tmp, mode);
66 if (ret < 0) {
67 ret = errno;
68 }
69
70 umask_error:
71 umask(old_umask);
72 error:
73 return ret;
74 }
This page took 0.030094 seconds and 4 git commands to generate.