archive-tar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2005, 2006 Rene Scharfe
  3. */
  4. #include "cache.h"
  5. #include "config.h"
  6. #include "tar.h"
  7. #include "archive.h"
  8. #include "object-store.h"
  9. #include "streaming.h"
  10. #include "run-command.h"
  11. #define RECORDSIZE (512)
  12. #define BLOCKSIZE (RECORDSIZE * 20)
  13. static char block[BLOCKSIZE];
  14. static unsigned long offset;
  15. static int tar_umask = 002;
  16. static int write_tar_filter_archive(const struct archiver *ar,
  17. struct archiver_args *args);
  18. /*
  19. * This is the max value that a ustar size header can specify, as it is fixed
  20. * at 11 octal digits. POSIX specifies that we switch to extended headers at
  21. * this size.
  22. *
  23. * Likewise for the mtime (which happens to use a buffer of the same size).
  24. */
  25. #if ULONG_MAX == 0xFFFFFFFF
  26. #define USTAR_MAX_SIZE ULONG_MAX
  27. #else
  28. #define USTAR_MAX_SIZE 077777777777UL
  29. #endif
  30. #if TIME_MAX == 0xFFFFFFFF
  31. #define USTAR_MAX_MTIME TIME_MAX
  32. #else
  33. #define USTAR_MAX_MTIME 077777777777ULL
  34. #endif
  35. /* writes out the whole block, but only if it is full */
  36. static void write_if_needed(void)
  37. {
  38. if (offset == BLOCKSIZE) {
  39. write_or_die(1, block, BLOCKSIZE);
  40. offset = 0;
  41. }
  42. }
  43. /*
  44. * queues up writes, so that all our write(2) calls write exactly one
  45. * full block; pads writes to RECORDSIZE
  46. */
  47. static void do_write_blocked(const void *data, unsigned long size)
  48. {
  49. const char *buf = data;
  50. if (offset) {
  51. unsigned long chunk = BLOCKSIZE - offset;
  52. if (size < chunk)
  53. chunk = size;
  54. memcpy(block + offset, buf, chunk);
  55. size -= chunk;
  56. offset += chunk;
  57. buf += chunk;
  58. write_if_needed();
  59. }
  60. while (size >= BLOCKSIZE) {
  61. write_or_die(1, buf, BLOCKSIZE);
  62. size -= BLOCKSIZE;
  63. buf += BLOCKSIZE;
  64. }
  65. if (size) {
  66. memcpy(block + offset, buf, size);
  67. offset += size;
  68. }
  69. }
  70. static void finish_record(void)
  71. {
  72. unsigned long tail;
  73. tail = offset % RECORDSIZE;
  74. if (tail) {
  75. memset(block + offset, 0, RECORDSIZE - tail);
  76. offset += RECORDSIZE - tail;
  77. }
  78. write_if_needed();
  79. }
  80. static void write_blocked(const void *data, unsigned long size)
  81. {
  82. do_write_blocked(data, size);
  83. finish_record();
  84. }
  85. /*
  86. * The end of tar archives is marked by 2*512 nul bytes and after that
  87. * follows the rest of the block (if any).
  88. */
  89. static void write_trailer(void)
  90. {
  91. int tail = BLOCKSIZE - offset;
  92. memset(block + offset, 0, tail);
  93. write_or_die(1, block, BLOCKSIZE);
  94. if (tail < 2 * RECORDSIZE) {
  95. memset(block, 0, offset);
  96. write_or_die(1, block, BLOCKSIZE);
  97. }
  98. }
  99. /*
  100. * queues up writes, so that all our write(2) calls write exactly one
  101. * full block; pads writes to RECORDSIZE
  102. */
  103. static int stream_blocked(struct repository *r, const struct object_id *oid)
  104. {
  105. struct git_istream *st;
  106. enum object_type type;
  107. unsigned long sz;
  108. char buf[BLOCKSIZE];
  109. ssize_t readlen;
  110. st = open_istream(r, oid, &type, &sz, NULL);
  111. if (!st)
  112. return error(_("cannot stream blob %s"), oid_to_hex(oid));
  113. for (;;) {
  114. readlen = read_istream(st, buf, sizeof(buf));
  115. if (readlen <= 0)
  116. break;
  117. do_write_blocked(buf, readlen);
  118. }
  119. close_istream(st);
  120. if (!readlen)
  121. finish_record();
  122. return readlen;
  123. }
  124. /*
  125. * pax extended header records have the format "%u %s=%s\n". %u contains
  126. * the size of the whole string (including the %u), the first %s is the
  127. * keyword, the second one is the value. This function constructs such a
  128. * string and appends it to a struct strbuf.
  129. */
  130. static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
  131. const char *value, size_t valuelen)
  132. {
  133. size_t orig_len = sb->len;
  134. size_t len, tmp;
  135. /* "%u %s=%s\n" */
  136. len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
  137. for (tmp = 1; len / 10 >= tmp; tmp *= 10)
  138. len++;
  139. strbuf_grow(sb, len);
  140. strbuf_addf(sb, "%"PRIuMAX" %s=", (uintmax_t)len, keyword);
  141. strbuf_add(sb, value, valuelen);
  142. strbuf_addch(sb, '\n');
  143. if (len != sb->len - orig_len)
  144. BUG("pax extended header length miscalculated as %"PRIuMAX
  145. ", should be %"PRIuMAX,
  146. (uintmax_t)len, (uintmax_t)(sb->len - orig_len));
  147. }
  148. /*
  149. * Like strbuf_append_ext_header, but for numeric values.
  150. */
  151. static void strbuf_append_ext_header_uint(struct strbuf *sb,
  152. const char *keyword,
  153. uintmax_t value)
  154. {
  155. char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
  156. int len;
  157. len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
  158. strbuf_append_ext_header(sb, keyword, buf, len);
  159. }
  160. static unsigned int ustar_header_chksum(const struct ustar_header *header)
  161. {
  162. const unsigned char *p = (const unsigned char *)header;
  163. unsigned int chksum = 0;
  164. while (p < (const unsigned char *)header->chksum)
  165. chksum += *p++;
  166. chksum += sizeof(header->chksum) * ' ';
  167. p += sizeof(header->chksum);
  168. while (p < (const unsigned char *)header + sizeof(struct ustar_header))
  169. chksum += *p++;
  170. return chksum;
  171. }
  172. static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
  173. {
  174. size_t i = pathlen;
  175. if (i > 1 && path[i - 1] == '/')
  176. i--;
  177. if (i > maxlen)
  178. i = maxlen;
  179. do {
  180. i--;
  181. } while (i > 0 && path[i] != '/');
  182. return i;
  183. }
  184. static void prepare_header(struct archiver_args *args,
  185. struct ustar_header *header,
  186. unsigned int mode, unsigned long size)
  187. {
  188. xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
  189. xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
  190. xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
  191. xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
  192. xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
  193. strlcpy(header->uname, "root", sizeof(header->uname));
  194. strlcpy(header->gname, "root", sizeof(header->gname));
  195. xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
  196. xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
  197. memcpy(header->magic, "ustar", 6);
  198. memcpy(header->version, "00", 2);
  199. xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
  200. }
  201. static void write_extended_header(struct archiver_args *args,
  202. const struct object_id *oid,
  203. const void *buffer, unsigned long size)
  204. {
  205. struct ustar_header header;
  206. unsigned int mode;
  207. memset(&header, 0, sizeof(header));
  208. *header.typeflag = TYPEFLAG_EXT_HEADER;
  209. mode = 0100666;
  210. xsnprintf(header.name, sizeof(header.name), "%s.paxheader", oid_to_hex(oid));
  211. prepare_header(args, &header, mode, size);
  212. write_blocked(&header, sizeof(header));
  213. write_blocked(buffer, size);
  214. }
  215. static int write_tar_entry(struct archiver_args *args,
  216. const struct object_id *oid,
  217. const char *path, size_t pathlen,
  218. unsigned int mode,
  219. void *buffer, unsigned long size)
  220. {
  221. struct ustar_header header;
  222. struct strbuf ext_header = STRBUF_INIT;
  223. unsigned long size_in_header;
  224. int err = 0;
  225. memset(&header, 0, sizeof(header));
  226. if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
  227. *header.typeflag = TYPEFLAG_DIR;
  228. mode = (mode | 0777) & ~tar_umask;
  229. } else if (S_ISLNK(mode)) {
  230. *header.typeflag = TYPEFLAG_LNK;
  231. mode |= 0777;
  232. } else if (S_ISREG(mode)) {
  233. *header.typeflag = TYPEFLAG_REG;
  234. mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
  235. } else {
  236. return error(_("unsupported file mode: 0%o (SHA1: %s)"),
  237. mode, oid_to_hex(oid));
  238. }
  239. if (pathlen > sizeof(header.name)) {
  240. size_t plen = get_path_prefix(path, pathlen,
  241. sizeof(header.prefix));
  242. size_t rest = pathlen - plen - 1;
  243. if (plen > 0 && rest <= sizeof(header.name)) {
  244. memcpy(header.prefix, path, plen);
  245. memcpy(header.name, path + plen + 1, rest);
  246. } else {
  247. xsnprintf(header.name, sizeof(header.name), "%s.data",
  248. oid_to_hex(oid));
  249. strbuf_append_ext_header(&ext_header, "path",
  250. path, pathlen);
  251. }
  252. } else
  253. memcpy(header.name, path, pathlen);
  254. if (S_ISLNK(mode)) {
  255. if (size > sizeof(header.linkname)) {
  256. xsnprintf(header.linkname, sizeof(header.linkname),
  257. "see %s.paxheader", oid_to_hex(oid));
  258. strbuf_append_ext_header(&ext_header, "linkpath",
  259. buffer, size);
  260. } else
  261. memcpy(header.linkname, buffer, size);
  262. }
  263. size_in_header = size;
  264. if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
  265. size_in_header = 0;
  266. strbuf_append_ext_header_uint(&ext_header, "size", size);
  267. }
  268. prepare_header(args, &header, mode, size_in_header);
  269. if (ext_header.len > 0) {
  270. write_extended_header(args, oid, ext_header.buf,
  271. ext_header.len);
  272. }
  273. strbuf_release(&ext_header);
  274. write_blocked(&header, sizeof(header));
  275. if (S_ISREG(mode) && size > 0) {
  276. if (buffer)
  277. write_blocked(buffer, size);
  278. else
  279. err = stream_blocked(args->repo, oid);
  280. }
  281. return err;
  282. }
  283. static void write_global_extended_header(struct archiver_args *args)
  284. {
  285. const struct object_id *oid = args->commit_oid;
  286. struct strbuf ext_header = STRBUF_INIT;
  287. struct ustar_header header;
  288. unsigned int mode;
  289. if (oid)
  290. strbuf_append_ext_header(&ext_header, "comment",
  291. oid_to_hex(oid),
  292. the_hash_algo->hexsz);
  293. if (args->time > USTAR_MAX_MTIME) {
  294. strbuf_append_ext_header_uint(&ext_header, "mtime",
  295. args->time);
  296. args->time = USTAR_MAX_MTIME;
  297. }
  298. if (!ext_header.len)
  299. return;
  300. memset(&header, 0, sizeof(header));
  301. *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
  302. mode = 0100666;
  303. xsnprintf(header.name, sizeof(header.name), "pax_global_header");
  304. prepare_header(args, &header, mode, ext_header.len);
  305. write_blocked(&header, sizeof(header));
  306. write_blocked(ext_header.buf, ext_header.len);
  307. strbuf_release(&ext_header);
  308. }
  309. static struct archiver **tar_filters;
  310. static int nr_tar_filters;
  311. static int alloc_tar_filters;
  312. static struct archiver *find_tar_filter(const char *name, size_t len)
  313. {
  314. int i;
  315. for (i = 0; i < nr_tar_filters; i++) {
  316. struct archiver *ar = tar_filters[i];
  317. if (!strncmp(ar->name, name, len) && !ar->name[len])
  318. return ar;
  319. }
  320. return NULL;
  321. }
  322. static int tar_filter_config(const char *var, const char *value, void *data)
  323. {
  324. struct archiver *ar;
  325. const char *name;
  326. const char *type;
  327. size_t namelen;
  328. if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
  329. return 0;
  330. ar = find_tar_filter(name, namelen);
  331. if (!ar) {
  332. ar = xcalloc(1, sizeof(*ar));
  333. ar->name = xmemdupz(name, namelen);
  334. ar->write_archive = write_tar_filter_archive;
  335. ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
  336. ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
  337. tar_filters[nr_tar_filters++] = ar;
  338. }
  339. if (!strcmp(type, "command")) {
  340. if (!value)
  341. return config_error_nonbool(var);
  342. free(ar->data);
  343. ar->data = xstrdup(value);
  344. return 0;
  345. }
  346. if (!strcmp(type, "remote")) {
  347. if (git_config_bool(var, value))
  348. ar->flags |= ARCHIVER_REMOTE;
  349. else
  350. ar->flags &= ~ARCHIVER_REMOTE;
  351. return 0;
  352. }
  353. return 0;
  354. }
  355. static int git_tar_config(const char *var, const char *value, void *cb)
  356. {
  357. if (!strcmp(var, "tar.umask")) {
  358. if (value && !strcmp(value, "user")) {
  359. tar_umask = umask(0);
  360. umask(tar_umask);
  361. } else {
  362. tar_umask = git_config_int(var, value);
  363. }
  364. return 0;
  365. }
  366. return tar_filter_config(var, value, cb);
  367. }
  368. static int write_tar_archive(const struct archiver *ar,
  369. struct archiver_args *args)
  370. {
  371. int err = 0;
  372. write_global_extended_header(args);
  373. err = write_archive_entries(args, write_tar_entry);
  374. if (!err)
  375. write_trailer();
  376. return err;
  377. }
  378. static int write_tar_filter_archive(const struct archiver *ar,
  379. struct archiver_args *args)
  380. {
  381. struct strbuf cmd = STRBUF_INIT;
  382. struct child_process filter = CHILD_PROCESS_INIT;
  383. const char *argv[2];
  384. int r;
  385. if (!ar->data)
  386. BUG("tar-filter archiver called with no filter defined");
  387. strbuf_addstr(&cmd, ar->data);
  388. if (args->compression_level >= 0)
  389. strbuf_addf(&cmd, " -%d", args->compression_level);
  390. argv[0] = cmd.buf;
  391. argv[1] = NULL;
  392. filter.argv = argv;
  393. filter.use_shell = 1;
  394. filter.in = -1;
  395. if (start_command(&filter) < 0)
  396. die_errno(_("unable to start '%s' filter"), argv[0]);
  397. close(1);
  398. if (dup2(filter.in, 1) < 0)
  399. die_errno(_("unable to redirect descriptor"));
  400. close(filter.in);
  401. r = write_tar_archive(ar, args);
  402. close(1);
  403. if (finish_command(&filter) != 0)
  404. die(_("'%s' filter reported error"), argv[0]);
  405. strbuf_release(&cmd);
  406. return r;
  407. }
  408. static struct archiver tar_archiver = {
  409. "tar",
  410. write_tar_archive,
  411. ARCHIVER_REMOTE
  412. };
  413. void init_tar_archiver(void)
  414. {
  415. int i;
  416. register_archiver(&tar_archiver);
  417. tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
  418. tar_filter_config("tar.tgz.remote", "true", NULL);
  419. tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
  420. tar_filter_config("tar.tar.gz.remote", "true", NULL);
  421. git_config(git_tar_config, NULL);
  422. for (i = 0; i < nr_tar_filters; i++) {
  423. /* omit any filters that never had a command configured */
  424. if (tar_filters[i]->data)
  425. register_archiver(tar_filters[i]);
  426. }
  427. }