sha1_file: let for_each_file_in_obj_subdir() handle subdir names

The function for_each_file_in_obj_subdir() takes a object subdirectory
number and expects the name of the same subdirectory to be included in
the path strbuf.  Avoid this redundancy by letting the function append
the hexadecimal subdirectory name itself.  This makes it a bit easier
and safer to use the function -- it becomes impossible to specify
different subdirectories in subdir_nr and path.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2017-06-24 14:12:30 +02:00 committed by Junio C Hamano
parent 5a5bd5765a
commit 0375f472d4
2 changed files with 14 additions and 9 deletions

View File

@ -3742,15 +3742,22 @@ int for_each_file_in_obj_subdir(int subdir_nr,
each_loose_subdir_fn subdir_cb, each_loose_subdir_fn subdir_cb,
void *data) void *data)
{ {
size_t baselen = path->len; size_t origlen, baselen;
DIR *dir = opendir(path->buf); DIR *dir;
struct dirent *de; struct dirent *de;
int r = 0; int r = 0;
origlen = path->len;
strbuf_complete(path, '/');
strbuf_addf(path, "%02x", subdir_nr);
baselen = path->len;
dir = opendir(path->buf);
if (!dir) { if (!dir) {
if (errno == ENOENT) if (errno != ENOENT)
return 0; r = error_errno("unable to open %s", path->buf);
return error_errno("unable to open %s", path->buf); strbuf_setlen(path, origlen);
return r;
} }
while ((de = readdir(dir))) { while ((de = readdir(dir))) {
@ -3788,6 +3795,8 @@ int for_each_file_in_obj_subdir(int subdir_nr,
if (!r && subdir_cb) if (!r && subdir_cb)
r = subdir_cb(subdir_nr, path->buf, data); r = subdir_cb(subdir_nr, path->buf, data);
strbuf_setlen(path, origlen);
return r; return r;
} }
@ -3797,15 +3806,12 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
each_loose_subdir_fn subdir_cb, each_loose_subdir_fn subdir_cb,
void *data) void *data)
{ {
size_t baselen = path->len;
int r = 0; int r = 0;
int i; int i;
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
strbuf_addf(path, "/%02x", i);
r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb, r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
subdir_cb, data); subdir_cb, data);
strbuf_setlen(path, baselen);
if (r) if (r)
break; break;
} }

View File

@ -109,7 +109,6 @@ static void find_short_object_filename(struct disambiguate_state *ds)
if (!alt->loose_objects_subdir_seen[subdir_nr]) { if (!alt->loose_objects_subdir_seen[subdir_nr]) {
struct strbuf *buf = alt_scratch_buf(alt); struct strbuf *buf = alt_scratch_buf(alt);
strbuf_addf(buf, "%02x/", subdir_nr);
for_each_file_in_obj_subdir(subdir_nr, buf, for_each_file_in_obj_subdir(subdir_nr, buf,
append_loose_object, append_loose_object,
NULL, NULL, NULL, NULL,