Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / libdm / libdm-file.c
blob3a3fea4d002c6c01d10ab3a63b057d7624a39aa2
1 /* $NetBSD$ */
3 /*
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
18 #include "dmlib.h"
20 #include <sys/file.h>
21 #include <fcntl.h>
22 #include <dirent.h>
24 static int _create_dir_recursive(const char *dir)
26 char *orig, *s;
27 int rc, r = 0;
29 log_verbose("Creating directory \"%s\"", dir);
30 /* Create parent directories */
31 orig = s = dm_strdup(dir);
32 while ((s = strchr(s, '/')) != NULL) {
33 *s = '\0';
34 if (*orig) {
35 rc = mkdir(orig, 0777);
36 if (rc < 0 && errno != EEXIST) {
37 if (errno != EROFS)
38 log_sys_error("mkdir", orig);
39 goto out;
42 *s++ = '/';
45 /* Create final directory */
46 rc = mkdir(dir, 0777);
47 if (rc < 0 && errno != EEXIST) {
48 if (errno != EROFS)
49 log_sys_error("mkdir", orig);
50 goto out;
53 r = 1;
54 out:
55 dm_free(orig);
56 return r;
59 int dm_create_dir(const char *dir)
61 struct stat info;
63 if (!*dir)
64 return 1;
66 if (stat(dir, &info) < 0)
67 return _create_dir_recursive(dir);
69 if (S_ISDIR(info.st_mode))
70 return 1;
72 log_error("Directory \"%s\" not found", dir);
73 return 0;
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)
85 errno = 0;
87 return prev_fail || fclose_fail ? EOF : 0;