shared: add io-util to read data from a fd into a GString

This commit is contained in:
Antonio Cardace 2019-12-19 11:30:03 +01:00
parent 6d86f3b661
commit c0f1a657c3
2 changed files with 34 additions and 0 deletions

View file

@ -438,3 +438,35 @@ nm_utils_file_stat (const char *filename, struct stat *out_st)
return -NM_ERRNO_NATIVE (errno);
return 0;
}
/**
* nm_utils_fd_read:
* @fd: the fd to read from.
* @out_string: (out): output string where read bytes will be stored.
*
* Returns: <0 on failure, which is -(errno)
* 0 on EOF or if the call would block (if the fd is nonblocking),
* >0 on success, which is the number of bytes read */
ssize_t
nm_utils_fd_read (int fd, GString *out_string)
{
size_t start_len;
ssize_t n_read;
g_return_val_if_fail (fd >= 0, -1);
g_return_val_if_fail (out_string, -1);
start_len = out_string->len;
g_string_set_size (out_string, start_len + 1024);
n_read = read (fd, &out_string->str[start_len], 1024);
if (n_read < 0) {
if (errno != EAGAIN) {
return -NM_ERRNO_NATIVE (errno);
}
n_read = 0;
} else {
g_string_set_size (out_string, start_len + n_read);
}
return n_read;
}

View file

@ -47,6 +47,8 @@ gboolean nm_utils_file_set_contents (const char *filename,
int *out_errsv,
GError **error);
ssize_t nm_utils_fd_read (int fd, GString *out_string);
struct stat;
int nm_utils_file_stat (const char *filename, struct stat *out_st);