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 */ 26d6f907dcSJoerg Wunsch 271dcc6ec7SPhilippe Charnier #ifndef lint 281dcc6ec7SPhilippe Charnier static const char rcsid[] = 291dcc6ec7SPhilippe Charnier "$Id$"; 301dcc6ec7SPhilippe Charnier #endif /* not lint */ 311dcc6ec7SPhilippe Charnier 321dcc6ec7SPhilippe Charnier #include <err.h> 331dcc6ec7SPhilippe Charnier #include <errno.h> 341dcc6ec7SPhilippe Charnier #include <fcntl.h> 35d6f907dcSJoerg Wunsch #include <stdio.h> 36d6f907dcSJoerg Wunsch #include <string.h> 37d6f907dcSJoerg Wunsch #include <stdlib.h> 381dcc6ec7SPhilippe Charnier #include <unistd.h> 39d6f907dcSJoerg Wunsch #include <sys/types.h> 40d6f907dcSJoerg Wunsch #include <sys/stat.h> 41d6f907dcSJoerg Wunsch #include <sys/param.h> 421dcc6ec7SPhilippe Charnier #include <dirent.h> 43d6f907dcSJoerg Wunsch 44d6f907dcSJoerg Wunsch #include "pwupd.h" 45d6f907dcSJoerg Wunsch 46d6f907dcSJoerg Wunsch void 47d6f907dcSJoerg Wunsch copymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid) 48d6f907dcSJoerg Wunsch { 49d6f907dcSJoerg Wunsch int rc = 0; 50d6f907dcSJoerg Wunsch char src[MAXPATHLEN]; 51d6f907dcSJoerg Wunsch char dst[MAXPATHLEN]; 52d6f907dcSJoerg Wunsch 53d6f907dcSJoerg Wunsch if (mkdir(dir, mode) != 0 && errno != EEXIST) { 541dcc6ec7SPhilippe Charnier warn("mkdir(%s)", dir); 55d6f907dcSJoerg Wunsch } else { 56d6f907dcSJoerg Wunsch int infd, outfd; 57d6f907dcSJoerg Wunsch struct stat st; 58d6f907dcSJoerg Wunsch 59d6f907dcSJoerg Wunsch static char counter = 0; 60d6f907dcSJoerg Wunsch static char *copybuf = NULL; 61d6f907dcSJoerg Wunsch 62d6f907dcSJoerg Wunsch ++counter; 63d6f907dcSJoerg Wunsch chown(dir, uid, gid); 64d6f907dcSJoerg Wunsch if (skel == NULL || *skel == '\0') 65d6f907dcSJoerg Wunsch rc = 1; 66d6f907dcSJoerg Wunsch else { 67d6f907dcSJoerg Wunsch DIR *d = opendir(skel); 68d6f907dcSJoerg Wunsch 69d6f907dcSJoerg Wunsch if (d != NULL) { 70d6f907dcSJoerg Wunsch struct dirent *e; 71d6f907dcSJoerg Wunsch 72d6f907dcSJoerg Wunsch while ((e = readdir(d)) != NULL) { 73d6f907dcSJoerg Wunsch char *p = e->d_name; 74d6f907dcSJoerg Wunsch 75d6f907dcSJoerg Wunsch sprintf(src, "%s/%s", skel, p); 76d6f907dcSJoerg Wunsch if (stat(src, &st) == 0) { 77d6f907dcSJoerg Wunsch if (strncmp(p, "dot.", 4) == 0) /* Conversion */ 78d6f907dcSJoerg Wunsch p += 3; 79d6f907dcSJoerg Wunsch sprintf(dst, "%s/%s", dir, p); 80d6f907dcSJoerg Wunsch if (S_ISDIR(st.st_mode)) { /* Recurse for this */ 81d6f907dcSJoerg Wunsch if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) 82d6f907dcSJoerg Wunsch copymkdir(dst, src, (st.st_mode & 0777), uid, gid); 83d6f907dcSJoerg Wunsch /* 84d6f907dcSJoerg Wunsch * Note: don't propogate 'special' attributes 85d6f907dcSJoerg Wunsch */ 86d6f907dcSJoerg Wunsch } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) { 87d6f907dcSJoerg Wunsch if ((infd = open(src, O_RDONLY)) == -1) { 88d6f907dcSJoerg Wunsch close(outfd); 89d6f907dcSJoerg Wunsch remove(dst); 90d6f907dcSJoerg Wunsch } else { 91d6f907dcSJoerg Wunsch int b; 92d6f907dcSJoerg Wunsch 93d6f907dcSJoerg Wunsch /* 94d6f907dcSJoerg Wunsch * Allocate our copy buffer if we need to 95d6f907dcSJoerg Wunsch */ 96d6f907dcSJoerg Wunsch if (copybuf == NULL) 97d6f907dcSJoerg Wunsch copybuf = malloc(4096); 98d6f907dcSJoerg Wunsch while ((b = read(infd, copybuf, 4096)) > 0) 99d6f907dcSJoerg Wunsch write(outfd, copybuf, b); 100d6f907dcSJoerg Wunsch close(infd); 101d6f907dcSJoerg Wunsch close(outfd); 102d6f907dcSJoerg Wunsch chown(dst, uid, gid); 103d6f907dcSJoerg Wunsch } 104d6f907dcSJoerg Wunsch } 105d6f907dcSJoerg Wunsch } 106d6f907dcSJoerg Wunsch } 107d6f907dcSJoerg Wunsch closedir(d); 108d6f907dcSJoerg Wunsch } 109d6f907dcSJoerg Wunsch } 110d6f907dcSJoerg Wunsch if (--counter == 0 && copybuf != NULL) { 111d6f907dcSJoerg Wunsch free(copybuf); 112d6f907dcSJoerg Wunsch copybuf = NULL; 113d6f907dcSJoerg Wunsch } 114d6f907dcSJoerg Wunsch } 115d6f907dcSJoerg Wunsch } 11648aee7f3SJoerg Wunsch 117