summaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2008-12-17 17:36:40 (GMT)
committerJunio C Hamano <gitster@pobox.com>2008-12-17 21:36:30 (GMT)
commitb11b7e13f48ee283738e21fea9f775cd40ca0562 (patch)
treef1c1a1a271f018865e1b95b5885cb71520da13a8 /strbuf.c
parent53682f0c9e7545c6fa891896a5de4ad441a76147 (diff)
downloadgit-b11b7e13f48ee283738e21fea9f775cd40ca0562.zip
git-b11b7e13f48ee283738e21fea9f775cd40ca0562.tar.gz
git-b11b7e13f48ee283738e21fea9f775cd40ca0562.tar.bz2
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 <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index 13be67e..bdf4954 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -288,6 +288,33 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
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;