]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
test_utils: fix `canRunCommand` implementation
authorBenoit Pierre <benoit.pierre@gmail.com>
Sat, 7 Jun 2025 22:04:23 +0000 (00:04 +0200)
committerBenoit Pierre <benoit.pierre@gmail.com>
Sat, 7 Jun 2025 22:05:34 +0000 (00:05 +0200)
Do not unconditionally cache the result of the first call (and
reused it for subsequent calls). Thankfully, the function is
only called once.

libarchive/test/test_read_set_format.c
test_utils/test_common.h
test_utils/test_main.c

index c760de0056d361682033fe5bed38c94e67a82de2..ad212b5cb71ef33b74ca71fa29e9a65896650d7d 100644 (file)
@@ -210,7 +210,7 @@ DEFINE_TEST(test_read_append_filter_wrong_program)
   /*
    * If we have "bunzip2 -q", try using that.
    */
-  if (!canRunCommand("bunzip2 -h")) {
+  if (!canRunCommand("bunzip2 -h", NULL)) {
     skipping("Can't run bunzip2 program on this platform");
     return;
   }
index 064f0a6ec241771db803cb85cdb94659f45f5efc..a9df300e3be8505e26db5d4987335d1449731daf 100644 (file)
@@ -340,8 +340,14 @@ int canGrzip(void);
 /* Return true if this platform can run the "gzip" program. */
 int canGzip(void);
 
-/* Return true if this platform can run the specified command. */
-int canRunCommand(const char *);
+/* Return true if this platform can run the specified command.
+ *
+ * Result can be optionally cached with `*tested`:
+ *   - 0 if not tested yet
+ *   - <0 if already tested negative
+ *   - >0 if already tested positive
+ */
+int canRunCommand(const char *cmd, int *tested);
 
 /* Return true if this platform can run the "lrzip" program. */
 int canLrzip(void);
index fe330e5a052e99300aebf05493f64d0b19cd9e24..1cadd2bfacc3b333a46490d48aa47ba64f4e3611 100644 (file)
@@ -2523,6 +2523,28 @@ static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
 #else
 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
 #endif
+
+/*
+ * Can this platform run the specified command?
+ */
+int
+canRunCommand(const char *cmd, int *tested)
+{
+  int value = tested ? *tested : 0;
+  if (!value) {
+    value = systemf("%s %s", cmd, redirectArgs) ? -1 : +1;
+    if (tested)
+      *tested = value;
+  }
+  return (value > 0);
+}
+
+#define CAN_RUN_FUNC(Program, Command) \
+    int can##Program(void) { \
+            static int tested = 0; \
+            return canRunCommand((Command), &tested); \
+    }
+
 /*
  * Can this platform run the bzip2 program?
  */
@@ -2571,18 +2593,6 @@ canGzip(void)
 /*
  * Can this platform run the lrzip program?
  */
-int
-canRunCommand(const char *cmd)
-{
-  static int tested = 0, value = 0;
-  if (!tested) {
-    tested = 1;
-    if (systemf("%s %s", cmd, redirectArgs) == 0)
-      value = 1;
-  }
-  return (value);
-}
-
 int
 canLrzip(void)
 {