install.c (6834B)
1 /* see LICENSE file for copyright and license details */ 2 #include <sys/stat.h> 3 #include <sys/types.h> 4 5 #include <errno.h> 6 #include <fcntl.h> 7 #include <grp.h> 8 #include <libgen.h> 9 #include <limits.h> 10 #include <pwd.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <unistd.h> 15 16 #include "fs.h" 17 #include "util.h" 18 #include "wexec.h" 19 20 static int dflag, Dflag, pflag, sflag; 21 static mode_t mode = 0755; 22 static uid_t uid = (uid_t)-1; 23 static gid_t gid = (gid_t)-1; 24 static int status; 25 26 static void 27 usage(void) 28 { 29 eprintf( 30 "usage: %s [-Dps] [-o owner] [-g group] [-m mode] [-t dir] source ... dest\n" 31 " %s -d [-Dp] [-o owner] [-g group] [-m mode] dir ...\n", 32 argv0, 33 argv0 34 ); 35 } 36 37 static void 38 lookup_owner(const char *owner, const char *group) 39 { 40 struct passwd *pw; 41 struct group *gr; 42 43 if (owner) { 44 errno = 0; 45 pw = getpwnam(owner); 46 if (pw) { 47 uid = pw->pw_uid; 48 } else { 49 if (errno) 50 eprintf("getpwnam %s:", owner); 51 uid = (uid_t)estrtonum(owner, 0, UINT_MAX); 52 } 53 } 54 if (group) { 55 errno = 0; 56 gr = getgrnam(group); 57 if (gr) { 58 gid = gr->gr_gid; 59 } else { 60 if (errno) 61 eprintf("getgrnam %s:", group); 62 gid = (gid_t)estrtonum(group, 0, UINT_MAX); 63 } 64 } 65 } 66 67 static void 68 chown_if_requested(const char *path) 69 { 70 if (uid == (uid_t)-1 && gid == (gid_t)-1) 71 return; 72 if (chown(path, uid, gid) < 0) { 73 weprintf("chown %s:", path); 74 status = 1; 75 } 76 } 77 78 static void 79 preserve_time(const char *src, const char *dst) 80 { 81 struct stat st; 82 struct timespec times[2]; 83 84 if (stat(src, &st) < 0) { 85 weprintf("stat %s:", src); 86 status = 1; 87 return; 88 } 89 times[0] = st.st_atim; 90 times[1] = st.st_mtim; 91 if (utimensat(AT_FDCWD, dst, times, 0) < 0) { 92 weprintf("utimensat %s:", dst); 93 status = 1; 94 } 95 } 96 97 static void 98 strip_binary(const char *path) 99 { 100 char *args[] = {"strip", "-p", (char *)path, NULL}; 101 102 if (wexecvp("strip", args) != 0) { 103 weprintf("strip %s:", path); 104 status = 1; 105 } 106 } 107 108 // leading path components of dst, not dst itself 109 static void 110 make_leading_dirs(const char *dst) 111 { 112 char buf[PATH_MAX]; 113 char *dir; 114 115 estrlcpy(buf, dst, sizeof(buf)); 116 dir = dirname(buf); 117 if (mkdirp(dir, 0755, 0755) < 0 && errno != EEXIST) { 118 weprintf("mkdir %s:", dir); 119 status = 1; 120 } 121 } 122 123 static void 124 install_dirs(char **argv) 125 { 126 for (; *argv; argv++) { 127 if (mkdirp(*argv, mode, 0755) < 0 && errno != EEXIST) { 128 weprintf("mkdir %s:", *argv); 129 status = 1; 130 continue; 131 } 132 if (chmod(*argv, mode) < 0) { 133 weprintf("chmod %s:", *argv); 134 status = 1; 135 } 136 chown_if_requested(*argv); 137 } 138 } 139 140 static void 141 install_one(const char *src, const char *dst) 142 { 143 if (Dflag) 144 make_leading_dirs(dst); 145 146 cp_fflag = 1; 147 cp_pflag = 0; 148 cp_rflag = 0; 149 cp_follow = 'L'; 150 cp(src, dst, 0); 151 if (cp_status) { 152 status = 1; 153 return; 154 } 155 156 if (sflag) 157 strip_binary(dst); 158 159 if (chmod(dst, mode) < 0) { 160 weprintf("chmod %s:", dst); 161 status = 1; 162 } 163 chown_if_requested(dst); 164 if (pflag) 165 preserve_time(src, dst); 166 } 167 168 // ?man install: copy files and set attributes 169 // ?man arguments: source ... dest 170 // makefiles write "install file -m 644 dest": options may follow operands, so 171 // move them in front of the first operand before they are parsed 172 static char ** 173 options_first(int *argcp, char **argv) 174 { 175 int argc = *argcp; 176 char **out = ecalloc(argc + 2, sizeof(*out)); 177 int n = 0, i, rest = 0; 178 179 out[n++] = argv[0]; 180 for (i = 1; i < argc; i++) { 181 const char *a = argv[i]; 182 size_t l = strlen(a); 183 184 if (strcmp(a, "--") == 0) 185 break; 186 if (a[0] != '-' || l < 2) 187 continue; 188 out[n++] = argv[i]; 189 // the last letter of a cluster takes the next word when it ends it 190 if (strchr("ogmt", a[l - 1]) && i + 1 < argc) 191 out[n++] = argv[++i]; 192 } 193 out[n++] = "--"; 194 for (i = 1; i < argc; i++) { 195 const char *a = argv[i]; 196 size_t l = strlen(a); 197 198 if (!rest && strcmp(a, "--") == 0) { 199 rest = 1; 200 continue; 201 } 202 if (!rest && a[0] == '-' && l >= 2) { 203 if (strchr("ogmt", a[l - 1]) && i + 1 < argc) 204 i++; 205 continue; 206 } 207 out[n++] = argv[i]; 208 } 209 out[n] = NULL; 210 *argcp = n; 211 return out; 212 } 213 214 // ?man install copies each source to dest, or into dest when dest is a 215 // ?man directory or -t is given, then applies mode/owner/group; with 216 // ?man with -d, every argument is a directory to create instead 217 int 218 main(int argc, char *argv[]) 219 { 220 const char *owner = NULL, *group = NULL, *modestr = NULL, *targetdir = NULL; 221 const char *dest; 222 struct stat st; 223 int isdir; 224 int i; 225 226 argv = options_first(&argc, argv); 227 ARGBEGIN 228 { 229 // ?man -c: ignored; accepted for compatibility with older install callers 230 case 'c': 231 break; 232 // ?man -d: create directories instead of installing files 233 case 'd': 234 dflag = 1; 235 break; 236 // ?man -D: create dest's leading directories before installing 237 case 'D': 238 Dflag = 1; 239 break; 240 // ?man -p: preserve source modification and access times 241 case 'p': 242 pflag = 1; 243 break; 244 // ?man -s: strip the installed file's symbol table 245 case 's': 246 sflag = 1; 247 break; 248 // ?man -o:owner set the installed file's owner (name or uid) 249 case 'o': 250 owner = EARGF(usage()); 251 break; 252 // ?man -g:group set the installed file's group (name or gid) 253 case 'g': 254 group = EARGF(usage()); 255 break; 256 // ?man -m:mode set the installed file's permissions 257 case 'm': 258 modestr = EARGF(usage()); 259 break; 260 // ?man -t:dir install every source into dir 261 case 't': 262 targetdir = EARGF(usage()); 263 break; 264 default: 265 usage(); 266 } 267 ARGEND; 268 269 if (argc < 1 || (dflag && (targetdir || sflag))) 270 usage(); 271 if (modestr) 272 mode = parsemode(modestr, 0, 0); 273 lookup_owner(owner, group); 274 275 if (dflag) { 276 install_dirs(argv); 277 goto shut; 278 } 279 280 if (targetdir) { 281 isdir = 1; 282 dest = targetdir; 283 if (Dflag && mkdirp(targetdir, 0755, 0755) < 0 && errno != EEXIST) { 284 weprintf("mkdir %s:", targetdir); 285 status = 1; 286 } 287 } else { 288 if (argc < 2) 289 usage(); 290 dest = argv[--argc]; 291 isdir = stat(dest, &st) == 0 && S_ISDIR(st.st_mode); 292 if (!isdir && argc > 1) 293 eprintf("%s: not a directory\n", dest); 294 } 295 296 for (i = 0; i < argc; i++) { 297 if (isdir) { 298 char buf[PATH_MAX], nb[PATH_MAX]; 299 const char *base; 300 301 estrlcpy(nb, argv[i], sizeof(nb)); 302 base = basename(nb); 303 estrlcpy(buf, dest, sizeof(buf)); 304 estrlcat(buf, "/", sizeof(buf)); 305 estrlcat(buf, base, sizeof(buf)); 306 install_one(argv[i], buf); 307 } else { 308 install_one(argv[i], dest); 309 } 310 } 311 312 shut: 313 return fshut(stdout, "<stdout>") || status; 314 }