summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ca9e3b1)
raw | patch | inline | side by side (parent: ca9e3b1)
author | Rene Scharfe <rene.scharfe@lsrfire.ath.cx> | |
Fri, 11 Aug 2006 12:01:45 +0000 (14:01 +0200) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Fri, 11 Aug 2006 23:06:34 +0000 (16:06 -0700) |
As Fredrik points out the current interface of has_extension() is
potentially confusing. Its parameters include both a nul-terminated
string and a length-limited string.
This patch drops the length argument, requiring two nul-terminated
strings; all callsites are updated. I checked that all of them indeed
provide nul-terminated strings. Filenames need to be nul-terminated
anyway if they are to be passed to open() etc. The performance penalty
of the additional strlen() is negligible compared to the system calls
which inevitably surround has_extension() calls.
Additionally, change has_extension() to use size_t inside instead of
int, as that is the exact type strlen() returns and memcmp() expects.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <junkio@cox.net>
potentially confusing. Its parameters include both a nul-terminated
string and a length-limited string.
This patch drops the length argument, requiring two nul-terminated
strings; all callsites are updated. I checked that all of them indeed
provide nul-terminated strings. Filenames need to be nul-terminated
anyway if they are to be passed to open() etc. The performance penalty
of the additional strlen() is negligible compared to the system calls
which inevitably surround has_extension() calls.
Additionally, change has_extension() to use size_t inside instead of
int, as that is the exact type strlen() returns and memcmp() expects.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <junkio@cox.net>
builtin-help.c | patch | blob | history | |
git-compat-util.h | patch | blob | history | |
http-fetch.c | patch | blob | history | |
index-pack.c | patch | blob | history | |
local-fetch.c | patch | blob | history | |
refs.c | patch | blob | history | |
sha1_file.c | patch | blob | history | |
verify-pack.c | patch | blob | history |
diff --git a/builtin-help.c b/builtin-help.c
index 7a7f7759e592208b4604b6987bce8be46c936882..6484cb9df2651de7430ee608ef7d14a4d3ac4c99 100644 (file)
--- a/builtin-help.c
+++ b/builtin-help.c
continue;
entlen = strlen(de->d_name);
- if (has_extension(de->d_name, entlen, ".exe"))
+ if (has_extension(de->d_name, ".exe"))
entlen -= 4;
if (longest < entlen)
diff --git a/git-compat-util.h b/git-compat-util.h
index dd9209365253971c70db772929fb4541f1c609b2..b2e18954c03ff502053cb74d142faab7d2a8dacb 100644 (file)
--- a/git-compat-util.h
+++ b/git-compat-util.h
}
}
-static inline int has_extension(const char *filename, int len, const char *ext)
+static inline int has_extension(const char *filename, const char *ext)
{
- int extlen = strlen(ext);
+ size_t len = strlen(filename);
+ size_t extlen = strlen(ext);
return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
}
diff --git a/http-fetch.c b/http-fetch.c
index 6ea39f0589e74767be31eed87d2248370108b5f7..de5fc44e660e3eb6f52191dd0983744447a2fe46 100644 (file)
--- a/http-fetch.c
+++ b/http-fetch.c
if (strlen(ls->dentry_name) == 63 &&
!strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
- has_extension(ls->dentry_name, 63, ".pack")) {
+ has_extension(ls->dentry_name, ".pack")) {
get_sha1_hex(ls->dentry_name + 18, sha1);
setup_index(ls->repo, sha1);
}
diff --git a/index-pack.c b/index-pack.c
index a91e39ecd6469c04da92391bdb24e5a05691b617..b20659c2591f2669e03570c9809249b37ec58c38 100644 (file)
--- a/index-pack.c
+++ b/index-pack.c
usage(index_pack_usage);
if (!index_name) {
int len = strlen(pack_name);
- if (!has_extension(pack_name, len, ".pack"))
+ if (!has_extension(pack_name, ".pack"))
die("packfile name '%s' does not end with '.pack'",
pack_name);
index_name_buf = xmalloc(len);
diff --git a/local-fetch.c b/local-fetch.c
index b6ec170c015661ec602a6c2bd2b2f10754dbbd9c..7d01845d392ab891c8cfe9db9a218e4c2d70e53e 100644 (file)
--- a/local-fetch.c
+++ b/local-fetch.c
while ((de = readdir(dir)) != NULL) {
int namelen = strlen(de->d_name);
if (namelen != 50 ||
- !has_extension(de->d_name, namelen, ".pack"))
+ !has_extension(de->d_name, ".pack"))
continue;
get_sha1_hex(de->d_name + 5, sha1);
setup_index(sha1);
index b01835f634603f2f89c188bd56aae6fb45983ba3..28a93946050d9825ca86b833b6c77368a4bcc94b 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
namelen = strlen(de->d_name);
if (namelen > 255)
continue;
- if (has_extension(de->d_name, namelen, ".lock"))
+ if (has_extension(de->d_name, ".lock"))
continue;
memcpy(path + baselen, de->d_name, namelen+1);
if (stat(git_path("%s", path), &st) < 0)
diff --git a/sha1_file.c b/sha1_file.c
index a1bb01ca3569279318333f8088ade35614167612..3db956dd5c96c6563a9505775570fbdfb4a8b720 100644 (file)
--- a/sha1_file.c
+++ b/sha1_file.c
int namelen = strlen(de->d_name);
struct packed_git *p;
- if (!has_extension(de->d_name, namelen, ".idx"))
+ if (!has_extension(de->d_name, ".idx"))
continue;
/* we have .idx. Is it a file we can map? */
diff --git a/verify-pack.c b/verify-pack.c
index f440a39678b23ebb9d58115f0481875add21e981..357970da391da39eb24a3154923dabd1c9baea53 100644 (file)
--- a/verify-pack.c
+++ b/verify-pack.c
* In addition to "foo.idx" we accept "foo.pack" and "foo";
* normalize these forms to "foo.idx" for add_packed_git().
*/
- if (has_extension(arg, len, ".pack")) {
+ if (has_extension(arg, ".pack")) {
strcpy(arg + len - 5, ".idx");
len--;
- } else if (!has_extension(arg, len, ".idx")) {
+ } else if (!has_extension(arg, ".idx")) {
if (len + 4 >= PATH_MAX)
return error("name too long: %s.idx", arg);
strcpy(arg + len, ".idx");