Cleanup xread() loops to use read_in_full()
authorHeikki Orsila <heikki.orsila@iki.fi>
Sat, 3 May 2008 13:27:26 +0000 (16:27 +0300)
committerJunio C Hamano <gitster@pobox.com>
Sun, 4 May 2008 05:15:25 +0000 (22:15 -0700)
Signed-off-by: Heikki Orsila <heikki.orsila@iki.fi>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
combine-diff.c
pack-write.c
pkt-line.c
sha1_file.c

index 41aaf83dd315746ae26d1c31ddd78b43c0cb6663..588c58bc55dfe78998d40a4dcce2a7b6ea6f7f5a 100644 (file)
@@ -701,7 +701,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
                else if (0 <= (fd = open(elem->path, O_RDONLY)) &&
                         !fstat(fd, &st)) {
                        size_t len = xsize_t(st.st_size);
-                       size_t sz = 0;
+                       ssize_t done;
                        int is_file, i;
 
                        elem->mode = canon_mode(st.st_mode);
@@ -716,14 +716,13 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 
                        result_size = len;
                        result = xmalloc(len + 1);
-                       while (sz < len) {
-                               ssize_t done = xread(fd, result+sz, len-sz);
-                               if (done == 0 && sz != len)
-                                       die("early EOF '%s'", elem->path);
-                               else if (done < 0)
-                                       die("read error '%s'", elem->path);
-                               sz += done;
-                       }
+
+                       done = read_in_full(fd, result, len);
+                       if (done < 0)
+                               die("read error '%s'", elem->path);
+                       else if (done < len)
+                               die("early EOF '%s'", elem->path);
+
                        result[len] = 0;
                }
                else {
index 665e2b29b8817aa6a8aa83ccd859ab51e7bb2234..c66c8af725f6d37b4edaeab75eebac7a387f4922 100644 (file)
@@ -183,7 +183,6 @@ void fixup_pack_header_footer(int pack_fd,
 
 char *index_pack_lockfile(int ip_out)
 {
-       int len, s;
        char packname[46];
 
        /*
@@ -193,11 +192,8 @@ char *index_pack_lockfile(int ip_out)
         * case, we need it to remove the corresponding .keep file
         * later on.  If we don't get that then tough luck with it.
         */
-       for (len = 0;
-                len < 46 && (s = xread(ip_out, packname+len, 46-len)) > 0;
-                len += s);
-       if (len == 46 && packname[45] == '\n' &&
-               memcmp(packname, "keep\t", 5) == 0) {
+       if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
+           memcmp(packname, "keep\t", 5) == 0) {
                char path[PATH_MAX];
                packname[45] = 0;
                snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
index 355546a1ad844492234dc7ee91528c525af5610a..f5d00863a6234c16db33637d19fefd2014780e87 100644 (file)
@@ -65,16 +65,11 @@ void packet_write(int fd, const char *fmt, ...)
 
 static void safe_read(int fd, void *buffer, unsigned size)
 {
-       size_t n = 0;
-
-       while (n < size) {
-               ssize_t ret = xread(fd, (char *) buffer + n, size - n);
-               if (ret < 0)
-                       die("read error (%s)", strerror(errno));
-               if (!ret)
-                       die("The remote end hung up unexpectedly");
-               n += ret;
-       }
+       ssize_t ret = read_in_full(fd, buffer, size);
+       if (ret < 0)
+               die("read error (%s)", strerror(errno));
+       else if (ret < size)
+               die("The remote end hung up unexpectedly");
 }
 
 int packet_read_line(int fd, char *buffer, unsigned size)
index c2ab7ea11d7a838c54e5cc8f91ae06a3ee0a2e67..3516777bc7614c23da02dd7a9aa28a48294d56cb 100644 (file)
@@ -2466,16 +2466,10 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
 
 int read_pack_header(int fd, struct pack_header *header)
 {
-       char *c = (char*)header;
-       ssize_t remaining = sizeof(struct pack_header);
-       do {
-               ssize_t r = xread(fd, c, remaining);
-               if (r <= 0)
-                       /* "eof before pack header was fully read" */
-                       return PH_ERROR_EOF;
-               remaining -= r;
-               c += r;
-       } while (remaining > 0);
+       if (read_in_full(fd, header, sizeof(*header)) < sizeof(*header))
+               /* "eof before pack header was fully read" */
+               return PH_ERROR_EOF;
+
        if (header->hdr_signature != htonl(PACK_SIGNATURE))
                /* "protocol error (pack signature mismatch detected)" */
                return PH_ERROR_PACK_SIGNATURE;