Skip to content

Commit e7be6b7

Browse files
committed
futils: provide an option to read a whole file by fd
1 parent b877122 commit e7be6b7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/util/futils.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,42 @@ int git_futils_readbuffer_fd(git_str *buf, git_file fd, size_t len)
179179
return 0;
180180
}
181181

182+
int git_futils_readbuffer_fd_full(git_str *buf, git_file fd)
183+
{
184+
static size_t blocksize = 10240;
185+
size_t alloc_len = 0, total_size = 0;
186+
ssize_t read_size = 0;
187+
188+
git_str_clear(buf);
189+
190+
while (true) {
191+
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, blocksize);
192+
193+
if (git_str_grow(buf, alloc_len) < 0)
194+
return -1;
195+
196+
/* p_read loops internally to read blocksize bytes */
197+
read_size = p_read(fd, buf->ptr, blocksize);
198+
199+
if (read_size < 0) {
200+
git_error_set(GIT_ERROR_OS, "failed to read descriptor");
201+
git_str_dispose(buf);
202+
return -1;
203+
}
204+
205+
total_size += read_size;
206+
207+
if ((size_t)read_size < blocksize) {
208+
break;
209+
}
210+
}
211+
212+
buf->ptr[total_size] = '\0';
213+
buf->size = total_size;
214+
215+
return 0;
216+
}
217+
182218
int git_futils_readbuffer_updated(
183219
git_str *out,
184220
const char *path,

src/util/futils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern int git_futils_readbuffer_updated(
2727
const char *path,
2828
unsigned char checksum[GIT_HASH_SHA1_SIZE],
2929
int *updated);
30+
extern int git_futils_readbuffer_fd_full(git_str *obj, git_file fd);
3031
extern int git_futils_readbuffer_fd(git_str *obj, git_file fd, size_t len);
3132

3233
/* Additional constants for `git_futils_writebuffer`'s `open_flags`. We

0 commit comments

Comments
 (0)