for_each_loose_file_in_objdir: take an optional strbuf path
We feed a root "objdir" path to this iterator function, which then copies the result into a strbuf, so that it can repeatedly append the object sub-directories to it. Let's make it easy for callers to just pass us a strbuf in the first place. We leave the original interface as a convenience for callers who want to just pass a const string like the result of get_object_directory(). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Feb 8, 2015 at 20:13 UTC
e6f875e052bb327d43d86b4e8335ebd4746d3e2a
2 files changed
+30
-10
cache.h
+9
@@ -1256,6 +1256,10 @@ extern int unpack_object_header(struct packed_git *, struct pack_window **, off_
1256
*
1257
* Any callback that is NULL will be ignored. Callbacks returning non-zero
1258
* will end the iteration.
1259
+ *
1260
+ * In the "buf" variant, "path" is a strbuf which will also be used as a
1261
+ * scratch buffer, but restored to its original contents before
1262
+ * the function returns.
1263
*/
1264
typedef int each_loose_object_fn(const unsigned char *sha1,
1265
const char *path,
@@ -1271,6 +1275,11 @@ int for_each_loose_file_in_objdir(const char *path,
1275
each_loose_cruft_fn cruft_cb,
1276
each_loose_subdir_fn subdir_cb,
1277
void *data);
1278
+int for_each_loose_file_in_objdir_buf(struct strbuf *path,
1279
+ each_loose_object_fn obj_cb,
1280
+ each_loose_cruft_fn cruft_cb,
1281
+ each_loose_subdir_fn subdir_cb,
1282
+ void *data);
1283
1284
/*
1285
* Iterate over loose and packed objects in both the local
sha1_file.c
+21
-10
@@ -3358,31 +3358,42 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
3358
return r;
3359
}
3360
3361
-int for_each_loose_file_in_objdir(const char *path,
3361
+int for_each_loose_file_in_objdir_buf(struct strbuf *path,
3362
each_loose_object_fn obj_cb,
3363
each_loose_cruft_fn cruft_cb,
3364
each_loose_subdir_fn subdir_cb,
3365
void *data)
3366
{
3367
- struct strbuf buf = STRBUF_INIT;
3368
- size_t baselen;
3367
+ size_t baselen = path->len;
3368
int r = 0;
3369
int i;
3370
3372
- strbuf_addstr(&buf, path);
3373
- strbuf_addch(&buf, '/');
3374
- baselen = buf.len;
3375
-
3371
for (i = 0; i < 256; i++) {
3377
- strbuf_addf(&buf, "%02x", i);
3378
- r = for_each_file_in_obj_subdir(i, &buf, obj_cb, cruft_cb,
3372
+ strbuf_addf(path, "/%02x", i);
3373
+ r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
3374
subdir_cb, data);
3380
- strbuf_setlen(&buf, baselen);
3375
+ strbuf_setlen(path, baselen);
3376
if (r)
3377
break;
3378
}
3379
3380
+ return r;
3381
+}
3382
+
3383
+int for_each_loose_file_in_objdir(const char *path,
3384
+ each_loose_object_fn obj_cb,
3385
+ each_loose_cruft_fn cruft_cb,
3386
+ each_loose_subdir_fn subdir_cb,
3387
+ void *data)
3388
+{
3389
+ struct strbuf buf = STRBUF_INIT;
3390
+ int r;
3391
+
3392
+ strbuf_addstr(&buf, path);
3393
+ r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
3394
+ subdir_cb, data);
3395
strbuf_release(&buf);
3396
+
3397
return r;
3398
}
3399