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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Mar 16, 2017 at 10:27 UTC
eaeefc3276c45ff8f8c24775b7dd93155bef7d48
4 files changed
+12
-12
builtin/index-pack.c
+3
-3
@@ -1402,10 +1402,10 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1402
int keep_fd, keep_msg_len = strlen(keep_msg);
1403
1404
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));
1407
1408
+ keep_fd = odb_pack_keep(keep_name ? keep_name : name);
1409
if (keep_fd < 0) {
1410
if (errno != EEXIST)
1411
die_errno(_("cannot write keep file '%s'"),
cache.h
+4
-4
@@ -1578,11 +1578,11 @@ extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
1578
extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
1579
1580
/*
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.
1584
*/
1585
-extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
1585
+extern int odb_pack_keep(const char *name);
1586
1587
/*
1588
* mmap the index file for the specified packfile (if it is not
environment.c
+2
-4
@@ -296,18 +296,16 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern)
296
return xmkstemp_mode(template, mode);
297
}
298
299
-int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1)
299
+int odb_pack_keep(const char *name)
300
{
301
int fd;
302
303
- snprintf(name, namesz, "%s/pack/pack-%s.keep",
304
- get_object_directory(), sha1_to_hex(sha1));
303
fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
304
if (0 <= fd)
305
return fd;
306
307
/* slow path */
310
- safe_create_leading_directories(name);
308
+ safe_create_leading_directories_const(name);
309
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
310
}
311
fast-import.c
+3
-1
@@ -944,7 +944,9 @@ static char *keep_pack(const char *curr_index_name)
944
static const char *keep_msg = "fast-import";
945
int keep_fd;
946
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);
950
if (keep_fd < 0)
951
die_errno("cannot create keep file");
952
write_or_die(keep_fd, keep_msg, strlen(keep_msg));