Make sure objects/pack exists before creating a new pack

In a repository created with git older than f49fb35 (git-init-db: create
"pack" subdirectory under objects, 2005-06-27), objects/pack/ directory is
not created upon initialization.  It was Ok because subdirectories are
created as needed inside directories init-db creates, and back then,
packfiles were recent invention.

After the said commit, new codepaths started relying on the presense of
objects/pack/ directory in the repository.  This was exacerbated with
8b4eb6b (Do not perform cross-directory renames when creating packs,
2008-09-22) that moved the location temporary pack files are created from
objects/ directory to objects/pack/ directory, because moving temporary to
the final location was done carefully with lazy leading directory creation.

Many packfile related operations in such an old repository can fail
mysteriously because of this.

This commit introduces two helper functions to make things work better.

 - odb_mkstemp() is a specialized version of mkstemp() to refactor the
   code and teach it to create leading directories as needed;

 - odb_pack_keep() refactors the code to create a ".keep" file while
   create leading directories as needed.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano
2009-02-24 23:11:29 -08:00
parent 718258e256
commit 6e180cdcec
7 changed files with 72 additions and 27 deletions

View File

@ -815,9 +815,8 @@ static void start_packfile(void)
struct pack_header hdr;
int pack_fd;
snprintf(tmpfile, sizeof(tmpfile),
"%s/pack/tmp_pack_XXXXXX", get_object_directory());
pack_fd = xmkstemp(tmpfile);
pack_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
"pack/tmp_pack_XXXXXX");
p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
strcpy(p->pack_name, tmpfile);
p->pack_fd = pack_fd;
@ -877,9 +876,8 @@ static char *create_index(void)
c = next;
}
snprintf(tmpfile, sizeof(tmpfile),
"%s/pack/tmp_idx_XXXXXX", get_object_directory());
idx_fd = xmkstemp(tmpfile);
idx_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
"pack/tmp_idx_XXXXXX");
f = sha1fd(idx_fd, tmpfile);
sha1write(f, array, 256 * sizeof(int));
SHA1_Init(&ctx);
@ -905,9 +903,7 @@ static char *keep_pack(char *curr_index_name)
chmod(pack_data->pack_name, 0444);
chmod(curr_index_name, 0444);
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
get_object_directory(), sha1_to_hex(pack_data->sha1));
keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1);
if (keep_fd < 0)
die("cannot create keep file");
write_or_die(keep_fd, keep_msg, strlen(keep_msg));