Make 'index_path()' use 'strbuf_readlink()'

This makes us able to properly index symlinks even on filesystems where
st_size doesn't match the true size of the link.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Linus Torvalds 2008-12-17 09:51:53 -08:00 committed by Junio C Hamano
parent a60272b38e
commit b760d3aa74

View File

@ -2523,8 +2523,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object) int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
{ {
int fd; int fd;
char *target; struct strbuf sb = STRBUF_INIT;
size_t len;
switch (st->st_mode & S_IFMT) { switch (st->st_mode & S_IFMT) {
case S_IFREG: case S_IFREG:
@ -2537,20 +2536,17 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
path); path);
break; break;
case S_IFLNK: case S_IFLNK:
len = xsize_t(st->st_size); if (strbuf_readlink(&sb, path, st->st_size)) {
target = xmalloc(len + 1);
if (readlink(path, target, len + 1) != st->st_size) {
char *errstr = strerror(errno); char *errstr = strerror(errno);
free(target);
return error("readlink(\"%s\"): %s", path, return error("readlink(\"%s\"): %s", path,
errstr); errstr);
} }
if (!write_object) if (!write_object)
hash_sha1_file(target, len, blob_type, sha1); hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
else if (write_sha1_file(target, len, blob_type, sha1)) else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
return error("%s: failed to insert into database", return error("%s: failed to insert into database",
path); path);
free(target); strbuf_release(&sb);
break; break;
case S_IFDIR: case S_IFDIR:
return resolve_gitlink_ref(path, "HEAD", sha1); return resolve_gitlink_ref(path, "HEAD", sha1);