1d6f907dcSJoerg Wunsch /*- 2ad7cf975SJoerg Wunsch * Copyright (C) 1996 3ad7cf975SJoerg Wunsch * David L. Nugent. All rights reserved. 4d6f907dcSJoerg Wunsch * 5d6f907dcSJoerg Wunsch * Redistribution and use in source and binary forms, with or without 6d6f907dcSJoerg Wunsch * modification, are permitted provided that the following conditions 7d6f907dcSJoerg Wunsch * are met: 8d6f907dcSJoerg Wunsch * 1. Redistributions of source code must retain the above copyright 9ad7cf975SJoerg Wunsch * notice, this list of conditions and the following disclaimer. 10d6f907dcSJoerg Wunsch * 2. Redistributions in binary form must reproduce the above copyright 11d6f907dcSJoerg Wunsch * notice, this list of conditions and the following disclaimer in the 12d6f907dcSJoerg Wunsch * documentation and/or other materials provided with the distribution. 13d6f907dcSJoerg Wunsch * 14ad7cf975SJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 15d6f907dcSJoerg Wunsch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16d6f907dcSJoerg Wunsch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17ad7cf975SJoerg Wunsch * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18d6f907dcSJoerg Wunsch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19d6f907dcSJoerg Wunsch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20d6f907dcSJoerg Wunsch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21d6f907dcSJoerg Wunsch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22d6f907dcSJoerg Wunsch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23d6f907dcSJoerg Wunsch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24d6f907dcSJoerg Wunsch * SUCH DAMAGE. 25d6f907dcSJoerg Wunsch * 26ad7cf975SJoerg Wunsch * $Id: cpdir.c,v 1.1.1.2 1996/12/09 23:55:15 joerg Exp $ 27d6f907dcSJoerg Wunsch */ 28d6f907dcSJoerg Wunsch 29d6f907dcSJoerg Wunsch #include <stdio.h> 30d6f907dcSJoerg Wunsch #include <string.h> 31d6f907dcSJoerg Wunsch #include <stdlib.h> 32d6f907dcSJoerg Wunsch #include <fcntl.h> 33d6f907dcSJoerg Wunsch #include <sys/types.h> 34d6f907dcSJoerg Wunsch #include <sys/stat.h> 35d6f907dcSJoerg Wunsch #include <dirent.h> 36d6f907dcSJoerg Wunsch #include <unistd.h> 37d6f907dcSJoerg Wunsch #include <sys/param.h> 38d6f907dcSJoerg Wunsch #include <errno.h> 39d6f907dcSJoerg Wunsch 40d6f907dcSJoerg Wunsch #include "pwupd.h" 41d6f907dcSJoerg Wunsch 42d6f907dcSJoerg Wunsch void 43d6f907dcSJoerg Wunsch copymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid) 44d6f907dcSJoerg Wunsch { 45d6f907dcSJoerg Wunsch int rc = 0; 46d6f907dcSJoerg Wunsch char src[MAXPATHLEN]; 47d6f907dcSJoerg Wunsch char dst[MAXPATHLEN]; 48d6f907dcSJoerg Wunsch 49d6f907dcSJoerg Wunsch if (mkdir(dir, mode) != 0 && errno != EEXIST) { 50d6f907dcSJoerg Wunsch sprintf(src, "mkdir(%s)", dir); 51d6f907dcSJoerg Wunsch perror(src); 52d6f907dcSJoerg Wunsch } else { 53d6f907dcSJoerg Wunsch int infd, outfd; 54d6f907dcSJoerg Wunsch struct stat st; 55d6f907dcSJoerg Wunsch 56d6f907dcSJoerg Wunsch static char counter = 0; 57d6f907dcSJoerg Wunsch static char *copybuf = NULL; 58d6f907dcSJoerg Wunsch 59d6f907dcSJoerg Wunsch ++counter; 60d6f907dcSJoerg Wunsch chown(dir, uid, gid); 61d6f907dcSJoerg Wunsch if (skel == NULL || *skel == '\0') 62d6f907dcSJoerg Wunsch rc = 1; 63d6f907dcSJoerg Wunsch else { 64d6f907dcSJoerg Wunsch DIR *d = opendir(skel); 65d6f907dcSJoerg Wunsch 66d6f907dcSJoerg Wunsch if (d != NULL) { 67d6f907dcSJoerg Wunsch struct dirent *e; 68d6f907dcSJoerg Wunsch 69d6f907dcSJoerg Wunsch while ((e = readdir(d)) != NULL) { 70d6f907dcSJoerg Wunsch char *p = e->d_name; 71d6f907dcSJoerg Wunsch 72d6f907dcSJoerg Wunsch sprintf(src, "%s/%s", skel, p); 73d6f907dcSJoerg Wunsch if (stat(src, &st) == 0) { 74d6f907dcSJoerg Wunsch if (strncmp(p, "dot.", 4) == 0) /* Conversion */ 75d6f907dcSJoerg Wunsch p += 3; 76d6f907dcSJoerg Wunsch sprintf(dst, "%s/%s", dir, p); 77d6f907dcSJoerg Wunsch if (S_ISDIR(st.st_mode)) { /* Recurse for this */ 78d6f907dcSJoerg Wunsch if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) 79d6f907dcSJoerg Wunsch copymkdir(dst, src, (st.st_mode & 0777), uid, gid); 80d6f907dcSJoerg Wunsch /* 81d6f907dcSJoerg Wunsch * Note: don't propogate 'special' attributes 82d6f907dcSJoerg Wunsch */ 83d6f907dcSJoerg Wunsch } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) { 84d6f907dcSJoerg Wunsch if ((infd = open(src, O_RDONLY)) == -1) { 85d6f907dcSJoerg Wunsch close(outfd); 86d6f907dcSJoerg Wunsch remove(dst); 87d6f907dcSJoerg Wunsch } else { 88d6f907dcSJoerg Wunsch int b; 89d6f907dcSJoerg Wunsch 90d6f907dcSJoerg Wunsch /* 91d6f907dcSJoerg Wunsch * Allocate our copy buffer if we need to 92d6f907dcSJoerg Wunsch */ 93d6f907dcSJoerg Wunsch if (copybuf == NULL) 94d6f907dcSJoerg Wunsch copybuf = malloc(4096); 95d6f907dcSJoerg Wunsch while ((b = read(infd, copybuf, 4096)) > 0) 96d6f907dcSJoerg Wunsch write(outfd, copybuf, b); 97d6f907dcSJoerg Wunsch close(infd); 98d6f907dcSJoerg Wunsch close(outfd); 99d6f907dcSJoerg Wunsch chown(dst, uid, gid); 100d6f907dcSJoerg Wunsch } 101d6f907dcSJoerg Wunsch } 102d6f907dcSJoerg Wunsch } 103d6f907dcSJoerg Wunsch } 104d6f907dcSJoerg Wunsch closedir(d); 105d6f907dcSJoerg Wunsch } 106d6f907dcSJoerg Wunsch } 107d6f907dcSJoerg Wunsch if (--counter == 0 && copybuf != NULL) { 108d6f907dcSJoerg Wunsch free(copybuf); 109d6f907dcSJoerg Wunsch copybuf = NULL; 110d6f907dcSJoerg Wunsch } 111d6f907dcSJoerg Wunsch } 112d6f907dcSJoerg Wunsch } 11348aee7f3SJoerg Wunsch 114