summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 53682f0)
raw | patch | inline | side by side (parent: 53682f0)
author | Linus Torvalds <torvalds@linux-foundation.org> | |
Wed, 17 Dec 2008 17:36:40 +0000 (09:36 -0800) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Wed, 17 Dec 2008 21:36:30 +0000 (13:36 -0800) |
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 <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
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 <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-apply.c | patch | blob | history | |
strbuf.c | patch | blob | history | |
strbuf.h | patch | blob | history |
diff --git a/builtin-apply.c b/builtin-apply.c
index 4c4d1e1774ade358dbc3283df5d1b764a32821b8..07244b0736047d6cfe67db7f059f7755906579f0 100644 (file)
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -1559,10 +1559,8 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
{
switch (st->st_mode & S_IFMT) {
case S_IFLNK:
- strbuf_grow(buf, st->st_size);
- if (readlink(path, buf->buf, st->st_size) != st->st_size)
- return -1;
- strbuf_setlen(buf, st->st_size);
+ if (strbuf_readlink(buf, path, st->st_size) < 0)
+ return error("unable to read symlink %s", path);
return 0;
case S_IFREG:
if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
diff --git a/strbuf.c b/strbuf.c
index 13be67e4d3e38472adbff019a34e3f5ce9621dd7..bdf49544d47b97475800b104ed9efe8f846db265 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
return sb->len - oldlen;
}
+#define STRBUF_MAXLINK (2*PATH_MAX)
+
+int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
+{
+ if (hint < 32)
+ hint = 32;
+
+ while (hint < STRBUF_MAXLINK) {
+ int len;
+
+ strbuf_grow(sb, hint);
+ len = readlink(path, sb->buf, hint);
+ if (len < 0) {
+ if (errno != ERANGE)
+ break;
+ } else if (len < hint) {
+ strbuf_setlen(sb, len);
+ return 0;
+ }
+
+ /* .. the buffer was too small - try again */
+ hint *= 2;
+ }
+ strbuf_release(sb);
+ return -1;
+}
+
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
{
int ch;
diff --git a/strbuf.h b/strbuf.h
index b1670d99456e96b5de01918e157e10df519cae13..89bd36e15a541e268117ff023afc6a43137cdd6e 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
/* XXX: if read fails, any partial read is undone */
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
+extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
extern int strbuf_getline(struct strbuf *, FILE *, int);