aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-25 21:06:59 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-29 18:19:53 +0200
commit4b4cee0c0c99f531a45157fd27f5441e511db109 (patch)
tree825aae023f27c2f578536af35dfd01b1dc96e18e
parentd87bffd89b9c0a26a65f0c629250fa87902b6cb8 (diff)
Replace file/getline usage with istream
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--bin/gensquashfs/Makemodule.am2
-rw-r--r--bin/gensquashfs/mkfs.c10
-rw-r--r--configure.ac2
-rw-r--r--include/compat.h6
-rw-r--r--include/fstree.h5
-rw-r--r--lib/compat/Makemodule.am2
-rw-r--r--lib/compat/getline.c60
-rw-r--r--lib/fstree/fstree_from_file.c76
-rw-r--r--tests/Makemodule.am10
-rw-r--r--tests/fstree_from_file.c11
-rw-r--r--tests/fstree_fuzz.c13
-rw-r--r--tests/str_table.c29
12 files changed, 44 insertions, 182 deletions
diff --git a/bin/gensquashfs/Makemodule.am b/bin/gensquashfs/Makemodule.am
index a9ae5b6..e030009 100644
--- a/bin/gensquashfs/Makemodule.am
+++ b/bin/gensquashfs/Makemodule.am
@@ -1,7 +1,7 @@
gensquashfs_SOURCES = bin/gensquashfs/mkfs.c bin/gensquashfs/mkfs.h
gensquashfs_SOURCES += bin/gensquashfs/options.c bin/gensquashfs/selinux.c
gensquashfs_SOURCES += bin/gensquashfs/dirscan.c bin/gensquashfs/dirscan_xattr.c
-gensquashfs_LDADD = libcommon.a libsquashfs.la libfstree.a
+gensquashfs_LDADD = libcommon.a libsquashfs.la libfstree.a libfstream.a
gensquashfs_LDADD += libcompat.a $(LZO_LIBS) $(PTHREAD_LIBS)
gensquashfs_CPPFLAGS = $(AM_CPPFLAGS)
gensquashfs_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
diff --git a/bin/gensquashfs/mkfs.c b/bin/gensquashfs/mkfs.c
index 66e10a9..ce23e6f 100644
--- a/bin/gensquashfs/mkfs.c
+++ b/bin/gensquashfs/mkfs.c
@@ -147,17 +147,9 @@ static int relabel_tree_dfs(const char *filename, sqfs_xattr_writer_t *xwr,
static int read_fstree(fstree_t *fs, options_t *opt, sqfs_xattr_writer_t *xwr,
void *selinux_handle)
{
- FILE *fp;
int ret;
- fp = fopen(opt->infile, "rb");
- if (fp == NULL) {
- perror(opt->infile);
- return -1;
- }
-
- ret = fstree_from_file(fs, opt->infile, fp);
- fclose(fp);
+ ret = fstree_from_file(fs, opt->infile);
if (ret == 0 && selinux_handle != NULL)
ret = relabel_tree_dfs(opt->cfg.filename, xwr,
diff --git a/configure.ac b/configure.ac
index 70a1dfd..e5038d0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -257,7 +257,7 @@ AC_CHECK_HEADERS([sys/xattr.h], [], [])
AC_CHECK_HEADERS([sys/sysinfo.h], [], [])
AC_CHECK_HEADERS([alloca.h], [], [])
-AC_CHECK_FUNCS([strndup getline getsubopt])
+AC_CHECK_FUNCS([strndup getsubopt])
##### generate output #####
diff --git a/include/compat.h b/include/compat.h
index 343dcf5..111168f 100644
--- a/include/compat.h
+++ b/include/compat.h
@@ -176,12 +176,6 @@ void w32_perror(const char *str);
#endif
#endif
-#ifndef HAVE_GETLINE
-#include <stdio.h>
-
-ssize_t getline(char **line, size_t *n, FILE *fp);
-#endif
-
#ifndef HAVE_STRNDUP
char *strndup(const char *str, size_t max_len);
#endif
diff --git a/include/fstree.h b/include/fstree.h
index 6628540..884ff51 100644
--- a/include/fstree.h
+++ b/include/fstree.h
@@ -153,15 +153,12 @@ tree_node_t *fstree_add_generic(fstree_t *fs, const char *path,
tree from it. File input paths are interpreted as relative to the current
working directory.
- Data is read from the given file pointer. The filename is only used for
- producing error messages.
-
On failure, an error report with filename and line number is written
to stderr.
Returns 0 on success.
*/
-int fstree_from_file(fstree_t *fs, const char *filename, FILE *fp);
+int fstree_from_file(fstree_t *fs, const char *filename);
/*
This function performs all the necessary post processing steps on the file
diff --git a/lib/compat/Makemodule.am b/lib/compat/Makemodule.am
index c1be522..4f4fc9c 100644
--- a/lib/compat/Makemodule.am
+++ b/lib/compat/Makemodule.am
@@ -1,4 +1,4 @@
-libcompat_a_SOURCES = lib/compat/getline.c lib/compat/getsubopt.c
+libcompat_a_SOURCES = lib/compat/getsubopt.c
libcompat_a_SOURCES += lib/compat/strndup.c lib/compat/mockups.c
libcompat_a_SOURCES += lib/compat/chdir.c include/compat.h
libcompat_a_SOURCES += lib/compat/path_to_windows.c
diff --git a/lib/compat/getline.c b/lib/compat/getline.c
deleted file mode 100644
index e63c50e..0000000
--- a/lib/compat/getline.c
+++ /dev/null
@@ -1,60 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * getline.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "config.h"
-#include "compat.h"
-
-#include <string.h>
-#include <stdlib.h>
-
-#ifndef HAVE_GETLINE
-ssize_t getline(char **line, size_t *n, FILE *fp)
-{
- size_t new_cap, len = 0, cap = 0;
- char *buffer = NULL, *new;
- int c;
-
- if (feof(fp) || ferror(fp))
- return -1;
-
- do {
- c = fgetc(fp);
-
- if (ferror(fp))
- return -1;
-
- if (c == EOF)
- c = '\n';
-
- if (len == cap) {
- new_cap = cap ? cap * 2 : 32;
- new = realloc(buffer, new_cap);
-
- if (new == NULL)
- return -1;
-
- buffer = new;
- cap = new_cap;
- }
-
- buffer[len++] = c;
- } while (c != '\n');
-
- if (len == cap) {
- new = realloc(buffer, cap ? cap * 2 : 32);
- if (new == NULL)
- return -1;
-
- buffer = new;
- }
-
- buffer[len] = '\0';
-
- *line = buffer;
- *n = len;
- return len;
-}
-#endif
diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c
index bf11755..678d565 100644
--- a/lib/fstree/fstree_from_file.c
+++ b/lib/fstree/fstree_from_file.c
@@ -7,12 +7,12 @@
#include "config.h"
#include "fstree.h"
+#include "fstream.h"
#include <stdlib.h>
-#include <stdint.h>
#include <string.h>
-#include <ctype.h>
#include <errno.h>
+#include <ctype.h>
static int add_generic(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *sb, const char *extra)
@@ -93,28 +93,6 @@ static const struct {
#define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0]))
-static void trim_line(char *line)
-{
- size_t i;
-
- for (i = 0; isspace(line[i]); ++i)
- ;
-
- if (line[i] == '#') {
- line[0] = '\0';
- return;
- }
-
- if (i > 0)
- memmove(line, line + i, strlen(line + i) + 1);
-
- i = strlen(line);
- while (i > 0 && isspace(line[i - 1]))
- --i;
-
- line[i] = '\0';
-}
-
static int handle_line(fstree_t *fs, const char *filename,
size_t line_num, char *line)
{
@@ -279,44 +257,38 @@ out_desc:
return -1;
}
-int fstree_from_file(fstree_t *fs, const char *filename, FILE *fp)
+int fstree_from_file(fstree_t *fs, const char *filename)
{
- size_t n, line_num = 0;
- ssize_t ret;
+ size_t line_num = 1;
+ istream_t *fp;
char *line;
+ int ret;
- for (;;) {
- line = NULL;
- n = 0;
- errno = 0;
-
- ret = getline(&line, &n, fp);
- ++line_num;
-
- if (ret < 0) {
- if (errno == 0) {
- free(line);
- break;
- }
-
- perror(filename);
- goto fail_line;
- }
-
- trim_line(line);
+ fp = istream_open_file(filename);
+ if (fp == NULL)
+ return -1;
- if (line[0] == '\0') {
- free(line);
- continue;
+ for (;;) {
+ ret = istream_get_line(fp, &line, &line_num,
+ ISTREAM_LINE_LTRIM | ISTREAM_LINE_SKIP_EMPTY);
+ if (ret < 0)
+ return -1;
+ if (ret > 0)
+ break;
+
+ if (line[0] != '#') {
+ if (handle_line(fs, filename, line_num, line))
+ goto fail_line;
}
- if (handle_line(fs, filename, line_num, line))
- goto fail_line;
-
free(line);
+ ++line_num;
}
+
+ sqfs_destroy(fp);
return 0;
fail_line:
free(line);
+ sqfs_destroy(fp);
return -1;
}
diff --git a/tests/Makemodule.am b/tests/Makemodule.am
index 58cd27e..8d12d40 100644
--- a/tests/Makemodule.am
+++ b/tests/Makemodule.am
@@ -2,7 +2,7 @@ test_canonicalize_name_SOURCES = tests/canonicalize_name.c tests/test.h
test_canonicalize_name_LDADD = libfstree.a
test_str_table_SOURCES = tests/str_table.c tests/test.h
-test_str_table_LDADD = libutil.a libcompat.a
+test_str_table_LDADD = libutil.a libfstream.a libcompat.a
test_str_table_CPPFLAGS = $(AM_CPPFLAGS) -DTESTPATH=$(top_srcdir)/tests
test_str_table_CPPFLAGS += -I$(top_srcdir)/lib/sqfs
@@ -45,15 +45,15 @@ test_get_path_LDADD = libfstree.a libcompat.a
test_fstree_sort_SOURCES = tests/fstree_sort.c tests/test.h
test_fstree_sort_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/lib/fstree
-test_fstree_sort_LDADD = libfstree.a libcompat.a
+test_fstree_sort_LDADD = libfstree.a libfstream.a libcompat.a
test_fstree_from_file_SOURCES = tests/fstree_from_file.c tests/test.h
test_fstree_from_file_CPPFLAGS = $(AM_CPPFLAGS) -DTESTPATH=$(top_srcdir)/tests/fstree1.txt
-test_fstree_from_file_LDADD = libfstree.a libcompat.a
+test_fstree_from_file_LDADD = libfstree.a libfstream.a libcompat.a
test_fstree_init_SOURCES = tests/fstree_init.c tests/test.h
test_fstree_init_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/lib/fstree
-test_fstree_init_LDADD = libfstree.a libcompat.a
+test_fstree_init_LDADD = libfstree.a libfstream.a libcompat.a
test_filename_sane_SOURCES = tests/filename_sane.c lib/fstree/filename_sane.c
@@ -225,7 +225,7 @@ test_tar_xattr_schily_bin_CPPFLAGS += -DTESTPATH=$(top_srcdir)/tests/tar
test_tar_xattr_schily_bin_CPPFLAGS += -DTESTFILE=xattr/xattr-schily-binary.tar
fstree_fuzz_SOURCES = tests/fstree_fuzz.c
-fstree_fuzz_LDADD = libfstree.a libcompat.a
+fstree_fuzz_LDADD = libfstree.a libfstream.a libcompat.a
tar_fuzz_SOURCES = tests/tar_fuzz.c
tar_fuzz_LDADD = libtar.a libfstream.a libcompat.a
diff --git a/tests/fstree_from_file.c b/tests/fstree_from_file.c
index c00cabf..f8f0892 100644
--- a/tests/fstree_from_file.c
+++ b/tests/fstree_from_file.c
@@ -9,21 +9,13 @@
#include "fstree.h"
#include "test.h"
-#define STR(x) #x
-#define STRVALUE(x) STR(x)
-
-#define TEST_PATH STRVALUE(TESTPATH)
-
int main(void)
{
tree_node_t *n;
fstree_t fs;
- FILE *fp;
-
- fp = test_open_read(TEST_PATH);
TEST_ASSERT(fstree_init(&fs, NULL) == 0);
- TEST_ASSERT(fstree_from_file(&fs, "testfile", fp) == 0);
+ TEST_ASSERT(fstree_from_file(&fs, TEST_PATH) == 0);
fstree_post_process(&fs);
n = fs.root->data.dir.children;
@@ -92,7 +84,6 @@ int main(void)
TEST_STR_EQUAL(n->name, "sock");
TEST_NULL(n->next);
- fclose(fp);
fstree_cleanup(&fs);
return EXIT_SUCCESS;
}
diff --git a/tests/fstree_fuzz.c b/tests/fstree_fuzz.c
index 42d8942..9618814 100644
--- a/tests/fstree_fuzz.c
+++ b/tests/fstree_fuzz.c
@@ -15,29 +15,20 @@ int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
fstree_t fs;
- FILE *fp;
if (argc != 2) {
fputs("Usage: fstree_fuzz <input_file>\n", stderr);
return EXIT_FAILURE;
}
- fp = fopen(argv[1], "r");
- if (fp == NULL) {
- perror(argv[1]);
- return EXIT_FAILURE;
- }
-
if (fstree_init(&fs, NULL))
- goto out_fp;
+ return EXIT_FAILURE;
- if (fstree_from_file(&fs, argv[1], fp))
+ if (fstree_from_file(&fs, argv[1]))
goto out_fs;
ret = EXIT_SUCCESS;
out_fs:
fstree_cleanup(&fs);
-out_fp:
- fclose(fp);
return ret;
}
diff --git a/tests/str_table.c b/tests/str_table.c
index 0935c89..7bcf4cc 100644
--- a/tests/str_table.c
+++ b/tests/str_table.c
@@ -7,46 +7,31 @@
#include "config.h"
#include "str_table.h"
+#include "fstream.h"
#include "compat.h"
#include "test.h"
-#define STR(x) #x
-#define STRVALUE(x) STR(x)
-
-#define TEST_PATH STRVALUE(TESTPATH)
-
static char *strings[1000];
static int read_strings(void)
{
+ istream_t *fp;
ssize_t ret;
char *line;
- size_t n;
- FILE *fp;
int i;
- fp = test_open_read("words.txt");
+ fp = istream_open_file("words.txt");
+ TEST_NOT_NULL(fp);
for (i = 0; i < 1000; ++i) {
- line = NULL;
- n = 0;
-
- ret = getline(&line, &n, fp);
- if (ret < 0) {
- perror("reading words");
- goto fail;
- }
+ ret = istream_get_line(fp, &line, NULL, 0);
+ TEST_EQUAL_I(ret, 0);
strings[i] = line;
}
- fclose(fp);
+ sqfs_destroy(fp);
return 0;
-fail:
- for (i = 0; i < 1000; ++i)
- free(strings[i]);
- fclose(fp);
- return -1;
}
int main(void)