- static char pathname[PATH_MAX];
- unsigned long len;
- int fd;
- struct stat st;
- void *map;
-
- len = snprintf(pathname, PATH_MAX, "%s.%s", base_name, suffix);
- if (len >= PATH_MAX)
- die("bad pack base-name");
- fd = open(pathname, O_RDONLY);
- if (fd < 0 || fstat(fd, &st))
- die("unable to open '%s'", pathname);
- len = st.st_size;
- if (!len)
- die("bad pack file '%s'", pathname);
- map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
- if (-1 == (int)(long)map)
- die("unable to mmap '%s'", pathname);
- close(fd);
- *sizep = len;
- return map;
+ if (min <= len)
+ return buffer + offset;
+ if (eof)
+ die("unable to fill input");
+ if (min > sizeof(buffer))
+ die("cannot fill %d bytes", min);
+ if (offset) {
+ SHA1_Update(&ctx, buffer, offset);
+ memcpy(buffer, buffer + offset, len);
+ offset = 0;
+ }
+ do {
+ int ret = xread(0, buffer + len, sizeof(buffer) - len);
+ if (ret <= 0) {
+ if (!ret)
+ die("early EOF");
+ die("read error on input: %s", strerror(errno));
+ }
+ len += ret;
+ } while (len < min);
+ return buffer;