Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/install.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/install.c
r1765 r2725 4 4 * SELinux support: by Yuichi Nakamura <ynakam@hitachisoft.jp> 5 5 * 6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 7 * 8 * TODO: -d option, need a way of recursively making directories and changing 9 * owner/group, will probably modify bb_make_directory(...) 6 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 10 7 */ 11 12 #include <libgen.h>13 #include <getopt.h> /* struct option */14 8 15 9 #include "libbb.h" … … 21 15 "preserve-timestamps\0" No_argument "p" 22 16 "strip\0" No_argument "s" 23 "group\0" No_argument"g"24 "mode\0" No_argument"m"25 "owner\0" No_argument"o"17 "group\0" Required_argument "g" 18 "mode\0" Required_argument "m" 19 "owner\0" Required_argument "o" 26 20 /* autofs build insists of using -b --suffix=.orig */ 27 21 /* TODO? (short option for --suffix is -S) */ … … 57 51 if (lsetfilecon(path, scontext) < 0) { 58 52 if (errno != ENOTSUP) { 59 bb_perror_msg("warning: failed to change context of %s to %s", path, scontext); 53 bb_perror_msg("warning: can't change context" 54 " of %s to %s", path, scontext); 60 55 } 61 56 } … … 67 62 #endif 68 63 69 int install_main(int argc, char **argv) ;64 int install_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 70 65 int install_main(int argc, char **argv) 71 66 { … … 79 74 const char *mode_str; 80 75 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE; 81 int flags; 76 int opts; 77 int min_args = 1; 82 78 int ret = EXIT_SUCCESS; 83 int isdir ;79 int isdir = 0; 84 80 #if ENABLE_SELINUX 85 81 security_context_t scontext; … … 90 86 OPT_v = 1 << 1, 91 87 OPT_b = 1 << 2, 92 OPT_DIRECTORY = 1 << 3, 93 OPT_PRESERVE_TIME = 1 << 4, 94 OPT_STRIP = 1 << 5, 95 OPT_GROUP = 1 << 6, 96 OPT_MODE = 1 << 7, 97 OPT_OWNER = 1 << 8, 98 #if ENABLE_SELINUX 99 OPT_SET_SECURITY_CONTEXT = 1 << 9, 100 OPT_PRESERVE_SECURITY_CONTEXT = 1 << 10, 88 OPT_MKDIR_LEADING = 1 << 3, 89 OPT_DIRECTORY = 1 << 4, 90 OPT_PRESERVE_TIME = 1 << 5, 91 OPT_STRIP = 1 << 6, 92 OPT_GROUP = 1 << 7, 93 OPT_MODE = 1 << 8, 94 OPT_OWNER = 1 << 9, 95 #if ENABLE_SELINUX 96 OPT_SET_SECURITY_CONTEXT = 1 << 10, 97 OPT_PRESERVE_SECURITY_CONTEXT = 1 << 11, 101 98 #endif 102 99 }; … … 105 102 applet_long_options = install_longopts; 106 103 #endif 107 opt_complementary = "s--d:d--s" USE_SELINUX(":Z--\xff:\xff--Z");104 opt_complementary = "s--d:d--s" IF_FEATURE_INSTALL_LONG_OPTIONS(IF_SELINUX(":Z--\xff:\xff--Z")); 108 105 /* -c exists for backwards compatibility, it's needed */ 109 106 /* -v is ignored ("print name of each created directory") */ 110 107 /* -b is ignored ("make a backup of each existing destination file") */ 111 flags = getopt32(argv, "cvb" "dpsg:m:o:" USE_SELINUX("Z:"),112 &gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext));108 opts = getopt32(argv, "cvb" "Ddpsg:m:o:" IF_SELINUX("Z:"), 109 &gid_str, &mode_str, &uid_str IF_SELINUX(, &scontext)); 113 110 argc -= optind; 114 111 argv += optind; 115 112 116 113 #if ENABLE_SELINUX 117 if ( flags & (OPT_PRESERVE_SECURITY_CONTEXT|OPT_SET_SECURITY_CONTEXT)) {114 if (opts & (OPT_PRESERVE_SECURITY_CONTEXT|OPT_SET_SECURITY_CONTEXT)) { 118 115 selinux_or_die(); 119 116 use_default_selinux_context = 0; 120 if ( flags & OPT_PRESERVE_SECURITY_CONTEXT) {117 if (opts & OPT_PRESERVE_SECURITY_CONTEXT) { 121 118 copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT; 122 119 } 123 if ( flags & OPT_SET_SECURITY_CONTEXT) {120 if (opts & OPT_SET_SECURITY_CONTEXT) { 124 121 setfscreatecon_or_die(scontext); 125 122 copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT; … … 128 125 #endif 129 126 130 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */ 131 if (flags & OPT_PRESERVE_TIME) { 127 /* preserve access and modification time, this is GNU behaviour, 128 * BSD only preserves modification time */ 129 if (opts & OPT_PRESERVE_TIME) { 132 130 copy_flags |= FILEUTILS_PRESERVE_STATUS; 133 131 } 134 mode = 0 666;135 if ( flags & OPT_MODE)132 mode = 0755; /* GNU coreutils 6.10 compat */ 133 if (opts & OPT_MODE) 136 134 bb_parse_mode(mode_str, &mode); 137 uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); 138 gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); 139 if (flags & (OPT_OWNER|OPT_GROUP)) 140 umask(0); 141 142 /* Create directories 143 * don't use bb_make_directory() as it can't change uid or gid 144 * perhaps bb_make_directory() should be improved. 145 */ 146 if (flags & OPT_DIRECTORY) { 147 while ((arg = *argv++) != NULL) { 148 char *slash = arg; 149 while (1) { 150 slash = strchr(slash + 1, '/'); 151 if (slash) 152 *slash = '\0'; 153 if (mkdir(arg, mode | 0111) == -1) { 154 if (errno != EEXIST) { 155 bb_perror_msg("cannot create %s", arg); 156 ret = EXIT_FAILURE; 157 break; 158 } 159 } /* dir was created, chown? */ 160 else if ((flags & (OPT_OWNER|OPT_GROUP)) 161 && lchown(arg, uid, gid) == -1 162 ) { 163 bb_perror_msg("cannot change ownership of %s", arg); 164 ret = EXIT_FAILURE; 165 break; 166 } 167 if (!slash) 168 break; 169 *slash = '/'; 170 } 171 } 172 return ret; 173 } 174 175 if (argc < 2) 135 uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); 136 gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); 137 138 last = argv[argc - 1]; 139 if (!(opts & OPT_DIRECTORY)) { 140 argv[argc - 1] = NULL; 141 min_args++; 142 143 /* coreutils install resolves link in this case, don't use lstat */ 144 isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode); 145 } 146 147 if (argc < min_args) 176 148 bb_show_usage(); 177 178 last = argv[argc - 1];179 argv[argc - 1] = NULL;180 /* coreutils install resolves link in this case, don't use lstat */181 isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);182 149 183 150 while ((arg = *argv++) != NULL) { 184 151 char *dest = last; 185 if (isdir) 186 dest = concat_path_file(last, basename(arg)); 187 if (copy_file(arg, dest, copy_flags)) { 188 /* copy is not made */ 189 ret = EXIT_FAILURE; 190 goto next; 191 } 192 193 /* Set the file mode */ 194 if ((flags & OPT_MODE) && chmod(dest, mode) == -1) { 195 bb_perror_msg("cannot change permissions of %s", dest); 152 if (opts & OPT_DIRECTORY) { 153 dest = arg; 154 /* GNU coreutils 6.9 does not set uid:gid 155 * on intermediate created directories 156 * (only on last one) */ 157 if (bb_make_directory(dest, 0755, FILEUTILS_RECUR)) { 158 ret = EXIT_FAILURE; 159 goto next; 160 } 161 } else { 162 if (opts & OPT_MKDIR_LEADING) { 163 char *ddir = xstrdup(dest); 164 bb_make_directory(dirname(ddir), 0755, FILEUTILS_RECUR); 165 /* errors are not checked. copy_file 166 * will fail if dir is not created. */ 167 free(ddir); 168 } 169 if (isdir) 170 dest = concat_path_file(last, bb_basename(arg)); 171 if (copy_file(arg, dest, copy_flags) != 0) { 172 /* copy is not made */ 173 ret = EXIT_FAILURE; 174 goto next; 175 } 176 if (opts & OPT_STRIP) { 177 char *args[4]; 178 args[0] = (char*)"strip"; 179 args[1] = (char*)"-p"; /* -p --preserve-dates */ 180 args[2] = dest; 181 args[3] = NULL; 182 if (spawn_and_wait(args)) { 183 bb_perror_msg("strip"); 184 ret = EXIT_FAILURE; 185 } 186 } 187 } 188 189 /* Set the file mode (always, not only with -m). 190 * GNU coreutils 6.10 is not affected by umask. */ 191 if (chmod(dest, mode) == -1) { 192 bb_perror_msg("can't change %s of %s", "permissions", dest); 196 193 ret = EXIT_FAILURE; 197 194 } … … 201 198 #endif 202 199 /* Set the user and group id */ 203 if (( flags & (OPT_OWNER|OPT_GROUP))200 if ((opts & (OPT_OWNER|OPT_GROUP)) 204 201 && lchown(dest, uid, gid) == -1 205 202 ) { 206 bb_perror_msg("can not change ownership of %s", dest);203 bb_perror_msg("can't change %s of %s", "ownership", dest); 207 204 ret = EXIT_FAILURE; 208 }209 if (flags & OPT_STRIP) {210 char *args[3];211 args[0] = (char*)"strip";212 args[1] = dest;213 args[2] = NULL;214 if (spawn_and_wait(args)) {215 bb_perror_msg("strip");216 ret = EXIT_FAILURE;217 }218 205 } 219 206 next:
Note:
See TracChangeset
for help on using the changeset viewer.