This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=5d2363cd96a1ac2719ac70739b6d0aa277b7a07d commit 5d2363cd96a1ac2719ac70739b6d0aa277b7a07d Author: Guillem Jover <[email protected]> AuthorDate: Fri Oct 21 01:50:32 2022 +0200 libdpkg: Add new varbuf_add_dir() function Add new functions that take care of adding a trailing «/» when there is none present yet. We have this pattern in the codebase but with unconditional addition of «/» which is not ideal. --- lib/dpkg/libdpkg.map | 1 + lib/dpkg/t/t-varbuf.c | 44 +++++++++++++++++++++++++++++++++++++++++++- lib/dpkg/varbuf.c | 8 ++++++++ lib/dpkg/varbuf.h | 1 + 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map index a44dd39af..6f07fd682 100644 --- a/lib/dpkg/libdpkg.map +++ b/lib/dpkg/libdpkg.map @@ -116,6 +116,7 @@ LIBDPKG_PRIVATE { varbuf_dup_char; varbuf_map_char; varbuf_add_buf; + varbuf_add_dir; varbuf_get_str; varbuf_end_str; varbuf_printf; diff --git a/lib/dpkg/t/t-varbuf.c b/lib/dpkg/t/t-varbuf.c index 24165cf6d..736bfe9dd 100644 --- a/lib/dpkg/t/t-varbuf.c +++ b/lib/dpkg/t/t-varbuf.c @@ -241,6 +241,47 @@ test_varbuf_map_char(void) varbuf_destroy(&vb); } +static void +test_varbuf_add_dir(void) +{ + struct varbuf vb; + + varbuf_init(&vb, 10); + + varbuf_add_dir(&vb, ""); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/"); + varbuf_add_dir(&vb, ""); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/"); + varbuf_add_dir(&vb, "aa"); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/aa/"); + varbuf_add_dir(&vb, ""); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/aa/"); + + varbuf_reset(&vb); + + varbuf_add_dir(&vb, "/foo/bar"); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/foo/bar/"); + + varbuf_reset(&vb); + + varbuf_add_dir(&vb, "/foo/bar/"); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/foo/bar/"); + varbuf_add_dir(&vb, "quux"); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/foo/bar/quux/"); + varbuf_add_dir(&vb, "zoo"); + varbuf_end_str(&vb); + test_str(vb.buf, ==, "/foo/bar/quux/zoo/"); + + varbuf_destroy(&vb); +} + static void test_varbuf_end_str(void) { @@ -392,7 +433,7 @@ test_varbuf_detach(void) TEST_ENTRY(test) { - test_plan(130); + test_plan(138); test_varbuf_init(); test_varbuf_prealloc(); @@ -403,6 +444,7 @@ TEST_ENTRY(test) test_varbuf_add_char(); test_varbuf_dup_char(); test_varbuf_map_char(); + test_varbuf_add_dir(); test_varbuf_end_str(); test_varbuf_get_str(); test_varbuf_printf(); diff --git a/lib/dpkg/varbuf.c b/lib/dpkg/varbuf.c index de399484d..7fc6f67be 100644 --- a/lib/dpkg/varbuf.c +++ b/lib/dpkg/varbuf.c @@ -104,6 +104,14 @@ varbuf_add_buf(struct varbuf *v, const void *s, size_t size) v->used += size; } +void +varbuf_add_dir(struct varbuf *v, const char *dirname) +{ + varbuf_add_str(v, dirname); + if (v->used == 0 || v->buf[v->used - 1] != '/') + varbuf_add_char(v, '/'); +} + void varbuf_end_str(struct varbuf *v) { diff --git a/lib/dpkg/varbuf.h b/lib/dpkg/varbuf.h index 707e4e919..78894b892 100644 --- a/lib/dpkg/varbuf.h +++ b/lib/dpkg/varbuf.h @@ -87,6 +87,7 @@ void varbuf_add_char(struct varbuf *v, int c); void varbuf_dup_char(struct varbuf *v, int c, size_t n); void varbuf_map_char(struct varbuf *v, int c_src, int c_dst); #define varbuf_add_str(v, s) varbuf_add_buf(v, s, strlen(s)) +void varbuf_add_dir(struct varbuf *v, const char *dirname); void varbuf_add_buf(struct varbuf *v, const void *s, size_t size); void varbuf_end_str(struct varbuf *v); const char *varbuf_get_str(struct varbuf *v); -- Dpkg.Org's dpkg

