@matheusbsbs / matheus-bagliano / commits / c7f9cb1428
Fork 0

builtin-apply: use strbuf's instead of buffer_desc's.

Signed-off-by: Pierre Habouzit <madcoder@debian.org> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 16, 2007 at 18:54 UTC c7f9cb14286fff23bfe65033d4ec63f84c77f554
1 file changed +72 -140
builtin-apply.c
+72 -140
@@ -1441,37 +1441,28 @@ static void show_stats(struct patch *patch)
1441 free(qname);
1442 }
1443
1444 -static int read_old_data(struct stat *st, const char *path, char **buf_p, unsigned long *alloc_p, unsigned long *size_p)
1444 +static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1445 {
1446 int fd;
1447 - unsigned long got;
1448 - struct strbuf nbuf;
1449 - unsigned long size = *size_p;
1450 - char *buf = *buf_p;
1447
1448 switch (st->st_mode & S_IFMT) {
1449 case S_IFLNK:
1454 - return readlink(path, buf, size) != size;
1450 + strbuf_grow(buf, st->st_size);
1451 + if (readlink(path, buf->buf, st->st_size) != st->st_size)
1452 + return -1;
1453 + strbuf_setlen(buf, st->st_size);
1454 + return 0;
1455 case S_IFREG:
1456 fd = open(path, O_RDONLY);
1457 if (fd < 0)
1458 return error("unable to open %s", path);
1459 - got = 0;
1460 - for (;;) {
1461 - ssize_t ret = xread(fd, buf + got, size - got);
1462 - if (ret <= 0)
1463 - break;
1464 - got += ret;
1459 + if (strbuf_read(buf, fd, st->st_size) < 0) {
1460 + close(fd);
1461 + return -1;
1462 }
1463 close(fd);
1467 - strbuf_init(&nbuf, 0);
1468 - if (convert_to_git(path, buf, size, &nbuf)) {
1469 - free(buf);
1470 - *buf_p = nbuf.buf;
1471 - *alloc_p = nbuf.alloc;
1472 - *size_p = nbuf.len;
1473 - }
1474 - return got != size;
1464 + convert_to_git(path, buf->buf, buf->len, buf);
1465 + return 0;
1466 default:
1467 return -1;
1468 }
@@ -1576,12 +1567,6 @@ static void remove_last_line(const char **rbuf, int *rsize)
1567 *rsize = offset + 1;
1568 }
1569
1579 -struct buffer_desc {
1580 - char *buffer;
1581 - unsigned long size;
1582 - unsigned long alloc;
1583 -};
1584 -
1570 static int apply_line(char *output, const char *patch, int plen)
1571 {
1572 /* plen is number of bytes to be copied from patch,
@@ -1651,10 +1636,9 @@ static int apply_line(char *output, const char *patch, int plen)
1636 return output + plen - buf;
1637 }
1638
1654 -static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, int inaccurate_eof)
1639 +static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int inaccurate_eof)
1640 {
1641 int match_beginning, match_end;
1657 - char *buf = desc->buffer;
1642 const char *patch = frag->patch;
1643 int offset, size = frag->size;
1644 char *old = xmalloc(size);
@@ -1765,24 +1749,17 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1749 lines = 0;
1750 pos = frag->newpos;
1751 for (;;) {
1768 - offset = find_offset(buf, desc->size,
1752 + offset = find_offset(buf->buf, buf->len,
1753 oldlines, oldsize, pos, &lines);
1770 - if (match_end && offset + oldsize != desc->size)
1754 + if (match_end && offset + oldsize != buf->len)
1755 offset = -1;
1756 if (match_beginning && offset)
1757 offset = -1;
1758 if (offset >= 0) {
1775 - int diff;
1776 - unsigned long size, alloc;
1777 -
1759 if (new_whitespace == strip_whitespace &&
1779 - (desc->size - oldsize - offset == 0)) /* end of file? */
1760 + (buf->len - oldsize - offset == 0)) /* end of file? */
1761 newsize -= new_blank_lines_at_end;
1762
1782 - diff = newsize - oldsize;
1783 - size = desc->size + diff;
1784 - alloc = desc->alloc;
1785 -
1763 /* Warn if it was necessary to reduce the number
1764 * of context lines.
1765 */
@@ -1792,19 +1769,8 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1769 " to apply fragment at %d\n",
1770 leading, trailing, pos + lines);
1771
1795 - if (size > alloc) {
1796 - alloc = size + 8192;
1797 - desc->alloc = alloc;
1798 - buf = xrealloc(buf, alloc);
1799 - desc->buffer = buf;
1800 - }
1801 - desc->size = size;
1802 - memmove(buf + offset + newsize,
1803 - buf + offset + oldsize,
1804 - size - offset - newsize);
1805 - memcpy(buf + offset, newlines, newsize);
1772 + strbuf_splice(buf, offset, oldsize, newlines, newsize);
1773 offset = 0;
1807 -
1774 break;
1775 }
1776
@@ -1840,12 +1806,11 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1806 return offset;
1807 }
1808
1843 -static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
1809 +static int apply_binary_fragment(struct strbuf *buf, struct patch *patch)
1810 {
1845 - unsigned long dst_size;
1811 struct fragment *fragment = patch->fragments;
1847 - void *data;
1848 - void *result;
1812 + unsigned long len;
1813 + void *dst;
1814
1815 /* Binary patch is irreversible without the optional second hunk */
1816 if (apply_in_reverse) {
@@ -1856,29 +1821,24 @@ static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
1821 ? patch->new_name : patch->old_name);
1822 fragment = fragment->next;
1823 }
1859 - data = (void*) fragment->patch;
1824 switch (fragment->binary_patch_method) {
1825 case BINARY_DELTA_DEFLATED:
1862 - result = patch_delta(desc->buffer, desc->size,
1863 - data,
1864 - fragment->size,
1865 - &dst_size);
1866 - free(desc->buffer);
1867 - desc->buffer = result;
1868 - break;
1826 + dst = patch_delta(buf->buf, buf->len, fragment->patch,
1827 + fragment->size, &len);
1828 + if (!dst)
1829 + return -1;
1830 + /* XXX patch_delta NUL-terminates */
1831 + strbuf_attach(buf, dst, len, len + 1);
1832 + return 0;
1833 case BINARY_LITERAL_DEFLATED:
1870 - free(desc->buffer);
1871 - desc->buffer = data;
1872 - dst_size = fragment->size;
1873 - break;
1834 + strbuf_reset(buf);
1835 + strbuf_add(buf, fragment->patch, fragment->size);
1836 + return 0;
1837 }
1875 - if (!desc->buffer)
1876 - return -1;
1877 - desc->size = desc->alloc = dst_size;
1878 - return 0;
1838 + return -1;
1839 }
1840
1881 -static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1841 +static int apply_binary(struct strbuf *buf, struct patch *patch)
1842 {
1843 const char *name = patch->old_name ? patch->old_name : patch->new_name;
1844 unsigned char sha1[20];
@@ -1897,7 +1857,7 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1857 /* See if the old one matches what the patch
1858 * applies to.
1859 */
1900 - hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
1860 + hash_sha1_file(buf->buf, buf->len, blob_type, sha1);
1861 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1862 return error("the patch applies to '%s' (%s), "
1863 "which does not match the "
@@ -1906,16 +1866,14 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1866 }
1867 else {
1868 /* Otherwise, the old one must be empty. */
1909 - if (desc->size)
1869 + if (buf->len)
1870 return error("the patch applies to an empty "
1871 "'%s' but it is not empty", name);
1872 }
1873
1874 get_sha1_hex(patch->new_sha1_prefix, sha1);
1875 if (is_null_sha1(sha1)) {
1916 - free(desc->buffer);
1917 - desc->alloc = desc->size = 0;
1918 - desc->buffer = NULL;
1876 + strbuf_release(buf);
1877 return 0; /* deletion patch */
1878 }
1879
@@ -1923,43 +1881,44 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1881 /* We already have the postimage */
1882 enum object_type type;
1883 unsigned long size;
1884 + char *result;
1885
1927 - free(desc->buffer);
1928 - desc->buffer = read_sha1_file(sha1, &type, &size);
1929 - if (!desc->buffer)
1886 + result = read_sha1_file(sha1, &type, &size);
1887 + if (!result)
1888 return error("the necessary postimage %s for "
1889 "'%s' cannot be read",
1890 patch->new_sha1_prefix, name);
1933 - desc->alloc = desc->size = size;
1934 - }
1935 - else {
1936 - /* We have verified desc matches the preimage;
1891 + /* XXX read_sha1_file NUL-terminates */
1892 + strbuf_attach(buf, result, size, size + 1);
1893 + } else {
1894 + /* We have verified buf matches the preimage;
1895 * apply the patch data to it, which is stored
1896 * in the patch->fragments->{patch,size}.
1897 */
1940 - if (apply_binary_fragment(desc, patch))
1898 + if (apply_binary_fragment(buf, patch))
1899 return error("binary patch does not apply to '%s'",
1900 name);
1901
1902 /* verify that the result matches */
1945 - hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
1903 + hash_sha1_file(buf->buf, buf->len, blob_type, sha1);
1904 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
1947 - return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)", name, patch->new_sha1_prefix, sha1_to_hex(sha1));
1905 + return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)",
1906 + name, patch->new_sha1_prefix, sha1_to_hex(sha1));
1907 }
1908
1909 return 0;
1910 }
1911
1953 -static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1912 +static int apply_fragments(struct strbuf *buf, struct patch *patch)
1913 {
1914 struct fragment *frag = patch->fragments;
1915 const char *name = patch->old_name ? patch->old_name : patch->new_name;
1916
1917 if (patch->is_binary)
1959 - return apply_binary(desc, patch);
1918 + return apply_binary(buf, patch);
1919
1920 while (frag) {
1962 - if (apply_one_fragment(desc, frag, patch->inaccurate_eof)) {
1921 + if (apply_one_fragment(buf, frag, patch->inaccurate_eof)) {
1922 error("patch failed: %s:%ld", name, frag->oldpos);
1923 if (!apply_with_reject)
1924 return -1;
@@ -1970,76 +1929,57 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1929 return 0;
1930 }
1931
1973 -static int read_file_or_gitlink(struct cache_entry *ce, char **buf_p,
1974 - unsigned long *size_p)
1932 +static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
1933 {
1934 if (!ce)
1935 return 0;
1936
1937 if (S_ISGITLINK(ntohl(ce->ce_mode))) {
1980 - *buf_p = xmalloc(100);
1981 - *size_p = snprintf(*buf_p, 100,
1982 - "Subproject commit %s\n", sha1_to_hex(ce->sha1));
1938 + strbuf_grow(buf, 100);
1939 + strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));
1940 } else {
1941 enum object_type type;
1985 - *buf_p = read_sha1_file(ce->sha1, &type, size_p);
1986 - if (!*buf_p)
1942 + unsigned long sz;
1943 + char *result;
1944 +
1945 + result = read_sha1_file(ce->sha1, &type, &sz);
1946 + if (!result)
1947 return -1;
1948 + /* XXX read_sha1_file NUL-terminates */
1949 + strbuf_attach(buf, result, sz, sz + 1);
1950 }
1951 return 0;
1952 }
1953
1954 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
1955 {
1994 - char *buf;
1995 - unsigned long size, alloc;
1996 - struct buffer_desc desc;
1956 + struct strbuf buf;
1957
1998 - size = 0;
1999 - alloc = 0;
2000 - buf = NULL;
1958 + strbuf_init(&buf, 0);
1959 if (cached) {
2002 - if (read_file_or_gitlink(ce, &buf, &size))
1960 + if (read_file_or_gitlink(ce, &buf))
1961 return error("read of %s failed", patch->old_name);
2004 - alloc = size;
1962 } else if (patch->old_name) {
1963 if (S_ISGITLINK(patch->old_mode)) {
2007 - if (ce)
2008 - read_file_or_gitlink(ce, &buf, &size);
2009 - else {
1964 + if (ce) {
1965 + read_file_or_gitlink(ce, &buf);
1966 + } else {
1967 /*
1968 * There is no way to apply subproject
1969 * patch without looking at the index.
1970 */
1971 patch->fragments = NULL;
2015 - size = 0;
1972 }
2017 - }
2018 - else {
2019 - size = xsize_t(st->st_size);
2020 - alloc = size + 8192;
2021 - buf = xmalloc(alloc);
2022 - if (read_old_data(st, patch->old_name,
2023 - &buf, &alloc, &size))
2024 - return error("read of %s failed",
2025 - patch->old_name);
1973 + } else {
1974 + if (read_old_data(st, patch->old_name, &buf))
1975 + return error("read of %s failed", patch->old_name);
1976 }
1977 }
1978
2029 - desc.size = size;
2030 - desc.alloc = alloc;
2031 - desc.buffer = buf;
2032 -
2033 - if (apply_fragments(&desc, patch) < 0)
1979 + if (apply_fragments(&buf, patch) < 0)
1980 return -1; /* note with --reject this succeeds. */
2035 -
2036 - /* NUL terminate the result */
2037 - if (desc.alloc <= desc.size)
2038 - desc.buffer = xrealloc(desc.buffer, desc.size + 1);
2039 - desc.buffer[desc.size] = 0;
2040 -
2041 - patch->result = desc.buffer;
2042 - patch->resultsize = desc.size;
1981 + patch->result = buf.buf;
1982 + patch->resultsize = buf.len;
1983
1984 if (0 < patch->is_delete && patch->resultsize)
1985 return error("removal patch leaves file contents");
@@ -2460,19 +2400,11 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
2400 size = nbuf.len;
2401 buf = nbuf.buf;
2402 }
2403 + write_or_die(fd, buf, size);
2404 + strbuf_release(&nbuf);
2405
2464 - while (size) {
2465 - int written = xwrite(fd, buf, size);
2466 - if (written < 0)
2467 - die("writing file %s: %s", path, strerror(errno));
2468 - if (!written)
2469 - die("out of space writing file %s", path);
2470 - buf += written;
2471 - size -= written;
2472 - }
2406 if (close(fd) < 0)
2407 die("closing file %s: %s", path, strerror(errno));
2475 - strbuf_release(&nbuf);
2408 return 0;
2409 }
2410