xref: /xnu-10002.1.13/SETUP/installfile/installfile.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 2012 Apple, Inc. All rights reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*1031c584SApple OSS Distributions  * file.
12*1031c584SApple OSS Distributions  *
13*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*1031c584SApple OSS Distributions  * limitations under the License.
20*1031c584SApple OSS Distributions  *
21*1031c584SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*1031c584SApple OSS Distributions  */
23*1031c584SApple OSS Distributions 
24*1031c584SApple OSS Distributions #include <stdio.h>
25*1031c584SApple OSS Distributions #include <stdlib.h>
26*1031c584SApple OSS Distributions #include <unistd.h>
27*1031c584SApple OSS Distributions #include <stdbool.h>
28*1031c584SApple OSS Distributions #include <errno.h>
29*1031c584SApple OSS Distributions #include <err.h>
30*1031c584SApple OSS Distributions #include <sysexits.h>
31*1031c584SApple OSS Distributions 
32*1031c584SApple OSS Distributions #include <sys/stat.h>
33*1031c584SApple OSS Distributions #include <sys/fcntl.h>
34*1031c584SApple OSS Distributions #include <sys/param.h>
35*1031c584SApple OSS Distributions #include <sys/time.h>
36*1031c584SApple OSS Distributions 
37*1031c584SApple OSS Distributions #include <copyfile.h>
38*1031c584SApple OSS Distributions 
39*1031c584SApple OSS Distributions void usage(void);
40*1031c584SApple OSS Distributions 
41*1031c584SApple OSS Distributions int
main(int argc,char * argv[])42*1031c584SApple OSS Distributions main(int argc, char * argv[])
43*1031c584SApple OSS Distributions {
44*1031c584SApple OSS Distributions 	struct stat sb;
45*1031c584SApple OSS Distributions 	void *mset;
46*1031c584SApple OSS Distributions 	mode_t mode;
47*1031c584SApple OSS Distributions 	bool gotmode = false;
48*1031c584SApple OSS Distributions 	int ch;
49*1031c584SApple OSS Distributions 	int ret;
50*1031c584SApple OSS Distributions 	int srcfd, dstfd;
51*1031c584SApple OSS Distributions 	const char *src = NULL;
52*1031c584SApple OSS Distributions 	const char *dst = NULL;
53*1031c584SApple OSS Distributions 	char dsttmpname[MAXPATHLEN];
54*1031c584SApple OSS Distributions 
55*1031c584SApple OSS Distributions 	while ((ch = getopt(argc, argv, "cSm:")) != -1) {
56*1031c584SApple OSS Distributions 		switch (ch) {
57*1031c584SApple OSS Distributions 		case 'c':
58*1031c584SApple OSS Distributions 		case 'S':
59*1031c584SApple OSS Distributions 			/* ignored for compatibility */
60*1031c584SApple OSS Distributions 			break;
61*1031c584SApple OSS Distributions 		case 'm':
62*1031c584SApple OSS Distributions 			gotmode = true;
63*1031c584SApple OSS Distributions 			mset = setmode(optarg);
64*1031c584SApple OSS Distributions 			if (!mset) {
65*1031c584SApple OSS Distributions 				errx(EX_USAGE, "Unrecognized mode %s", optarg);
66*1031c584SApple OSS Distributions 			}
67*1031c584SApple OSS Distributions 
68*1031c584SApple OSS Distributions 			mode = getmode(mset, 0);
69*1031c584SApple OSS Distributions 			free(mset);
70*1031c584SApple OSS Distributions 			break;
71*1031c584SApple OSS Distributions 		case '?':
72*1031c584SApple OSS Distributions 		default:
73*1031c584SApple OSS Distributions 			usage();
74*1031c584SApple OSS Distributions 		}
75*1031c584SApple OSS Distributions 	}
76*1031c584SApple OSS Distributions 
77*1031c584SApple OSS Distributions 	argc -= optind;
78*1031c584SApple OSS Distributions 	argv += optind;
79*1031c584SApple OSS Distributions 
80*1031c584SApple OSS Distributions 	if (argc < 2) {
81*1031c584SApple OSS Distributions 		usage();
82*1031c584SApple OSS Distributions 	}
83*1031c584SApple OSS Distributions 
84*1031c584SApple OSS Distributions 	src = argv[0];
85*1031c584SApple OSS Distributions 	dst = argv[1];
86*1031c584SApple OSS Distributions 
87*1031c584SApple OSS Distributions 	srcfd = open(src, O_RDONLY, 0);
88*1031c584SApple OSS Distributions 	if (srcfd < 0) {
89*1031c584SApple OSS Distributions 		err(EX_NOINPUT, "open(%s)", src);
90*1031c584SApple OSS Distributions 	}
91*1031c584SApple OSS Distributions 
92*1031c584SApple OSS Distributions 	ret = fstat(srcfd, &sb);
93*1031c584SApple OSS Distributions 	if (ret < 0) {
94*1031c584SApple OSS Distributions 		err(EX_NOINPUT, "fstat(%s)", src);
95*1031c584SApple OSS Distributions 	}
96*1031c584SApple OSS Distributions 
97*1031c584SApple OSS Distributions 	if (!S_ISREG(sb.st_mode)) {
98*1031c584SApple OSS Distributions 		err(EX_USAGE, "%s is not a regular file", src);
99*1031c584SApple OSS Distributions 	}
100*1031c584SApple OSS Distributions 
101*1031c584SApple OSS Distributions 	snprintf(dsttmpname, sizeof(dsttmpname), "%s.XXXXXX", dst);
102*1031c584SApple OSS Distributions 
103*1031c584SApple OSS Distributions 	dstfd = mkstemp(dsttmpname);
104*1031c584SApple OSS Distributions 	if (dstfd < 0) {
105*1031c584SApple OSS Distributions 		err(EX_UNAVAILABLE, "mkstemp(%s)", dsttmpname);
106*1031c584SApple OSS Distributions 	}
107*1031c584SApple OSS Distributions 
108*1031c584SApple OSS Distributions 	ret = fcopyfile(srcfd, dstfd, NULL,
109*1031c584SApple OSS Distributions 	    COPYFILE_DATA);
110*1031c584SApple OSS Distributions 	if (ret < 0) {
111*1031c584SApple OSS Distributions 		err(EX_UNAVAILABLE, "fcopyfile(%s, %s)", src, dsttmpname);
112*1031c584SApple OSS Distributions 	}
113*1031c584SApple OSS Distributions 
114*1031c584SApple OSS Distributions 	ret = futimes(dstfd, NULL);
115*1031c584SApple OSS Distributions 	if (ret < 0) {
116*1031c584SApple OSS Distributions 		err(EX_UNAVAILABLE, "futimes(%s)", dsttmpname);
117*1031c584SApple OSS Distributions 	}
118*1031c584SApple OSS Distributions 
119*1031c584SApple OSS Distributions 	if (gotmode) {
120*1031c584SApple OSS Distributions 		ret = fchmod(dstfd, mode);
121*1031c584SApple OSS Distributions 		if (ret < 0) {
122*1031c584SApple OSS Distributions 			err(EX_NOINPUT, "fchmod(%s, %ho)", dsttmpname, mode);
123*1031c584SApple OSS Distributions 		}
124*1031c584SApple OSS Distributions 	}
125*1031c584SApple OSS Distributions 
126*1031c584SApple OSS Distributions 	ret = rename(dsttmpname, dst);
127*1031c584SApple OSS Distributions 	if (ret < 0) {
128*1031c584SApple OSS Distributions 		err(EX_NOINPUT, "rename(%s, %s)", dsttmpname, dst);
129*1031c584SApple OSS Distributions 	}
130*1031c584SApple OSS Distributions 
131*1031c584SApple OSS Distributions 	ret = close(dstfd);
132*1031c584SApple OSS Distributions 	if (ret < 0) {
133*1031c584SApple OSS Distributions 		err(EX_NOINPUT, "close(dst)");
134*1031c584SApple OSS Distributions 	}
135*1031c584SApple OSS Distributions 
136*1031c584SApple OSS Distributions 	ret = close(srcfd);
137*1031c584SApple OSS Distributions 	if (ret < 0) {
138*1031c584SApple OSS Distributions 		err(EX_NOINPUT, "close(src)");
139*1031c584SApple OSS Distributions 	}
140*1031c584SApple OSS Distributions 
141*1031c584SApple OSS Distributions 	return 0;
142*1031c584SApple OSS Distributions }
143*1031c584SApple OSS Distributions 
144*1031c584SApple OSS Distributions void
usage(void)145*1031c584SApple OSS Distributions usage(void)
146*1031c584SApple OSS Distributions {
147*1031c584SApple OSS Distributions 	fprintf(stderr, "Usage: %s [-c] [-S] [-m <mode>] <src> <dst>\n",
148*1031c584SApple OSS Distributions 	    getprogname());
149*1031c584SApple OSS Distributions 	exit(EX_USAGE);
150*1031c584SApple OSS Distributions }
151