1 /* $NetBSD: libdm-file.c,v 1.2 2011/01/05 14:57:28 haad Exp $ */ 2 3 /* 4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. 5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. 6 * 7 * This file is part of the device-mapper userspace tools. 8 * 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. 12 * 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 16 */ 17 18 #include "dmlib.h" 19 20 #include <sys/file.h> 21 #include <fcntl.h> 22 #include <dirent.h> 23 24 /* 25 * Created directories permissions are controled by umask values and 26 * they should be set by api user before calling this function. 27 * Changing directory owners is also left on caller. 28 */ 29 static int _create_dir_recursive(const char *dir) 30 { 31 char *orig, *s; 32 int rc, r = 0; 33 34 log_verbose("Creating directory \"%s\"", dir); 35 /* Create parent directories */ 36 orig = s = dm_strdup(dir); 37 while ((s = strchr(s, '/')) != NULL) { 38 *s = '\0'; 39 if (*orig) { 40 rc = mkdir(orig, 0777); 41 if (rc < 0 && errno != EEXIST) { 42 if (errno != EROFS) 43 log_sys_error("mkdir", orig); 44 goto out; 45 } 46 } 47 *s++ = '/'; 48 } 49 50 /* Create final directory */ 51 rc = mkdir(dir, 0777); 52 if (rc < 0 && errno != EEXIST) { 53 if (errno != EROFS) 54 log_sys_error("mkdir", orig); 55 goto out; 56 } 57 58 r = 1; 59 out: 60 dm_free(orig); 61 return r; 62 } 63 64 int dm_create_dir(const char *dir) 65 { 66 struct stat info; 67 68 if (!*dir) 69 return 1; 70 71 if (stat(dir, &info) < 0) 72 return _create_dir_recursive(dir); 73 74 if (S_ISDIR(info.st_mode)) 75 return 1; 76 77 log_error("Directory \"%s\" not found", dir); 78 return 0; 79 } 80 81 int dm_fclose(FILE *stream) 82 { 83 int prev_fail = ferror(stream); 84 int fclose_fail = fclose(stream); 85 86 /* If there was a previous failure, but fclose succeeded, 87 clear errno, since ferror does not set it, and its value 88 may be unrelated to the ferror-reported failure. */ 89 if (prev_fail && !fclose_fail) 90 errno = 0; 91 92 return prev_fail || fclose_fail ? EOF : 0; 93 } 94