mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-11-02 03:11:12 +00:00
a178a4e7bf
We currently use safewrite from inside libvirt and don't want to publish any such function name. However, we do want to use it in applications like virsh, libvirtd and libvirt_proxy that link with libvirt. To that end, this change moves that function definition (along with the nearly identical saferead) into a new file, util-lib.c. To avoid maintaining separate copies of even such small functions, we simply include that new file from util.c. Then, the separate applications that need to use safewrite simply compile and link with util-lib.c. Of course, this does mean that each of those applications will containing two copies of these functions. However, the functions are so small that it's not worth worrying about that. * src/util.c (saferead, safewrite): Move function definitions to util-lib.c and include that .c file. * src/util-lib.c (saferead, safewrite): New file. Functions from src/util.c with slight change (s/int r =/ssize_t r =/) to reflect read/write return type. * src/util-lib.h: Declare the two moved functions. * src/util.h: Remove declarations. Include src/util-lib.h. * proxy/Makefile.am (libvirt_proxy_SOURCES): Add src/util-lib.c. * qemud/Makefile.am (libvirtd_SOURCES): Likewise. * src/Makefile.am (virsh_SOURCES): Add util-lib.c. Remove some SP-before-TAB.
53 lines
949 B
C
53 lines
949 B
C
/*
|
|
* common, generic utility functions
|
|
*
|
|
* Copyright (C) 2006, 2007, 2008 Red Hat, Inc.
|
|
* See COPYING.LIB for the License of this software
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
|
|
#include "util-lib.h"
|
|
|
|
/* Like read(), but restarts after EINTR */
|
|
int saferead(int fd, void *buf, size_t count)
|
|
{
|
|
size_t nread = 0;
|
|
while (count > 0) {
|
|
ssize_t r = read(fd, buf, count);
|
|
if (r < 0 && errno == EINTR)
|
|
continue;
|
|
if (r < 0)
|
|
return r;
|
|
if (r == 0)
|
|
return nread;
|
|
buf = (char *)buf + r;
|
|
count -= r;
|
|
nread += r;
|
|
}
|
|
return nread;
|
|
}
|
|
|
|
/* Like write(), but restarts after EINTR */
|
|
ssize_t safewrite(int fd, const void *buf, size_t count)
|
|
{
|
|
size_t nwritten = 0;
|
|
while (count > 0) {
|
|
ssize_t r = write(fd, buf, count);
|
|
|
|
if (r < 0 && errno == EINTR)
|
|
continue;
|
|
if (r < 0)
|
|
return r;
|
|
if (r == 0)
|
|
return nwritten;
|
|
buf = (const char *)buf + r;
|
|
count -= r;
|
|
nwritten += r;
|
|
}
|
|
return nwritten;
|
|
}
|