Files
shadow/lib/write_full.c
Alejandro Colomar f45498a6c2 libmisc/write_full.c: Improve write_full()
Documentation:

-  Correct the comment documenting the function:

   write_full() doesn't write "up to" count bytes (which is write(2)'s
   behavior, and exactly what this function is designed to avoid), but
   rather exactly count bytes (on success).

-  While fixing the documentation, take the time to add a man-page-like
   comment as in other APIs.  Especially, since we'll have to document
   a few other changes from this patch, such as the modified return
   values.

-  Partial writes are still possible on error.  It's the caller's
   responsibility to handle that possibility.

API:

-  In write(2), it's useful to know how many bytes were transferred,
   since it can have short writes.  In this API, since it either writes
   it all or fails, that value is useless, and callers only want to know
   if it succeeded or not.  Thus, just return 0 or -1.

Implementation:

-  Use `== -1` instead of `< 0` to check for write(2) syscall errors.
   This is wisdom from Michael Kerrisk.  This convention is useful
   because it more explicitly tells maintainers that the only value
   which can lead to that path is -1.  Otherwise, a maintainer of the
   code might be confused to think that other negative values are
   possible.  Keep it simple.

-  The path under `if (res == 0)` was unreachable, since the loop
   condition `while (count > 0)` precludes that possibility.  Remove the
   dead code.

-  Use a temporary variable of type `const char *` to avoid a cast.

-  Rename `res`, which just holds the result from write(2), to `w`,
   which more clearly shows that it's just a very-short-lived variable
   (by it's one-letter name), and also relates itself more to write(2).
   I find it more readable.

-  Move the definition of `w` to the top of the function.  Now that the
   function is significantly shorter, the lifetime of the variable is
   clearer, and I find it more readable this way.

Use:

-  Also use `== -1` to check errors.

Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2023-08-18 20:35:15 -05:00

78 lines
1.5 KiB
C

/*
* SPDX-FileCopyrightText: 2023, Christian Göttsche <cgzones@googlemail.com>
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <config.h>
#ident "$Id$"
#include <errno.h>
#include <unistd.h>
#include "prototypes.h"
/*
* SYNOPSIS
* int write_full(int fd, const void *buf, size_t count);
*
* ARGUMENTS
* fd File descriptor.
* buf Source buffer to write(2) into 'fd'.
* count Size of 'buf'.
*
* DESCRIPTION
* Write 'count' bytes from the buffer starting at 'buf' to the
* file referred to by 'fd'.
*
* This function is similar to write(2), except that it retries
* in case of a short write.
*
* Since this function either performs a full write, or fails, the
* return value is simpler than for write(2).
*
* RETURN VALUE
* 0 On success.
* -1 On error.
*
* ERRORS
* See write(2).
*
* CAVEATS
* This function can still perform partial writes: if the function
* fails in the loop after one or more write(2) calls have
* succeeded, it will report a failure, but some data may have been
* written. In such a case, it's the caller's responsibility to
* make sure that the partial write is not problematic, and
* remediate it if it is --maybe by trying to remove the file--.
*/
int
write_full(int fd, const void *buf, size_t count)
{
ssize_t w;
const unsigned char *p;
p = buf;
while (count > 0) {
w = write(fd, p, count);
if (w == -1) {
if (errno == EINTR)
continue;
return -1;
}
p += w;
count -= w;
}
return 0;
}