@@ -78,19 +78,6 @@ static void write_trailer(void)
78
78
}
79
79
}
80
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
-
94
81
/*
95
82
* pax extended header records have the format "%u %s=%s\n". %u contains
96
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)
100
87
static void strbuf_append_ext_header (struct strbuf * sb , const char * keyword ,
101
88
const char * value , unsigned int valuelen )
102
89
{
103
- char * p ;
104
- int len , total , tmp ;
90
+ int len , tmp ;
105
91
106
92
/* "%u %s=%s\n" */
107
93
len = 1 + 1 + strlen (keyword ) + 1 + valuelen + 1 ;
108
94
for (tmp = len ; tmp > 9 ; tmp /= 10 )
109
95
len ++ ;
110
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' );
123
101
}
124
102
125
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,
153
131
struct strbuf ext_header ;
154
132
155
133
memset (& header , 0 , sizeof (header ));
156
- ext_header .buf = NULL ;
157
- ext_header .len = ext_header .alloc = 0 ;
134
+ strbuf_init (& ext_header );
158
135
159
136
if (!sha1 ) {
160
137
* header .typeflag = TYPEFLAG_GLOBAL_HEADER ;
@@ -225,8 +202,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
225
202
226
203
if (ext_header .len > 0 ) {
227
204
write_entry (sha1 , NULL , 0 , ext_header .buf , ext_header .len );
228
- free (ext_header .buf );
229
205
}
206
+ strbuf_release (& ext_header );
230
207
write_blocked (& header , sizeof (header ));
231
208
if (S_ISREG (mode ) && buffer && size > 0 )
232
209
write_blocked (buffer , size );
@@ -235,11 +212,11 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
235
212
static void write_global_extended_header (const unsigned char * sha1 )
236
213
{
237
214
struct strbuf ext_header ;
238
- ext_header . buf = NULL ;
239
- ext_header . len = ext_header . alloc = 0 ;
215
+
216
+ strbuf_init ( & ext_header ) ;
240
217
strbuf_append_ext_header (& ext_header , "comment" , sha1_to_hex (sha1 ), 40 );
241
218
write_entry (NULL , NULL , 0 , ext_header .buf , ext_header .len );
242
- free ( ext_header . buf );
219
+ strbuf_release ( & ext_header );
243
220
}
244
221
245
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,
260
237
const char * base , int baselen ,
261
238
const char * filename , unsigned mode , int stage )
262
239
{
263
- static struct strbuf path ;
240
+ static struct strbuf path = STRBUF_INIT ;
264
241
int filenamelen = strlen (filename );
265
242
void * buffer ;
266
243
enum object_type type ;
267
244
unsigned long size ;
268
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 );
283
250
if (S_ISDIR (mode ) || S_ISGITLINK (mode )) {
284
- strbuf_append_string (& path , "/" );
251
+ strbuf_addch (& path , '/' );
285
252
buffer = NULL ;
286
253
size = 0 ;
287
254
} else {
0 commit comments