Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
8520a98d | 2 | #include "cache.h" |
5cea57f3 | 3 | #include "debug.h" |
8520a98d | 4 | #include "strbuf.h" |
e7f01d1e | 5 | #include <linux/kernel.h> |
8520a98d | 6 | #include <linux/string.h> |
7f7c536f | 7 | #include <linux/zalloc.h> |
a43783ae | 8 | #include <errno.h> |
8520a98d | 9 | #include <stdio.h> |
215a0d30 | 10 | #include <stdlib.h> |
91854f9a | 11 | #include <unistd.h> |
07800601 | 12 | |
07800601 IM |
13 | /* |
14 | * Used as the default ->buf value, so that people can always assume | |
15 | * buf is non NULL and ->buf is NUL terminated even for a freshly | |
16 | * initialized strbuf. | |
17 | */ | |
18 | char strbuf_slopbuf[1]; | |
19 | ||
5cea57f3 | 20 | int strbuf_init(struct strbuf *sb, ssize_t hint) |
07800601 IM |
21 | { |
22 | sb->alloc = sb->len = 0; | |
23 | sb->buf = strbuf_slopbuf; | |
24 | if (hint) | |
5cea57f3 MH |
25 | return strbuf_grow(sb, hint); |
26 | return 0; | |
07800601 IM |
27 | } |
28 | ||
29 | void strbuf_release(struct strbuf *sb) | |
30 | { | |
31 | if (sb->alloc) { | |
74cf249d | 32 | zfree(&sb->buf); |
07800601 IM |
33 | strbuf_init(sb, 0); |
34 | } | |
35 | } | |
36 | ||
37 | char *strbuf_detach(struct strbuf *sb, size_t *sz) | |
38 | { | |
39 | char *res = sb->alloc ? sb->buf : NULL; | |
40 | if (sz) | |
41 | *sz = sb->len; | |
42 | strbuf_init(sb, 0); | |
43 | return res; | |
44 | } | |
45 | ||
5cea57f3 | 46 | int strbuf_grow(struct strbuf *sb, size_t extra) |
07800601 | 47 | { |
5cea57f3 MH |
48 | char *buf; |
49 | size_t nr = sb->len + extra + 1; | |
50 | ||
51 | if (nr < sb->alloc) | |
52 | return 0; | |
53 | ||
54 | if (nr <= sb->len) | |
55 | return -E2BIG; | |
56 | ||
57 | if (alloc_nr(sb->alloc) > nr) | |
58 | nr = alloc_nr(sb->alloc); | |
59 | ||
60 | /* | |
61 | * Note that sb->buf == strbuf_slopbuf if sb->alloc == 0, and it is | |
62 | * a static variable. Thus we have to avoid passing it to realloc. | |
63 | */ | |
64 | buf = realloc(sb->alloc ? sb->buf : NULL, nr * sizeof(*buf)); | |
65 | if (!buf) | |
66 | return -ENOMEM; | |
67 | ||
68 | sb->buf = buf; | |
69 | sb->alloc = nr; | |
70 | return 0; | |
07800601 IM |
71 | } |
72 | ||
5cea57f3 | 73 | int strbuf_addch(struct strbuf *sb, int c) |
0741208a | 74 | { |
5cea57f3 MH |
75 | int ret = strbuf_grow(sb, 1); |
76 | if (ret) | |
77 | return ret; | |
78 | ||
0741208a ACM |
79 | sb->buf[sb->len++] = c; |
80 | sb->buf[sb->len] = '\0'; | |
5cea57f3 | 81 | return 0; |
0741208a ACM |
82 | } |
83 | ||
5cea57f3 | 84 | int strbuf_add(struct strbuf *sb, const void *data, size_t len) |
07800601 | 85 | { |
5cea57f3 MH |
86 | int ret = strbuf_grow(sb, len); |
87 | if (ret) | |
88 | return ret; | |
89 | ||
07800601 | 90 | memcpy(sb->buf + sb->len, data, len); |
5cea57f3 | 91 | return strbuf_setlen(sb, sb->len + len); |
07800601 IM |
92 | } |
93 | ||
5cea57f3 | 94 | static int strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap) |
07800601 | 95 | { |
5cea57f3 | 96 | int len, ret; |
c7118369 | 97 | va_list ap_saved; |
07800601 | 98 | |
5cea57f3 MH |
99 | if (!strbuf_avail(sb)) { |
100 | ret = strbuf_grow(sb, 64); | |
101 | if (ret) | |
102 | return ret; | |
103 | } | |
c7118369 NK |
104 | |
105 | va_copy(ap_saved, ap); | |
f787d951 | 106 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); |
ce49d843 SS |
107 | if (len < 0) { |
108 | va_end(ap_saved); | |
5cea57f3 | 109 | return len; |
ce49d843 | 110 | } |
07800601 | 111 | if (len > strbuf_avail(sb)) { |
5cea57f3 | 112 | ret = strbuf_grow(sb, len); |
ce49d843 SS |
113 | if (ret) { |
114 | va_end(ap_saved); | |
5cea57f3 | 115 | return ret; |
ce49d843 | 116 | } |
c7118369 | 117 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved); |
07800601 | 118 | if (len > strbuf_avail(sb)) { |
5cea57f3 | 119 | pr_debug("this should not happen, your vsnprintf is broken"); |
ce49d843 | 120 | va_end(ap_saved); |
5cea57f3 | 121 | return -EINVAL; |
07800601 IM |
122 | } |
123 | } | |
ce49d843 | 124 | va_end(ap_saved); |
5cea57f3 | 125 | return strbuf_setlen(sb, sb->len + len); |
07800601 IM |
126 | } |
127 | ||
5cea57f3 | 128 | int strbuf_addf(struct strbuf *sb, const char *fmt, ...) |
c7118369 NK |
129 | { |
130 | va_list ap; | |
5cea57f3 | 131 | int ret; |
c7118369 NK |
132 | |
133 | va_start(ap, fmt); | |
5cea57f3 | 134 | ret = strbuf_addv(sb, fmt, ap); |
c7118369 | 135 | va_end(ap); |
5cea57f3 | 136 | return ret; |
c7118369 NK |
137 | } |
138 | ||
f37a291c | 139 | ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint) |
07800601 IM |
140 | { |
141 | size_t oldlen = sb->len; | |
142 | size_t oldalloc = sb->alloc; | |
5cea57f3 MH |
143 | int ret; |
144 | ||
145 | ret = strbuf_grow(sb, hint ? hint : 8192); | |
146 | if (ret) | |
147 | return ret; | |
07800601 | 148 | |
07800601 IM |
149 | for (;;) { |
150 | ssize_t cnt; | |
151 | ||
152 | cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); | |
153 | if (cnt < 0) { | |
154 | if (oldalloc == 0) | |
155 | strbuf_release(sb); | |
156 | else | |
157 | strbuf_setlen(sb, oldlen); | |
5cea57f3 | 158 | return cnt; |
07800601 IM |
159 | } |
160 | if (!cnt) | |
161 | break; | |
162 | sb->len += cnt; | |
5cea57f3 MH |
163 | ret = strbuf_grow(sb, 8192); |
164 | if (ret) | |
165 | return ret; | |
07800601 IM |
166 | } |
167 | ||
168 | sb->buf[sb->len] = '\0'; | |
169 | return sb->len - oldlen; | |
170 | } |