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=6df548cb19e2d775ac08521f0d72a7e681b9706a

commit 6df548cb19e2d775ac08521f0d72a7e681b9706a
Author: Guillem Jover <[email protected]>
AuthorDate: Thu Mar 17 19:36:33 2022 +0100

    libdpkg: Refactor file_is_exec()
    
    Move the function from dpkg into libdpkg, as we will use it in another
    call site.
---
 lib/dpkg/file.c      | 16 ++++++++++++++++
 lib/dpkg/file.h      |  2 ++
 lib/dpkg/libdpkg.map |  1 +
 lib/dpkg/t/t-file.c  |  9 ++++++++-
 src/main/help.c      |  4 ++--
 5 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/lib/dpkg/file.c b/lib/dpkg/file.c
index 31483ef30..85687dbb5 100644
--- a/lib/dpkg/file.c
+++ b/lib/dpkg/file.c
@@ -36,6 +36,22 @@
 #include <dpkg/buffer.h>
 #include <dpkg/file.h>
 
+/**
+ * Check whether a filename is executable.
+ *
+ * @param pathname The filename to check.
+ */
+bool
+file_is_exec(const char *filename)
+{
+       struct stat st;
+
+       if (stat(filename, &st) < 0)
+               return false;
+
+       return st.st_mode & 0111;
+}
+
 /**
  * Copy file ownership and permissions from one file to another.
  *
diff --git a/lib/dpkg/file.h b/lib/dpkg/file.h
index ead11c73a..0136f6901 100644
--- a/lib/dpkg/file.h
+++ b/lib/dpkg/file.h
@@ -48,6 +48,8 @@ struct file_stat {
        char *gname;
 };
 
+bool file_is_exec(const char *filename);
+
 void file_copy_perms(const char *src, const char *dst);
 
 int
diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map
index 9f345f9eb..a44dd39af 100644
--- a/lib/dpkg/libdpkg.map
+++ b/lib/dpkg/libdpkg.map
@@ -153,6 +153,7 @@ LIBDPKG_PRIVATE {
        treewalk_close;
        treewalk;
 
+       file_is_exec;
        file_copy_perms;
        file_show;
        file_slurp;
diff --git a/lib/dpkg/t/t-file.c b/lib/dpkg/t/t-file.c
index 278df9141..ae089b25e 100644
--- a/lib/dpkg/t/t-file.c
+++ b/lib/dpkg/t/t-file.c
@@ -26,6 +26,7 @@
 #include <dpkg/file.h>
 
 #include <sys/types.h>
+#include <sys/stat.h>
 
 #include <errno.h>
 #include <unistd.h>
@@ -86,13 +87,19 @@ test_file_slurp(void)
        test_pass(err.type == DPKG_MSG_NONE);
        varbuf_destroy(&vb);
 
+       test_fail(file_is_exec(test_file));
+       test_pass(chmod(test_file, 755) == 0);
+       test_pass(file_is_exec(test_file));
+       test_pass(chmod(test_file, 750) == 0);
+       test_pass(file_is_exec(test_file));
+
        test_pass(unlink(test_file) == 0);
        free(test_file);
 }
 
 TEST_ENTRY(test)
 {
-       test_plan(26);
+       test_plan(31);
 
        test_file_slurp();
 }
diff --git a/src/main/help.c b/src/main/help.c
index 7762aca1a..f29d9cd4a 100644
--- a/src/main/help.c
+++ b/src/main/help.c
@@ -34,6 +34,7 @@
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
 #include <dpkg/path.h>
+#include <dpkg/file.h>
 #include <dpkg/db-fsys.h>
 
 #include "main.h"
@@ -78,7 +79,6 @@ bool
 find_command(const char *prog)
 {
   struct varbuf filename = VARBUF_INIT;
-  struct stat stab;
   const char *path_list;
   const char *path, *path_end;
   size_t path_len;
@@ -98,7 +98,7 @@ find_command(const char *prog)
     varbuf_add_str(&filename, prog);
     varbuf_end_str(&filename);
 
-    if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111)) {
+    if (file_is_exec(filename.buf)) {
       varbuf_destroy(&filename);
       return true;
     }

-- 
Dpkg.Org's dpkg

Reply via email to