THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

571 lines
13KB

  1. #include "cache.h"
  2. #include "pkt-line.h"
  3. #include "run-command.h"
  4. char packet_buffer[LARGE_PACKET_MAX];
  5. static const char *packet_trace_prefix = "git";
  6. static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
  7. static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
  8. void packet_trace_identity(const char *prog)
  9. {
  10. packet_trace_prefix = xstrdup(prog);
  11. }
  12. static const char *get_trace_prefix(void)
  13. {
  14. return in_async() ? "sideband" : packet_trace_prefix;
  15. }
  16. static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
  17. {
  18. if (!sideband) {
  19. trace_verbatim(&trace_pack, buf, len);
  20. return 1;
  21. } else if (len && *buf == '\1') {
  22. trace_verbatim(&trace_pack, buf + 1, len - 1);
  23. return 1;
  24. } else {
  25. /* it's another non-pack sideband */
  26. return 0;
  27. }
  28. }
  29. static void packet_trace(const char *buf, unsigned int len, int write)
  30. {
  31. int i;
  32. struct strbuf out;
  33. static int in_pack, sideband;
  34. if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
  35. return;
  36. if (in_pack) {
  37. if (packet_trace_pack(buf, len, sideband))
  38. return;
  39. } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
  40. in_pack = 1;
  41. sideband = *buf == '\1';
  42. packet_trace_pack(buf, len, sideband);
  43. /*
  44. * Make a note in the human-readable trace that the pack data
  45. * started.
  46. */
  47. buf = "PACK ...";
  48. len = strlen(buf);
  49. }
  50. if (!trace_want(&trace_packet))
  51. return;
  52. /* +32 is just a guess for header + quoting */
  53. strbuf_init(&out, len+32);
  54. strbuf_addf(&out, "packet: %12s%c ",
  55. get_trace_prefix(), write ? '>' : '<');
  56. /* XXX we should really handle printable utf8 */
  57. for (i = 0; i < len; i++) {
  58. /* suppress newlines */
  59. if (buf[i] == '\n')
  60. continue;
  61. if (buf[i] >= 0x20 && buf[i] <= 0x7e)
  62. strbuf_addch(&out, buf[i]);
  63. else
  64. strbuf_addf(&out, "\\%o", buf[i]);
  65. }
  66. strbuf_addch(&out, '\n');
  67. trace_strbuf(&trace_packet, &out);
  68. strbuf_release(&out);
  69. }
  70. /*
  71. * If we buffered things up above (we don't, but we should),
  72. * we'd flush it here
  73. */
  74. void packet_flush(int fd)
  75. {
  76. packet_trace("0000", 4, 1);
  77. if (write_in_full(fd, "0000", 4) < 0)
  78. die_errno(_("unable to write flush packet"));
  79. }
  80. void packet_delim(int fd)
  81. {
  82. packet_trace("0001", 4, 1);
  83. if (write_in_full(fd, "0001", 4) < 0)
  84. die_errno(_("unable to write delim packet"));
  85. }
  86. int packet_flush_gently(int fd)
  87. {
  88. packet_trace("0000", 4, 1);
  89. if (write_in_full(fd, "0000", 4) < 0)
  90. return error(_("flush packet write failed"));
  91. return 0;
  92. }
  93. void packet_buf_flush(struct strbuf *buf)
  94. {
  95. packet_trace("0000", 4, 1);
  96. strbuf_add(buf, "0000", 4);
  97. }
  98. void packet_buf_delim(struct strbuf *buf)
  99. {
  100. packet_trace("0001", 4, 1);
  101. strbuf_add(buf, "0001", 4);
  102. }
  103. void set_packet_header(char *buf, int size)
  104. {
  105. static char hexchar[] = "0123456789abcdef";
  106. #define hex(a) (hexchar[(a) & 15])
  107. buf[0] = hex(size >> 12);
  108. buf[1] = hex(size >> 8);
  109. buf[2] = hex(size >> 4);
  110. buf[3] = hex(size);
  111. #undef hex
  112. }
  113. static void format_packet(struct strbuf *out, const char *prefix,
  114. const char *fmt, va_list args)
  115. {
  116. size_t orig_len, n;
  117. orig_len = out->len;
  118. strbuf_addstr(out, "0000");
  119. strbuf_addstr(out, prefix);
  120. strbuf_vaddf(out, fmt, args);
  121. n = out->len - orig_len;
  122. if (n > LARGE_PACKET_MAX)
  123. die(_("protocol error: impossibly long line"));
  124. set_packet_header(&out->buf[orig_len], n);
  125. packet_trace(out->buf + orig_len + 4, n - 4, 1);
  126. }
  127. static int packet_write_fmt_1(int fd, int gently, const char *prefix,
  128. const char *fmt, va_list args)
  129. {
  130. static struct strbuf buf = STRBUF_INIT;
  131. strbuf_reset(&buf);
  132. format_packet(&buf, prefix, fmt, args);
  133. if (write_in_full(fd, buf.buf, buf.len) < 0) {
  134. if (!gently) {
  135. check_pipe(errno);
  136. die_errno(_("packet write with format failed"));
  137. }
  138. return error(_("packet write with format failed"));
  139. }
  140. return 0;
  141. }
  142. void packet_write_fmt(int fd, const char *fmt, ...)
  143. {
  144. va_list args;
  145. va_start(args, fmt);
  146. packet_write_fmt_1(fd, 0, "", fmt, args);
  147. va_end(args);
  148. }
  149. int packet_write_fmt_gently(int fd, const char *fmt, ...)
  150. {
  151. int status;
  152. va_list args;
  153. va_start(args, fmt);
  154. status = packet_write_fmt_1(fd, 1, "", fmt, args);
  155. va_end(args);
  156. return status;
  157. }
  158. static int packet_write_gently(const int fd_out, const char *buf, size_t size)
  159. {
  160. static char packet_write_buffer[LARGE_PACKET_MAX];
  161. size_t packet_size;
  162. if (size > sizeof(packet_write_buffer) - 4)
  163. return error(_("packet write failed - data exceeds max packet size"));
  164. packet_trace(buf, size, 1);
  165. packet_size = size + 4;
  166. set_packet_header(packet_write_buffer, packet_size);
  167. memcpy(packet_write_buffer + 4, buf, size);
  168. if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
  169. return error(_("packet write failed"));
  170. return 0;
  171. }
  172. void packet_write(int fd_out, const char *buf, size_t size)
  173. {
  174. if (packet_write_gently(fd_out, buf, size))
  175. die_errno(_("packet write failed"));
  176. }
  177. void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
  178. {
  179. va_list args;
  180. va_start(args, fmt);
  181. format_packet(buf, "", fmt, args);
  182. va_end(args);
  183. }
  184. void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
  185. {
  186. size_t orig_len, n;
  187. orig_len = buf->len;
  188. strbuf_addstr(buf, "0000");
  189. strbuf_add(buf, data, len);
  190. n = buf->len - orig_len;
  191. if (n > LARGE_PACKET_MAX)
  192. die(_("protocol error: impossibly long line"));
  193. set_packet_header(&buf->buf[orig_len], n);
  194. packet_trace(data, len, 1);
  195. }
  196. int write_packetized_from_fd(int fd_in, int fd_out)
  197. {
  198. static char buf[LARGE_PACKET_DATA_MAX];
  199. int err = 0;
  200. ssize_t bytes_to_write;
  201. while (!err) {
  202. bytes_to_write = xread(fd_in, buf, sizeof(buf));
  203. if (bytes_to_write < 0)
  204. return COPY_READ_ERROR;
  205. if (bytes_to_write == 0)
  206. break;
  207. err = packet_write_gently(fd_out, buf, bytes_to_write);
  208. }
  209. if (!err)
  210. err = packet_flush_gently(fd_out);
  211. return err;
  212. }
  213. int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
  214. {
  215. int err = 0;
  216. size_t bytes_written = 0;
  217. size_t bytes_to_write;
  218. while (!err) {
  219. if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
  220. bytes_to_write = LARGE_PACKET_DATA_MAX;
  221. else
  222. bytes_to_write = len - bytes_written;
  223. if (bytes_to_write == 0)
  224. break;
  225. err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
  226. bytes_written += bytes_to_write;
  227. }
  228. if (!err)
  229. err = packet_flush_gently(fd_out);
  230. return err;
  231. }
  232. static int get_packet_data(int fd, char **src_buf, size_t *src_size,
  233. void *dst, unsigned size, int options)
  234. {
  235. ssize_t ret;
  236. if (fd >= 0 && src_buf && *src_buf)
  237. BUG("multiple sources given to packet_read");
  238. /* Read up to "size" bytes from our source, whatever it is. */
  239. if (src_buf && *src_buf) {
  240. ret = size < *src_size ? size : *src_size;
  241. memcpy(dst, *src_buf, ret);
  242. *src_buf += ret;
  243. *src_size -= ret;
  244. } else {
  245. ret = read_in_full(fd, dst, size);
  246. if (ret < 0)
  247. die_errno(_("read error"));
  248. }
  249. /* And complain if we didn't get enough bytes to satisfy the read. */
  250. if (ret != size) {
  251. if (options & PACKET_READ_GENTLE_ON_EOF)
  252. return -1;
  253. die(_("the remote end hung up unexpectedly"));
  254. }
  255. return ret;
  256. }
  257. static int packet_length(const char *linelen)
  258. {
  259. int val = hex2chr(linelen);
  260. return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
  261. }
  262. enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
  263. size_t *src_len, char *buffer,
  264. unsigned size, int *pktlen,
  265. int options)
  266. {
  267. int len;
  268. char linelen[4];
  269. if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
  270. *pktlen = -1;
  271. return PACKET_READ_EOF;
  272. }
  273. len = packet_length(linelen);
  274. if (len < 0) {
  275. die(_("protocol error: bad line length character: %.4s"), linelen);
  276. } else if (!len) {
  277. packet_trace("0000", 4, 0);
  278. *pktlen = 0;
  279. return PACKET_READ_FLUSH;
  280. } else if (len == 1) {
  281. packet_trace("0001", 4, 0);
  282. *pktlen = 0;
  283. return PACKET_READ_DELIM;
  284. } else if (len < 4) {
  285. die(_("protocol error: bad line length %d"), len);
  286. }
  287. len -= 4;
  288. if ((unsigned)len >= size)
  289. die(_("protocol error: bad line length %d"), len);
  290. if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
  291. *pktlen = -1;
  292. return PACKET_READ_EOF;
  293. }
  294. if ((options & PACKET_READ_CHOMP_NEWLINE) &&
  295. len && buffer[len-1] == '\n')
  296. len--;
  297. buffer[len] = 0;
  298. packet_trace(buffer, len, 0);
  299. if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
  300. starts_with(buffer, "ERR "))
  301. die(_("remote error: %s"), buffer + 4);
  302. *pktlen = len;
  303. return PACKET_READ_NORMAL;
  304. }
  305. int packet_read(int fd, char **src_buffer, size_t *src_len,
  306. char *buffer, unsigned size, int options)
  307. {
  308. int pktlen = -1;
  309. packet_read_with_status(fd, src_buffer, src_len, buffer, size,
  310. &pktlen, options);
  311. return pktlen;
  312. }
  313. static char *packet_read_line_generic(int fd,
  314. char **src, size_t *src_len,
  315. int *dst_len)
  316. {
  317. int len = packet_read(fd, src, src_len,
  318. packet_buffer, sizeof(packet_buffer),
  319. PACKET_READ_CHOMP_NEWLINE);
  320. if (dst_len)
  321. *dst_len = len;
  322. return (len > 0) ? packet_buffer : NULL;
  323. }
  324. char *packet_read_line(int fd, int *len_p)
  325. {
  326. return packet_read_line_generic(fd, NULL, NULL, len_p);
  327. }
  328. int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
  329. {
  330. int len = packet_read(fd, NULL, NULL,
  331. packet_buffer, sizeof(packet_buffer),
  332. PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
  333. if (dst_len)
  334. *dst_len = len;
  335. if (dst_line)
  336. *dst_line = (len > 0) ? packet_buffer : NULL;
  337. return len;
  338. }
  339. char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
  340. {
  341. return packet_read_line_generic(-1, src, src_len, dst_len);
  342. }
  343. ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
  344. {
  345. int packet_len;
  346. size_t orig_len = sb_out->len;
  347. size_t orig_alloc = sb_out->alloc;
  348. for (;;) {
  349. strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
  350. packet_len = packet_read(fd_in, NULL, NULL,
  351. /* strbuf_grow() above always allocates one extra byte to
  352. * store a '\0' at the end of the string. packet_read()
  353. * writes a '\0' extra byte at the end, too. Let it know
  354. * that there is already room for the extra byte.
  355. */
  356. sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
  357. PACKET_READ_GENTLE_ON_EOF);
  358. if (packet_len <= 0)
  359. break;
  360. sb_out->len += packet_len;
  361. }
  362. if (packet_len < 0) {
  363. if (orig_alloc == 0)
  364. strbuf_release(sb_out);
  365. else
  366. strbuf_setlen(sb_out, orig_len);
  367. return packet_len;
  368. }
  369. return sb_out->len - orig_len;
  370. }
  371. int recv_sideband(const char *me, int in_stream, int out)
  372. {
  373. char buf[LARGE_PACKET_MAX + 1];
  374. int len;
  375. struct strbuf scratch = STRBUF_INIT;
  376. enum sideband_type sideband_type;
  377. while (1) {
  378. len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX,
  379. 0);
  380. if (!demultiplex_sideband(me, buf, len, 0, &scratch,
  381. &sideband_type))
  382. continue;
  383. switch (sideband_type) {
  384. case SIDEBAND_PRIMARY:
  385. write_or_die(out, buf + 1, len - 1);
  386. break;
  387. default: /* errors: message already written */
  388. return sideband_type;
  389. }
  390. }
  391. }
  392. /* Packet Reader Functions */
  393. void packet_reader_init(struct packet_reader *reader, int fd,
  394. char *src_buffer, size_t src_len,
  395. int options)
  396. {
  397. memset(reader, 0, sizeof(*reader));
  398. reader->fd = fd;
  399. reader->src_buffer = src_buffer;
  400. reader->src_len = src_len;
  401. reader->buffer = packet_buffer;
  402. reader->buffer_size = sizeof(packet_buffer);
  403. reader->options = options;
  404. reader->me = "git";
  405. }
  406. enum packet_read_status packet_reader_read(struct packet_reader *reader)
  407. {
  408. struct strbuf scratch = STRBUF_INIT;
  409. if (reader->line_peeked) {
  410. reader->line_peeked = 0;
  411. return reader->status;
  412. }
  413. /*
  414. * Consume all progress packets until a primary payload packet is
  415. * received
  416. */
  417. while (1) {
  418. enum sideband_type sideband_type;
  419. reader->status = packet_read_with_status(reader->fd,
  420. &reader->src_buffer,
  421. &reader->src_len,
  422. reader->buffer,
  423. reader->buffer_size,
  424. &reader->pktlen,
  425. reader->options);
  426. if (!reader->use_sideband)
  427. break;
  428. if (demultiplex_sideband(reader->me, reader->buffer,
  429. reader->pktlen, 1, &scratch,
  430. &sideband_type))
  431. break;
  432. }
  433. if (reader->status == PACKET_READ_NORMAL)
  434. /* Skip the sideband designator if sideband is used */
  435. reader->line = reader->use_sideband ?
  436. reader->buffer + 1 : reader->buffer;
  437. else
  438. reader->line = NULL;
  439. return reader->status;
  440. }
  441. enum packet_read_status packet_reader_peek(struct packet_reader *reader)
  442. {
  443. /* Only allow peeking a single line */
  444. if (reader->line_peeked)
  445. return reader->status;
  446. /* Peek a line by reading it and setting peeked flag */
  447. packet_reader_read(reader);
  448. reader->line_peeked = 1;
  449. return reader->status;
  450. }
  451. void packet_writer_init(struct packet_writer *writer, int dest_fd)
  452. {
  453. writer->dest_fd = dest_fd;
  454. writer->use_sideband = 0;
  455. }
  456. void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
  457. {
  458. va_list args;
  459. va_start(args, fmt);
  460. packet_write_fmt_1(writer->dest_fd, 0,
  461. writer->use_sideband ? "\001" : "", fmt, args);
  462. va_end(args);
  463. }
  464. void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
  465. {
  466. va_list args;
  467. va_start(args, fmt);
  468. packet_write_fmt_1(writer->dest_fd, 0,
  469. writer->use_sideband ? "\003" : "ERR ", fmt, args);
  470. va_end(args);
  471. }
  472. void packet_writer_delim(struct packet_writer *writer)
  473. {
  474. packet_delim(writer->dest_fd);
  475. }
  476. void packet_writer_flush(struct packet_writer *writer)
  477. {
  478. packet_flush(writer->dest_fd);
  479. }