2007/10/3, Kazuo TAKADA <<a href="mailto:kztakada@sm.sony.co.jp">kztakada@sm.sony.co.jp</a>>:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br><br>If the dd command fails in a sequence of copying, it always returns<br>EXIT_SUCCESS. So, I can't judge whether the command had succeeded or<br>failed.<br><br>Its behavior doesn't conform to POSIX.<br><br>
POSIX 1003.1:<br> <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/dd.html">http://www.opengroup.org/onlinepubs/009695399/utilities/dd.html</a><br> | EXIT STATUS<br> | The following exit values shall be returned:
<br> |<br> | 0<br> | The input file was copied successfully.<br> | >0<br> | An error occurred.<br><br>For example, copy some data to a small size device repeatly.<br><br>--------------------------------------------------
<br>(with a root account)<br># ./busybox dd if=/dev/zero of=/dev/ram<br>dd: writing '/dev/ram': No space left on device<br>32769+0 records in<br>32768+0 records out<br># echo $?<br>0 <= should be failed!!
<br><br># ./busybox dd if=/dev/zero of=/dev/ram count=1<br>1+0 records in<br>1+0 records out<br># echo $?<br>0<br><br># /bin/dd if=/dev/zero of=/dev/ram <= GNU's one<br>dd: writing to `/dev/ram': No space left on device
<br>32769+0 records in<br>32768+0 records out<br># echo $?<br>1<br>--------------------------------------------------<br><br>The old busybox-1.2.2.1 returns EXIT_FAILURE. It is the version that<br>dd had not reconstructed yet.
<br><br>Can you accept the patch below?<br>write_and_stats() is the function which returns a bool value.<br><br>----------------------------------------------------------------------<br>--- coreutils/dd.c.orig 2007-09-03 20:48:
39.000000000 +0900<br>+++ coreutils/dd.c 2007-10-03 10:31:50.000000000 +0900<br>@@ -106,7 +106,7 @@<br> #endif<br> };<br> size_t ibs = 512, obs = 512;<br>- ssize_t n, w;<br>+ ssize_t n, w = 0;
<br> char *ibuf, *obuf;<br> /* And these are all zeroed at once! */<br> struct {<br>@@ -303,13 +303,17 @@<br> tmp += d;<br> oc += d;<br> if (oc == obs) {
<br>- if (write_and_stats(ofd, obuf, obs, obs, outfile))<br>+ if (write_and_stats(ofd, obuf, obs, obs, outfile)) {<br>+ w = -1;
<br> goto out_status;<br>+ }<br> oc = 0;<br> }<br> }
<br>- } else if (write_and_stats(ofd, ibuf, n, obs, outfile))<br>+ } else if (write_and_stats(ofd, ibuf, n, obs, outfile)) {<br>+ w = -1;<br> goto out_status;
<br>+ }<br> }<br><br> if (ENABLE_FEATURE_DD_IBS_OBS && oc) {<br>@@ -330,5 +334,5 @@<br> out_status:<br> dd_output_status(0);<br><br>- return EXIT_SUCCESS;<br>+ return (w >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);
<br> }<br>----------------------------------------------------------------------</blockquote><div><br> It may be faster to do <br><br>if ((w = write_and_stats(...)))<br> goto out_status;<br><br> and<br><br>return w;<br>
<br> because write_and_stats returns 1 on failure. You can even tweak<br> it to return either EXIT_SUCCESS or EXIT_FAILURE and change the<br> if to:<br><br>if ((w = write_and_stats(...)) == EXIT_SUCCESS)<br> goto out_status;
<br><br> (which is the same but symbolically different).<br> </div><br></div> Loïc<br>