This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch master
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=7ebd4d381b439baa730ad941340b3a1ac129497c

commit 7ebd4d381b439baa730ad941340b3a1ac129497c
Author: Guillem Jover <guil...@debian.org>
AuthorDate: Sat Jan 13 14:46:46 2018 +0100

    libdpkg: Add new file_slurp() function
---
 debian/changelog       |  1 +
 lib/dpkg/file.c        | 44 +++++++++++++++++++++++
 lib/dpkg/file.h        |  5 +++
 lib/dpkg/t/.gitignore  |  1 +
 lib/dpkg/t/Makefile.am |  1 +
 lib/dpkg/t/t-file.c    | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 148 insertions(+)

diff --git a/debian/changelog b/debian/changelog
index 4d0f1d143..12f817c00 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -195,6 +195,7 @@ dpkg (1.19.1) UNRELEASED; urgency=medium
     - Switch from strchr() + strlen() to strchrnul().
     - libdpkg: Change dpkg_error to track errno values.
     - libdpkg: Add new varbuf_new() and varbuf_free() functions.
+    - libdpkg: Add new file_slurp() function.
   * Build system:
     - Set distribution tarball format to ustar, instead of default v7 format.
     - Mark PO4A and POD2MAN as precious variables.
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 <guil...@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();
+}

-- 
Dpkg.Org's dpkg

Reply via email to