"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 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 16 | #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
Glenn L McGrath | ca43b48 | 2004-01-23 21:57:16 +0000 | [diff] [blame] | 17 | static const struct option install_long_options[] = { |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 18 | { "directory", 0, NULL, 'd' }, |
| 19 | { "preserve-timestamps", 0, NULL, 'p' }, |
| 20 | { "strip", 0, NULL, 's' }, |
| 21 | { "group", 0, NULL, 'g' }, |
| 22 | { "mode", 0, NULL, 'm' }, |
| 23 | { "owner", 0, NULL, 'o' }, |
| 24 | { 0, 0, 0, 0 } |
Glenn L McGrath | ca43b48 | 2004-01-23 21:57:16 +0000 | [diff] [blame] | 25 | }; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 26 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 27 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 28 | int install_main(int argc, char **argv) |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 29 | { |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 30 | struct stat statbuf; |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 31 | mode_t mode; |
| 32 | uid_t uid; |
| 33 | gid_t gid; |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 34 | const char *gid_str; |
| 35 | const char *uid_str; |
| 36 | const char *mode_str; |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 37 | int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE; |
Rob Landley | ae907f3 | 2005-10-09 11:16:01 +0000 | [diff] [blame] | 38 | int ret = EXIT_SUCCESS, flags, i, isdir; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 40 | enum { |
| 41 | OPT_CMD = 0x1, |
| 42 | OPT_DIRECTORY = 0x2, |
| 43 | OPT_PRESERVE_TIME = 0x4, |
| 44 | OPT_STRIP = 0x8, |
| 45 | OPT_GROUP = 0x10, |
| 46 | OPT_MODE = 0x20, |
| 47 | OPT_OWNER = 0x40, |
| 48 | }; |
| 49 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 50 | #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 51 | applet_long_options = install_long_options; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 52 | #endif |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 53 | opt_complementary = "?:s--d:d--s"; |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 54 | /* -c exists for backwards compatibility, its needed */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 55 | flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 56 | |
| 57 | /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 58 | if (flags & OPT_PRESERVE_TIME) { |
Glenn L McGrath | 11e6947 | 2003-11-27 22:40:08 +0000 | [diff] [blame] | 59 | copy_flags |= FILEUTILS_PRESERVE_STATUS; |
| 60 | } |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 61 | mode = 0666; |
| 62 | if (flags & OPT_MODE) bb_parse_mode(mode_str, &mode); |
| 63 | uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); |
| 64 | gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); |
| 65 | if (flags & (OPT_OWNER|OPT_GROUP)) umask(0); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 66 | |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 67 | /* Create directories |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 68 | * 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] | 69 | * perhaps bb_make_directory() should be improved. |
| 70 | */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 71 | if (flags & OPT_DIRECTORY) { |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 72 | for (argv += optind; *argv; argv++) { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 73 | char *old_argv_ptr = *argv + 1; |
| 74 | char *argv_ptr; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 75 | do { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 76 | argv_ptr = strchr(old_argv_ptr, '/'); |
| 77 | old_argv_ptr = argv_ptr; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 78 | if (argv_ptr) { |
| 79 | *argv_ptr = '\0'; |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 80 | old_argv_ptr++; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 81 | } |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 82 | if (mkdir(*argv, mode | 0111) == -1) { |
Glenn L McGrath | 711bb92 | 2004-01-23 20:28:53 +0000 | [diff] [blame] | 83 | if (errno != EEXIST) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 84 | bb_perror_msg("cannot create %s", *argv); |
Glenn L McGrath | 711bb92 | 2004-01-23 20:28:53 +0000 | [diff] [blame] | 85 | ret = EXIT_FAILURE; |
| 86 | break; |
| 87 | } |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 88 | } |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 89 | if ((flags & (OPT_OWNER|OPT_GROUP)) |
| 90 | && lchown(*argv, uid, gid) == -1 |
| 91 | ) { |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 92 | bb_perror_msg("cannot change ownership of %s", *argv); |
| 93 | ret = EXIT_FAILURE; |
| 94 | break; |
| 95 | } |
| 96 | if (argv_ptr) { |
| 97 | *argv_ptr = '/'; |
| 98 | } |
| 99 | } while (old_argv_ptr); |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 100 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 101 | return ret; |
Glenn L McGrath | a406a9c | 2003-09-24 05:00:29 +0000 | [diff] [blame] | 102 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 104 | isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode); |
| 105 | |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 106 | for (i = optind; i < argc - 1; i++) { |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 107 | char *dest; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 108 | |
Rob Landley | ae907f3 | 2005-10-09 11:16:01 +0000 | [diff] [blame] | 109 | dest = argv[argc - 1]; |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 110 | if (isdir) |
| 111 | dest = concat_path_file(argv[argc - 1], basename(argv[i])); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 112 | ret |= copy_file(argv[i], dest, copy_flags); |
| 113 | |
| 114 | /* Set the file mode */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 115 | if ((flags & OPT_MODE) && chmod(dest, mode) == -1) { |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 116 | bb_perror_msg("cannot change permissions of %s", dest); |
Glenn L McGrath | 578eff5 | 2004-01-23 10:57:00 +0000 | [diff] [blame] | 117 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Set the user and group id */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 121 | if ((flags & (OPT_OWNER|OPT_GROUP)) |
| 122 | && lchown(dest, uid, gid) == -1 |
| 123 | ) { |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 124 | bb_perror_msg("cannot change ownership of %s", dest); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 125 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 126 | } |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 127 | if (flags & OPT_STRIP) { |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 128 | if (execlp("strip", "strip", dest, NULL) == -1) { |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 129 | bb_perror_msg("strip"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 130 | ret = EXIT_FAILURE; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 133 | if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest); |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 134 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 136 | return ret; |
Glenn L McGrath | eebcc1d | 2003-09-24 03:22:57 +0000 | [diff] [blame] | 137 | } |