shar.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*-
  2. * Copyright (c) 2008 Jaakko Heinonen
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <archive.h>
  29. #include <archive_entry.h>
  30. #include <assert.h>
  31. #include <err.h>
  32. #include <errno.h>
  33. #include <fcntl.h>
  34. #include <limits.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sysexits.h>
  39. #include <unistd.h>
  40. #include "tree.h"
  41. /* command line options */
  42. static int b_opt; /* use alternative shar binary format */
  43. static int r_opt; /* recurse into subdirectories */
  44. static char *o_arg; /* output file name */
  45. static void
  46. usage(void)
  47. {
  48. fprintf(stderr, "Usage: shar [-br] [-o filename] file ...\n");
  49. exit(EX_USAGE);
  50. }
  51. /*
  52. * Initialize archive structure and create a shar archive.
  53. */
  54. static struct archive *
  55. shar_create(void)
  56. {
  57. struct archive *a;
  58. if ((a = archive_write_new()) == NULL)
  59. errx(EXIT_FAILURE, "%s", archive_error_string(a));
  60. if (b_opt)
  61. archive_write_set_format_shar_dump(a);
  62. else
  63. archive_write_set_format_shar(a);
  64. archive_write_set_compression_none(a);
  65. if (archive_write_open_filename(a, o_arg) != ARCHIVE_OK)
  66. errx(EX_CANTCREAT, "%s", archive_error_string(a));
  67. return (a);
  68. }
  69. /* buffer for file data */
  70. static char buffer[32768];
  71. /*
  72. * Write file data to an archive entry.
  73. */
  74. static int
  75. shar_write_entry_data(struct archive *a, const int fd)
  76. {
  77. ssize_t bytes_read, bytes_written;
  78. assert(a != NULL);
  79. assert(fd >= 0);
  80. bytes_read = read(fd, buffer, sizeof(buffer));
  81. while (bytes_read != 0) {
  82. if (bytes_read < 0) {
  83. archive_set_error(a, errno, "Read failed");
  84. return (ARCHIVE_WARN);
  85. }
  86. bytes_written = archive_write_data(a, buffer, bytes_read);
  87. if (bytes_written < 0)
  88. return (ARCHIVE_WARN);
  89. bytes_read = read(fd, buffer, sizeof(buffer));
  90. }
  91. return (ARCHIVE_OK);
  92. }
  93. /*
  94. * Write a file to the archive. We have special handling for symbolic links.
  95. */
  96. static int
  97. shar_write_entry(struct archive *a, const char *pathname, const char *accpath,
  98. const struct stat *st)
  99. {
  100. struct archive_entry *entry;
  101. int fd = -1;
  102. int ret = ARCHIVE_OK;
  103. assert(a != NULL);
  104. assert(pathname != NULL);
  105. assert(accpath != NULL);
  106. assert(st != NULL);
  107. entry = archive_entry_new();
  108. if (S_ISREG(st->st_mode) && st->st_size > 0) {
  109. /* regular file */
  110. if ((fd = open(accpath, O_RDONLY)) == -1) {
  111. warn("%s", accpath);
  112. ret = ARCHIVE_WARN;
  113. goto out;
  114. }
  115. } else if (S_ISLNK(st->st_mode)) {
  116. /* symbolic link */
  117. char lnkbuff[PATH_MAX + 1];
  118. int lnklen;
  119. if ((lnklen = readlink(accpath, lnkbuff, PATH_MAX)) == -1) {
  120. warn("%s", accpath);
  121. ret = ARCHIVE_WARN;
  122. goto out;
  123. }
  124. lnkbuff[lnklen] = '\0';
  125. archive_entry_set_symlink(entry, lnkbuff);
  126. }
  127. archive_entry_copy_stat(entry, st);
  128. archive_entry_set_pathname(entry, pathname);
  129. if (!S_ISREG(st->st_mode) || st->st_size == 0)
  130. archive_entry_set_size(entry, 0);
  131. if (archive_write_header(a, entry) != ARCHIVE_OK) {
  132. warnx("%s: %s", pathname, archive_error_string(a));
  133. ret = ARCHIVE_WARN;
  134. goto out;
  135. }
  136. if (fd >= 0) {
  137. if ((ret = shar_write_entry_data(a, fd)) != ARCHIVE_OK)
  138. warnx("%s: %s", accpath, archive_error_string(a));
  139. }
  140. out:
  141. archive_entry_free(entry);
  142. if (fd >= 0)
  143. close(fd);
  144. return (ret);
  145. }
  146. /*
  147. * Write single path to the archive. The path can be a regular file, directory
  148. * or device. Symbolic links are followed.
  149. */
  150. static int
  151. shar_write_path(struct archive *a, const char *pathname)
  152. {
  153. struct stat st;
  154. assert(a != NULL);
  155. assert(pathname != NULL);
  156. if ((stat(pathname, &st)) == -1) {
  157. warn("%s", pathname);
  158. return (ARCHIVE_WARN);
  159. }
  160. return (shar_write_entry(a, pathname, pathname, &st));
  161. }
  162. /*
  163. * Write tree to the archive. If pathname is a symbolic link it will be
  164. * followed. Other symbolic links are stored as such to the archive.
  165. */
  166. static int
  167. shar_write_tree(struct archive *a, const char *pathname)
  168. {
  169. struct tree *t;
  170. const struct stat *lst, *st;
  171. int error = 0;
  172. int tree_ret;
  173. int first;
  174. assert(a != NULL);
  175. assert(pathname != NULL);
  176. t = tree_open(pathname);
  177. for (first = 1; (tree_ret = tree_next(t)); first = 0) {
  178. if (tree_ret == TREE_ERROR_DIR) {
  179. warnx("%s: %s", tree_current_path(t),
  180. strerror(tree_errno(t)));
  181. error = 1;
  182. continue;
  183. } else if (tree_ret != TREE_REGULAR)
  184. continue;
  185. if ((lst = tree_current_lstat(t)) == NULL) {
  186. warn("%s", tree_current_path(t));
  187. error = 1;
  188. continue;
  189. }
  190. /*
  191. * If the symlink was given on command line then
  192. * follow it rather than write it as symlink.
  193. */
  194. if (first && S_ISLNK(lst->st_mode)) {
  195. if ((st = tree_current_stat(t)) == NULL) {
  196. warn("%s", tree_current_path(t));
  197. error = 1;
  198. continue;
  199. }
  200. } else
  201. st = lst;
  202. if (shar_write_entry(a, tree_current_path(t),
  203. tree_current_access_path(t), st) != ARCHIVE_OK)
  204. error = 1;
  205. tree_descend(t);
  206. }
  207. tree_close(t);
  208. return ((error != 0) ? ARCHIVE_WARN : ARCHIVE_OK);
  209. }
  210. /*
  211. * Create a shar archive and write files/trees into it.
  212. */
  213. static int
  214. shar_write(char **fn, size_t nfn)
  215. {
  216. struct archive *a;
  217. size_t i;
  218. int error = 0;
  219. assert(fn != NULL);
  220. assert(nfn > 0);
  221. a = shar_create();
  222. for (i = 0; i < nfn; i++) {
  223. if (r_opt) {
  224. if (shar_write_tree(a, fn[i]) != ARCHIVE_OK)
  225. error = 1;
  226. } else {
  227. if (shar_write_path(a, fn[i]) != ARCHIVE_OK)
  228. error = 1;
  229. }
  230. }
  231. if (archive_write_free(a) != ARCHIVE_OK)
  232. errx(EXIT_FAILURE, "%s", archive_error_string(a));
  233. if (error != 0)
  234. warnx("Error exit delayed from previous errors.");
  235. return (error);
  236. }
  237. int
  238. main(int argc, char **argv)
  239. {
  240. int opt;
  241. while ((opt = getopt(argc, argv, "bro:")) != -1) {
  242. switch (opt) {
  243. case 'b':
  244. b_opt = 1;
  245. break;
  246. case 'o':
  247. o_arg = optarg;
  248. break;
  249. case 'r':
  250. r_opt = 1;
  251. break;
  252. default:
  253. usage();
  254. /* NOTREACHED */
  255. }
  256. }
  257. argc -= optind;
  258. argv += optind;
  259. if(argc < 1)
  260. usage();
  261. if (shar_write(argv, argc) != 0)
  262. exit(EXIT_FAILURE);
  263. else
  264. exit(EXIT_SUCCESS);
  265. /* NOTREACHED */
  266. }