aboutsummaryrefslogtreecommitdiff
path: root/coreutils/install.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-12-28 05:44:47 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-12-28 05:44:47 +0000
commit9a44c4f91ce7e517d5325fd3743e6ad9d54ef3f0 (patch)
tree34292ef12cab59b118e91a6c58844ae25f1bee94 /coreutils/install.c
parentba092336f00644c1233735bae4b81382309955d8 (diff)
downloadbusybox-w32-9a44c4f91ce7e517d5325fd3743e6ad9d54ef3f0.tar.gz
busybox-w32-9a44c4f91ce7e517d5325fd3743e6ad9d54ef3f0.tar.bz2
busybox-w32-9a44c4f91ce7e517d5325fd3743e6ad9d54ef3f0.zip
bb_xget[pw/gr]nam were horribly misnamed - fixed.
uidgid_get -> get_uidgid, add additional param (numeric_ok). Make chown use it. chown: fix "chown user: ...." install: fix incorrect use of bb_xget[pw/gr]nam
Diffstat (limited to 'coreutils/install.c')
-rw-r--r--coreutils/install.c82
1 files changed, 44 insertions, 38 deletions
diff --git a/coreutils/install.c b/coreutils/install.c
index 3e003905e..aa7e8bf2b 100644
--- a/coreutils/install.c
+++ b/coreutils/install.c
@@ -13,58 +13,62 @@
13#include <libgen.h> 13#include <libgen.h>
14#include <getopt.h> /* struct option */ 14#include <getopt.h> /* struct option */
15 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
23
24#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS 16#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
25static const struct option install_long_options[] = { 17static const struct option install_long_options[] = {
26 { "directory", 0, NULL, 'd' }, 18 { "directory", 0, NULL, 'd' },
27 { "preserve-timestamps", 0, NULL, 'p' }, 19 { "preserve-timestamps", 0, NULL, 'p' },
28 { "strip", 0, NULL, 's' }, 20 { "strip", 0, NULL, 's' },
29 { "group", 0, NULL, 'g' }, 21 { "group", 0, NULL, 'g' },
30 { "mode", 0, NULL, 'm' }, 22 { "mode", 0, NULL, 'm' },
31 { "owner", 0, NULL, 'o' }, 23 { "owner", 0, NULL, 'o' },
32 { 0, 0, 0, 0 } 24 { 0, 0, 0, 0 }
33}; 25};
34#endif 26#endif
35 27
36int install_main(int argc, char **argv) 28int install_main(int argc, char **argv)
37{ 29{
30 struct stat statbuf;
38 mode_t mode; 31 mode_t mode;
39 uid_t uid; 32 uid_t uid;
40 gid_t gid; 33 gid_t gid;
41 char *gid_str = "-1"; 34 const char *gid_str;
42 char *uid_str = "-1"; 35 const char *uid_str;
43 char *mode_str = "0755"; 36 const char *mode_str;
44 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE; 37 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
45 int ret = EXIT_SUCCESS, flags, i, isdir; 38 int ret = EXIT_SUCCESS, flags, i, isdir;
46 39
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
47#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS 50#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
48 applet_long_options = install_long_options; 51 applet_long_options = install_long_options;
49#endif 52#endif
50 opt_complementary = "?:s--d:d--s"; 53 opt_complementary = "?:s--d:d--s";
51 /* -c exists for backwards compatibility, its needed */ 54 /* -c exists for backwards compatibility, its needed */
52 flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */ 55 flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str);
53 56
54 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */ 57 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
55 if (flags & INSTALL_OPT_PRESERVE_TIME) { 58 if (flags & OPT_PRESERVE_TIME) {
56 copy_flags |= FILEUTILS_PRESERVE_STATUS; 59 copy_flags |= FILEUTILS_PRESERVE_STATUS;
57 } 60 }
58 bb_parse_mode(mode_str, &mode); 61 mode = 0666;
59 gid = get_ug_id(gid_str, bb_xgetgrnam); 62 if (flags & OPT_MODE) bb_parse_mode(mode_str, &mode);
60 uid = get_ug_id(uid_str, bb_xgetpwnam); 63 uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
61 umask(0); 64 gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
65 if (flags & (OPT_OWNER|OPT_GROUP)) umask(0);
62 66
63 /* Create directories 67 /* Create directories
64 * don't use bb_make_directory() as it can't change uid or gid 68 * don't use bb_make_directory() as it can't change uid or gid
65 * perhaps bb_make_directory() should be improved. 69 * perhaps bb_make_directory() should be improved.
66 */ 70 */
67 if (flags & INSTALL_OPT_DIRECTORY) { 71 if (flags & OPT_DIRECTORY) {
68 for (argv += optind; *argv; argv++) { 72 for (argv += optind; *argv; argv++) {
69 char *old_argv_ptr = *argv + 1; 73 char *old_argv_ptr = *argv + 1;
70 char *argv_ptr; 74 char *argv_ptr;
@@ -75,14 +79,16 @@ int install_main(int argc, char **argv)
75 *argv_ptr = '\0'; 79 *argv_ptr = '\0';
76 old_argv_ptr++; 80 old_argv_ptr++;
77 } 81 }
78 if (mkdir(*argv, mode) == -1) { 82 if (mkdir(*argv, mode | 0111) == -1) {
79 if (errno != EEXIST) { 83 if (errno != EEXIST) {
80 bb_perror_msg("cannot create %s", *argv); 84 bb_perror_msg("cannot create %s", *argv);
81 ret = EXIT_FAILURE; 85 ret = EXIT_FAILURE;
82 break; 86 break;
83 } 87 }
84 } 88 }
85 else if (lchown(*argv, uid, gid) == -1) { 89 if ((flags & (OPT_OWNER|OPT_GROUP))
90 && lchown(*argv, uid, gid) == -1
91 ) {
86 bb_perror_msg("cannot change ownership of %s", *argv); 92 bb_perror_msg("cannot change ownership of %s", *argv);
87 ret = EXIT_FAILURE; 93 ret = EXIT_FAILURE;
88 break; 94 break;
@@ -95,36 +101,36 @@ int install_main(int argc, char **argv)
95 return ret; 101 return ret;
96 } 102 }
97 103
98 { 104 isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
99 struct stat statbuf; 105
100 isdir = lstat(argv[argc - 1], &statbuf)<0
101 ? 0 : S_ISDIR(statbuf.st_mode);
102 }
103 for (i = optind; i < argc - 1; i++) { 106 for (i = optind; i < argc - 1; i++) {
104 char *dest; 107 char *dest;
105 108
106 dest = argv[argc - 1]; 109 dest = argv[argc - 1];
107 if (isdir) dest = concat_path_file(argv[argc - 1], basename(argv[i])); 110 if (isdir)
111 dest = concat_path_file(argv[argc - 1], basename(argv[i]));
108 ret |= copy_file(argv[i], dest, copy_flags); 112 ret |= copy_file(argv[i], dest, copy_flags);
109 113
110 /* Set the file mode */ 114 /* Set the file mode */
111 if (chmod(dest, mode) == -1) { 115 if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
112 bb_perror_msg("cannot change permissions of %s", dest); 116 bb_perror_msg("cannot change permissions of %s", dest);
113 ret = EXIT_FAILURE; 117 ret = EXIT_FAILURE;
114 } 118 }
115 119
116 /* Set the user and group id */ 120 /* Set the user and group id */
117 if (lchown(dest, uid, gid) == -1) { 121 if ((flags & (OPT_OWNER|OPT_GROUP))
122 && lchown(dest, uid, gid) == -1
123 ) {
118 bb_perror_msg("cannot change ownership of %s", dest); 124 bb_perror_msg("cannot change ownership of %s", dest);
119 ret = EXIT_FAILURE; 125 ret = EXIT_FAILURE;
120 } 126 }
121 if (flags & INSTALL_OPT_STRIP) { 127 if (flags & OPT_STRIP) {
122 if (execlp("strip", "strip", dest, NULL) == -1) { 128 if (execlp("strip", "strip", dest, NULL) == -1) {
123 bb_error_msg("strip failed"); 129 bb_perror_msg("strip");
124 ret = EXIT_FAILURE; 130 ret = EXIT_FAILURE;
125 } 131 }
126 } 132 }
127 if(ENABLE_FEATURE_CLEAN_UP && isdir) free(dest); 133 if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
128 } 134 }
129 135
130 return ret; 136 return ret;