append.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #ifdef _MSC_VER
  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[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. int i;
  58. libtar_hashptr_t hp;
  59. tar_dev_t *td = NULL;
  60. tar_ino_t *ti = NULL;
  61. char path[MAXPATHLEN];
  62. #ifdef DEBUG
  63. printf("==> tar_append_file(TAR=0x%lx (\"%s\"), realname=\"%s\", "
  64. "savename=\"%s\")\n", t, t->pathname, realname,
  65. (savename ? savename : "[NULL]"));
  66. #endif
  67. #ifdef WIN32
  68. if (stat(realname, &s) != 0)
  69. #else
  70. if (lstat(realname, &s) != 0)
  71. #endif
  72. {
  73. #ifdef DEBUG
  74. perror("lstat()");
  75. #endif
  76. return -1;
  77. }
  78. /* set header block */
  79. #ifdef DEBUG
  80. puts(" tar_append_file(): setting header block...");
  81. #endif
  82. memset(&(t->th_buf), 0, sizeof(struct tar_header));
  83. th_set_from_stat(t, &s);
  84. /* set the header path */
  85. #ifdef DEBUG
  86. puts(" tar_append_file(): setting header path...");
  87. #endif
  88. th_set_path(t, (savename ? savename : realname));
  89. /* check if it's a hardlink */
  90. #ifdef DEBUG
  91. puts(" tar_append_file(): checking inode cache for hardlink...");
  92. #endif
  93. libtar_hashptr_reset(&hp);
  94. if (libtar_hash_getkey(t->h, &hp, &(s.st_dev),
  95. (libtar_matchfunc_t)dev_match) != 0)
  96. td = (tar_dev_t *)libtar_hashptr_data(&hp);
  97. else
  98. {
  99. #ifdef DEBUG
  100. printf("+++ adding hash for device (0x%lx, 0x%lx)...\n",
  101. major(s.st_dev), minor(s.st_dev));
  102. #endif
  103. td = (tar_dev_t *)calloc(1, sizeof(tar_dev_t));
  104. td->td_dev = s.st_dev;
  105. td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash);
  106. if (td->td_h == NULL)
  107. return -1;
  108. if (libtar_hash_add(t->h, td) == -1)
  109. return -1;
  110. }
  111. libtar_hashptr_reset(&hp);
  112. if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino),
  113. (libtar_matchfunc_t)ino_match) != 0)
  114. {
  115. ti = (tar_ino_t *)libtar_hashptr_data(&hp);
  116. #ifdef DEBUG
  117. printf(" tar_append_file(): encoding hard link \"%s\" "
  118. "to \"%s\"...\n", realname, ti->ti_name);
  119. #endif
  120. t->th_buf.typeflag = LNKTYPE;
  121. th_set_link(t, ti->ti_name);
  122. }
  123. else
  124. {
  125. #ifdef DEBUG
  126. printf("+++ adding entry: device (0x%lx,0x%lx), inode %ld "
  127. "(\"%s\")...\n", major(s.st_dev), minor(s.st_dev),
  128. s.st_ino, realname);
  129. #endif
  130. ti = (tar_ino_t *)calloc(1, sizeof(tar_ino_t));
  131. if (ti == NULL)
  132. return -1;
  133. ti->ti_ino = s.st_ino;
  134. snprintf(ti->ti_name, sizeof(ti->ti_name), "%s",
  135. savename ? savename : realname);
  136. libtar_hash_add(td->td_h, ti);
  137. }
  138. #ifndef WIN32
  139. /* check if it's a symlink */
  140. if (TH_ISSYM(t))
  141. {
  142. #ifdef WIN32
  143. i = -1;
  144. #else
  145. i = readlink(realname, path, sizeof(path));
  146. #endif
  147. if (i == -1)
  148. return -1;
  149. if (i >= MAXPATHLEN)
  150. i = MAXPATHLEN - 1;
  151. path[i] = '\0';
  152. #ifdef DEBUG
  153. printf(" tar_append_file(): encoding symlink \"%s\" -> "
  154. "\"%s\"...\n", realname, path);
  155. #endif
  156. th_set_link(t, path);
  157. }
  158. #endif
  159. /* print file info */
  160. if (t->options & TAR_VERBOSE)
  161. th_print_long_ls(t);
  162. #ifdef DEBUG
  163. puts(" tar_append_file(): writing header");
  164. #endif
  165. /* write header */
  166. if (th_write(t) != 0)
  167. {
  168. #ifdef DEBUG
  169. printf("t->fd = %d\n", t->fd);
  170. #endif
  171. return -1;
  172. }
  173. #ifdef DEBUG
  174. puts(" tar_append_file(): back from th_write()");
  175. #endif
  176. /* if it's a regular file, write the contents as well */
  177. if (TH_ISREG(t) && tar_append_regfile(t, realname) != 0)
  178. return -1;
  179. return 0;
  180. }
  181. /* write EOF indicator */
  182. int
  183. tar_append_eof(TAR *t)
  184. {
  185. int i, j;
  186. char block[T_BLOCKSIZE];
  187. memset(&block, 0, T_BLOCKSIZE);
  188. for (j = 0; j < 2; j++)
  189. {
  190. i = tar_block_write(t, &block);
  191. if (i != T_BLOCKSIZE)
  192. {
  193. if (i != -1)
  194. errno = EINVAL;
  195. return -1;
  196. }
  197. }
  198. return 0;
  199. }
  200. /* add file contents to a tarchive */
  201. int
  202. tar_append_regfile(TAR *t, char *realname)
  203. {
  204. char block[T_BLOCKSIZE];
  205. int filefd;
  206. int i, j;
  207. size_t size;
  208. #ifdef _WIN32
  209. filefd = open(realname, O_RDONLY | O_BINARY);
  210. #else
  211. filefd = open(realname, O_RDONLY);
  212. #endif
  213. if (filefd == -1)
  214. {
  215. #ifdef DEBUG
  216. perror("open()");
  217. #endif
  218. return -1;
  219. }
  220. size = th_get_size(t);
  221. for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
  222. {
  223. j = read(filefd, &block, T_BLOCKSIZE);
  224. if (j != T_BLOCKSIZE)
  225. {
  226. if (j != -1)
  227. errno = EINVAL;
  228. return -1;
  229. }
  230. if (tar_block_write(t, &block) == -1)
  231. return -1;
  232. }
  233. if (i > 0)
  234. {
  235. j = read(filefd, &block, i);
  236. if (j == -1)
  237. return -1;
  238. memset(&(block[i]), 0, T_BLOCKSIZE - i);
  239. if (tar_block_write(t, &block) == -1)
  240. return -1;
  241. }
  242. close(filefd);
  243. return 0;
  244. }