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