|
|
17b0f1 |
From d7b56e186521ce2e48e27edda121d780a3d62d27 Mon Sep 17 00:00:00 2001
|
|
|
17b0f1 |
From: Jan Synacek <jsynacek@redhat.com>
|
|
|
17b0f1 |
Date: Thu, 23 Nov 2017 08:53:50 +0100
|
|
|
17b0f1 |
Subject: [PATCH] fileio: add new helper call read_line() as bounded getline()
|
|
|
17b0f1 |
replacement
|
|
|
17b0f1 |
|
|
|
17b0f1 |
read_line() is much like getline(), and returns a line read from a
|
|
|
17b0f1 |
FILE*, of arbitrary sizes. In contrast to gets() it will grow the buffer
|
|
|
17b0f1 |
dynamically, and in contrast to getline() it will place a user-specified
|
|
|
17b0f1 |
boundary on the line.
|
|
|
17b0f1 |
|
|
|
17b0f1 |
(cherry-picked from commit 4f9a66a32dda1d9a28f9bb3fa31c2148524bc46a)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
Resolves: #1503106
|
|
|
17b0f1 |
---
|
|
|
17b0f1 |
src/shared/fileio.c | 77 ++++++++++++++++++++++++++++++++++++++++++
|
|
|
17b0f1 |
src/shared/fileio.h | 2 ++
|
|
|
17b0f1 |
src/test/test-fileio.c | 44 ++++++++++++++++++++++++
|
|
|
17b0f1 |
3 files changed, 123 insertions(+)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
|
|
|
17b0f1 |
index ff6b1a7ed7..1077375735 100644
|
|
|
17b0f1 |
--- a/src/shared/fileio.c
|
|
|
17b0f1 |
+++ b/src/shared/fileio.c
|
|
|
17b0f1 |
@@ -815,3 +815,80 @@ int get_status_field(const char *filename, const char *pattern, char **field) {
|
|
|
17b0f1 |
|
|
|
17b0f1 |
return 0;
|
|
|
17b0f1 |
}
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+int read_line(FILE *f, size_t limit, char **ret) {
|
|
|
17b0f1 |
+ _cleanup_free_ char *buffer = NULL;
|
|
|
17b0f1 |
+ size_t n = 0, allocated = 0, count = 0;
|
|
|
17b0f1 |
+ int r;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert(f);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ /* Something like a bounded version of getline().
|
|
|
17b0f1 |
+ *
|
|
|
17b0f1 |
+ * Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
|
|
|
17b0f1 |
+ * returned.
|
|
|
17b0f1 |
+ *
|
|
|
17b0f1 |
+ * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
|
|
|
17b0f1 |
+ * the number of characters in the returned string). When EOF is hit, 0 is returned.
|
|
|
17b0f1 |
+ *
|
|
|
17b0f1 |
+ * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
|
|
|
17b0f1 |
+ * delimiters. If the limit is hit we fail and return -ENOBUFS.
|
|
|
17b0f1 |
+ *
|
|
|
17b0f1 |
+ * If a line shall be skipped ret may be initialized as NULL. */
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (ret) {
|
|
|
17b0f1 |
+ if (!GREEDY_REALLOC(buffer, allocated, 1))
|
|
|
17b0f1 |
+ return -ENOMEM;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ flockfile(f);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ for (;;) {
|
|
|
17b0f1 |
+ int c;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (n >= limit) {
|
|
|
17b0f1 |
+ funlockfile(f);
|
|
|
17b0f1 |
+ return -ENOBUFS;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ errno = 0;
|
|
|
17b0f1 |
+ c = fgetc_unlocked(f);
|
|
|
17b0f1 |
+ if (c == EOF) {
|
|
|
17b0f1 |
+ /* if we read an error, and have no data to return, then propagate the error */
|
|
|
17b0f1 |
+ if (ferror_unlocked(f) && n == 0) {
|
|
|
17b0f1 |
+ r = errno > 0 ? -errno : -EIO;
|
|
|
17b0f1 |
+ funlockfile(f);
|
|
|
17b0f1 |
+ return r;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ break;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ count++;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
|
|
|
17b0f1 |
+ break;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (ret) {
|
|
|
17b0f1 |
+ if (!GREEDY_REALLOC(buffer, allocated, n + 2)) {
|
|
|
17b0f1 |
+ funlockfile(f);
|
|
|
17b0f1 |
+ return -ENOMEM;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ buffer[n] = (char) c;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ n++;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ funlockfile(f);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (ret) {
|
|
|
17b0f1 |
+ buffer[n] = 0;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ *ret = buffer;
|
|
|
17b0f1 |
+ buffer = NULL;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ return (int) count;
|
|
|
17b0f1 |
+}
|
|
|
17b0f1 |
diff --git a/src/shared/fileio.h b/src/shared/fileio.h
|
|
|
17b0f1 |
index 5ae51c1e28..f33464dce7 100644
|
|
|
17b0f1 |
--- a/src/shared/fileio.h
|
|
|
17b0f1 |
+++ b/src/shared/fileio.h
|
|
|
17b0f1 |
@@ -43,3 +43,5 @@ int write_env_file(const char *fname, char **l);
|
|
|
17b0f1 |
int executable_is_script(const char *path, char **interpreter);
|
|
|
17b0f1 |
|
|
|
17b0f1 |
int get_status_field(const char *filename, const char *pattern, char **field);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+int read_line(FILE *f, size_t limit, char **ret);
|
|
|
17b0f1 |
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
|
|
|
17b0f1 |
index 63e4a19b76..fc59693228 100644
|
|
|
17b0f1 |
--- a/src/test/test-fileio.c
|
|
|
17b0f1 |
+++ b/src/test/test-fileio.c
|
|
|
17b0f1 |
@@ -392,6 +392,49 @@ static void test_load_env_file_pairs(void) {
|
|
|
17b0f1 |
unlink(fn);
|
|
|
17b0f1 |
}
|
|
|
17b0f1 |
|
|
|
17b0f1 |
+static void test_read_line(void) {
|
|
|
17b0f1 |
+ _cleanup_fclose_ FILE *f = NULL;
|
|
|
17b0f1 |
+ _cleanup_free_ char *line = NULL;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ char buffer[] =
|
|
|
17b0f1 |
+ "Some test data\n"
|
|
|
17b0f1 |
+ "With newlines, and a NUL byte\0"
|
|
|
17b0f1 |
+ "\n"
|
|
|
17b0f1 |
+ "an empty line\n"
|
|
|
17b0f1 |
+ "an ignored line\n"
|
|
|
17b0f1 |
+ "and a very long line that is supposed to be truncated, because it is so long\n";
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ f = fmemopen(buffer, sizeof(buffer), "re");
|
|
|
17b0f1 |
+ assert_se(f);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
|
|
|
17b0f1 |
+ line = mfree(line);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte"));
|
|
|
17b0f1 |
+ line = mfree(line);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
|
|
|
17b0f1 |
+ line = mfree(line);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line"));
|
|
|
17b0f1 |
+ line = mfree(line);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, (size_t) -1, NULL) == 16);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, 16, &line) == -ENOBUFS);
|
|
|
17b0f1 |
+ line = mfree(line);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
|
|
|
17b0f1 |
+ * character after the previous limit. Let's make use of tha to continue our test. */
|
|
|
17b0f1 |
+ assert_se(read_line(f, 1024, &line) == 61 && streq(line, "line that is supposed to be truncated, because it is so long"));
|
|
|
17b0f1 |
+ line = mfree(line);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
|
|
|
17b0f1 |
+ line = mfree(line);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
|
|
|
17b0f1 |
+}
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
int main(int argc, char *argv[]) {
|
|
|
17b0f1 |
log_parse_environment();
|
|
|
17b0f1 |
log_open();
|
|
|
17b0f1 |
@@ -405,6 +448,7 @@ int main(int argc, char *argv[]) {
|
|
|
17b0f1 |
test_write_string_file();
|
|
|
17b0f1 |
test_write_string_file_no_create();
|
|
|
17b0f1 |
test_load_env_file_pairs();
|
|
|
17b0f1 |
+ test_read_line();
|
|
|
17b0f1 |
|
|
|
17b0f1 |
return 0;
|
|
|
17b0f1 |
}
|