Coverage Report

Created: 2025-06-22 06:56

/src/util-linux/include/all-io.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * No copyright is claimed.  This code is in the public domain; do with
3
 * it what you wish.
4
 *
5
 * Written by Karel Zak <kzak@redhat.com>
6
 *            Petr Uzel <petr.uzel@suse.cz>
7
 */
8
9
#ifndef UTIL_LINUX_ALL_IO_H
10
#define UTIL_LINUX_ALL_IO_H
11
12
#include <string.h>
13
#include <unistd.h>
14
#include <errno.h>
15
#include <sys/types.h>
16
#ifdef HAVE_SYS_SENDFILE_H
17
# include <sys/sendfile.h>
18
#endif
19
20
#include "c.h"
21
22
static inline int write_all(int fd, const void *buf, size_t count)
23
0
{
24
0
  while (count) {
25
0
    ssize_t tmp;
26
27
0
    errno = 0;
28
0
    tmp = write(fd, buf, count);
29
0
    if (tmp > 0) {
30
0
      count -= tmp;
31
0
      if (count)
32
0
        buf = (const void *) ((const char *) buf + tmp);
33
0
    } else if (errno != EINTR && errno != EAGAIN)
34
0
      return -1;
35
0
    if (errno == EAGAIN) /* Try later, *sigh* */
36
0
      xusleep(250000);
37
0
  }
38
0
  return 0;
39
0
}
Unexecuted instantiation: gen_uuid.c:write_all
Unexecuted instantiation: probe.c:write_all
Unexecuted instantiation: blkdev.c:write_all
Unexecuted instantiation: fileutils.c:write_all
Unexecuted instantiation: path.c:write_all
Unexecuted instantiation: sysfs.c:write_all
Unexecuted instantiation: canonicalize.c:write_all
Unexecuted instantiation: env.c:write_all
40
41
static inline int fwrite_all(const void *ptr, size_t size,
42
           size_t nmemb, FILE *stream)
43
0
{
44
0
  while (nmemb) {
45
0
    size_t tmp;
46
0
47
0
    errno = 0;
48
0
    tmp = fwrite(ptr, size, nmemb, stream);
49
0
    if (tmp > 0) {
50
0
      nmemb -= tmp;
51
0
      if (nmemb)
52
0
        ptr = (const void *) ((const char *) ptr + (tmp * size));
53
0
    } else if (errno != EINTR && errno != EAGAIN)
54
0
      return -1;
55
0
    if (errno == EAGAIN)  /* Try later, *sigh* */
56
0
      xusleep(250000);
57
0
  }
58
0
  return 0;
59
0
}
Unexecuted instantiation: gen_uuid.c:fwrite_all
Unexecuted instantiation: probe.c:fwrite_all
Unexecuted instantiation: blkdev.c:fwrite_all
Unexecuted instantiation: fileutils.c:fwrite_all
Unexecuted instantiation: path.c:fwrite_all
Unexecuted instantiation: sysfs.c:fwrite_all
Unexecuted instantiation: canonicalize.c:fwrite_all
Unexecuted instantiation: env.c:fwrite_all
60
61
static inline ssize_t read_all(int fd, char *buf, size_t count)
62
0
{
63
0
  ssize_t ret;
64
0
  ssize_t c = 0;
65
0
  int tries = 0;
66
67
0
  memset(buf, 0, count);
68
0
  while (count > 0) {
69
0
    ret = read(fd, buf, count);
70
0
    if (ret < 0) {
71
0
      if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) {
72
0
        xusleep(250000);
73
0
        continue;
74
0
      }
75
0
      return c ? c : -1;
76
0
    }
77
0
    if (ret == 0)
78
0
      return c;
79
0
    tries = 0;
80
0
    count -= ret;
81
0
    buf += ret;
82
0
    c += ret;
83
0
  }
84
0
  return c;
85
0
}
Unexecuted instantiation: gen_uuid.c:read_all
Unexecuted instantiation: probe.c:read_all
Unexecuted instantiation: blkdev.c:read_all
Unexecuted instantiation: fileutils.c:read_all
Unexecuted instantiation: path.c:read_all
Unexecuted instantiation: sysfs.c:read_all
Unexecuted instantiation: canonicalize.c:read_all
Unexecuted instantiation: env.c:read_all
86
87
static inline ssize_t read_all_alloc(int fd, char **buf)
88
0
{
89
0
  size_t size = 1024, c = 0;
90
0
  ssize_t ret;
91
92
0
  *buf = malloc(size);
93
0
  if (!*buf)
94
0
    return -1;
95
96
0
  while (1) {
97
0
    ret = read_all(fd, *buf + c, size - c);
98
0
    if (ret < 0) {
99
0
      free(*buf);
100
0
      *buf = NULL;
101
0
      return -1;
102
0
    }
103
104
0
    if (ret == 0)
105
0
      return c;
106
107
0
    c += ret;
108
0
    if (c == size) {
109
0
      size *= 2;
110
0
      *buf = realloc(*buf, size);
111
0
      if (!*buf)
112
0
        return -1;
113
0
    }
114
0
  }
115
0
}
Unexecuted instantiation: gen_uuid.c:read_all_alloc
Unexecuted instantiation: probe.c:read_all_alloc
Unexecuted instantiation: blkdev.c:read_all_alloc
Unexecuted instantiation: fileutils.c:read_all_alloc
Unexecuted instantiation: path.c:read_all_alloc
Unexecuted instantiation: sysfs.c:read_all_alloc
Unexecuted instantiation: canonicalize.c:read_all_alloc
Unexecuted instantiation: env.c:read_all_alloc
116
117
static inline ssize_t sendfile_all(int out, int in, off_t *off, size_t count)
118
0
{
119
0
#if defined(HAVE_SENDFILE) && defined(__linux__)
120
0
  ssize_t ret;
121
0
  ssize_t c = 0;
122
0
  int tries = 0;
123
0
  while (count) {
124
0
    ret = sendfile(out, in, off, count);
125
0
    if (ret < 0) {
126
0
      if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) {
127
0
        xusleep(250000);
128
0
        continue;
129
0
      }
130
0
      return c ? c : -1;
131
0
    }
132
0
    if (ret == 0)
133
0
      return c;
134
0
    tries = 0;
135
0
    count -= ret;
136
0
    c += ret;
137
0
  }
138
0
  return c;
139
#else
140
  errno = ENOSYS;
141
  return -1;
142
#endif
143
0
}
Unexecuted instantiation: gen_uuid.c:sendfile_all
Unexecuted instantiation: probe.c:sendfile_all
Unexecuted instantiation: blkdev.c:sendfile_all
Unexecuted instantiation: fileutils.c:sendfile_all
Unexecuted instantiation: path.c:sendfile_all
Unexecuted instantiation: sysfs.c:sendfile_all
Unexecuted instantiation: canonicalize.c:sendfile_all
Unexecuted instantiation: env.c:sendfile_all
144
#endif /* UTIL_LINUX_ALL_IO_H */