Skip to content

Commit ba5e05f

Browse files
committed
Merge branch 'jk/pack-name-cleanups' into maint
Code clean-up. * jk/pack-name-cleanups: index-pack: make pointer-alias fallbacks safer replace snprintf with odb_pack_name() odb_pack_keep(): stop generating keepfile name sha1_file.c: make pack-name helper globally accessible move odb_* declarations out of git-compat-util.h
2 parents 8f71209 + f207548 commit ba5e05f

File tree

6 files changed

+57
-46
lines changed

6 files changed

+57
-46
lines changed

builtin/index-pack.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,9 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
13861386
unsigned char *sha1)
13871387
{
13881388
const char *report = "pack";
1389-
char name[PATH_MAX];
1389+
struct strbuf pack_name = STRBUF_INIT;
1390+
struct strbuf index_name = STRBUF_INIT;
1391+
struct strbuf keep_name_buf = STRBUF_INIT;
13901392
int err;
13911393

13921394
if (!from_stdin) {
@@ -1402,43 +1404,36 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14021404
int keep_fd, keep_msg_len = strlen(keep_msg);
14031405

14041406
if (!keep_name)
1405-
keep_fd = odb_pack_keep(name, sizeof(name), sha1);
1406-
else
1407-
keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
1407+
keep_name = odb_pack_name(&keep_name_buf, sha1, "keep");
14081408

1409+
keep_fd = odb_pack_keep(keep_name);
14091410
if (keep_fd < 0) {
14101411
if (errno != EEXIST)
14111412
die_errno(_("cannot write keep file '%s'"),
1412-
keep_name ? keep_name : name);
1413+
keep_name);
14131414
} else {
14141415
if (keep_msg_len > 0) {
14151416
write_or_die(keep_fd, keep_msg, keep_msg_len);
14161417
write_or_die(keep_fd, "\n", 1);
14171418
}
14181419
if (close(keep_fd) != 0)
14191420
die_errno(_("cannot close written keep file '%s'"),
1420-
keep_name ? keep_name : name);
1421+
keep_name);
14211422
report = "keep";
14221423
}
14231424
}
14241425

14251426
if (final_pack_name != curr_pack_name) {
1426-
if (!final_pack_name) {
1427-
snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
1428-
get_object_directory(), sha1_to_hex(sha1));
1429-
final_pack_name = name;
1430-
}
1427+
if (!final_pack_name)
1428+
final_pack_name = odb_pack_name(&pack_name, sha1, "pack");
14311429
if (finalize_object_file(curr_pack_name, final_pack_name))
14321430
die(_("cannot store pack file"));
14331431
} else if (from_stdin)
14341432
chmod(final_pack_name, 0444);
14351433

14361434
if (final_index_name != curr_index_name) {
1437-
if (!final_index_name) {
1438-
snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
1439-
get_object_directory(), sha1_to_hex(sha1));
1440-
final_index_name = name;
1441-
}
1435+
if (!final_index_name)
1436+
final_index_name = odb_pack_name(&index_name, sha1, "idx");
14421437
if (finalize_object_file(curr_index_name, final_index_name))
14431438
die(_("cannot store index file"));
14441439
} else
@@ -1464,6 +1459,10 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14641459
input_offset += err;
14651460
}
14661461
}
1462+
1463+
strbuf_release(&index_name);
1464+
strbuf_release(&pack_name);
1465+
strbuf_release(&keep_name_buf);
14671466
}
14681467

14691468
static int git_index_pack_config(const char *k, const char *v, void *cb)

cache.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,27 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
15901590

15911591
extern void pack_report(void);
15921592

1593+
/*
1594+
* Create a temporary file rooted in the object database directory.
1595+
*/
1596+
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
1597+
1598+
/*
1599+
* Generate the filename to be used for a pack file with checksum "sha1" and
1600+
* extension "ext". The result is written into the strbuf "buf", overwriting
1601+
* any existing contents. A pointer to buf->buf is returned as a convenience.
1602+
*
1603+
* Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx"
1604+
*/
1605+
extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
1606+
1607+
/*
1608+
* Create a pack .keep file named "name" (which should generally be the output
1609+
* of odb_pack_name). Returns a file descriptor opened for writing, or -1 on
1610+
* error.
1611+
*/
1612+
extern int odb_pack_keep(const char *name);
1613+
15931614
/*
15941615
* mmap the index file for the specified packfile (if it is not
15951616
* already mmapped). Return 0 on success.

environment.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,16 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern)
296296
return xmkstemp_mode(template, mode);
297297
}
298298

299-
int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1)
299+
int odb_pack_keep(const char *name)
300300
{
301301
int fd;
302302

303-
snprintf(name, namesz, "%s/pack/pack-%s.keep",
304-
get_object_directory(), sha1_to_hex(sha1));
305303
fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
306304
if (0 <= fd)
307305
return fd;
308306

309307
/* slow path */
310-
safe_create_leading_directories(name);
308+
safe_create_leading_directories_const(name);
311309
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
312310
}
313311

fast-import.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -940,41 +940,40 @@ static const char *create_index(void)
940940

941941
static char *keep_pack(const char *curr_index_name)
942942
{
943-
static char name[PATH_MAX];
944943
static const char *keep_msg = "fast-import";
944+
struct strbuf name = STRBUF_INIT;
945945
int keep_fd;
946946

947-
keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1);
947+
odb_pack_name(&name, pack_data->sha1, "keep");
948+
keep_fd = odb_pack_keep(name.buf);
948949
if (keep_fd < 0)
949950
die_errno("cannot create keep file");
950951
write_or_die(keep_fd, keep_msg, strlen(keep_msg));
951952
if (close(keep_fd))
952953
die_errno("failed to write keep file");
953954

954-
snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
955-
get_object_directory(), sha1_to_hex(pack_data->sha1));
956-
if (finalize_object_file(pack_data->pack_name, name))
955+
odb_pack_name(&name, pack_data->sha1, "pack");
956+
if (finalize_object_file(pack_data->pack_name, name.buf))
957957
die("cannot store pack file");
958958

959-
snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
960-
get_object_directory(), sha1_to_hex(pack_data->sha1));
961-
if (finalize_object_file(curr_index_name, name))
959+
odb_pack_name(&name, pack_data->sha1, "idx");
960+
if (finalize_object_file(curr_index_name, name.buf))
962961
die("cannot store index file");
963962
free((void *)curr_index_name);
964-
return name;
963+
return strbuf_detach(&name, NULL);
965964
}
966965

967966
static void unkeep_all_packs(void)
968967
{
969-
static char name[PATH_MAX];
968+
struct strbuf name = STRBUF_INIT;
970969
int k;
971970

972971
for (k = 0; k < pack_id; k++) {
973972
struct packed_git *p = all_packs[k];
974-
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
975-
get_object_directory(), sha1_to_hex(p->sha1));
976-
unlink_or_warn(name);
973+
odb_pack_name(&name, p->sha1, "keep");
974+
unlink_or_warn(name.buf);
977975
}
976+
strbuf_release(&name);
978977
}
979978

980979
static int loosen_small_pack(const struct packed_git *p)
@@ -1033,6 +1032,7 @@ static void end_packfile(void)
10331032
die("core git rejected index %s", idx_name);
10341033
all_packs[pack_id] = new_p;
10351034
install_packed_git(new_p);
1035+
free(idx_name);
10361036

10371037
/* Print the boundary */
10381038
if (pack_edges) {

git-compat-util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,6 @@ extern FILE *xfopen(const char *path, const char *mode);
798798
extern FILE *xfdopen(int fd, const char *mode);
799799
extern int xmkstemp(char *template);
800800
extern int xmkstemp_mode(char *template, int mode);
801-
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
802-
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
803801
extern char *xgetcwd(void);
804802
extern FILE *fopen_for_writing(const char *path);
805803

sha1_file.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,31 +203,26 @@ static const char *alt_sha1_path(struct alternate_object_database *alt,
203203
return buf->buf;
204204
}
205205

206-
/*
207-
* Return the name of the pack or index file with the specified sha1
208-
* in its filename. *base and *name are scratch space that must be
209-
* provided by the caller. which should be "pack" or "idx".
210-
*/
211-
static char *sha1_get_pack_name(const unsigned char *sha1,
212-
struct strbuf *buf,
213-
const char *which)
206+
char *odb_pack_name(struct strbuf *buf,
207+
const unsigned char *sha1,
208+
const char *ext)
214209
{
215210
strbuf_reset(buf);
216211
strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
217-
sha1_to_hex(sha1), which);
212+
sha1_to_hex(sha1), ext);
218213
return buf->buf;
219214
}
220215

221216
char *sha1_pack_name(const unsigned char *sha1)
222217
{
223218
static struct strbuf buf = STRBUF_INIT;
224-
return sha1_get_pack_name(sha1, &buf, "pack");
219+
return odb_pack_name(&buf, sha1, "pack");
225220
}
226221

227222
char *sha1_pack_index_name(const unsigned char *sha1)
228223
{
229224
static struct strbuf buf = STRBUF_INIT;
230-
return sha1_get_pack_name(sha1, &buf, "idx");
225+
return odb_pack_name(&buf, sha1, "idx");
231226
}
232227

233228
struct alternate_object_database *alt_odb_list;

0 commit comments

Comments
 (0)