summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorAntony Deepak Thomas <antonydeepak@gmail.com>2021-09-29 12:47:49 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-09-29 12:47:49 +0900
commit46a0f5cac8a62bc4d9dadebd26eb550253bc1d29 (patch)
treef69b467b74cb2fdb6339b4e47da90eda16c23e31 /src/basic/fileio.c
parent83455d0c8b40b12193cec4090c9256e8d3e9535c (diff)
downloadsystemd-46a0f5cac8a62bc4d9dadebd26eb550253bc1d29.tar.gz
fileio: introduce read_virtual_file_fd()
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 6a4a65723f..e5de493e13 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -375,9 +375,8 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
return 1;
}
-int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size) {
+int read_virtual_file_fd(int fd, size_t max_size, char **ret_contents, size_t *ret_size) {
_cleanup_free_ char *buf = NULL;
- _cleanup_close_ int fd = -1;
size_t n, size;
int n_retries;
bool truncated = false;
@@ -395,10 +394,7 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
* contents* may be returned. (Though the read is still done using one syscall.) Returns 0 on
* partial success, 1 if untruncated contents were read. */
- fd = open(filename, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
+ assert(fd >= 0);
assert(max_size <= READ_VIRTUAL_BYTES_MAX || max_size == SIZE_MAX);
/* Limit the number of attempts to read the number of bytes returned by fstat(). */
@@ -434,8 +430,8 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
n_retries--;
} else if (n_retries > 1) {
- /* Files in /proc are generally smaller than the page size so let's start with a page size
- * buffer from malloc and only use the max buffer on the final try. */
+ /* Files in /proc are generally smaller than the page size so let's start with
+ * a page size buffer from malloc and only use the max buffer on the final try. */
size = MIN3(page_size() - 1, READ_VIRTUAL_BYTES_MAX, max_size);
n_retries = 1;
} else {
@@ -524,6 +520,18 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
return !truncated;
}
+int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size) {
+ _cleanup_close_ int fd = -1;
+
+ assert(filename);
+
+ fd = open(filename, O_RDONLY | O_NOCTTY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+
+ return read_virtual_file_fd(fd, max_size, ret_contents, ret_size);
+}
+
int read_full_stream_full(
FILE *f,
const char *filename,