dd73fb693cc57b750e8337fe2f96c9e6a8cd508f
[oweals/busybox.git] / coreutils / install.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6  *
7  * TODO: -d option, need a way of recursively making directories and changing
8  *           owner/group, will probably modify bb_make_directory(...)
9  */
10
11 #include "busybox.h"
12 #include "libcoreutils/coreutils.h"
13 #include <libgen.h>
14 #include <getopt.h> /* struct option */
15
16 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
17 static const struct option install_long_options[] = {
18         { "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 }
25 };
26 #endif
27
28 int install_main(int argc, char **argv);
29 int install_main(int argc, char **argv)
30 {
31         struct stat statbuf;
32         mode_t mode;
33         uid_t uid;
34         gid_t gid;
35         const char *gid_str;
36         const char *uid_str;
37         const char *mode_str;
38         int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
39         int ret = EXIT_SUCCESS, flags, i, isdir;
40
41         enum {
42                 OPT_CMD           =  0x1,
43                 OPT_DIRECTORY     =  0x2,
44                 OPT_PRESERVE_TIME =  0x4,
45                 OPT_STRIP         =  0x8,
46                 OPT_GROUP         = 0x10,
47                 OPT_MODE          = 0x20,
48                 OPT_OWNER         = 0x40,
49         };
50
51 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
52         applet_long_options = install_long_options;
53 #endif
54         opt_complementary = "?:s--d:d--s";
55         /* -c exists for backwards compatibility, its needed */
56         flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str);
57
58         /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
59         if (flags & OPT_PRESERVE_TIME) {
60                 copy_flags |= FILEUTILS_PRESERVE_STATUS;
61         }
62         mode = 0666;
63         if (flags & OPT_MODE) bb_parse_mode(mode_str, &mode);
64         uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
65         gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
66         if (flags & (OPT_OWNER|OPT_GROUP)) umask(0);
67
68         /* Create directories
69          * don't use bb_make_directory() as it can't change uid or gid
70          * perhaps bb_make_directory() should be improved.
71          */
72         if (flags & OPT_DIRECTORY) {
73                 for (argv += optind; *argv; argv++) {
74                         char *old_argv_ptr = *argv + 1;
75                         char *argv_ptr;
76                         do {
77                                 argv_ptr = strchr(old_argv_ptr, '/');
78                                 old_argv_ptr = argv_ptr;
79                                 if (argv_ptr) {
80                                         *argv_ptr = '\0';
81                                         old_argv_ptr++;
82                                 }
83                                 if (mkdir(*argv, mode | 0111) == -1) {
84                                         if (errno != EEXIST) {
85                                                 bb_perror_msg("cannot create %s", *argv);
86                                                 ret = EXIT_FAILURE;
87                                                 break;
88                                         }
89                                 }
90                                 if ((flags & (OPT_OWNER|OPT_GROUP))
91                                  && lchown(*argv, uid, gid) == -1
92                                 ) {
93                                         bb_perror_msg("cannot change ownership of %s", *argv);
94                                         ret = EXIT_FAILURE;
95                                         break;
96                                 }
97                                 if (argv_ptr) {
98                                         *argv_ptr = '/';
99                                 }
100                         } while (old_argv_ptr);
101                 }
102                 return ret;
103         }
104
105         isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
106
107         for (i = optind; i < argc - 1; i++) {
108                 char *dest;
109
110                 dest = argv[argc - 1];
111                 if (isdir)
112                         dest = concat_path_file(argv[argc - 1], basename(argv[i]));
113                 ret |= copy_file(argv[i], dest, copy_flags);
114
115                 /* Set the file mode */
116                 if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
117                         bb_perror_msg("cannot change permissions of %s", dest);
118                         ret = EXIT_FAILURE;
119                 }
120
121                 /* Set the user and group id */
122                 if ((flags & (OPT_OWNER|OPT_GROUP))
123                  && lchown(dest, uid, gid) == -1
124                 ) {
125                         bb_perror_msg("cannot change ownership of %s", dest);
126                         ret = EXIT_FAILURE;
127                 }
128                 if (flags & OPT_STRIP) {
129                         if (execlp("strip", "strip", dest, NULL) == -1) {
130                                 bb_perror_msg("strip");
131                                 ret = EXIT_FAILURE;
132                         }
133                 }
134                 if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
135         }
136
137         return ret;
138 }