From 82c9d6614bcd80bc2b6646f3943971fa0ec20135 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 16 Mar 2017 10:27:00 -0400 Subject: move odb_* declarations out of git-compat-util.h These functions were originally conceived as wrapper functions similar to xmkstemp(). They were later moved by 463db9b10 (wrapper: move odb_* to environment.c, 2010-11-06). The more appropriate place for a declaration is in cache.h. While we're at it, let's add some basic docstrings. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- cache.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'cache.h') diff --git a/cache.h b/cache.h index 61fc86e6d7..8b7b008704 100644 --- a/cache.h +++ b/cache.h @@ -1563,6 +1563,18 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1, extern void pack_report(void); +/* + * Create a temporary file rooted in the object database directory. + */ +extern int odb_mkstemp(char *template, size_t limit, const char *pattern); + +/* + * Create a pack .keep file in the object database's pack directory, for + * a pack with checksum "sha1". The return value is a file descriptor opened + * for writing, or -1 on error. The name of the keep file is written to "name". + */ +extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1); + /* * mmap the index file for the specified packfile (if it is not * already mmapped). Return 0 on success. -- cgit v1.2.3 From 1cec8c634fb76ecee862ff88066f55b63b7d5ff7 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 16 Mar 2017 10:27:06 -0400 Subject: sha1_file.c: make pack-name helper globally accessible We provide sha1_pack_name() and sha1_pack_index_name(), but the more generic form (which takes its own strbuf and an arbitrary extension) is only used to implement the other two. Let's make it available, but clean up a few things: 1. Name it odb_pack_name(), as the original sha1_get_pack_name() is long but not all that descriptive. 2. Switch the strbuf argument to the beginning, so that it matches similar path-building functions like git_path_buf(). 3. Clean up the out-dated docstring and move it to the public declaration. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- cache.h | 9 +++++++++ sha1_file.c | 17 ++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) (limited to 'cache.h') diff --git a/cache.h b/cache.h index 8b7b008704..baba4fe541 100644 --- a/cache.h +++ b/cache.h @@ -1568,6 +1568,15 @@ extern void pack_report(void); */ extern int odb_mkstemp(char *template, size_t limit, const char *pattern); +/* + * Generate the filename to be used for a pack file with checksum "sha1" and + * extension "ext". The result is written into the strbuf "buf", overwriting + * any existing contents. A pointer to buf->buf is returned as a convenience. + * + * Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx" + */ +extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext); + /* * Create a pack .keep file in the object database's pack directory, for * a pack with checksum "sha1". The return value is a file descriptor opened diff --git a/sha1_file.c b/sha1_file.c index ec957db5e1..df98c7f0dc 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -203,31 +203,26 @@ static const char *alt_sha1_path(struct alternate_object_database *alt, return buf->buf; } -/* - * Return the name of the pack or index file with the specified sha1 - * in its filename. *base and *name are scratch space that must be - * provided by the caller. which should be "pack" or "idx". - */ -static char *sha1_get_pack_name(const unsigned char *sha1, - struct strbuf *buf, - const char *which) + char *odb_pack_name(struct strbuf *buf, + const unsigned char *sha1, + const char *ext) { strbuf_reset(buf); strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(), - sha1_to_hex(sha1), which); + sha1_to_hex(sha1), ext); return buf->buf; } char *sha1_pack_name(const unsigned char *sha1) { static struct strbuf buf = STRBUF_INIT; - return sha1_get_pack_name(sha1, &buf, "pack"); + return odb_pack_name(&buf, sha1, "pack"); } char *sha1_pack_index_name(const unsigned char *sha1) { static struct strbuf buf = STRBUF_INIT; - return sha1_get_pack_name(sha1, &buf, "idx"); + return odb_pack_name(&buf, sha1, "idx"); } struct alternate_object_database *alt_odb_list; -- cgit v1.2.3 From eaeefc3276c45ff8f8c24775b7dd93155bef7d48 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 16 Mar 2017 10:27:12 -0400 Subject: 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 Signed-off-by: Junio C Hamano --- builtin/index-pack.c | 6 +++--- cache.h | 8 ++++---- environment.c | 6 ++---- fast-import.c | 4 +++- 4 files changed, 12 insertions(+), 12 deletions(-) (limited to 'cache.h') diff --git a/builtin/index-pack.c b/builtin/index-pack.c index f4b87c6c9f..a58bc6bee4 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1402,10 +1402,10 @@ static void final(const char *final_pack_name, const char *curr_pack_name, int keep_fd, keep_msg_len = strlen(keep_msg); if (!keep_name) - keep_fd = odb_pack_keep(name, sizeof(name), sha1); - else - keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600); + snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", + get_object_directory(), sha1_to_hex(sha1)); + keep_fd = odb_pack_keep(keep_name ? keep_name : name); if (keep_fd < 0) { if (errno != EEXIST) die_errno(_("cannot write keep file '%s'"), diff --git a/cache.h b/cache.h index baba4fe541..0c177b7acd 100644 --- a/cache.h +++ b/cache.h @@ -1578,11 +1578,11 @@ extern int odb_mkstemp(char *template, size_t limit, const char *pattern); extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext); /* - * Create a pack .keep file in the object database's pack directory, for - * a pack with checksum "sha1". The return value is a file descriptor opened - * for writing, or -1 on error. The name of the keep file is written to "name". + * Create a pack .keep file named "name" (which should generally be the output + * of odb_pack_name). Returns a file descriptor opened for writing, or -1 on + * error. */ -extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1); +extern int odb_pack_keep(const char *name); /* * mmap the index file for the specified packfile (if it is not diff --git a/environment.c b/environment.c index c07fb17fb7..4cd20d7067 100644 --- a/environment.c +++ b/environment.c @@ -296,18 +296,16 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern) return xmkstemp_mode(template, mode); } -int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1) +int odb_pack_keep(const char *name) { int fd; - snprintf(name, namesz, "%s/pack/pack-%s.keep", - get_object_directory(), sha1_to_hex(sha1)); fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600); if (0 <= fd) return fd; /* slow path */ - safe_create_leading_directories(name); + safe_create_leading_directories_const(name); return open(name, O_RDWR|O_CREAT|O_EXCL, 0600); } diff --git a/fast-import.c b/fast-import.c index 64fe602f0b..7f8371be3d 100644 --- a/fast-import.c +++ b/fast-import.c @@ -944,7 +944,9 @@ static char *keep_pack(const char *curr_index_name) static const char *keep_msg = "fast-import"; int keep_fd; - keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1); + snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", + get_object_directory(), sha1_to_hex(pack_data->sha1)); + keep_fd = odb_pack_keep(name); if (keep_fd < 0) die_errno("cannot create keep file"); write_or_die(keep_fd, keep_msg, strlen(keep_msg)); -- cgit v1.2.3