blob: 57845cffda64512bc819c3e4d3b930047c3ebad9 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00002/*
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00003 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 *
20 * TODO: -d option, need a way of recursively making directories and changing
21 * owner/group, will probably modify bb_make_directory(...)
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000022 */
23
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <errno.h>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000027#include <stdlib.h>
Glenn L McGratha406a9c2003-09-24 05:00:29 +000028#include <string.h>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000029#include <unistd.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000030#include <getopt.h> /* struct option */
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000031
Glenn L McGrath11e69472003-11-27 22:40:08 +000032#include "busybox.h"
33#include "libcoreutils/coreutils.h"
34
35#define INSTALL_OPT_CMD 1
36#define INSTALL_OPT_DIRECTORY 2
37#define INSTALL_OPT_PRESERVE_TIME 4
38#define INSTALL_OPT_STRIP 8
39#define INSTALL_OPT_GROUP 16
40#define INSTALL_OPT_MODE 32
41#define INSTALL_OPT_OWNER 64
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000042
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000043#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
Glenn L McGrathca43b482004-01-23 21:57:16 +000044static const struct option install_long_options[] = {
45 { "directory", 0, NULL, 'd' },
46 { "preserve-timestamps", 0, NULL, 'p' },
47 { "strip", 0, NULL, 's' },
48 { "group", 0, NULL, 'g' },
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049 { "mode", 0, NULL, 'm' },
Glenn L McGrathca43b482004-01-23 21:57:16 +000050 { "owner", 0, NULL, 'o' },
51 { 0, 0, 0, 0 }
52};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000053#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054
Rob Landleydfba7412006-03-06 20:47:33 +000055int install_main(int argc, char **argv)
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000056{
Glenn L McGrath578eff52004-01-23 10:57:00 +000057 mode_t mode;
58 uid_t uid;
59 gid_t gid;
60 char *gid_str = "-1";
61 char *uid_str = "-1";
62 char *mode_str = "0755";
Glenn L McGrath11e69472003-11-27 22:40:08 +000063 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
Rob Landleyae907f32005-10-09 11:16:01 +000064 int ret = EXIT_SUCCESS, flags, i, isdir;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000065
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000066#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
Glenn L McGrathca43b482004-01-23 21:57:16 +000067 bb_applet_long_options = install_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000068#endif
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +000069 bb_opt_complementally = "?:s--d:d--s";
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +000070 /* -c exists for backwards compatibility, its needed */
Glenn L McGrath11e69472003-11-27 22:40:08 +000071 flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
72
73 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
74 if (flags & INSTALL_OPT_PRESERVE_TIME) {
75 copy_flags |= FILEUTILS_PRESERVE_STATUS;
76 }
Glenn L McGrath578eff52004-01-23 10:57:00 +000077 bb_parse_mode(mode_str, &mode);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000078 gid = get_ug_id(gid_str, bb_xgetgrnam);
79 uid = get_ug_id(uid_str, bb_xgetpwnam);
Glenn L McGrath578eff52004-01-23 10:57:00 +000080 umask(0);
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000081
Glenn L McGrath578eff52004-01-23 10:57:00 +000082 /* Create directories
83 * dont use bb_make_directory() as it cant change uid or gid
84 * perhaps bb_make_directory() should be improved.
85 */
Glenn L McGrath11e69472003-11-27 22:40:08 +000086 if (flags & INSTALL_OPT_DIRECTORY) {
Glenn L McGratha406a9c2003-09-24 05:00:29 +000087 for (argv += optind; *argv; argv++) {
Glenn L McGrath578eff52004-01-23 10:57:00 +000088 char *old_argv_ptr = *argv + 1;
89 char *argv_ptr;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000090 do {
Glenn L McGrath578eff52004-01-23 10:57:00 +000091 argv_ptr = strchr(old_argv_ptr, '/');
92 old_argv_ptr = argv_ptr;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000093 if (argv_ptr) {
94 *argv_ptr = '\0';
Glenn L McGrath578eff52004-01-23 10:57:00 +000095 old_argv_ptr++;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000096 }
Glenn L McGrath711bb922004-01-23 20:28:53 +000097 if (mkdir(*argv, mode) == -1) {
98 if (errno != EEXIST) {
99 bb_perror_msg("coulnt create %s", *argv);
100 ret = EXIT_FAILURE;
101 break;
102 }
Glenn L McGrath578eff52004-01-23 10:57:00 +0000103 }
104 else if (lchown(*argv, uid, gid) == -1) {
105 bb_perror_msg("cannot change ownership of %s", *argv);
106 ret = EXIT_FAILURE;
107 break;
108 }
109 if (argv_ptr) {
110 *argv_ptr = '/';
111 }
112 } while (old_argv_ptr);
Glenn L McGratha406a9c2003-09-24 05:00:29 +0000113 }
114 return(ret);
115 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000116
Rob Landleyae907f32005-10-09 11:16:01 +0000117 {
118 struct stat statbuf;
119 isdir = lstat(argv[argc - 1], &statbuf)<0
120 ? 0 : S_ISDIR(statbuf.st_mode);
121 }
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000122 for (i = optind; i < argc - 1; i++) {
Eric Andersen5e678872006-01-30 19:48:23 +0000123 char *dest;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000124
Rob Landleyae907f32005-10-09 11:16:01 +0000125 dest = argv[argc - 1];
126 if (isdir) dest = concat_path_file(argv[argc - 1], basename(argv[i]));
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000127 ret |= copy_file(argv[i], dest, copy_flags);
128
129 /* Set the file mode */
130 if (chmod(dest, mode) == -1) {
131 bb_perror_msg("cannot change permissions of %s", dest);
Glenn L McGrath578eff52004-01-23 10:57:00 +0000132 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000133 }
134
135 /* Set the user and group id */
Glenn L McGrath11e69472003-11-27 22:40:08 +0000136 if (lchown(dest, uid, gid) == -1) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000137 bb_perror_msg("cannot change ownership of %s", dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000138 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000139 }
Glenn L McGrath11e69472003-11-27 22:40:08 +0000140 if (flags & INSTALL_OPT_STRIP) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000141 if (execlp("strip", "strip", dest, NULL) == -1) {
142 bb_error_msg("strip failed");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000143 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000144 }
145 }
Rob Landleyae907f32005-10-09 11:16:01 +0000146 if(ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000147 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000148
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000149 return(ret);
150}