archive_write_set_format_shar.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2008 Joerg Sonnenberger
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  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 "archive_platform.h"
  27. __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_shar.c,v 1.20 2008/08/31 07:10:40 kientzle Exp $");
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. #include <stdio.h>
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_private.h"
  41. #include "archive_write_private.h"
  42. struct shar {
  43. int dump;
  44. int end_of_line;
  45. struct archive_entry *entry;
  46. int has_data;
  47. char *last_dir;
  48. /* Line buffer for uuencoded dump format */
  49. char outbuff[45];
  50. size_t outpos;
  51. int wrote_header;
  52. struct archive_string work;
  53. struct archive_string quoted_name;
  54. };
  55. static int archive_write_shar_finish(struct archive_write *);
  56. static int archive_write_shar_destroy(struct archive_write *);
  57. static int archive_write_shar_header(struct archive_write *,
  58. struct archive_entry *);
  59. static ssize_t archive_write_shar_data_sed(struct archive_write *,
  60. const void * buff, size_t);
  61. static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
  62. const void * buff, size_t);
  63. static int archive_write_shar_finish_entry(struct archive_write *);
  64. /*
  65. * Copy the given string to the buffer, quoting all shell meta characters
  66. * found.
  67. */
  68. static void
  69. shar_quote(struct archive_string *buf, const char *str, int in_shell)
  70. {
  71. static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
  72. size_t len;
  73. while (*str != '\0') {
  74. if ((len = strcspn(str, meta)) != 0) {
  75. archive_strncat(buf, str, len);
  76. str += len;
  77. } else if (*str == '\n') {
  78. if (in_shell)
  79. archive_strcat(buf, "\"\n\"");
  80. else
  81. archive_strcat(buf, "\\n");
  82. ++str;
  83. } else {
  84. archive_strappend_char(buf, '\\');
  85. archive_strappend_char(buf, *str);
  86. ++str;
  87. }
  88. }
  89. }
  90. /*
  91. * Set output format to 'shar' format.
  92. */
  93. int
  94. archive_write_set_format_shar(struct archive *_a)
  95. {
  96. struct archive_write *a = (struct archive_write *)_a;
  97. struct shar *shar;
  98. /* If someone else was already registered, unregister them. */
  99. if (a->format_destroy != NULL)
  100. (a->format_destroy)(a);
  101. shar = (struct shar *)malloc(sizeof(*shar));
  102. if (shar == NULL) {
  103. archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
  104. return (ARCHIVE_FATAL);
  105. }
  106. memset(shar, 0, sizeof(*shar));
  107. archive_string_init(&shar->work);
  108. archive_string_init(&shar->quoted_name);
  109. a->format_data = shar;
  110. a->pad_uncompressed = 0;
  111. a->format_name = "shar";
  112. a->format_write_header = archive_write_shar_header;
  113. a->format_finish = archive_write_shar_finish;
  114. a->format_destroy = archive_write_shar_destroy;
  115. a->format_write_data = archive_write_shar_data_sed;
  116. a->format_finish_entry = archive_write_shar_finish_entry;
  117. a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
  118. a->archive.archive_format_name = "shar";
  119. return (ARCHIVE_OK);
  120. }
  121. /*
  122. * An alternate 'shar' that uses uudecode instead of 'sed' to encode
  123. * file contents and can therefore be used to archive binary files.
  124. * In addition, this variant also attempts to restore ownership, file modes,
  125. * and other extended file information.
  126. */
  127. int
  128. archive_write_set_format_shar_dump(struct archive *_a)
  129. {
  130. struct archive_write *a = (struct archive_write *)_a;
  131. struct shar *shar;
  132. archive_write_set_format_shar(&a->archive);
  133. shar = (struct shar *)a->format_data;
  134. shar->dump = 1;
  135. a->format_write_data = archive_write_shar_data_uuencode;
  136. a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
  137. a->archive.archive_format_name = "shar dump";
  138. return (ARCHIVE_OK);
  139. }
  140. static int
  141. archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
  142. {
  143. const char *linkname;
  144. const char *name;
  145. char *p, *pp;
  146. struct shar *shar;
  147. shar = (struct shar *)a->format_data;
  148. if (!shar->wrote_header) {
  149. archive_strcat(&shar->work, "#!/bin/sh\n");
  150. archive_strcat(&shar->work, "# This is a shell archive\n");
  151. shar->wrote_header = 1;
  152. }
  153. /* Save the entry for the closing. */
  154. if (shar->entry)
  155. archive_entry_free(shar->entry);
  156. shar->entry = archive_entry_clone(entry);
  157. name = archive_entry_pathname(entry);
  158. /* Handle some preparatory issues. */
  159. switch(archive_entry_filetype(entry)) {
  160. case AE_IFREG:
  161. /* Only regular files have non-zero size. */
  162. break;
  163. case AE_IFDIR:
  164. archive_entry_set_size(entry, 0);
  165. /* Don't bother trying to recreate '.' */
  166. if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
  167. return (ARCHIVE_OK);
  168. break;
  169. case AE_IFIFO:
  170. case AE_IFCHR:
  171. case AE_IFBLK:
  172. /* All other file types have zero size in the archive. */
  173. archive_entry_set_size(entry, 0);
  174. break;
  175. default:
  176. archive_entry_set_size(entry, 0);
  177. if (archive_entry_hardlink(entry) == NULL &&
  178. archive_entry_symlink(entry) == NULL) {
  179. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  180. "shar format cannot archive this");
  181. return (ARCHIVE_WARN);
  182. }
  183. }
  184. archive_string_empty(&shar->quoted_name);
  185. shar_quote(&shar->quoted_name, name, 1);
  186. /* Stock preparation for all file types. */
  187. archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s);
  188. if (archive_entry_filetype(entry) != AE_IFDIR) {
  189. /* Try to create the dir. */
  190. p = strdup(name);
  191. pp = strrchr(p, '/');
  192. /* If there is a / character, try to create the dir. */
  193. if (pp != NULL) {
  194. *pp = '\0';
  195. /* Try to avoid a lot of redundant mkdir commands. */
  196. if (strcmp(p, ".") == 0) {
  197. /* Don't try to "mkdir ." */
  198. free(p);
  199. } else if (shar->last_dir == NULL) {
  200. archive_strcat(&shar->work, "mkdir -p ");
  201. shar_quote(&shar->work, p, 1);
  202. archive_strcat(&shar->work,
  203. " > /dev/null 2>&1\n");
  204. shar->last_dir = p;
  205. } else if (strcmp(p, shar->last_dir) == 0) {
  206. /* We've already created this exact dir. */
  207. free(p);
  208. } else if (strlen(p) < strlen(shar->last_dir) &&
  209. strncmp(p, shar->last_dir, strlen(p)) == 0) {
  210. /* We've already created a subdir. */
  211. free(p);
  212. } else {
  213. archive_strcat(&shar->work, "mkdir -p ");
  214. shar_quote(&shar->work, p, 1);
  215. archive_strcat(&shar->work,
  216. " > /dev/null 2>&1\n");
  217. shar->last_dir = p;
  218. }
  219. } else {
  220. free(p);
  221. }
  222. }
  223. /* Handle file-type specific issues. */
  224. shar->has_data = 0;
  225. if ((linkname = archive_entry_hardlink(entry)) != NULL) {
  226. archive_strcat(&shar->work, "ln -f ");
  227. shar_quote(&shar->work, linkname, 1);
  228. archive_string_sprintf(&shar->work, " %s\n",
  229. shar->quoted_name.s);
  230. } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
  231. archive_strcat(&shar->work, "ln -fs ");
  232. shar_quote(&shar->work, linkname, 1);
  233. archive_string_sprintf(&shar->work, " %s\n",
  234. shar->quoted_name.s);
  235. } else {
  236. switch(archive_entry_filetype(entry)) {
  237. case AE_IFREG:
  238. if (archive_entry_size(entry) == 0) {
  239. /* More portable than "touch." */
  240. archive_string_sprintf(&shar->work,
  241. "test -e \"%s\" || :> \"%s\"\n",
  242. shar->quoted_name.s, shar->quoted_name.s);
  243. } else {
  244. if (shar->dump) {
  245. archive_string_sprintf(&shar->work,
  246. "uudecode -p > %s << 'SHAR_END'\n",
  247. shar->quoted_name.s);
  248. archive_string_sprintf(&shar->work,
  249. "begin %o ",
  250. archive_entry_mode(entry) & 0777);
  251. shar_quote(&shar->work, name, 0);
  252. archive_strcat(&shar->work, "\n");
  253. } else {
  254. archive_string_sprintf(&shar->work,
  255. "sed 's/^X//' > %s << 'SHAR_END'\n",
  256. shar->quoted_name.s);
  257. }
  258. shar->has_data = 1;
  259. shar->end_of_line = 1;
  260. shar->outpos = 0;
  261. }
  262. break;
  263. case AE_IFDIR:
  264. archive_string_sprintf(&shar->work,
  265. "mkdir -p %s > /dev/null 2>&1\n",
  266. shar->quoted_name.s);
  267. /* Record that we just created this directory. */
  268. if (shar->last_dir != NULL)
  269. free(shar->last_dir);
  270. shar->last_dir = strdup(name);
  271. /* Trim a trailing '/'. */
  272. pp = strrchr(shar->last_dir, '/');
  273. if (pp != NULL && pp[1] == '\0')
  274. *pp = '\0';
  275. /*
  276. * TODO: Put dir name/mode on a list to be fixed
  277. * up at end of archive.
  278. */
  279. break;
  280. case AE_IFIFO:
  281. archive_string_sprintf(&shar->work,
  282. "mkfifo %s\n", shar->quoted_name.s);
  283. break;
  284. case AE_IFCHR:
  285. archive_string_sprintf(&shar->work,
  286. "mknod %s c %d %d\n", shar->quoted_name.s,
  287. archive_entry_rdevmajor(entry),
  288. archive_entry_rdevminor(entry));
  289. break;
  290. case AE_IFBLK:
  291. archive_string_sprintf(&shar->work,
  292. "mknod %s b %d %d\n", shar->quoted_name.s,
  293. archive_entry_rdevmajor(entry),
  294. archive_entry_rdevminor(entry));
  295. break;
  296. default:
  297. return (ARCHIVE_WARN);
  298. }
  299. }
  300. return (ARCHIVE_OK);
  301. }
  302. static ssize_t
  303. archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
  304. {
  305. static const size_t ensured = 65533;
  306. struct shar *shar;
  307. const char *src;
  308. char *buf, *buf_end;
  309. int ret;
  310. size_t written = n;
  311. shar = (struct shar *)a->format_data;
  312. if (!shar->has_data || n == 0)
  313. return (0);
  314. src = (const char *)buff;
  315. /*
  316. * ensure is the number of bytes in buffer before expanding the
  317. * current character. Each operation writes the current character
  318. * and optionally the start-of-new-line marker. This can happen
  319. * twice before entering the loop, so make sure three additional
  320. * bytes can be written.
  321. */
  322. if (archive_string_ensure(&shar->work, ensured + 3) == NULL)
  323. __archive_errx(1, "Out of memory");
  324. if (shar->work.length > ensured) {
  325. ret = (*a->compressor.write)(a, shar->work.s,
  326. shar->work.length);
  327. if (ret != ARCHIVE_OK)
  328. return (ARCHIVE_FATAL);
  329. archive_string_empty(&shar->work);
  330. }
  331. buf = shar->work.s + shar->work.length;
  332. buf_end = shar->work.s + ensured;
  333. if (shar->end_of_line) {
  334. *buf++ = 'X';
  335. shar->end_of_line = 0;
  336. }
  337. while (n-- != 0) {
  338. if ((*buf++ = *src++) == '\n') {
  339. if (n == 0)
  340. shar->end_of_line = 1;
  341. else
  342. *buf++ = 'X';
  343. }
  344. if (buf >= buf_end) {
  345. shar->work.length = buf - shar->work.s;
  346. ret = (*a->compressor.write)(a, shar->work.s,
  347. shar->work.length);
  348. if (ret != ARCHIVE_OK)
  349. return (ARCHIVE_FATAL);
  350. archive_string_empty(&shar->work);
  351. buf = shar->work.s;
  352. }
  353. }
  354. shar->work.length = buf - shar->work.s;
  355. return (written);
  356. }
  357. #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
  358. static void
  359. uuencode_group(const char _in[3], char out[4])
  360. {
  361. const unsigned char *in = (const unsigned char *)_in;
  362. int t;
  363. t = (in[0] << 16) | (in[1] << 8) | in[2];
  364. out[0] = UUENC( 0x3f & (t >> 18) );
  365. out[1] = UUENC( 0x3f & (t >> 12) );
  366. out[2] = UUENC( 0x3f & (t >> 6) );
  367. out[3] = UUENC( 0x3f & t );
  368. }
  369. static void
  370. uuencode_line(struct shar *shar, const char *inbuf, size_t len)
  371. {
  372. char tmp_buf[3], *buf;
  373. size_t alloc_len;
  374. /* len <= 45 -> expanded to 60 + len byte + new line */
  375. alloc_len = shar->work.length + 62;
  376. if (archive_string_ensure(&shar->work, alloc_len) == NULL)
  377. __archive_errx(1, "Out of memory");
  378. buf = shar->work.s + shar->work.length;
  379. *buf++ = UUENC(len);
  380. while (len >= 3) {
  381. uuencode_group(inbuf, buf);
  382. len -= 3;
  383. inbuf += 3;
  384. buf += 4;
  385. }
  386. if (len != 0) {
  387. tmp_buf[0] = inbuf[0];
  388. if (len == 1)
  389. tmp_buf[1] = '\0';
  390. else
  391. tmp_buf[1] = inbuf[1];
  392. tmp_buf[2] = '\0';
  393. uuencode_group(inbuf, buf);
  394. buf += 4;
  395. }
  396. *buf++ = '\n';
  397. if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62))
  398. __archive_errx(1, "Buffer overflow");
  399. shar->work.length = buf - shar->work.s;
  400. }
  401. static ssize_t
  402. archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
  403. size_t length)
  404. {
  405. struct shar *shar;
  406. const char *src;
  407. size_t n;
  408. int ret;
  409. shar = (struct shar *)a->format_data;
  410. if (!shar->has_data)
  411. return (ARCHIVE_OK);
  412. src = (const char *)buff;
  413. if (shar->outpos != 0) {
  414. n = 45 - shar->outpos;
  415. if (n > length)
  416. n = length;
  417. memcpy(shar->outbuff + shar->outpos, src, n);
  418. if (shar->outpos + n < 45) {
  419. shar->outpos += n;
  420. return length;
  421. }
  422. uuencode_line(shar, shar->outbuff, 45);
  423. src += n;
  424. n = length - n;
  425. } else {
  426. n = length;
  427. }
  428. while (n >= 45) {
  429. uuencode_line(shar, src, 45);
  430. src += 45;
  431. n -= 45;
  432. if (shar->work.length < 65536)
  433. continue;
  434. ret = (*a->compressor.write)(a, shar->work.s,
  435. shar->work.length);
  436. if (ret != ARCHIVE_OK)
  437. return (ARCHIVE_FATAL);
  438. archive_string_empty(&shar->work);
  439. }
  440. if (n != 0) {
  441. memcpy(shar->outbuff, src, n);
  442. shar->outpos = n;
  443. }
  444. return (length);
  445. }
  446. static int
  447. archive_write_shar_finish_entry(struct archive_write *a)
  448. {
  449. const char *g, *p, *u;
  450. struct shar *shar;
  451. int ret;
  452. shar = (struct shar *)a->format_data;
  453. if (shar->entry == NULL)
  454. return (0);
  455. if (shar->dump) {
  456. /* Finish uuencoded data. */
  457. if (shar->has_data) {
  458. if (shar->outpos > 0)
  459. uuencode_line(shar, shar->outbuff,
  460. shar->outpos);
  461. archive_strcat(&shar->work, "`\nend\n");
  462. archive_strcat(&shar->work, "SHAR_END\n");
  463. }
  464. /* Restore file mode, owner, flags. */
  465. /*
  466. * TODO: Don't immediately restore mode for
  467. * directories; defer that to end of script.
  468. */
  469. archive_string_sprintf(&shar->work, "chmod %o ",
  470. archive_entry_mode(shar->entry) & 07777);
  471. shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
  472. archive_strcat(&shar->work, "\n");
  473. u = archive_entry_uname(shar->entry);
  474. g = archive_entry_gname(shar->entry);
  475. if (u != NULL || g != NULL) {
  476. archive_strcat(&shar->work, "chown ");
  477. if (u != NULL)
  478. shar_quote(&shar->work, u, 1);
  479. if (g != NULL) {
  480. archive_strcat(&shar->work, ":");
  481. shar_quote(&shar->work, g, 1);
  482. }
  483. shar_quote(&shar->work,
  484. archive_entry_pathname(shar->entry), 1);
  485. archive_strcat(&shar->work, "\n");
  486. }
  487. if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
  488. archive_string_sprintf(&shar->work, "chflags %s ",
  489. p, archive_entry_pathname(shar->entry));
  490. shar_quote(&shar->work,
  491. archive_entry_pathname(shar->entry), 1);
  492. archive_strcat(&shar->work, "\n");
  493. }
  494. /* TODO: restore ACLs */
  495. } else {
  496. if (shar->has_data) {
  497. /* Finish sed-encoded data: ensure last line ends. */
  498. if (!shar->end_of_line)
  499. archive_strappend_char(&shar->work, '\n');
  500. archive_strcat(&shar->work, "SHAR_END\n");
  501. }
  502. }
  503. archive_entry_free(shar->entry);
  504. shar->entry = NULL;
  505. if (shar->work.length < 65536)
  506. return (ARCHIVE_OK);
  507. ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
  508. if (ret != ARCHIVE_OK)
  509. return (ARCHIVE_FATAL);
  510. archive_string_empty(&shar->work);
  511. return (ARCHIVE_OK);
  512. }
  513. static int
  514. archive_write_shar_finish(struct archive_write *a)
  515. {
  516. struct shar *shar;
  517. int ret;
  518. /*
  519. * TODO: Accumulate list of directory names/modes and
  520. * fix them all up at end-of-archive.
  521. */
  522. shar = (struct shar *)a->format_data;
  523. /*
  524. * Only write the end-of-archive markers if the archive was
  525. * actually started. This avoids problems if someone sets
  526. * shar format, then sets another format (which would invoke
  527. * shar_finish to free the format-specific data).
  528. */
  529. if (shar->wrote_header == 0)
  530. return (ARCHIVE_OK);
  531. archive_strcat(&shar->work, "exit\n");
  532. ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
  533. if (ret != ARCHIVE_OK)
  534. return (ARCHIVE_FATAL);
  535. /* Shar output is never padded. */
  536. archive_write_set_bytes_in_last_block(&a->archive, 1);
  537. /*
  538. * TODO: shar should also suppress padding of
  539. * uncompressed data within gzip/bzip2 streams.
  540. */
  541. return (ARCHIVE_OK);
  542. }
  543. static int
  544. archive_write_shar_destroy(struct archive_write *a)
  545. {
  546. struct shar *shar;
  547. shar = (struct shar *)a->format_data;
  548. if (shar == NULL)
  549. return (ARCHIVE_OK);
  550. archive_entry_free(shar->entry);
  551. free(shar->last_dir);
  552. archive_string_free(&(shar->work));
  553. archive_string_free(&(shar->quoted_name));
  554. free(shar);
  555. a->format_data = NULL;
  556. return (ARCHIVE_OK);
  557. }