Skip to content

Commit 17e4748

Browse files
dvlasenkdcantrell
authored andcommitted
[lib] add full_write() utility function
Signed-off-by: Denys Vlasenko <[email protected]>
1 parent 6302f0a commit 17e4748

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

include/rpminspect.h

+3
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ void dump_cfg(const struct rpminspect *);
322322
void *read_file_bytes(const char *path, off_t *len);
323323
string_list_t *read_file(const char *);
324324

325+
/* io.c */
326+
ssize_t full_write(int fd, const void *buf, size_t len);
327+
325328
/* release.c */
326329
char *read_release(const rpmfile_t *);
327330
const char *get_before_rel(struct rpminspect *);

lib/io.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright The rpminspect Project Authors
3+
* SPDX-License-Identifier: LGPL-3.0-or-later
4+
*/
5+
6+
#include <unistd.h>
7+
8+
/*
9+
* Write *all* of the supplied buffer out to a fd.
10+
* Do multiple writes if necessary.
11+
* Returns the amount written, or -1 if error was seen
12+
* on the very first write.
13+
* The write will be incomplete only if an error occurs
14+
* on one of subsequent writes.
15+
*/
16+
ssize_t full_write(int fd, const void *buf, size_t len)
17+
{
18+
ssize_t total = 0;
19+
20+
while (len != 0) {
21+
ssize_t cc = write(fd, buf, len);
22+
23+
if (cc < 0) {
24+
if (total) {
25+
/* we already wrote some! */
26+
/* user can do another write to know the error code */
27+
return total;
28+
}
29+
30+
return cc; /* write() returns -1 on failure. */
31+
}
32+
33+
total += cc;
34+
buf = ((const char *)buf) + cc;
35+
len -= cc;
36+
}
37+
38+
return total;
39+
}

lib/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ librpminspect_sources = [
7777
'inspect_upstream.c',
7878
'inspect_virus.c',
7979
'inspect_xml.c',
80+
'io.c',
8081
'joinpath.c',
8182
'koji.c',
8283
'listfuncs.c',

0 commit comments

Comments
 (0)