"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 2 | /* |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 4 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 6 | * |
| 7 | * TODO: -d option, need a way of recursively making directories and changing |
| 8 | * owner/group, will probably modify bb_make_directory(...) |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 11 | #include "busybox.h" |
| 12 | #include "libcoreutils/coreutils.h" |
Rob Landley | 519d7df | 2006-08-09 20:56:23 +0000 | [diff] [blame] | 13 | #include <libgen.h> |
| 14 | #include <getopt.h> /* struct option */ |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 15 | |
| 16 | #define INSTALL_OPT_CMD 1 |
| 17 | #define INSTALL_OPT_DIRECTORY 2 |
| 18 | #define INSTALL_OPT_PRESERVE_TIME 4 |
| 19 | #define INSTALL_OPT_STRIP 8 |
| 20 | #define INSTALL_OPT_GROUP 16 |
| 21 | #define INSTALL_OPT_MODE 32 |
| 22 | #define INSTALL_OPT_OWNER 64 |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 23 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 24 | #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
Glenn L McGrath | ca43b48 | 2004-01-23 21:57:16 +0000 | [diff] [blame] | 25 | static const struct option install_long_options[] = { |
| 26 | { "directory", 0, NULL, 'd' }, |
| 27 | { "preserve-timestamps", 0, NULL, 'p' }, |
| 28 | { "strip", 0, NULL, 's' }, |
| 29 | { "group", 0, NULL, 'g' }, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 30 | { "mode", 0, NULL, 'm' }, |
Glenn L McGrath | ca43b48 | 2004-01-23 21:57:16 +0000 | [diff] [blame] | 31 | { "owner", 0, NULL, 'o' }, |
| 32 | { 0, 0, 0, 0 } |
| 33 | }; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 34 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 35 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 36 | int install_main(int argc, char **argv) |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 37 | { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 38 | mode_t mode; |
| 39 | uid_t uid; |
| 40 | gid_t gid; |
| 41 | char *gid_str = "-1"; |
| 42 | char *uid_str = "-1"; |
| 43 | char *mode_str = "0755"; |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 44 | int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE; |
Rob Landley | ae907f3 | 2005-10-09 11:16:01 +0000 | [diff] [blame] | 45 | int ret = EXIT_SUCCESS, flags, i, isdir; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 46 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 47 | #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 48 | applet_long_options = install_long_options; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 49 | #endif |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 50 | opt_complementary = "?:s--d:d--s"; |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 51 | /* -c exists for backwards compatibility, its needed */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 52 | flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */ |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 53 | |
| 54 | /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */ |
| 55 | if (flags & INSTALL_OPT_PRESERVE_TIME) { |
| 56 | copy_flags |= FILEUTILS_PRESERVE_STATUS; |
| 57 | } |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 58 | bb_parse_mode(mode_str, &mode); |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 59 | gid = get_ug_id(gid_str, bb_xgetgrnam); |
| 60 | uid = get_ug_id(uid_str, bb_xgetpwnam); |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 61 | umask(0); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 62 | |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 63 | /* Create directories |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 64 | * don't use bb_make_directory() as it can't change uid or gid |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 65 | * perhaps bb_make_directory() should be improved. |
| 66 | */ |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 67 | if (flags & INSTALL_OPT_DIRECTORY) { |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 68 | for (argv += optind; *argv; argv++) { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 69 | char *old_argv_ptr = *argv + 1; |
| 70 | char *argv_ptr; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 71 | do { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 72 | argv_ptr = strchr(old_argv_ptr, '/'); |
| 73 | old_argv_ptr = argv_ptr; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 74 | if (argv_ptr) { |
| 75 | *argv_ptr = '\0'; |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 76 | old_argv_ptr++; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 77 | } |
Glenn L McGrath | 711bb92 | 2004-01-23 20:28:53 +0000 | [diff] [blame] | 78 | if (mkdir(*argv, mode) == -1) { |
| 79 | if (errno != EEXIST) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 80 | bb_perror_msg("cannot create %s", *argv); |
Glenn L McGrath | 711bb92 | 2004-01-23 20:28:53 +0000 | [diff] [blame] | 81 | ret = EXIT_FAILURE; |
| 82 | break; |
| 83 | } |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 84 | } |
| 85 | else if (lchown(*argv, uid, gid) == -1) { |
| 86 | bb_perror_msg("cannot change ownership of %s", *argv); |
| 87 | ret = EXIT_FAILURE; |
| 88 | break; |
| 89 | } |
| 90 | if (argv_ptr) { |
| 91 | *argv_ptr = '/'; |
| 92 | } |
| 93 | } while (old_argv_ptr); |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 94 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 95 | return ret; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 96 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 97 | |
Rob Landley | ae907f3 | 2005-10-09 11:16:01 +0000 | [diff] [blame] | 98 | { |
| 99 | struct stat statbuf; |
| 100 | isdir = lstat(argv[argc - 1], &statbuf)<0 |
| 101 | ? 0 : S_ISDIR(statbuf.st_mode); |
| 102 | } |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 103 | for (i = optind; i < argc - 1; i++) { |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 104 | char *dest; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 105 | |
Rob Landley | ae907f3 | 2005-10-09 11:16:01 +0000 | [diff] [blame] | 106 | dest = argv[argc - 1]; |
| 107 | if (isdir) dest = concat_path_file(argv[argc - 1], basename(argv[i])); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 108 | ret |= copy_file(argv[i], dest, copy_flags); |
| 109 | |
| 110 | /* Set the file mode */ |
| 111 | if (chmod(dest, mode) == -1) { |
| 112 | bb_perror_msg("cannot change permissions of %s", dest); |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 113 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* Set the user and group id */ |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 117 | if (lchown(dest, uid, gid) == -1) { |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 118 | bb_perror_msg("cannot change ownership of %s", dest); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 119 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 120 | } |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 121 | if (flags & INSTALL_OPT_STRIP) { |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 122 | if (execlp("strip", "strip", dest, NULL) == -1) { |
| 123 | bb_error_msg("strip failed"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 124 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Rob Landley | ae907f3 | 2005-10-09 11:16:01 +0000 | [diff] [blame] | 127 | if(ENABLE_FEATURE_CLEAN_UP && isdir) free(dest); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 128 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 130 | return ret; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 131 | } |