@matheusbsbs / matheus-bagliano / commits / 7a604f16b7
Fork 0

Simplify strbuf uses in archive-tar.c using strbuf API

This is just cleaner way to deal with strbufs, using its API rather than reinventing it in the module (e.g. strbuf_append_string is just the plain strbuf_addstr function, and it was used to perform what strbuf_addch does anyways). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 6, 2007 at 13:20 UTC 7a604f16b71e3bfd1c6e30d400f05be918e5376e
1 file changed +16 -49
archive-tar.c
+16 -49
@@ -78,19 +78,6 @@ static void write_trailer(void)
78 }
79 }
80
81 -static void strbuf_append_string(struct strbuf *sb, const char *s)
82 -{
83 - int slen = strlen(s);
84 - int total = sb->len + slen;
85 - if (total + 1 > sb->alloc) {
86 - sb->buf = xrealloc(sb->buf, total + 1);
87 - sb->alloc = total + 1;
88 - }
89 - memcpy(sb->buf + sb->len, s, slen);
90 - sb->len = total;
91 - sb->buf[total] = '\0';
92 -}
93 -
81 /*
82 * pax extended header records have the format "%u %s=%s\n". %u contains
83 * the size of the whole string (including the %u), the first %s is the
@@ -100,26 +87,17 @@ static void strbuf_append_string(struct strbuf *sb, const char *s)
87 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
88 const char *value, unsigned int valuelen)
89 {
103 - char *p;
104 - int len, total, tmp;
90 + int len, tmp;
91
92 /* "%u %s=%s\n" */
93 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
94 for (tmp = len; tmp > 9; tmp /= 10)
95 len++;
96
111 - total = sb->len + len;
112 - if (total > sb->alloc) {
113 - sb->buf = xrealloc(sb->buf, total);
114 - sb->alloc = total;
115 - }
116 -
117 - p = sb->buf;
118 - p += sprintf(p, "%u %s=", len, keyword);
119 - memcpy(p, value, valuelen);
120 - p += valuelen;
121 - *p = '\n';
122 - sb->len = total;
97 + strbuf_grow(sb, len);
98 + strbuf_addf(sb, "%u %s=", len, keyword);
99 + strbuf_add(sb, value, valuelen);
100 + strbuf_addch(sb, '\n');
101 }
102
103 static unsigned int ustar_header_chksum(const struct ustar_header *header)
@@ -153,8 +131,7 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
131 struct strbuf ext_header;
132
133 memset(&header, 0, sizeof(header));
156 - ext_header.buf = NULL;
157 - ext_header.len = ext_header.alloc = 0;
134 + strbuf_init(&ext_header);
135
136 if (!sha1) {
137 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
@@ -225,8 +202,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
202
203 if (ext_header.len > 0) {
204 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
228 - free(ext_header.buf);
205 }
206 + strbuf_release(&ext_header);
207 write_blocked(&header, sizeof(header));
208 if (S_ISREG(mode) && buffer && size > 0)
209 write_blocked(buffer, size);
@@ -235,11 +212,11 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
212 static void write_global_extended_header(const unsigned char *sha1)
213 {
214 struct strbuf ext_header;
238 - ext_header.buf = NULL;
239 - ext_header.len = ext_header.alloc = 0;
215 +
216 + strbuf_init(&ext_header);
217 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
218 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
242 - free(ext_header.buf);
219 + strbuf_release(&ext_header);
220 }
221
222 static int git_tar_config(const char *var, const char *value)
@@ -260,28 +237,18 @@ static int write_tar_entry(const unsigned char *sha1,
237 const char *base, int baselen,
238 const char *filename, unsigned mode, int stage)
239 {
263 - static struct strbuf path;
240 + static struct strbuf path = STRBUF_INIT;
241 int filenamelen = strlen(filename);
242 void *buffer;
243 enum object_type type;
244 unsigned long size;
245
269 - if (!path.alloc) {
270 - path.buf = xmalloc(PATH_MAX);
271 - path.alloc = PATH_MAX;
272 - path.len = path.eof = 0;
273 - }
274 - if (path.alloc < baselen + filenamelen + 1) {
275 - free(path.buf);
276 - path.buf = xmalloc(baselen + filenamelen + 1);
277 - path.alloc = baselen + filenamelen + 1;
278 - }
279 - memcpy(path.buf, base, baselen);
280 - memcpy(path.buf + baselen, filename, filenamelen);
281 - path.len = baselen + filenamelen;
282 - path.buf[path.len] = '\0';
246 + strbuf_grow(&path, MAX(PATH_MAX, baselen + filenamelen + 1));
247 + strbuf_reset(&path);
248 + strbuf_add(&path, base, baselen);
249 + strbuf_add(&path, filename, filenamelen);
250 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
284 - strbuf_append_string(&path, "/");
251 + strbuf_addch(&path, '/');
252 buffer = NULL;
253 size = 0;
254 } else {