git-p4: improve import performance by avoiding reallocations.
[git/git-bigfiles.git] / test-sha1.c
blob9b98d07c786b36f005f4b3ffd51926f3c1a343dd
1 #include "cache.h"
3 int main(int ac, char **av)
5 git_SHA_CTX ctx;
6 unsigned char sha1[20];
7 unsigned bufsz = 8192;
8 char *buffer;
10 if (ac == 2)
11 bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
13 if (!bufsz)
14 bufsz = 8192;
16 while ((buffer = malloc(bufsz)) == NULL) {
17 fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
18 bufsz /= 2;
19 if (bufsz < 1024)
20 die("OOPS");
23 git_SHA1_Init(&ctx);
25 while (1) {
26 ssize_t sz, this_sz;
27 char *cp = buffer;
28 unsigned room = bufsz;
29 this_sz = 0;
30 while (room) {
31 sz = xread(0, cp, room);
32 if (sz == 0)
33 break;
34 if (sz < 0)
35 die("test-sha1: %s", strerror(errno));
36 this_sz += sz;
37 cp += sz;
38 room -= sz;
40 if (this_sz == 0)
41 break;
42 git_SHA1_Update(&ctx, buffer, this_sz);
44 git_SHA1_Final(sha1, &ctx);
45 puts(sha1_to_hex(sha1));
46 exit(0);