Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 1 | /* |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame^] | 2 | * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * |
| 19 | * TODO: -d option, need a way of recursively making directories and changing |
| 20 | * owner/group, will probably modify bb_make_directory(...) |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <errno.h> |
| 26 | #include <getopt.h> |
| 27 | #include <stdlib.h> |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 28 | #include <string.h> |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 31 | #include "busybox.h" |
| 32 | #include "libcoreutils/coreutils.h" |
| 33 | |
| 34 | #define INSTALL_OPT_CMD 1 |
| 35 | #define INSTALL_OPT_DIRECTORY 2 |
| 36 | #define INSTALL_OPT_PRESERVE_TIME 4 |
| 37 | #define INSTALL_OPT_STRIP 8 |
| 38 | #define INSTALL_OPT_GROUP 16 |
| 39 | #define INSTALL_OPT_MODE 32 |
| 40 | #define INSTALL_OPT_OWNER 64 |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | ca43b48 | 2004-01-23 21:57:16 +0000 | [diff] [blame] | 42 | static const struct option install_long_options[] = { |
| 43 | { "directory", 0, NULL, 'd' }, |
| 44 | { "preserve-timestamps", 0, NULL, 'p' }, |
| 45 | { "strip", 0, NULL, 's' }, |
| 46 | { "group", 0, NULL, 'g' }, |
| 47 | { "mode", 0, NULL, 'm' }, |
| 48 | { "owner", 0, NULL, 'o' }, |
| 49 | { 0, 0, 0, 0 } |
| 50 | }; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 51 | |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 52 | extern int install_main(int argc, char **argv) |
| 53 | { |
| 54 | struct stat statbuf; |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 55 | mode_t mode; |
| 56 | uid_t uid; |
| 57 | gid_t gid; |
| 58 | char *gid_str = "-1"; |
| 59 | char *uid_str = "-1"; |
| 60 | char *mode_str = "0755"; |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 61 | int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE; |
| 62 | int ret = EXIT_SUCCESS; |
| 63 | int flags; |
| 64 | int i; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 65 | |
Glenn L McGrath | ca43b48 | 2004-01-23 21:57:16 +0000 | [diff] [blame] | 66 | bb_applet_long_options = install_long_options; |
Glenn L McGrath | 654a99b | 2004-01-23 21:40:19 +0000 | [diff] [blame] | 67 | bb_opt_complementaly = "s~d:d~s"; |
Glenn L McGrath | ca43b48 | 2004-01-23 21:57:16 +0000 | [diff] [blame] | 68 | /* -c exists for backwards compatability, its needed */ |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 69 | flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */ |
| 70 | |
Glenn L McGrath | 654a99b | 2004-01-23 21:40:19 +0000 | [diff] [blame] | 71 | /* Check valid options were given */ |
| 72 | if(flags & 0x80000000UL) { |
| 73 | bb_show_usage(); |
| 74 | } |
| 75 | |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 76 | /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */ |
| 77 | if (flags & INSTALL_OPT_PRESERVE_TIME) { |
| 78 | copy_flags |= FILEUTILS_PRESERVE_STATUS; |
| 79 | } |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 80 | bb_parse_mode(mode_str, &mode); |
| 81 | gid = get_ug_id(gid_str, my_getgrnam); |
| 82 | uid = get_ug_id(uid_str, my_getpwnam); |
| 83 | umask(0); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 84 | |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 85 | /* Create directories |
| 86 | * dont use bb_make_directory() as it cant change uid or gid |
| 87 | * perhaps bb_make_directory() should be improved. |
| 88 | */ |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 89 | if (flags & INSTALL_OPT_DIRECTORY) { |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 90 | for (argv += optind; *argv; argv++) { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 91 | char *old_argv_ptr = *argv + 1; |
| 92 | char *argv_ptr; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 93 | do { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 94 | argv_ptr = strchr(old_argv_ptr, '/'); |
| 95 | old_argv_ptr = argv_ptr; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 96 | if (argv_ptr) { |
| 97 | *argv_ptr = '\0'; |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 98 | old_argv_ptr++; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 99 | } |
Glenn L McGrath | 711bb92 | 2004-01-23 20:28:53 +0000 | [diff] [blame] | 100 | if (mkdir(*argv, mode) == -1) { |
| 101 | if (errno != EEXIST) { |
| 102 | bb_perror_msg("coulnt create %s", *argv); |
| 103 | ret = EXIT_FAILURE; |
| 104 | break; |
| 105 | } |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 106 | } |
| 107 | else if (lchown(*argv, uid, gid) == -1) { |
| 108 | bb_perror_msg("cannot change ownership of %s", *argv); |
| 109 | ret = EXIT_FAILURE; |
| 110 | break; |
| 111 | } |
| 112 | if (argv_ptr) { |
| 113 | *argv_ptr = '/'; |
| 114 | } |
| 115 | } while (old_argv_ptr); |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 116 | } |
| 117 | return(ret); |
| 118 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 119 | |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 120 | cp_mv_stat2(argv[argc - 1], &statbuf, lstat); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 121 | for (i = optind; i < argc - 1; i++) { |
| 122 | unsigned char *dest; |
| 123 | |
| 124 | if (S_ISDIR(statbuf.st_mode)) { |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 125 | dest = concat_path_file(argv[argc - 1], basename(argv[i])); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 126 | } else { |
| 127 | dest = argv[argc - 1]; |
| 128 | } |
| 129 | ret |= copy_file(argv[i], dest, copy_flags); |
| 130 | |
| 131 | /* Set the file mode */ |
| 132 | if (chmod(dest, mode) == -1) { |
| 133 | bb_perror_msg("cannot change permissions of %s", dest); |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 134 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Set the user and group id */ |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 138 | if (lchown(dest, uid, gid) == -1) { |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 139 | bb_perror_msg("cannot change ownership of %s", dest); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 140 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 141 | } |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 142 | if (flags & INSTALL_OPT_STRIP) { |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 143 | if (execlp("strip", "strip", dest, NULL) == -1) { |
| 144 | bb_error_msg("strip failed"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 145 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 149 | |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 150 | return(ret); |
| 151 | } |