blob: 9a1306746662b6355a6bc665481e69757f2c9417 [file] [log] [blame]
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00001/*
2 * Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
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 McGratheebcc1d2003-09-24 03:22:57 +000021 */
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 McGratha406a9c2003-09-24 05:00:29 +000028#include <string.h>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000029#include <unistd.h>
30
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
42extern int install_main(int argc, char **argv)
43{
44 struct stat statbuf;
Glenn L McGrath11e69472003-11-27 22:40:08 +000045 mode_t mode = 0755;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000046 uid_t uid = -1;
47 gid_t gid = -1;
Glenn L McGrath11e69472003-11-27 22:40:08 +000048 char *gid_str;
49 char *uid_str;
50 char *mode_str;
51 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
52 int ret = EXIT_SUCCESS;
53 int flags;
54 int i;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000055
56 /* -c exists for backwards compatability, its needed */
Glenn L McGrath11e69472003-11-27 22:40:08 +000057 flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
58
59 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
60 if (flags & INSTALL_OPT_PRESERVE_TIME) {
61 copy_flags |= FILEUTILS_PRESERVE_STATUS;
62 }
63 if (flags & INSTALL_OPT_GROUP) {
64 gid = get_ug_id(gid_str, my_getgrnam);
65 }
66 if (flags & INSTALL_OPT_MODE) {
67 bb_parse_mode(mode_str, &mode);
68 }
69 if (flags & INSTALL_OPT_OWNER) {
70 uid = get_ug_id(uid_str, my_getpwnam);
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000071 }
72
Glenn L McGrath11e69472003-11-27 22:40:08 +000073 /* Create directories */
74 if (flags & INSTALL_OPT_DIRECTORY) {
75
Glenn L McGratha406a9c2003-09-24 05:00:29 +000076 for (argv += optind; *argv; argv++) {
77 unsigned char *dir_name = *argv;
78 unsigned char *argv_ptr;
79
80 ret |= bb_make_directory(dir_name, mode, FILEUTILS_RECUR);
81 do {
82 argv_ptr = strrchr(dir_name, '/');
83
84 /* Skip the "." and ".." directories */
85 if ((dir_name[0] == '.') && ((dir_name[1] == '\0') || ((dir_name[1] == '.') && (dir_name[2] == '\0')))) {
86 break;
87 }
Glenn L McGrath11e69472003-11-27 22:40:08 +000088 if (lchown(dir_name, uid, gid) == -1) {
Glenn L McGratha406a9c2003-09-24 05:00:29 +000089 bb_perror_msg("cannot change ownership of %s", argv_ptr);
90 ret |= EXIT_FAILURE;
91 }
92 if (argv_ptr) {
93 *argv_ptr = '\0';
94 }
95 } while (argv_ptr);
96 }
97 return(ret);
98 }
Glenn L McGrath11e69472003-11-27 22:40:08 +000099
100 cp_mv_stat2(argv[argc - 1], &statbuf, lstat);
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000101 for (i = optind; i < argc - 1; i++) {
102 unsigned char *dest;
103
104 if (S_ISDIR(statbuf.st_mode)) {
Glenn L McGrath11e69472003-11-27 22:40:08 +0000105 dest = concat_path_file(argv[argc - 1], basename(argv[i]));
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000106 } else {
107 dest = argv[argc - 1];
108 }
109 ret |= copy_file(argv[i], dest, copy_flags);
110
111 /* Set the file mode */
112 if (chmod(dest, mode) == -1) {
113 bb_perror_msg("cannot change permissions of %s", dest);
114 ret |= EXIT_FAILURE;
115 }
116
117 /* Set the user and group id */
Glenn L McGrath11e69472003-11-27 22:40:08 +0000118 if (lchown(dest, uid, gid) == -1) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000119 bb_perror_msg("cannot change ownership of %s", dest);
120 ret |= EXIT_FAILURE;
121 }
Glenn L McGrath11e69472003-11-27 22:40:08 +0000122 if (flags & INSTALL_OPT_STRIP) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +0000123 if (execlp("strip", "strip", dest, NULL) == -1) {
124 bb_error_msg("strip failed");
125 ret |= EXIT_FAILURE;
126 }
127 }
128 }
129
130 return(ret);
131}