Skip to content

Commit b760d3a

Browse files
torvaldsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a60272b commit b760d3a

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

sha1_file.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,8 +2523,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
25232523
int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
25242524
{
25252525
int fd;
2526-
char *target;
2527-
size_t len;
2526+
struct strbuf sb = STRBUF_INIT;
25282527

25292528
switch (st->st_mode & S_IFMT) {
25302529
case S_IFREG:
@@ -2537,20 +2536,17 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
25372536
path);
25382537
break;
25392538
case S_IFLNK:
2540-
len = xsize_t(st->st_size);
2541-
target = xmalloc(len + 1);
2542-
if (readlink(path, target, len + 1) != st->st_size) {
2539+
if (strbuf_readlink(&sb, path, st->st_size)) {
25432540
char *errstr = strerror(errno);
2544-
free(target);
25452541
return error("readlink(\"%s\"): %s", path,
25462542
errstr);
25472543
}
25482544
if (!write_object)
2549-
hash_sha1_file(target, len, blob_type, sha1);
2550-
else if (write_sha1_file(target, len, blob_type, sha1))
2545+
hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
2546+
else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
25512547
return error("%s: failed to insert into database",
25522548
path);
2553-
free(target);
2549+
strbuf_release(&sb);
25542550
break;
25552551
case S_IFDIR:
25562552
return resolve_gitlink_ref(path, "HEAD", sha1);

0 commit comments

Comments
 (0)