shar.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/cdefs.h>
  27. #ifdef __FBSDID
  28. __FBSDID("$FreeBSD$");
  29. #endif
  30. #include <sys/stat.h>
  31. #include <sys/types.h>
  32. #include <archive.h>
  33. #include <archive_entry.h>
  34. #include <assert.h>
  35. #include <err.h>
  36. #include <errno.h>
  37. #include <fcntl.h>
  38. #include <limits.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <sysexits.h>
  43. #include <unistd.h>
  44. #include "tree.h"
  45. /* command line options */
  46. static int b_opt; /* use alternative shar binary format */
  47. static int r_opt; /* recurse into subdirectories */
  48. static char *o_arg; /* output file name */
  49. static void
  50. usage(void)
  51. {
  52. fprintf(stderr, "Usage: shar [-br] [-o filename] file ...\n");
  53. exit(EX_USAGE);
  54. }
  55. /*
  56. * Initialize archive structure and create a shar archive.
  57. */
  58. static struct archive *
  59. shar_create(void)
  60. {
  61. struct archive *a;
  62. if ((a = archive_write_new()) == NULL)
  63. errx(EXIT_FAILURE, "%s", archive_error_string(a));
  64. if (b_opt)
  65. archive_write_set_format_shar_dump(a);
  66. else
  67. archive_write_set_format_shar(a);
  68. archive_write_set_compression_none(a);
  69. if (archive_write_open_filename(a, o_arg) != ARCHIVE_OK)
  70. errx(EX_CANTCREAT, "%s", archive_error_string(a));
  71. return (a);
  72. }
  73. /* buffer for file data */
  74. static char buffer[32768];
  75. /*
  76. * Write file data to an archive entry.
  77. */
  78. static int
  79. shar_write_entry_data(struct archive *a, const int fd)
  80. {
  81. ssize_t bytes_read, bytes_written;
  82. assert(a != NULL);
  83. assert(fd >= 0);
  84. bytes_read = read(fd, buffer, sizeof(buffer));
  85. while (bytes_read != 0) {
  86. if (bytes_read < 0) {
  87. archive_set_error(a, errno, "Read failed");
  88. return (ARCHIVE_WARN);
  89. }
  90. bytes_written = archive_write_data(a, buffer, bytes_read);
  91. if (bytes_written < 0)
  92. return (ARCHIVE_WARN);
  93. bytes_read = read(fd, buffer, sizeof(buffer));
  94. }
  95. return (ARCHIVE_OK);
  96. }
  97. /*
  98. * Write a file to the archive. We have special handling for symbolic links.
  99. */
  100. static int
  101. shar_write_entry(struct archive *a, const char *pathname, const char *accpath,
  102. const struct stat *st)
  103. {
  104. struct archive_entry *entry;
  105. int fd = -1;
  106. int ret = ARCHIVE_OK;
  107. assert(a != NULL);
  108. assert(pathname != NULL);
  109. assert(accpath != NULL);
  110. assert(st != NULL);
  111. entry = archive_entry_new();
  112. if (S_ISREG(st->st_mode) && st->st_size > 0) {
  113. /* regular file */
  114. if ((fd = open(accpath, O_RDONLY)) == -1) {
  115. warn("%s", accpath);
  116. ret = ARCHIVE_WARN;
  117. goto out;
  118. }
  119. } else if (S_ISLNK(st->st_mode)) {
  120. /* symbolic link */
  121. char lnkbuff[PATH_MAX + 1];
  122. int lnklen;
  123. if ((lnklen = readlink(accpath, lnkbuff, PATH_MAX)) == -1) {
  124. warn("%s", accpath);
  125. ret = ARCHIVE_WARN;
  126. goto out;
  127. }
  128. lnkbuff[lnklen] = '\0';
  129. archive_entry_set_symlink(entry, lnkbuff);
  130. }
  131. archive_entry_copy_stat(entry, st);
  132. archive_entry_set_pathname(entry, pathname);
  133. if (!S_ISREG(st->st_mode) || st->st_size == 0)
  134. archive_entry_set_size(entry, 0);
  135. if (archive_write_header(a, entry) != ARCHIVE_OK) {
  136. warnx("%s: %s", pathname, archive_error_string(a));
  137. ret = ARCHIVE_WARN;
  138. goto out;
  139. }
  140. if (fd >= 0) {
  141. if ((ret = shar_write_entry_data(a, fd)) != ARCHIVE_OK)
  142. warnx("%s: %s", accpath, archive_error_string(a));
  143. }
  144. out:
  145. archive_entry_free(entry);
  146. if (fd >= 0)
  147. close(fd);
  148. return (ret);
  149. }
  150. /*
  151. * Write single path to the archive. The path can be a regular file, directory
  152. * or device. Symbolic links are followed.
  153. */
  154. static int
  155. shar_write_path(struct archive *a, const char *pathname)
  156. {
  157. struct stat st;
  158. assert(a != NULL);
  159. assert(pathname != NULL);
  160. if ((stat(pathname, &st)) == -1) {
  161. warn("%s", pathname);
  162. return (ARCHIVE_WARN);
  163. }
  164. return (shar_write_entry(a, pathname, pathname, &st));
  165. }
  166. /*
  167. * Write tree to the archive. If pathname is a symbolic link it will be
  168. * followed. Other symbolic links are stored as such to the archive.
  169. */
  170. static int
  171. shar_write_tree(struct archive *a, const char *pathname)
  172. {
  173. struct tree *t;
  174. const struct stat *lst, *st;
  175. int error = 0;
  176. int tree_ret;
  177. int first;
  178. assert(a != NULL);
  179. assert(pathname != NULL);
  180. t = tree_open(pathname);
  181. for (first = 1; (tree_ret = tree_next(t)); first = 0) {
  182. if (tree_ret == TREE_ERROR_DIR) {
  183. warnx("%s: %s", tree_current_path(t),
  184. strerror(tree_errno(t)));
  185. error = 1;
  186. continue;
  187. } else if (tree_ret != TREE_REGULAR)
  188. continue;
  189. if ((lst = tree_current_lstat(t)) == NULL) {
  190. warn("%s", tree_current_path(t));
  191. error = 1;
  192. continue;
  193. }
  194. /*
  195. * If the symlink was given on command line then
  196. * follow it rather than write it as symlink.
  197. */
  198. if (first && S_ISLNK(lst->st_mode)) {
  199. if ((st = tree_current_stat(t)) == NULL) {
  200. warn("%s", tree_current_path(t));
  201. error = 1;
  202. continue;
  203. }
  204. } else
  205. st = lst;
  206. if (shar_write_entry(a, tree_current_path(t),
  207. tree_current_access_path(t), st) != ARCHIVE_OK)
  208. error = 1;
  209. tree_descend(t);
  210. }
  211. tree_close(t);
  212. return ((error != 0) ? ARCHIVE_WARN : ARCHIVE_OK);
  213. }
  214. /*
  215. * Create a shar archive and write files/trees into it.
  216. */
  217. static int
  218. shar_write(char **fn, size_t nfn)
  219. {
  220. struct archive *a;
  221. size_t i;
  222. int error = 0;
  223. assert(fn != NULL);
  224. assert(nfn > 0);
  225. a = shar_create();
  226. for (i = 0; i < nfn; i++) {
  227. if (r_opt) {
  228. if (shar_write_tree(a, fn[i]) != ARCHIVE_OK)
  229. error = 1;
  230. } else {
  231. if (shar_write_path(a, fn[i]) != ARCHIVE_OK)
  232. error = 1;
  233. }
  234. }
  235. if (archive_write_free(a) != ARCHIVE_OK)
  236. errx(EXIT_FAILURE, "%s", archive_error_string(a));
  237. if (error != 0)
  238. warnx("Error exit delayed from previous errors.");
  239. return (error);
  240. }
  241. int
  242. main(int argc, char **argv)
  243. {
  244. int opt;
  245. while ((opt = getopt(argc, argv, "bro:")) != -1) {
  246. switch (opt) {
  247. case 'b':
  248. b_opt = 1;
  249. break;
  250. case 'o':
  251. o_arg = optarg;
  252. break;
  253. case 'r':
  254. r_opt = 1;
  255. break;
  256. default:
  257. usage();
  258. /* NOTREACHED */
  259. }
  260. }
  261. argc -= optind;
  262. argv += optind;
  263. if(argc < 1)
  264. usage();
  265. if (shar_write(argv, argc) != 0)
  266. exit(EXIT_FAILURE);
  267. else
  268. exit(EXIT_SUCCESS);
  269. /* NOTREACHED */
  270. }