1
0

append.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** append.c - libtar code to append files to a tar archive
  7. **
  8. ** Mark D. Roth <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #include <libtarint/internal.h>
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #if defined(_WIN32) && !defined(__CYGWIN__)
  17. # include <libtar/compat.h>
  18. #else
  19. # include <sys/param.h>
  20. #endif
  21. #include <libtar/compat.h>
  22. #include <sys/types.h>
  23. #ifdef STDC_HEADERS
  24. # include <stdlib.h>
  25. # include <string.h>
  26. #endif
  27. #ifdef HAVE_UNISTD_H
  28. # include <unistd.h>
  29. #endif
  30. #ifdef _MSC_VER
  31. #include <io.h>
  32. #endif
  33. struct tar_dev
  34. {
  35. dev_t td_dev;
  36. libtar_hash_t *td_h;
  37. };
  38. typedef struct tar_dev tar_dev_t;
  39. struct tar_ino
  40. {
  41. ino_t ti_ino;
  42. char ti_name[TAR_MAXPATHLEN];
  43. };
  44. typedef struct tar_ino tar_ino_t;
  45. /* free memory associated with a tar_dev_t */
  46. void
  47. tar_dev_free(tar_dev_t *tdp)
  48. {
  49. libtar_hash_free(tdp->td_h, free);
  50. free(tdp);
  51. }
  52. /* appends a file to the tar archive */
  53. int
  54. tar_append_file(TAR *t, char *realname, char *savename)
  55. {
  56. struct stat s;
  57. libtar_hashptr_t hp;
  58. tar_dev_t *td = NULL;
  59. tar_ino_t *ti = NULL;
  60. #if !defined(_WIN32) || defined(__CYGWIN__)
  61. int i;
  62. char path[TAR_MAXPATHLEN];
  63. #endif
  64. #ifdef DEBUG
  65. printf("==> tar_append_file(TAR=0x%lx (\"%s\"), realname=\"%s\", "
  66. "savename=\"%s\")\n", t, t->pathname, realname,
  67. (savename ? savename : "[NULL]"));
  68. #endif
  69. #ifdef WIN32
  70. if (stat(realname, &s) != 0)
  71. #else
  72. if (lstat(realname, &s) != 0)
  73. #endif
  74. {
  75. #ifdef DEBUG
  76. perror("lstat()");
  77. #endif
  78. return -1;
  79. }
  80. /* set header block */
  81. #ifdef DEBUG
  82. puts(" tar_append_file(): setting header block...");
  83. #endif
  84. memset(&(t->th_buf), 0, sizeof(struct tar_header));
  85. th_set_from_stat(t, &s);
  86. /* set the header path */
  87. #ifdef DEBUG
  88. puts(" tar_append_file(): setting header path...");
  89. #endif
  90. th_set_path(t, (savename ? savename : realname));
  91. /* check if it's a hardlink */
  92. #ifdef DEBUG
  93. puts(" tar_append_file(): checking inode cache for hardlink...");
  94. #endif
  95. libtar_hashptr_reset(&hp);
  96. if (libtar_hash_getkey(t->h, &hp, &(s.st_dev),
  97. (libtar_matchfunc_t)dev_match) != 0)
  98. td = (tar_dev_t *)libtar_hashptr_data(&hp);
  99. else
  100. {
  101. #ifdef DEBUG
  102. printf("+++ adding hash for device (0x%lx, 0x%lx)...\n",
  103. major(s.st_dev), minor(s.st_dev));
  104. #endif
  105. td = (tar_dev_t *)calloc(1, sizeof(tar_dev_t));
  106. td->td_dev = s.st_dev;
  107. td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash);
  108. if (td->td_h == NULL)
  109. return -1;
  110. if (libtar_hash_add(t->h, td) == -1)
  111. return -1;
  112. }
  113. libtar_hashptr_reset(&hp);
  114. if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino),
  115. (libtar_matchfunc_t)ino_match) != 0)
  116. {
  117. ti = (tar_ino_t *)libtar_hashptr_data(&hp);
  118. #ifdef DEBUG
  119. printf(" tar_append_file(): encoding hard link \"%s\" "
  120. "to \"%s\"...\n", realname, ti->ti_name);
  121. #endif
  122. t->th_buf.typeflag = LNKTYPE;
  123. th_set_link(t, ti->ti_name);
  124. }
  125. else
  126. {
  127. #ifdef DEBUG
  128. printf("+++ adding entry: device (0x%lx,0x%lx), inode %ld "
  129. "(\"%s\")...\n", major(s.st_dev), minor(s.st_dev),
  130. s.st_ino, realname);
  131. #endif
  132. ti = (tar_ino_t *)calloc(1, sizeof(tar_ino_t));
  133. if (ti == NULL)
  134. return -1;
  135. ti->ti_ino = s.st_ino;
  136. snprintf(ti->ti_name, sizeof(ti->ti_name), "%s",
  137. savename ? savename : realname);
  138. libtar_hash_add(td->td_h, ti);
  139. }
  140. #if !defined(_WIN32) || defined(__CYGWIN__)
  141. /* check if it's a symlink */
  142. if (TH_ISSYM(t))
  143. {
  144. #ifdef WIN32
  145. i = -1;
  146. #else
  147. i = readlink(realname, path, sizeof(path));
  148. #endif
  149. if (i == -1)
  150. return -1;
  151. if (i >= TAR_MAXPATHLEN)
  152. i = TAR_MAXPATHLEN - 1;
  153. path[i] = '\0';
  154. #ifdef DEBUG
  155. printf(" tar_append_file(): encoding symlink \"%s\" -> "
  156. "\"%s\"...\n", realname, path);
  157. #endif
  158. th_set_link(t, path);
  159. }
  160. #endif
  161. /* print file info */
  162. if (t->options & TAR_VERBOSE)
  163. th_print_long_ls(t);
  164. #ifdef DEBUG
  165. puts(" tar_append_file(): writing header");
  166. #endif
  167. /* write header */
  168. if (th_write(t) != 0)
  169. {
  170. #ifdef DEBUG
  171. printf("t->fd = %d\n", t->fd);
  172. #endif
  173. return -1;
  174. }
  175. #ifdef DEBUG
  176. puts(" tar_append_file(): back from th_write()");
  177. #endif
  178. /* if it's a regular file, write the contents as well */
  179. if (TH_ISREG(t) && tar_append_regfile(t, realname) != 0)
  180. return -1;
  181. return 0;
  182. }
  183. /* write EOF indicator */
  184. int
  185. tar_append_eof(TAR *t)
  186. {
  187. int i, j;
  188. char block[T_BLOCKSIZE];
  189. memset(&block, 0, T_BLOCKSIZE);
  190. for (j = 0; j < 2; j++)
  191. {
  192. i = tar_block_write(t, &block);
  193. if (i != T_BLOCKSIZE)
  194. {
  195. if (i != -1)
  196. errno = EINVAL;
  197. return -1;
  198. }
  199. }
  200. return 0;
  201. }
  202. /* add file contents to a tarchive */
  203. int
  204. tar_append_regfile(TAR *t, char *realname)
  205. {
  206. char block[T_BLOCKSIZE];
  207. int filefd;
  208. int i, j;
  209. size_t size;
  210. #ifdef _WIN32
  211. filefd = open(realname, O_RDONLY | O_BINARY);
  212. #else
  213. filefd = open(realname, O_RDONLY);
  214. #endif
  215. if (filefd == -1)
  216. {
  217. #ifdef DEBUG
  218. perror("open()");
  219. #endif
  220. return -1;
  221. }
  222. size = th_get_size(t);
  223. for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
  224. {
  225. j = read(filefd, &block, T_BLOCKSIZE);
  226. if (j != T_BLOCKSIZE)
  227. {
  228. if (j != -1)
  229. errno = EINVAL;
  230. return -1;
  231. }
  232. if (tar_block_write(t, &block) == -1)
  233. return -1;
  234. }
  235. if (i > 0)
  236. {
  237. j = read(filefd, &block, i);
  238. if (j == -1)
  239. return -1;
  240. memset(&(block[i]), 0, T_BLOCKSIZE - i);
  241. if (tar_block_write(t, &block) == -1)
  242. return -1;
  243. }
  244. close(filefd);
  245. return 0;
  246. }