Skip to content

Commit 2990034

Browse files
committed
Merge branch 'jc/maint-1.6.0-pack-directory' into maint-1.6.1
* jc/maint-1.6.0-pack-directory: Fix odb_mkstemp() on AIX Make sure objects/pack exists before creating a new pack Conflicts: wrapper.c
2 parents bf0fe35 + 2c626e5 commit 2990034

File tree

7 files changed

+73
-27
lines changed

7 files changed

+73
-27
lines changed

builtin-pack-objects.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,8 @@ static void write_pack_file(void)
488488
} else {
489489
char tmpname[PATH_MAX];
490490
int fd;
491-
snprintf(tmpname, sizeof(tmpname),
492-
"%s/pack/tmp_pack_XXXXXX", get_object_directory());
493-
fd = xmkstemp(tmpname);
491+
fd = odb_mkstemp(tmpname, sizeof(tmpname),
492+
"pack/tmp_pack_XXXXXX");
494493
pack_tmp_name = xstrdup(tmpname);
495494
f = sha1fd(fd, pack_tmp_name);
496495
}

fast-import.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,8 @@ static void start_packfile(void)
816816
struct pack_header hdr;
817817
int pack_fd;
818818

819-
snprintf(tmpfile, sizeof(tmpfile),
820-
"%s/pack/tmp_pack_XXXXXX", get_object_directory());
821-
pack_fd = xmkstemp(tmpfile);
819+
pack_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
820+
"pack/tmp_pack_XXXXXX");
822821
p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
823822
strcpy(p->pack_name, tmpfile);
824823
p->pack_fd = pack_fd;
@@ -878,9 +877,8 @@ static char *create_index(void)
878877
c = next;
879878
}
880879

881-
snprintf(tmpfile, sizeof(tmpfile),
882-
"%s/pack/tmp_idx_XXXXXX", get_object_directory());
883-
idx_fd = xmkstemp(tmpfile);
880+
idx_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
881+
"pack/tmp_idx_XXXXXX");
884882
f = sha1fd(idx_fd, tmpfile);
885883
sha1write(f, array, 256 * sizeof(int));
886884
git_SHA1_Init(&ctx);
@@ -906,9 +904,7 @@ static char *keep_pack(char *curr_index_name)
906904
chmod(pack_data->pack_name, 0444);
907905
chmod(curr_index_name, 0444);
908906

909-
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
910-
get_object_directory(), sha1_to_hex(pack_data->sha1));
911-
keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
907+
keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1);
912908
if (keep_fd < 0)
913909
die("cannot create keep file");
914910
write_or_die(keep_fd, keep_msg, strlen(keep_msg));

git-compat-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ extern ssize_t xwrite(int fd, const void *buf, size_t len);
303303
extern int xdup(int fd);
304304
extern FILE *xfdopen(int fd, const char *mode);
305305
extern int xmkstemp(char *template);
306+
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
307+
extern int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1);
306308

307309
static inline size_t xsize_t(off_t len)
308310
{

index-pack.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,8 @@ static char *open_pack_file(char *pack_name)
171171
input_fd = 0;
172172
if (!pack_name) {
173173
static char tmpfile[PATH_MAX];
174-
snprintf(tmpfile, sizeof(tmpfile),
175-
"%s/pack/tmp_pack_XXXXXX", get_object_directory());
176-
output_fd = xmkstemp(tmpfile);
174+
output_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
175+
"pack/tmp_pack_XXXXXX");
177176
pack_name = xstrdup(tmpfile);
178177
} else
179178
output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
@@ -793,22 +792,24 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
793792

794793
if (keep_msg) {
795794
int keep_fd, keep_msg_len = strlen(keep_msg);
796-
if (!keep_name) {
797-
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
798-
get_object_directory(), sha1_to_hex(sha1));
799-
keep_name = name;
800-
}
801-
keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
795+
796+
if (!keep_name)
797+
keep_fd = odb_pack_keep(name, sizeof(name), sha1);
798+
else
799+
keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
800+
802801
if (keep_fd < 0) {
803802
if (errno != EEXIST)
804-
die("cannot write keep file");
803+
die("cannot write keep file '%s' (%s)",
804+
keep_name, strerror(errno));
805805
} else {
806806
if (keep_msg_len > 0) {
807807
write_or_die(keep_fd, keep_msg, keep_msg_len);
808808
write_or_die(keep_fd, "\n", 1);
809809
}
810810
if (close(keep_fd) != 0)
811-
die("cannot write keep file");
811+
die("cannot close written keep file '%s' (%s)",
812+
keep_name, strerror(errno));
812813
report = "keep";
813814
}
814815
}

pack-write.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ char *write_idx_file(char *index_name, struct pack_idx_entry **objects,
4444

4545
if (!index_name) {
4646
static char tmpfile[PATH_MAX];
47-
snprintf(tmpfile, sizeof(tmpfile),
48-
"%s/pack/tmp_idx_XXXXXX", get_object_directory());
49-
fd = xmkstemp(tmpfile);
47+
fd = odb_mkstemp(tmpfile, sizeof(tmpfile), "pack/tmp_idx_XXXXXX");
5048
index_name = xstrdup(tmpfile);
5149
} else {
5250
unlink(index_name);
@@ -239,7 +237,7 @@ char *index_pack_lockfile(int ip_out)
239237
char packname[46];
240238

241239
/*
242-
* The first thing we expects from index-pack's output
240+
* The first thing we expect from index-pack's output
243241
* is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
244242
* %40s is the newly created pack SHA1 name. In the "keep"
245243
* case, we need it to remove the corresponding .keep file

t/t5300-pack-object.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ test_expect_success \
180180

181181
unset GIT_OBJECT_DIRECTORY
182182

183+
test_expect_success 'survive missing objects/pack directory' '
184+
(
185+
rm -fr missing-pack &&
186+
mkdir missing-pack &&
187+
cd missing-pack &&
188+
git init &&
189+
GOP=.git/objects/pack
190+
rm -fr $GOP &&
191+
git index-pack --stdin --keep=test <../test-3-${packname_3}.pack &&
192+
test -f $GOP/pack-${packname_3}.pack &&
193+
test_cmp $GOP/pack-${packname_3}.pack ../test-3-${packname_3}.pack &&
194+
test -f $GOP/pack-${packname_3}.idx &&
195+
test_cmp $GOP/pack-${packname_3}.idx ../test-3-${packname_3}.idx &&
196+
test -f $GOP/pack-${packname_3}.keep
197+
)
198+
'
199+
183200
test_expect_success \
184201
'verify pack' \
185202
'git verify-pack test-1-${packname_1}.idx \

wrapper.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,36 @@ int git_inflate(z_streamp strm, int flush)
256256
error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
257257
return ret;
258258
}
259+
260+
int odb_mkstemp(char *template, size_t limit, const char *pattern)
261+
{
262+
int fd;
263+
264+
snprintf(template, limit, "%s/%s",
265+
get_object_directory(), pattern);
266+
fd = mkstemp(template);
267+
if (0 <= fd)
268+
return fd;
269+
270+
/* slow path */
271+
/* some mkstemp implementations erase template on failure */
272+
snprintf(template, limit, "%s/%s",
273+
get_object_directory(), pattern);
274+
safe_create_leading_directories(template);
275+
return xmkstemp(template);
276+
}
277+
278+
int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
279+
{
280+
int fd;
281+
282+
snprintf(name, namesz, "%s/pack/pack-%s.keep",
283+
get_object_directory(), sha1_to_hex(sha1));
284+
fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
285+
if (0 <= fd)
286+
return fd;
287+
288+
/* slow path */
289+
safe_create_leading_directories(name);
290+
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
291+
}

0 commit comments

Comments
 (0)