4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of the device-mapper userspace tools.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 static int _create_dir_recursive(const char *dir
)
29 log_verbose("Creating directory \"%s\"", dir
);
30 /* Create parent directories */
31 orig
= s
= dm_strdup(dir
);
32 while ((s
= strchr(s
, '/')) != NULL
) {
35 rc
= mkdir(orig
, 0777);
36 if (rc
< 0 && errno
!= EEXIST
) {
38 log_sys_error("mkdir", orig
);
45 /* Create final directory */
46 rc
= mkdir(dir
, 0777);
47 if (rc
< 0 && errno
!= EEXIST
) {
49 log_sys_error("mkdir", orig
);
59 int dm_create_dir(const char *dir
)
66 if (stat(dir
, &info
) < 0)
67 return _create_dir_recursive(dir
);
69 if (S_ISDIR(info
.st_mode
))
72 log_error("Directory \"%s\" not found", dir
);
76 int dm_fclose(FILE *stream
)
78 int prev_fail
= ferror(stream
);
79 int fclose_fail
= fclose(stream
);
81 /* If there was a previous failure, but fclose succeeded,
82 clear errno, since ferror does not set it, and its value
83 may be unrelated to the ferror-reported failure. */
84 if (prev_fail
&& !fclose_fail
)
87 return prev_fail
|| fclose_fail
? EOF
: 0;