Skip to content

Commit b11b7e1

Browse files
torvaldsgitster
authored andcommitted
Add generic 'strbuf_readlink()' helper function
It was already what 'git apply' did in read_old_data(), just export it as a real function, and make it be more generic. In particular, this handles the case of the lstat() st_size data not matching the readlink() return value properly (which apparently happens at least on NTFS under Linux). But as a result of this you could also use the new function without even knowing how big the link is going to be, and it will allocate an appropriately sized buffer. So we pass in the st_size of the link as just a hint, rather than a fixed requirement. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 53682f0 commit b11b7e1

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

builtin-apply.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,10 +1559,8 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
15591559
{
15601560
switch (st->st_mode & S_IFMT) {
15611561
case S_IFLNK:
1562-
strbuf_grow(buf, st->st_size);
1563-
if (readlink(path, buf->buf, st->st_size) != st->st_size)
1564-
return -1;
1565-
strbuf_setlen(buf, st->st_size);
1562+
if (strbuf_readlink(buf, path, st->st_size) < 0)
1563+
return error("unable to read symlink %s", path);
15661564
return 0;
15671565
case S_IFREG:
15681566
if (strbuf_read_file(buf, path, st->st_size) != st->st_size)

strbuf.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,33 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
288288
return sb->len - oldlen;
289289
}
290290

291+
#define STRBUF_MAXLINK (2*PATH_MAX)
292+
293+
int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
294+
{
295+
if (hint < 32)
296+
hint = 32;
297+
298+
while (hint < STRBUF_MAXLINK) {
299+
int len;
300+
301+
strbuf_grow(sb, hint);
302+
len = readlink(path, sb->buf, hint);
303+
if (len < 0) {
304+
if (errno != ERANGE)
305+
break;
306+
} else if (len < hint) {
307+
strbuf_setlen(sb, len);
308+
return 0;
309+
}
310+
311+
/* .. the buffer was too small - try again */
312+
hint *= 2;
313+
}
314+
strbuf_release(sb);
315+
return -1;
316+
}
317+
291318
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
292319
{
293320
int ch;

strbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
124124
/* XXX: if read fails, any partial read is undone */
125125
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
126126
extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
127+
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
127128

128129
extern int strbuf_getline(struct strbuf *, FILE *, int);
129130

0 commit comments

Comments
 (0)