summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGuillem Jover <guillem@debian.org>2018-01-13 14:46:46 +0100
committerGuillem Jover <guillem@debian.org>2018-08-30 03:14:08 +0200
commit7ebd4d381b439baa730ad941340b3a1ac129497c (patch)
tree70d159a76ed67b040e1e0681058003aa8d3cf820 /lib
parent2edd9981db1f572588b2493f81d67915183865e8 (diff)
downloaddpkg-7ebd4d381b439baa730ad941340b3a1ac129497c.tar.gz
libdpkg: Add new file_slurp() function
Diffstat (limited to 'lib')
-rw-r--r--lib/dpkg/file.c44
-rw-r--r--lib/dpkg/file.h5
-rw-r--r--lib/dpkg/t/.gitignore1
-rw-r--r--lib/dpkg/t/Makefile.am1
-rw-r--r--lib/dpkg/t/t-file.c96
5 files changed, 147 insertions, 0 deletions
diff --git a/lib/dpkg/file.c b/lib/dpkg/file.c
index 364f8aed6..c6a756ae3 100644
--- a/lib/dpkg/file.c
+++ b/lib/dpkg/file.c
@@ -33,6 +33,7 @@
#include <dpkg/i18n.h>
#include <dpkg/subproc.h>
#include <dpkg/command.h>
+#include <dpkg/fdio.h>
#include <dpkg/file.h>
/**
@@ -60,6 +61,49 @@ file_copy_perms(const char *src, const char *dst)
ohshite(_("unable to set mode of target file '%.250s'"), dst);
}
+static int
+file_slurp_fd(int fd, const char *filename, struct varbuf *vb,
+ struct dpkg_error *err)
+{
+ struct stat st;
+
+ if (fstat(fd, &st) < 0)
+ return dpkg_put_errno(err, _("cannot stat %s"), filename);
+
+ if (!S_ISREG(st.st_mode))
+ return dpkg_put_error(err, _("%s is not a regular file"),
+ filename);
+
+ if (st.st_size == 0)
+ return 0;
+
+ varbuf_init(vb, st.st_size);
+ if (fd_read(fd, vb->buf, st.st_size) < 0)
+ return dpkg_put_errno(err, _("cannot read %s"), filename);
+ vb->used = st.st_size;
+
+ return 0;
+}
+
+int
+file_slurp(const char *filename, struct varbuf *vb, struct dpkg_error *err)
+{
+ int fd;
+ int rc;
+
+ varbuf_init(vb, 0);
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return dpkg_put_errno(err, _("cannot open %s"), filename);
+
+ rc = file_slurp_fd(fd, filename, vb, err);
+
+ (void)close(fd);
+
+ return rc;
+}
+
static void
file_lock_setup(struct flock *fl, short type)
{
diff --git a/lib/dpkg/file.h b/lib/dpkg/file.h
index a1dd270ec..0f46cc07b 100644
--- a/lib/dpkg/file.h
+++ b/lib/dpkg/file.h
@@ -26,6 +26,8 @@
#include <stdbool.h>
#include <dpkg/macros.h>
+#include <dpkg/error.h>
+#include <dpkg/varbuf.h>
DPKG_BEGIN_DECLS
@@ -48,6 +50,9 @@ struct file_stat {
void file_copy_perms(const char *src, const char *dst);
+int
+file_slurp(const char *filename, struct varbuf *vb, struct dpkg_error *err);
+
enum file_lock_flags {
FILE_LOCK_NOWAIT,
FILE_LOCK_WAIT,
diff --git a/lib/dpkg/t/.gitignore b/lib/dpkg/t/.gitignore
index f88d0fa1c..6d5e0abd7 100644
--- a/lib/dpkg/t/.gitignore
+++ b/lib/dpkg/t/.gitignore
@@ -9,6 +9,7 @@ t-command
t-deb-version
t-ehandle
t-error
+t-file
t-macros
t-mod-db
t-path
diff --git a/lib/dpkg/t/Makefile.am b/lib/dpkg/t/Makefile.am
index b2f227569..e655d72d5 100644
--- a/lib/dpkg/t/Makefile.am
+++ b/lib/dpkg/t/Makefile.am
@@ -26,6 +26,7 @@ test_programs = \
t-ehandle \
t-error \
t-string \
+ t-file \
t-buffer \
t-path \
t-progname \
diff --git a/lib/dpkg/t/t-file.c b/lib/dpkg/t/t-file.c
new file mode 100644
index 000000000..db900a5e3
--- /dev/null
+++ b/lib/dpkg/t/t-file.c
@@ -0,0 +1,96 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-file.c - test file functions
+ *
+ * Copyright © 2018 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <dpkg/test.h>
+#include <dpkg/dpkg.h>
+#include <dpkg/file.h>
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+static const char ref_data[] =
+ "this is a test string\n"
+ "within a test file\n"
+ "containing multiple lines\n"
+;
+
+static void
+test_file_slurp(void)
+{
+ struct varbuf vb = VARBUF_INIT;
+ struct dpkg_error err = DPKG_ERROR_INIT;
+ char *test_file;
+ char *test_dir;
+ int fd;
+
+ test_pass(file_slurp("/nonexistent", &vb, &err) < 0);
+ test_pass(vb.used == 0);
+ test_pass(vb.buf == NULL);
+ test_pass(err.syserrno == ENOENT);
+ test_error(err);
+ varbuf_destroy(&vb);
+
+ test_dir = test_alloc(strdup("test.XXXXXX"));
+ test_pass(mkdtemp(test_dir) != NULL);
+ test_pass(file_slurp(test_dir, &vb, &err) < 0);
+ test_pass(vb.used == 0);
+ test_pass(vb.buf == NULL);
+ test_pass(err.syserrno == 0);
+ test_error(err);
+ varbuf_destroy(&vb);
+ test_pass(rmdir(test_dir) == 0);
+
+ test_file = test_alloc(strdup("test.XXXXXX"));
+ fd = mkstemp(test_file);
+ test_pass(fd >= 0);
+
+ test_pass(file_slurp(test_file, &vb, &err) == 0);
+ test_pass(vb.used == 0);
+ test_pass(vb.buf == NULL);
+ test_pass(err.syserrno == 0);
+ test_pass(err.type == DPKG_MSG_NONE);
+ varbuf_destroy(&vb);
+
+ test_pass(write(fd, ref_data, strlen(ref_data)) == (ssize_t)strlen(ref_data));
+ test_pass(lseek(fd, 0, SEEK_SET) == 0);
+
+ test_pass(file_slurp(test_file, &vb, &err) == 0);
+ test_pass(vb.used == strlen(ref_data));
+ test_str(vb.buf, ==, ref_data);
+ test_pass(err.syserrno == 0);
+ test_pass(err.type == DPKG_MSG_NONE);
+ varbuf_destroy(&vb);
+
+ test_pass(unlink(test_file) == 0);
+}
+
+TEST_ENTRY(test)
+{
+ test_plan(26);
+
+ test_file_slurp();
+}