@matheusbsbs / matheus-bagliano / commits / 4d548150ac
Fork 0

Clean up sha1 file writing

This cleans up and future-proofs the sha1 file writing in sha1_file.c. In particular, instead of doing a simple "write()" call and just verifying that it succeeds (or - as in one place - just assuming it does), it uses "write_buffer()" to write data to the file descriptor while correctly checking for partial writes, EINTR etc. It also splits up write_sha1_to_fd() to be a lot more readable: if we need to re-create the compressed object, we do so in a separate helper function, making the logic a whole lot more modular and obvious. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 24, 2006 at 08:30 UTC 4d548150ace0816dd5fe678cdbde75b13d5e5249
1 file changed +78 -61
sha1_file.c
+78 -61
@@ -1399,6 +1399,25 @@ int move_temp_to_file(const char *tmpfile, char *filename)
1399 return 0;
1400 }
1401
1402 +static int write_buffer(int fd, const void *buf, size_t len)
1403 +{
1404 + while (len) {
1405 + ssize_t size;
1406 +
1407 + size = write(fd, buf, len);
1408 + if (!size)
1409 + return error("file write: disk full");
1410 + if (size < 0) {
1411 + if (errno == EINTR || errno == EAGAIN)
1412 + continue;
1413 + return error("file write error (%s)", strerror(errno));
1414 + }
1415 + len -= size;
1416 + buf += size;
1417 + }
1418 + return 0;
1419 +}
1420 +
1421 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1422 {
1423 int size;
@@ -1465,8 +1484,8 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
1484 deflateEnd(&stream);
1485 size = stream.total_out;
1486
1468 - if (write(fd, compressed, size) != size)
1469 - die("unable to write file");
1487 + if (write_buffer(fd, compressed, size) < 0)
1488 + die("unable to write sha1 file");
1489 fchmod(fd, 0444);
1490 close(fd);
1491 free(compressed);
@@ -1474,73 +1493,70 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
1493 return move_temp_to_file(tmpfile, filename);
1494 }
1495
1477 -int write_sha1_to_fd(int fd, const unsigned char *sha1)
1496 +/*
1497 + * We need to unpack and recompress the object for writing
1498 + * it out to a different file.
1499 + */
1500 +static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
1501 {
1479 - ssize_t size;
1480 - unsigned long objsize;
1481 - int posn = 0;
1482 - void *map = map_sha1_file_internal(sha1, &objsize);
1483 - void *buf = map;
1484 - void *temp_obj = NULL;
1502 + size_t size;
1503 z_stream stream;
1504 + unsigned char *unpacked;
1505 + unsigned long len;
1506 + char type[20];
1507 + char hdr[50];
1508 + int hdrlen;
1509 + void *buf;
1510
1487 - if (!buf) {
1488 - unsigned char *unpacked;
1489 - unsigned long len;
1490 - char type[20];
1491 - char hdr[50];
1492 - int hdrlen;
1493 - // need to unpack and recompress it by itself
1494 - unpacked = read_packed_sha1(sha1, type, &len);
1511 + // need to unpack and recompress it by itself
1512 + unpacked = read_packed_sha1(sha1, type, &len);
1513
1496 - hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1514 + hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1515
1498 - /* Set it up */
1499 - memset(&stream, 0, sizeof(stream));
1500 - deflateInit(&stream, Z_BEST_COMPRESSION);
1501 - size = deflateBound(&stream, len + hdrlen);
1502 - temp_obj = buf = xmalloc(size);
1503 -
1504 - /* Compress it */
1505 - stream.next_out = buf;
1506 - stream.avail_out = size;
1507 -
1508 - /* First header.. */
1509 - stream.next_in = (void *)hdr;
1510 - stream.avail_in = hdrlen;
1511 - while (deflate(&stream, 0) == Z_OK)
1512 - /* nothing */;
1516 + /* Set it up */
1517 + memset(&stream, 0, sizeof(stream));
1518 + deflateInit(&stream, Z_BEST_COMPRESSION);
1519 + size = deflateBound(&stream, len + hdrlen);
1520 + buf = xmalloc(size);
1521
1514 - /* Then the data itself.. */
1515 - stream.next_in = unpacked;
1516 - stream.avail_in = len;
1517 - while (deflate(&stream, Z_FINISH) == Z_OK)
1518 - /* nothing */;
1519 - deflateEnd(&stream);
1520 - free(unpacked);
1521 -
1522 - objsize = stream.total_out;
1523 - }
1522 + /* Compress it */
1523 + stream.next_out = buf;
1524 + stream.avail_out = size;
1525
1525 - do {
1526 - size = write(fd, buf + posn, objsize - posn);
1527 - if (size <= 0) {
1528 - if (!size) {
1529 - fprintf(stderr, "write closed\n");
1530 - } else {
1531 - perror("write ");
1532 - }
1533 - return -1;
1534 - }
1535 - posn += size;
1536 - } while (posn < objsize);
1526 + /* First header.. */
1527 + stream.next_in = (void *)hdr;
1528 + stream.avail_in = hdrlen;
1529 + while (deflate(&stream, 0) == Z_OK)
1530 + /* nothing */;
1531
1538 - if (map)
1539 - munmap(map, objsize);
1540 - if (temp_obj)
1541 - free(temp_obj);
1532 + /* Then the data itself.. */
1533 + stream.next_in = unpacked;
1534 + stream.avail_in = len;
1535 + while (deflate(&stream, Z_FINISH) == Z_OK)
1536 + /* nothing */;
1537 + deflateEnd(&stream);
1538 + free(unpacked);
1539
1543 - return 0;
1540 + *objsize = stream.total_out;
1541 + return buf;
1542 +}
1543 +
1544 +int write_sha1_to_fd(int fd, const unsigned char *sha1)
1545 +{
1546 + int retval;
1547 + unsigned long objsize;
1548 + void *buf = map_sha1_file_internal(sha1, &objsize);
1549 +
1550 + if (buf) {
1551 + retval = write_buffer(fd, buf, objsize);
1552 + munmap(buf, objsize);
1553 + return retval;
1554 + }
1555 +
1556 + buf = repack_object(sha1, &objsize);
1557 + retval = write_buffer(fd, buf, objsize);
1558 + free(buf);
1559 + return retval;
1560 }
1561
1562 int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
@@ -1579,7 +1595,8 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1595 SHA1_Update(&c, discard, sizeof(discard) -
1596 stream.avail_out);
1597 } while (stream.avail_in && ret == Z_OK);
1582 - write(local, buffer, *bufposn - stream.avail_in);
1598 + if (write_buffer(local, buffer, *bufposn - stream.avail_in) < 0)
1599 + die("unable to write sha1 file");
1600 memmove(buffer, buffer + *bufposn - stream.avail_in,
1601 stream.avail_in);
1602 *bufposn = stream.avail_in;