cpdir.c revision 30259
120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  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
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2930259Scharnier	"$Id$";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3230259Scharnier#include <err.h>
3330259Scharnier#include <errno.h>
3430259Scharnier#include <fcntl.h>
3520253Sjoerg#include <stdio.h>
3620253Sjoerg#include <string.h>
3720253Sjoerg#include <stdlib.h>
3830259Scharnier#include <unistd.h>
3920253Sjoerg#include <sys/types.h>
4020253Sjoerg#include <sys/stat.h>
4130259Scharnier#include <sys/param.h>
4220253Sjoerg#include <dirent.h>
4320253Sjoerg
4420253Sjoerg#include "pwupd.h"
4520253Sjoerg
4620253Sjoergvoid
4720253Sjoergcopymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid)
4820253Sjoerg{
4920253Sjoerg	int             rc = 0;
5020253Sjoerg	char            src[MAXPATHLEN];
5120253Sjoerg	char            dst[MAXPATHLEN];
5220253Sjoerg
5320253Sjoerg	if (mkdir(dir, mode) != 0 && errno != EEXIST) {
5430259Scharnier		warn("mkdir(%s)", dir);
5520253Sjoerg	} else {
5620253Sjoerg		int             infd, outfd;
5720253Sjoerg		struct stat     st;
5820253Sjoerg
5920253Sjoerg		static char     counter = 0;
6020253Sjoerg		static char    *copybuf = NULL;
6120253Sjoerg
6220253Sjoerg		++counter;
6320253Sjoerg		chown(dir, uid, gid);
6420253Sjoerg		if (skel == NULL || *skel == '\0')
6520253Sjoerg			rc = 1;
6620253Sjoerg		else {
6720253Sjoerg			DIR            *d = opendir(skel);
6820253Sjoerg
6920253Sjoerg			if (d != NULL) {
7020253Sjoerg				struct dirent  *e;
7120253Sjoerg
7220253Sjoerg				while ((e = readdir(d)) != NULL) {
7320253Sjoerg					char           *p = e->d_name;
7420253Sjoerg
7520253Sjoerg					sprintf(src, "%s/%s", skel, p);
7620253Sjoerg					if (stat(src, &st) == 0) {
7720253Sjoerg						if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
7820253Sjoerg							p += 3;
7920253Sjoerg						sprintf(dst, "%s/%s", dir, p);
8020253Sjoerg						if (S_ISDIR(st.st_mode)) {	/* Recurse for this */
8120253Sjoerg							if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0)
8220253Sjoerg								copymkdir(dst, src, (st.st_mode & 0777), uid, gid);
8320253Sjoerg							/*
8420253Sjoerg							 * Note: don't propogate 'special' attributes
8520253Sjoerg							 */
8620253Sjoerg						} else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
8720253Sjoerg							if ((infd = open(src, O_RDONLY)) == -1) {
8820253Sjoerg								close(outfd);
8920253Sjoerg								remove(dst);
9020253Sjoerg							} else {
9120253Sjoerg								int             b;
9220253Sjoerg
9320253Sjoerg								/*
9420253Sjoerg								 * Allocate our copy buffer if we need to
9520253Sjoerg								 */
9620253Sjoerg								if (copybuf == NULL)
9720253Sjoerg									copybuf = malloc(4096);
9820253Sjoerg								while ((b = read(infd, copybuf, 4096)) > 0)
9920253Sjoerg									write(outfd, copybuf, b);
10020253Sjoerg								close(infd);
10120253Sjoerg								close(outfd);
10220253Sjoerg								chown(dst, uid, gid);
10320253Sjoerg							}
10420253Sjoerg						}
10520253Sjoerg					}
10620253Sjoerg				}
10720253Sjoerg				closedir(d);
10820253Sjoerg			}
10920253Sjoerg		}
11020253Sjoerg		if (--counter == 0 && copybuf != NULL) {
11120253Sjoerg			free(copybuf);
11220253Sjoerg			copybuf = NULL;
11320253Sjoerg		}
11420253Sjoerg	}
11520253Sjoerg}
11620267Sjoerg
117