From: Jonas Fonseca <fonseca@diku.dk>
To: git@vger.kernel.org
Subject: [PATCH] Refactor sha1_pack_index_name and sha1_pack_name to use a common backend
Date: Mon, 28 Aug 2006 02:16:10 +0200 [thread overview]
Message-ID: <20060828001610.GC20904@diku.dk> (raw)
Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
---
Tested with doing a local cg-clone, since there doesn't seem to be any
tests of users, such as git-local-fetch, under t/.
This adds another pair of static buffers, if that's a problem and the
cleanup is still wanted I can change it to use malloc.
sha1_file.c | 64 +++++++++++++++++++++++------------------------------------
1 files changed, 25 insertions(+), 39 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index 01aa745..5a846f5 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -147,65 +147,51 @@ char *sha1_file_name(const unsigned char
return base;
}
-char *sha1_pack_name(const unsigned char *sha1)
+static int fill_sha1_pack_name(const unsigned char *sha1, char base[], size_t baselen,
+ int dir_offset, const char *extension)
{
static const char hex[] = "0123456789abcdef";
- static char *name, *base, *buf;
- static const char *last_objdir;
const char *sha1_file_directory = get_object_directory();
+ char *buf;
int i;
- if (!last_objdir || strcmp(last_objdir, sha1_file_directory)) {
- int len = strlen(sha1_file_directory);
- if (base)
- free(base);
- base = xmalloc(len + 60);
- sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
- name = base + len + 11;
- if (last_objdir)
- free((char *) last_objdir);
- last_objdir = strdup(sha1_file_directory);
+ base[dir_offset] = 0;
+ if (strcmp(base, sha1_file_directory)) {
+ dir_offset = strlen(sha1_file_directory);
+ if (snprintf(base, baselen,
+ "%s/pack/pack-1234567890123456789012345678901234567890.%s",
+ sha1_file_directory, extension) >= baselen)
+ die("pack name too long");
}
+ base[dir_offset] = '/';
- buf = name;
+ buf = base + dir_offset + 11;
for (i = 0; i < 20; i++) {
unsigned int val = *sha1++;
+
*buf++ = hex[val >> 4];
*buf++ = hex[val & 0xf];
}
-
- return base;
+
+ return dir_offset;
}
char *sha1_pack_index_name(const unsigned char *sha1)
{
- static const char hex[] = "0123456789abcdef";
- static char *name, *base, *buf;
- static const char *last_objdir;
- const char *sha1_file_directory = get_object_directory();
- int i;
+ static char base[PATH_MAX + 1];
+ static int offset;
- if (!last_objdir || strcmp(last_objdir, sha1_file_directory)) {
- int len = strlen(sha1_file_directory);
- if (base)
- free(base);
- base = xmalloc(len + 60);
- sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
- name = base + len + 11;
- if (last_objdir)
- free((char *) last_objdir);
- last_objdir = strdup(sha1_file_directory);
- }
+ offset = fill_sha1_pack_name(sha1, base, sizeof(base), offset, "idx");
+ return base;
+}
- buf = name;
+char *sha1_pack_name(const unsigned char *sha1)
+{
+ static char base[PATH_MAX + 1];
+ static int offset;
- for (i = 0; i < 20; i++) {
- unsigned int val = *sha1++;
- *buf++ = hex[val >> 4];
- *buf++ = hex[val & 0xf];
- }
-
+ offset = fill_sha1_pack_name(sha1, base, sizeof(base), offset, "pack");
return base;
}
--
1.4.2.g2f76-dirty
--
Jonas Fonseca
next reply other threads:[~2006-08-28 0:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-08-28 0:16 Jonas Fonseca [this message]
2006-08-28 4:40 ` [PATCH] Refactor sha1_pack_index_name and sha1_pack_name to use a common backend Junio C Hamano
2006-08-28 13:34 ` Jonas Fonseca
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20060828001610.GC20904@diku.dk \
--to=fonseca@diku.dk \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).