Fix potential data-loss in utils.WriteFile
authorIustin Pop <iustin@google.com>
Wed, 27 Apr 2011 11:45:57 +0000 (13:45 +0200)
committerIustin Pop <iustin@google.com>
Wed, 27 Apr 2011 15:26:25 +0000 (17:26 +0200)
os.write can do incomplete writes, as long as at least some bytes have
been written (like write(2)):

>>> os.write(fd, " " * 1300)
1300
>>> os.write(fd, " " * 1300)
1300
>>> os.write(fd, " " * 1300)
1300
>>> os.write(fd, " " * 1300)
980
>>> os.write(fd, " " * 1300)
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
OSError: [Errno 28] No space left on device

Note that incomplete write that only wrote 980 bytes, before the
exception.

To workaround this, we simply iterate until all data is
written. Unittests could be written by using a parameter instead of
hardcoding os.write and checking for incomplete writes.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

lib/utils/io.py

index d3a333f..a1ef785 100644 (file)
@@ -135,7 +135,13 @@ def WriteFile(file_name, fn=None, data=None,
       if callable(prewrite):
         prewrite(fd)
       if data is not None:
-        os.write(fd, data)
+        to_write = len(data)
+        offset = 0
+        while offset < to_write:
+          written = os.write(fd, buffer(data, offset))
+          assert written >= 0
+          offset += written
+        assert offset == to_write
       else:
         fn(fd)
       if callable(postwrite):