Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 : : * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
4 : : *
5 : : * This file is part of the device-mapper userspace tools.
6 : : *
7 : : * This copyrighted material is made available to anyone wishing to use,
8 : : * modify, copy, or redistribute it subject to the terms and conditions
9 : : * of the GNU Lesser General Public License v.2.1.
10 : : *
11 : : * You should have received a copy of the GNU Lesser General Public License
12 : : * along with this program; if not, write to the Free Software Foundation,
13 : : * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 : : */
15 : :
16 : : #include "dmlib.h"
17 : :
18 : : #include <sys/file.h>
19 : : #include <fcntl.h>
20 : : #include <dirent.h>
21 : :
22 : 0 : static int _create_dir_recursive(const char *dir)
23 : : {
24 : : char *orig, *s;
25 : 0 : int rc, r = 0;
26 : :
27 [ # # ]: 0 : log_verbose("Creating directory \"%s\"", dir);
28 : : /* Create parent directories */
29 : 0 : orig = s = dm_strdup(dir);
30 [ # # ]: 0 : while ((s = strchr(s, '/')) != NULL) {
31 : 0 : *s = '\0';
32 [ # # ]: 0 : if (*orig) {
33 : 0 : rc = mkdir(orig, 0777);
34 [ # # # # ]: 0 : if (rc < 0 && errno != EEXIST) {
35 [ # # ]: 0 : if (errno != EROFS)
36 [ # # ]: 0 : log_sys_error("mkdir", orig);
37 : 0 : goto out;
38 : : }
39 : : }
40 : 0 : *s++ = '/';
41 : : }
42 : :
43 : : /* Create final directory */
44 : 0 : rc = mkdir(dir, 0777);
45 [ # # # # ]: 0 : if (rc < 0 && errno != EEXIST) {
46 [ # # ]: 0 : if (errno != EROFS)
47 [ # # ]: 0 : log_sys_error("mkdir", orig);
48 : 0 : goto out;
49 : : }
50 : :
51 : 0 : r = 1;
52 : : out:
53 : 0 : dm_free(orig);
54 : 0 : return r;
55 : : }
56 : :
57 : 2 : int dm_create_dir(const char *dir)
58 : : {
59 : : struct stat info;
60 : :
61 [ - + ]: 2 : if (!*dir)
62 : 0 : return 1;
63 : :
64 [ - + ]: 2 : if (stat(dir, &info) < 0)
65 : 0 : return _create_dir_recursive(dir);
66 : :
67 [ + - ]: 2 : if (S_ISDIR(info.st_mode))
68 : 2 : return 1;
69 : :
70 [ # # ]: 0 : log_error("Directory \"%s\" not found", dir);
71 : 2 : return 0;
72 : : }
73 : :
74 : 0 : int dm_fclose(FILE *stream)
75 : : {
76 : 0 : int prev_fail = ferror(stream);
77 : 0 : int fclose_fail = fclose(stream);
78 : :
79 : : /* If there was a previous failure, but fclose succeeded,
80 : : clear errno, since ferror does not set it, and its value
81 : : may be unrelated to the ferror-reported failure. */
82 [ # # # # ]: 0 : if (prev_fail && !fclose_fail)
83 : 0 : errno = 0;
84 : :
85 [ # # ][ # # ]: 0 : return prev_fail || fclose_fail ? EOF : 0;
86 : : }
|