3
0

install.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.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(...)
  21. */
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <errno.h>
  25. #include <getopt.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "busybox.h"
  30. #include "libcoreutils/coreutils.h"
  31. #define INSTALL_OPT_CMD 1
  32. #define INSTALL_OPT_DIRECTORY 2
  33. #define INSTALL_OPT_PRESERVE_TIME 4
  34. #define INSTALL_OPT_STRIP 8
  35. #define INSTALL_OPT_GROUP 16
  36. #define INSTALL_OPT_MODE 32
  37. #define INSTALL_OPT_OWNER 64
  38. static const struct option install_long_options[] = {
  39. { "directory", 0, NULL, 'd' },
  40. { "preserve-timestamps", 0, NULL, 'p' },
  41. { "strip", 0, NULL, 's' },
  42. { "group", 0, NULL, 'g' },
  43. { "mode", 0, NULL, 'm' },
  44. { "owner", 0, NULL, 'o' },
  45. { 0, 0, 0, 0 }
  46. };
  47. extern int install_main(int argc, char **argv)
  48. {
  49. struct stat statbuf;
  50. mode_t mode;
  51. uid_t uid;
  52. gid_t gid;
  53. char *gid_str = "-1";
  54. char *uid_str = "-1";
  55. char *mode_str = "0755";
  56. int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
  57. int ret = EXIT_SUCCESS;
  58. int flags;
  59. int i;
  60. bb_applet_long_options = install_long_options;
  61. bb_opt_complementaly = "s~d:d~s";
  62. /* -c exists for backwards compatability, its needed */
  63. flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
  64. /* Check valid options were given */
  65. if(flags & BB_GETOPT_ERROR) {
  66. bb_show_usage();
  67. }
  68. /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
  69. if (flags & INSTALL_OPT_PRESERVE_TIME) {
  70. copy_flags |= FILEUTILS_PRESERVE_STATUS;
  71. }
  72. bb_parse_mode(mode_str, &mode);
  73. gid = get_ug_id(gid_str, my_getgrnam);
  74. uid = get_ug_id(uid_str, my_getpwnam);
  75. umask(0);
  76. /* Create directories
  77. * dont use bb_make_directory() as it cant change uid or gid
  78. * perhaps bb_make_directory() should be improved.
  79. */
  80. if (flags & INSTALL_OPT_DIRECTORY) {
  81. for (argv += optind; *argv; argv++) {
  82. char *old_argv_ptr = *argv + 1;
  83. char *argv_ptr;
  84. do {
  85. argv_ptr = strchr(old_argv_ptr, '/');
  86. old_argv_ptr = argv_ptr;
  87. if (argv_ptr) {
  88. *argv_ptr = '\0';
  89. old_argv_ptr++;
  90. }
  91. if (mkdir(*argv, mode) == -1) {
  92. if (errno != EEXIST) {
  93. bb_perror_msg("coulnt create %s", *argv);
  94. ret = EXIT_FAILURE;
  95. break;
  96. }
  97. }
  98. else if (lchown(*argv, uid, gid) == -1) {
  99. bb_perror_msg("cannot change ownership of %s", *argv);
  100. ret = EXIT_FAILURE;
  101. break;
  102. }
  103. if (argv_ptr) {
  104. *argv_ptr = '/';
  105. }
  106. } while (old_argv_ptr);
  107. }
  108. return(ret);
  109. }
  110. cp_mv_stat2(argv[argc - 1], &statbuf, lstat);
  111. for (i = optind; i < argc - 1; i++) {
  112. unsigned char *dest;
  113. if (S_ISDIR(statbuf.st_mode)) {
  114. dest = concat_path_file(argv[argc - 1], basename(argv[i]));
  115. } else {
  116. dest = argv[argc - 1];
  117. }
  118. ret |= copy_file(argv[i], dest, copy_flags);
  119. /* Set the file mode */
  120. if (chmod(dest, mode) == -1) {
  121. bb_perror_msg("cannot change permissions of %s", dest);
  122. ret = EXIT_FAILURE;
  123. }
  124. /* Set the user and group id */
  125. if (lchown(dest, uid, gid) == -1) {
  126. bb_perror_msg("cannot change ownership of %s", dest);
  127. ret = EXIT_FAILURE;
  128. }
  129. if (flags & INSTALL_OPT_STRIP) {
  130. if (execlp("strip", "strip", dest, NULL) == -1) {
  131. bb_error_msg("strip failed");
  132. ret = EXIT_FAILURE;
  133. }
  134. }
  135. }
  136. return(ret);
  137. }