Skip to content

Commit eaeefc3

Browse files
peffgitster
authored andcommitted
odb_pack_keep(): stop generating keepfile name
The odb_pack_keep() function generates the name of a .keep file and opens it. This has two problems: 1. It requires a fixed-size buffer to create the filename and doesn't notice when the result is truncated. 2. Of the two callers, one sometimes wants to open a filename it already has, which makes things awkward (it has to do so manually, and skips the leading-directory creation). Instead, let's have odb_pack_keep() just open the file. Generating the name isn't hard, and a future patch will switch callers over to odb_pack_name() anyway. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1cec8c6 commit eaeefc3

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

builtin/index-pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,10 +1402,10 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14021402
int keep_fd, keep_msg_len = strlen(keep_msg);
14031403

14041404
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);
1405+
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
1406+
get_object_directory(), sha1_to_hex(sha1));
14081407

1408+
keep_fd = odb_pack_keep(keep_name ? keep_name : name);
14091409
if (keep_fd < 0) {
14101410
if (errno != EEXIST)
14111411
die_errno(_("cannot write keep file '%s'"),

cache.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,11 +1578,11 @@ extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
15781578
extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
15791579

15801580
/*
1581-
* Create a pack .keep file in the object database's pack directory, for
1582-
* a pack with checksum "sha1". The return value is a file descriptor opened
1583-
* for writing, or -1 on error. The name of the keep file is written to "name".
1581+
* Create a pack .keep file named "name" (which should generally be the output
1582+
* of odb_pack_name). Returns a file descriptor opened for writing, or -1 on
1583+
* error.
15841584
*/
1585-
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
1585+
extern int odb_pack_keep(const char *name);
15861586

15871587
/*
15881588
* mmap the index file for the specified packfile (if it is not

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,9 @@ static char *keep_pack(const char *curr_index_name)
944944
static const char *keep_msg = "fast-import";
945945
int keep_fd;
946946

947-
keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1);
947+
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
948+
get_object_directory(), sha1_to_hex(pack_data->sha1));
949+
keep_fd = odb_pack_keep(name);
948950
if (keep_fd < 0)
949951
die_errno("cannot create keep file");
950952
write_or_die(keep_fd, keep_msg, strlen(keep_msg));

0 commit comments

Comments
 (0)