blob: aa7e8bf2bf8814b56035348c3efe38e550ba9e48 [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00006 *
7 * TODO: -d option, need a way of recursively making directories and changing
8 * owner/group, will probably modify bb_make_directory(...)
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00009 */
10
Glenn L McGrath11e69472003-11-27 22:40:08 +000011#include "busybox.h"
12#include "libcoreutils/coreutils.h"
Rob Landley519d7df2006-08-09 20:56:23 +000013#include <libgen.h>
14#include <getopt.h> /* struct option */
Glenn L McGrath11e69472003-11-27 22:40:08 +000015
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000016#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
Glenn L McGrathca43b482004-01-23 21:57:16 +000017static const struct option install_long_options[] = {
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000018 { "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 McGrathca43b482004-01-23 21:57:16 +000025};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000026#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000027
Rob Landleydfba7412006-03-06 20:47:33 +000028int install_main(int argc, char **argv)
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000029{
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000030 struct stat statbuf;
Glenn L McGrath578eff52004-01-23 10:57:00 +000031 mode_t mode;
32 uid_t uid;
33 gid_t gid;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000034 const char *gid_str;
35 const char *uid_str;
36 const char *mode_str;
Glenn L McGrath11e69472003-11-27 22:40:08 +000037 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
Rob Landleyae907f32005-10-09 11:16:01 +000038 int ret = EXIT_SUCCESS, flags, i, isdir;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000039
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000040 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-Fischer01d23ad2006-05-26 20:19:22 +000050#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
Denis Vlasenko67b23e62006-10-03 21:00:06 +000051 applet_long_options = install_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000052#endif
Denis Vlasenko67b23e62006-10-03 21:00:06 +000053 opt_complementary = "?:s--d:d--s";
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +000054 /* -c exists for backwards compatibility, its needed */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000055 flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str);
Glenn L McGrath11e69472003-11-27 22:40:08 +000056
57 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000058 if (flags & OPT_PRESERVE_TIME) {
Glenn L McGrath11e69472003-11-27 22:40:08 +000059 copy_flags |= FILEUTILS_PRESERVE_STATUS;
60 }
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000061 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 McGratheebcc1d2003-09-24 03:22:57 +000066
Glenn L McGrath578eff52004-01-23 10:57:00 +000067 /* Create directories
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000068 * don't use bb_make_directory() as it can't change uid or gid
Glenn L McGrath578eff52004-01-23 10:57:00 +000069 * perhaps bb_make_directory() should be improved.
70 */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000071 if (flags & OPT_DIRECTORY) {
Glenn L McGratha406a9c2003-09-24 05:00:29 +000072 for (argv += optind; *argv; argv++) {
Glenn L McGrath578eff52004-01-23 10:57:00 +000073 char *old_argv_ptr = *argv + 1;
74 char *argv_ptr;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000075 do {
Glenn L McGrath578eff52004-01-23 10:57:00 +000076 argv_ptr = strchr(old_argv_ptr, '/');
77 old_argv_ptr = argv_ptr;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000078 if (argv_ptr) {
79 *argv_ptr = '\0';
Glenn L McGrath578eff52004-01-23 10:57:00 +000080 old_argv_ptr++;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000081 }
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000082 if (mkdir(*argv, mode | 0111) == -1) {
Glenn L McGrath711bb922004-01-23 20:28:53 +000083 if (errno != EEXIST) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000084 bb_perror_msg("cannot create %s", *argv);
Glenn L McGrath711bb922004-01-23 20:28:53 +000085 ret = EXIT_FAILURE;
86 break;
87 }
Glenn L McGrath578eff52004-01-23 10:57:00 +000088 }
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000089 if ((flags & (OPT_OWNER|OPT_GROUP))
90 && lchown(*argv, uid, gid) == -1
91 ) {
Glenn L McGrath578eff52004-01-23 10:57:00 +000092 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 McGratha406a9c2003-09-24 05:00:29 +0000100 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000101 return ret;
Glenn L McGratha406a9c2003-09-24 05:00:29 +0000102 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000104 isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
105
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000106 for (i = optind; i < argc - 1; i++) {
Eric Andersen5e678872006-01-30 19:48:23 +0000107 char *dest;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000108
Rob Landleyae907f32005-10-09 11:16:01 +0000109 dest = argv[argc - 1];
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000110 if (isdir)
111 dest = concat_path_file(argv[argc - 1], basename(argv[i]));
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000112 ret |= copy_file(argv[i], dest, copy_flags);
113
114 /* Set the file mode */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000115 if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000116 bb_perror_msg("cannot change permissions of %s", dest);
Glenn L McGrath578eff52004-01-23 10:57:00 +0000117 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000118 }
119
120 /* Set the user and group id */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000121 if ((flags & (OPT_OWNER|OPT_GROUP))
122 && lchown(dest, uid, gid) == -1
123 ) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000124 bb_perror_msg("cannot change ownership of %s", dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000125 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000126 }
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000127 if (flags & OPT_STRIP) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000128 if (execlp("strip", "strip", dest, NULL) == -1) {
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000129 bb_perror_msg("strip");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000130 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000131 }
132 }
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000133 if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000134 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000135
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000136 return ret;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000137}