server-info: drop objdirlen pointer arithmetic

When writing objects/info/packs, we use the basename of each pack
(i.e., just the "pack-1234abcd.pack" part). We compute that manually by
adding "objdirlen + 6" to the name.

This _should_ work consistently, as we do not include non-local packs,
meaning everything should be in $objdir/pack/. Before f13d7db4af
(server-info.c: use pack_local like everybody else., 2005-12-05), this
was definitely true, since we computed "local" based on comparing the
objdir string.  Since then, we're relying on the code on packfile.c to
match our expectations of p->pack_name and p->local.

I think our expectations do still hold today, but we can be a bit more
defensive by just using pack_basename() to get the base. That
future-proofs us, and should hopefully be more obviously safe to
somebody reading the code.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2019-04-05 14:14:04 -04:00 committed by Junio C Hamano
parent 79bb8b3c80
commit b9fb142eac
1 changed files with 2 additions and 8 deletions

View File

@ -93,16 +93,13 @@ static struct pack_info {
int new_num;
} **info;
static int num_pack;
static const char *objdir;
static int objdirlen;
static struct pack_info *find_pack_by_name(const char *name)
{
int i;
for (i = 0; i < num_pack; i++) {
struct packed_git *p = info[i]->p;
/* skip "/pack/" after ".git/objects" */
if (!strcmp(p->pack_name + objdirlen + 6, name))
if (!strcmp(pack_basename(p), name))
return info[i];
}
return NULL;
@ -196,9 +193,6 @@ static void init_pack_info(const char *infofile, int force)
int stale;
int i = 0;
objdir = get_object_directory();
objdirlen = strlen(objdir);
for (p = get_all_packs(the_repository); p; p = p->next) {
/* we ignore things on alternate path since they are
* not available to the pullers in general.
@ -246,7 +240,7 @@ static int write_pack_info_file(FILE *fp)
{
int i;
for (i = 0; i < num_pack; i++) {
if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
if (fprintf(fp, "P %s\n", pack_basename(info[i]->p)) < 0)
return -1;
}
if (fputc('\n', fp) == EOF)