system/corennnnn
修訂 | 3faffd2687ddfeb114c60c2c605330698d23f39a (tree) |
---|---|
時間 | 2016-11-09 00:24:23 |
作者 | Paul Drews <paul.drews@inte...> |
Commiter | Chih-Wei Huang |
Use progressive offset in buffer for successive writes
The code for copying from one raw file to another looped
back and tried again with a suitably smaller number of bytes
if the entire buffer was not written on the first try.
However, it failed to advance an offset in the buffer past
the bytes that were written. This could conceivably happen
under rare circumstances, leading to a corrupted disk image.
This fix defines an offset into the buffer, advancing it by
the number of bytes written on each successive retry.
Signed-off-by: Paul Drews <paul.drews@intel.com>
Change-Id: I76b8e0b3c0d3b0c32bd2114d0867ac7489808279
@@ -36,6 +36,7 @@ write_raw_image(const char *dst, const char *src, loff_t offset, int test) | ||
36 | 36 | int dst_fd = -1; |
37 | 37 | int src_fd = -1; |
38 | 38 | uint8_t buffer[2048]; |
39 | + ssize_t buf_offset; | |
39 | 40 | ssize_t nr_bytes; |
40 | 41 | ssize_t tmp; |
41 | 42 | int done = 0; |
@@ -80,8 +81,9 @@ write_raw_image(const char *dst, const char *src, loff_t offset, int test) | ||
80 | 81 | if (test) |
81 | 82 | nr_bytes = 0; |
82 | 83 | |
84 | + buf_offset = 0; | |
83 | 85 | while (nr_bytes > 0) { |
84 | - if ((tmp = write(dst_fd, buffer, nr_bytes)) < 0) { | |
86 | + if ((tmp = write(dst_fd, &buffer[buf_offset], nr_bytes)) < 0) { | |
85 | 87 | /* XXX: Should we not even bother with EINTR? */ |
86 | 88 | if (errno == EINTR) |
87 | 89 | continue; |
@@ -91,6 +93,7 @@ write_raw_image(const char *dst, const char *src, loff_t offset, int test) | ||
91 | 93 | if (!tmp) |
92 | 94 | continue; |
93 | 95 | nr_bytes -= tmp; |
96 | + buf_offset += tmp; | |
94 | 97 | } |
95 | 98 | } |
96 | 99 |