summaryrefslogtreecommitdiffstats
path: root/libdm/libdm-file.c
diff options
context:
space:
mode:
authorAlasdair Kergon <agk@redhat.com>2005-01-06 18:22:44 +0000
committerAlasdair Kergon <agk@redhat.com>2005-01-06 18:22:44 +0000
commitf34c169a1bf04984b8a268aefc555d5abf72e4d2 (patch)
treed0806f65824724211bb396b6c49f78d9527eeaf3 /libdm/libdm-file.c
parent235592e9e89844de387ffd0cc5c06cf4e0ce6212 (diff)
downloadlvm2-f34c169a1bf04984b8a268aefc555d5abf72e4d2.tar.gz
lvm2-f34c169a1bf04984b8a268aefc555d5abf72e4d2.tar.xz
lvm2-f34c169a1bf04984b8a268aefc555d5abf72e4d2.zip
Attempt to fix /dev/mapper/control transparently if it's wrong.
Diffstat (limited to 'libdm/libdm-file.c')
-rw-r--r--libdm/libdm-file.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/libdm/libdm-file.c b/libdm/libdm-file.c
new file mode 100644
index 00000000..5295b9e9
--- /dev/null
+++ b/libdm/libdm-file.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
+ * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of the device-mapper userspace tools.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "lib.h"
+#include "libdm-file.h"
+
+#include <sys/file.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <malloc.h>
+
+static int _create_dir_recursive(const char *dir)
+{
+ char *orig, *s;
+ int rc;
+
+ log_verbose("Creating directory \"%s\"", dir);
+ /* Create parent directories */
+ orig = s = strdup(dir);
+ while ((s = strchr(s, '/')) != NULL) {
+ *s = '\0';
+ if (*orig) {
+ rc = mkdir(orig, 0777);
+ if (rc < 0 && errno != EEXIST) {
+ log_error("%s: mkdir failed: %s", orig,
+ strerror(errno));
+ free(orig);
+ return 0;
+ }
+ }
+ *s++ = '/';
+ }
+ free(orig);
+
+ /* Create final directory */
+ rc = mkdir(dir, 0777);
+ if (rc < 0 && errno != EEXIST) {
+ log_error("%s: mkdir failed: %s", orig,
+ strerror(errno));
+ return 0;
+ }
+ return 1;
+}
+
+int create_dir(const char *dir)
+{
+ struct stat info;
+
+ if (!*dir)
+ return 1;
+
+ if (stat(dir, &info) < 0)
+ return _create_dir_recursive(dir);
+
+ if (S_ISDIR(info.st_mode))
+ return 1;
+
+ log_error("Directory \"%s\" not found", dir);
+ return 0;
+}
+