1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-22 17:35:59 +03:00
lvm2/libdm/libdm-file.c
Jim Meyering 3f311f1daa Export dm_create_dir (was create_dir) to help fix LVM2 link error
* lib/libdm-file.c (dm_create_dir): Rename from create_dir.
* lib/libdevmapper.h (dm_create_dir): Declare.
* lib/.exported_symbols: Add dm_create_dir.
* lib/ioctl/libdm-iface.c (_create_control): Update sole use.
Patch by Jun'ichi Nomura.  Details in
http://www.redhat.com/archives/lvm-devel/2007-July/msg00040.html
2007-07-28 10:48:36 +00:00

88 lines
1.9 KiB
C

/*
* 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>
static int _create_dir_recursive(const char *dir)
{
char *orig, *s;
int rc, r = 0;
log_verbose("Creating directory \"%s\"", dir);
/* Create parent directories */
orig = s = dm_strdup(dir);
while ((s = strchr(s, '/')) != NULL) {
*s = '\0';
if (*orig) {
rc = mkdir(orig, 0777);
if (rc < 0 && errno != EEXIST) {
if (errno != EROFS)
log_sys_error("mkdir", orig);
goto out;
}
}
*s++ = '/';
}
/* Create final directory */
rc = mkdir(dir, 0777);
if (rc < 0 && errno != EEXIST) {
if (errno != EROFS)
log_sys_error("mkdir", orig);
goto out;
}
r = 1;
out:
dm_free(orig);
return r;
}
int dm_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;
}
int dm_fclose(FILE *stream)
{
int prev_fail = ferror(stream);
int fclose_fail = fclose(stream);
/* If there was a previous failure, but fclose succeeded,
clear errno, since ferror does not set it, and its value
may be unrelated to the ferror-reported failure. */
if (prev_fail && !fclose_fail)
errno = 0;
return prev_fail || fclose_fail ? EOF : 0;
}