test-dump-cache-tree: avoid overflow of cache-tree name
[gitweb.git] / strbuf.c
index a7ba0281306e5dc271a446363946b6635a2dff15..f3c44fb3b33b1419c2d2952a74477467fa8f6c4d 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -364,19 +364,19 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 
        strbuf_grow(sb, hint ? hint : 8192);
        for (;;) {
-               ssize_t cnt;
+               ssize_t want = sb->alloc - sb->len - 1;
+               ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
 
-               cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
-               if (cnt < 0) {
+               if (got < 0) {
                        if (oldalloc == 0)
                                strbuf_release(sb);
                        else
                                strbuf_setlen(sb, oldlen);
                        return -1;
                }
-               if (!cnt)
+               sb->len += got;
+               if (got < want)
                        break;
-               sb->len += cnt;
                strbuf_grow(sb, 8192);
        }
 
@@ -526,9 +526,10 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
        return 0;
 }
 
-int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
+ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 {
-       int fd, len;
+       int fd;
+       ssize_t len;
 
        fd = open(path, O_RDONLY);
        if (fd < 0)
@@ -712,29 +713,42 @@ char *xstrfmt(const char *fmt, ...)
 
 void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
 {
+       size_t hint = 128;
        size_t len;
 
-       /*
-        * strftime reports "0" if it could not fit the result in the buffer.
-        * Unfortunately, it also reports "0" if the requested time string
-        * takes 0 bytes. So if we were to probe and grow, we have to choose
-        * some arbitrary cap beyond which we guess that the format probably
-        * just results in a 0-length output. Since we have to choose some
-        * reasonable cap anyway, and since it is not that big, we may
-        * as well just grow to their in the first place.
-        */
-       strbuf_grow(sb, 128);
+       if (!*fmt)
+               return;
+
+       strbuf_grow(sb, hint);
        len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
 
        if (!len) {
                /*
-                * Either we failed, or the format actually produces a 0-length
-                * output. There's not much we can do, so we leave it blank.
-                * However, the output array is left in an undefined state, so
-                * we must re-assert our NUL terminator.
+                * strftime reports "0" if it could not fit the result in the buffer.
+                * Unfortunately, it also reports "0" if the requested time string
+                * takes 0 bytes. So our strategy is to munge the format so that the
+                * output contains at least one character, and then drop the extra
+                * character before returning.
                 */
-               sb->buf[sb->len] = '\0';
-       } else {
-               sb->len += len;
+               struct strbuf munged_fmt = STRBUF_INIT;
+               strbuf_addf(&munged_fmt, "%s ", fmt);
+               while (!len) {
+                       hint *= 2;
+                       strbuf_grow(sb, hint);
+                       len = strftime(sb->buf + sb->len, sb->alloc - sb->len,
+                                      munged_fmt.buf, tm);
+               }
+               strbuf_release(&munged_fmt);
+               len--; /* drop munged space */
        }
+       strbuf_setlen(sb, sb->len + len);
+}
+
+void strbuf_add_unique_abbrev(struct strbuf *sb, const unsigned char *sha1,
+                             int abbrev_len)
+{
+       int r;
+       strbuf_grow(sb, GIT_SHA1_HEXSZ + 1);
+       r = find_unique_abbrev_r(sb->buf + sb->len, sha1, abbrev_len);
+       strbuf_setlen(sb, sb->len + r);
 }