cpdir.c revision 20267
120253Sjoerg/*- 220253Sjoerg * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>. 320253Sjoerg * All rights reserved. 420253Sjoerg * 520253Sjoerg * Redistribution and use in source and binary forms, with or without 620253Sjoerg * modification, are permitted provided that the following conditions 720253Sjoerg * are met: 820253Sjoerg * 1. Redistributions of source code must retain the above copyright 920253Sjoerg * notice, this list of conditions and the following disclaimer as 1020253Sjoerg * the first lines of this file unmodified. 1120253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1220253Sjoerg * notice, this list of conditions and the following disclaimer in the 1320253Sjoerg * documentation and/or other materials provided with the distribution. 1420253Sjoerg * 3. All advertising materials mentioning features or use of this software 1520253Sjoerg * must display the following acknowledgement: 1620253Sjoerg * This product includes software developed by David L. Nugent. 1720253Sjoerg * 4. The name of the author may not be used to endorse or promote products 1820253Sjoerg * derived from this software without specific prior written permission. 1920253Sjoerg * 2020253Sjoerg * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND 2120253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2220253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2320253Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE 2420253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2520253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2620253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2720253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2820253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2920253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3020253Sjoerg * SUCH DAMAGE. 3120253Sjoerg * 3220253Sjoerg * $Id$ 3320253Sjoerg */ 3420253Sjoerg 3520253Sjoerg#include <stdio.h> 3620253Sjoerg#include <string.h> 3720253Sjoerg#include <stdlib.h> 3820253Sjoerg#include <fcntl.h> 3920253Sjoerg#include <sys/types.h> 4020253Sjoerg#include <sys/stat.h> 4120253Sjoerg#include <dirent.h> 4220253Sjoerg#include <unistd.h> 4320253Sjoerg#include <sys/param.h> 4420253Sjoerg#include <errno.h> 4520253Sjoerg 4620253Sjoerg#include "pwupd.h" 4720253Sjoerg 4820253Sjoergvoid 4920253Sjoergcopymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid) 5020253Sjoerg{ 5120253Sjoerg int rc = 0; 5220253Sjoerg char src[MAXPATHLEN]; 5320253Sjoerg char dst[MAXPATHLEN]; 5420253Sjoerg 5520253Sjoerg if (mkdir(dir, mode) != 0 && errno != EEXIST) { 5620253Sjoerg sprintf(src, "mkdir(%s)", dir); 5720253Sjoerg perror(src); 5820253Sjoerg } else { 5920253Sjoerg int infd, outfd; 6020253Sjoerg struct stat st; 6120253Sjoerg 6220253Sjoerg static char counter = 0; 6320253Sjoerg static char *copybuf = NULL; 6420253Sjoerg 6520253Sjoerg ++counter; 6620253Sjoerg chown(dir, uid, gid); 6720253Sjoerg if (skel == NULL || *skel == '\0') 6820253Sjoerg rc = 1; 6920253Sjoerg else { 7020253Sjoerg DIR *d = opendir(skel); 7120253Sjoerg 7220253Sjoerg if (d != NULL) { 7320253Sjoerg struct dirent *e; 7420253Sjoerg 7520253Sjoerg while ((e = readdir(d)) != NULL) { 7620253Sjoerg char *p = e->d_name; 7720253Sjoerg 7820253Sjoerg sprintf(src, "%s/%s", skel, p); 7920253Sjoerg if (stat(src, &st) == 0) { 8020253Sjoerg if (strncmp(p, "dot.", 4) == 0) /* Conversion */ 8120253Sjoerg p += 3; 8220253Sjoerg sprintf(dst, "%s/%s", dir, p); 8320253Sjoerg if (S_ISDIR(st.st_mode)) { /* Recurse for this */ 8420253Sjoerg if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) 8520253Sjoerg copymkdir(dst, src, (st.st_mode & 0777), uid, gid); 8620253Sjoerg /* 8720253Sjoerg * Note: don't propogate 'special' attributes 8820253Sjoerg */ 8920253Sjoerg } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) { 9020253Sjoerg if ((infd = open(src, O_RDONLY)) == -1) { 9120253Sjoerg close(outfd); 9220253Sjoerg remove(dst); 9320253Sjoerg } else { 9420253Sjoerg int b; 9520253Sjoerg 9620253Sjoerg /* 9720253Sjoerg * Allocate our copy buffer if we need to 9820253Sjoerg */ 9920253Sjoerg if (copybuf == NULL) 10020253Sjoerg copybuf = malloc(4096); 10120253Sjoerg while ((b = read(infd, copybuf, 4096)) > 0) 10220253Sjoerg write(outfd, copybuf, b); 10320253Sjoerg close(infd); 10420253Sjoerg close(outfd); 10520253Sjoerg chown(dst, uid, gid); 10620253Sjoerg } 10720253Sjoerg } 10820253Sjoerg } 10920253Sjoerg } 11020253Sjoerg closedir(d); 11120253Sjoerg } 11220253Sjoerg } 11320253Sjoerg if (--counter == 0 && copybuf != NULL) { 11420253Sjoerg free(copybuf); 11520253Sjoerg copybuf = NULL; 11620253Sjoerg } 11720253Sjoerg } 11820253Sjoerg} 11920267Sjoerg 120