1
0

append.c 6.0 KB

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