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=9d410b39199e46dab8268b70e4557901f3e8af85 commit 9d410b39199e46dab8268b70e4557901f3e8af85 Author: Guillem Jover <[email protected]> AuthorDate: Sat Apr 20 01:08:06 2024 +0200 libdpkg: Add new file_getcwd() function This new function abstracts the logic to retry getting the current working directory by growing a buffer so that the result can fit. --- lib/dpkg/file.c | 14 ++++++++++++++ lib/dpkg/file.h | 3 +++ lib/dpkg/libdpkg.map | 1 + lib/dpkg/t/t-file.c | 17 ++++++++++++++++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/dpkg/file.c b/lib/dpkg/file.c index 0995d5c7d..7005223dc 100644 --- a/lib/dpkg/file.c +++ b/lib/dpkg/file.c @@ -36,6 +36,20 @@ #include <dpkg/buffer.h> #include <dpkg/file.h> +/** + * Get the current working directory. + * + */ +void +file_getcwd(struct varbuf *cwd) +{ + varbuf_reset(cwd); + varbuf_grow(cwd, 64); + while (getcwd(cwd->buf, cwd->size) == NULL) + varbuf_grow(cwd, cwd->size * 2); + varbuf_trunc(cwd, strlen(cwd->buf)); +} + /** * Read the symlink content into a varbuf. * diff --git a/lib/dpkg/file.h b/lib/dpkg/file.h index c1c25fa9d..f5bc50d1a 100644 --- a/lib/dpkg/file.h +++ b/lib/dpkg/file.h @@ -48,6 +48,9 @@ struct file_stat { char *gname; }; +void +file_getcwd(struct varbuf *cwd); + ssize_t file_readlink(const char *slink, struct varbuf *content, size_t content_len); diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map index db6271114..c2b0048e6 100644 --- a/lib/dpkg/libdpkg.map +++ b/lib/dpkg/libdpkg.map @@ -164,6 +164,7 @@ LIBDPKG_PRIVATE { treewalk_close; treewalk; + file_getcwd; file_readlink; file_is_exec; file_copy_perms; diff --git a/lib/dpkg/t/t-file.c b/lib/dpkg/t/t-file.c index 0004df409..79106b9f7 100644 --- a/lib/dpkg/t/t-file.c +++ b/lib/dpkg/t/t-file.c @@ -39,6 +39,20 @@ static const char ref_data[] = "containing multiple lines\n" ; +static void +test_file_getcwd(void) +{ + char *env; + struct varbuf cwd; + + env = getenv("abs_builddir"); + file_getcwd(&cwd); + + test_str(env, ==, cwd.buf); + + varbuf_destroy(&cwd); +} + static void test_file_slurp(void) { @@ -100,7 +114,8 @@ test_file_slurp(void) TEST_ENTRY(test) { - test_plan(32); + test_plan(33); + test_file_getcwd(); test_file_slurp(); } -- Dpkg.Org's dpkg

