Skip to content

Commit 7a604f1

Browse files
MadCodergitster
authored andcommitted
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 <[email protected]>
1 parent b449f4c commit 7a604f1

File tree

1 file changed

+16
-49
lines changed

1 file changed

+16
-49
lines changed

archive-tar.c

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,6 @@ static void write_trailer(void)
7878
}
7979
}
8080

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-
9481
/*
9582
* pax extended header records have the format "%u %s=%s\n". %u contains
9683
* 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)
10087
static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
10188
const char *value, unsigned int valuelen)
10289
{
103-
char *p;
104-
int len, total, tmp;
90+
int len, tmp;
10591

10692
/* "%u %s=%s\n" */
10793
len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
10894
for (tmp = len; tmp > 9; tmp /= 10)
10995
len++;
11096

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');
123101
}
124102

125103
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,
153131
struct strbuf ext_header;
154132

155133
memset(&header, 0, sizeof(header));
156-
ext_header.buf = NULL;
157-
ext_header.len = ext_header.alloc = 0;
134+
strbuf_init(&ext_header);
158135

159136
if (!sha1) {
160137
*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
@@ -225,8 +202,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
225202

226203
if (ext_header.len > 0) {
227204
write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
228-
free(ext_header.buf);
229205
}
206+
strbuf_release(&ext_header);
230207
write_blocked(&header, sizeof(header));
231208
if (S_ISREG(mode) && buffer && size > 0)
232209
write_blocked(buffer, size);
@@ -235,11 +212,11 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
235212
static void write_global_extended_header(const unsigned char *sha1)
236213
{
237214
struct strbuf ext_header;
238-
ext_header.buf = NULL;
239-
ext_header.len = ext_header.alloc = 0;
215+
216+
strbuf_init(&ext_header);
240217
strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
241218
write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
242-
free(ext_header.buf);
219+
strbuf_release(&ext_header);
243220
}
244221

245222
static int git_tar_config(const char *var, const char *value)
@@ -260,28 +237,18 @@ static int write_tar_entry(const unsigned char *sha1,
260237
const char *base, int baselen,
261238
const char *filename, unsigned mode, int stage)
262239
{
263-
static struct strbuf path;
240+
static struct strbuf path = STRBUF_INIT;
264241
int filenamelen = strlen(filename);
265242
void *buffer;
266243
enum object_type type;
267244
unsigned long size;
268245

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);
283250
if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
284-
strbuf_append_string(&path, "/");
251+
strbuf_addch(&path, '/');
285252
buffer = NULL;
286253
size = 0;
287254
} else {

0 commit comments

Comments
 (0)