blob: 74e1d9acd0db370742f167109c36836e693e899a [file] [log] [blame]
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00001/*
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00002 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00003 *
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 McGratheebcc1d2003-09-24 03:22:57 +000021 */
22
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <errno.h>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000026#include <stdlib.h>
Glenn L McGratha406a9c2003-09-24 05:00:29 +000027#include <string.h>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000028#include <unistd.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000029#include <getopt.h> /* struct option */
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000030
Glenn L McGrath11e69472003-11-27 22:40:08 +000031#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 McGratheebcc1d2003-09-24 03:22:57 +000041
Glenn L McGrathca43b482004-01-23 21:57:16 +000042static 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 Andersenc7bda1c2004-03-15 08:29:22 +000051
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000052extern int install_main(int argc, char **argv)
53{
54 struct stat statbuf;
Glenn L McGrath578eff52004-01-23 10:57:00 +000055 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 McGrath11e69472003-11-27 22:40:08 +000061 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
62 int ret = EXIT_SUCCESS;
63 int flags;
64 int i;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000065
Glenn L McGrathca43b482004-01-23 21:57:16 +000066 bb_applet_long_options = install_long_options;
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000067 bb_opt_complementally = "!s~d:d~s";
Glenn L McGrathca43b482004-01-23 21:57:16 +000068 /* -c exists for backwards compatability, its needed */
Glenn L McGrath11e69472003-11-27 22:40:08 +000069 flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
70
71 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
72 if (flags & INSTALL_OPT_PRESERVE_TIME) {
73 copy_flags |= FILEUTILS_PRESERVE_STATUS;
74 }
Glenn L McGrath578eff52004-01-23 10:57:00 +000075 bb_parse_mode(mode_str, &mode);
76 gid = get_ug_id(gid_str, my_getgrnam);
77 uid = get_ug_id(uid_str, my_getpwnam);
78 umask(0);
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000079
Glenn L McGrath578eff52004-01-23 10:57:00 +000080 /* Create directories
81 * dont use bb_make_directory() as it cant change uid or gid
82 * perhaps bb_make_directory() should be improved.
83 */
Glenn L McGrath11e69472003-11-27 22:40:08 +000084 if (flags & INSTALL_OPT_DIRECTORY) {
Glenn L McGratha406a9c2003-09-24 05:00:29 +000085 for (argv += optind; *argv; argv++) {
Glenn L McGrath578eff52004-01-23 10:57:00 +000086 char *old_argv_ptr = *argv + 1;
87 char *argv_ptr;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000088 do {
Glenn L McGrath578eff52004-01-23 10:57:00 +000089 argv_ptr = strchr(old_argv_ptr, '/');
90 old_argv_ptr = argv_ptr;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000091 if (argv_ptr) {
92 *argv_ptr = '\0';
Glenn L McGrath578eff52004-01-23 10:57:00 +000093 old_argv_ptr++;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000094 }
Glenn L McGrath711bb922004-01-23 20:28:53 +000095 if (mkdir(*argv, mode) == -1) {
96 if (errno != EEXIST) {
97 bb_perror_msg("coulnt create %s", *argv);
98 ret = EXIT_FAILURE;
99 break;
100 }
Glenn L McGrath578eff52004-01-23 10:57:00 +0000101 }
102 else if (lchown(*argv, uid, gid) == -1) {
103 bb_perror_msg("cannot change ownership of %s", *argv);
104 ret = EXIT_FAILURE;
105 break;
106 }
107 if (argv_ptr) {
108 *argv_ptr = '/';
109 }
110 } while (old_argv_ptr);
Glenn L McGratha406a9c2003-09-24 05:00:29 +0000111 }
112 return(ret);
113 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000114
Glenn L McGrath11e69472003-11-27 22:40:08 +0000115 cp_mv_stat2(argv[argc - 1], &statbuf, lstat);
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000116 for (i = optind; i < argc - 1; i++) {
117 unsigned char *dest;
118
119 if (S_ISDIR(statbuf.st_mode)) {
Glenn L McGrath11e69472003-11-27 22:40:08 +0000120 dest = concat_path_file(argv[argc - 1], basename(argv[i]));
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000121 } else {
122 dest = argv[argc - 1];
123 }
124 ret |= copy_file(argv[i], dest, copy_flags);
125
126 /* Set the file mode */
127 if (chmod(dest, mode) == -1) {
128 bb_perror_msg("cannot change permissions of %s", dest);
Glenn L McGrath578eff52004-01-23 10:57:00 +0000129 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000130 }
131
132 /* Set the user and group id */
Glenn L McGrath11e69472003-11-27 22:40:08 +0000133 if (lchown(dest, uid, gid) == -1) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000134 bb_perror_msg("cannot change ownership of %s", dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000135 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000136 }
Glenn L McGrath11e69472003-11-27 22:40:08 +0000137 if (flags & INSTALL_OPT_STRIP) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000138 if (execlp("strip", "strip", dest, NULL) == -1) {
139 bb_error_msg("strip failed");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000140 ret = EXIT_FAILURE;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000141 }
142 }
143 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000144
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000145 return(ret);
146}