From 7a1c13a48cf020c40dda1721d5c2ffd95e8e669a Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Sun, 10 Dec 2023 19:39:29 -0800
Subject: [PATCH v2] libopkg: Use libgen.h to provide basename API

Also ensure that copy of filename is passed into archive_entry_set_pathname
so it can be operated upon by posix basename which expect non-const
character pointer as input.

This became evident with latest musl where basename declaration was
dropped from string.h [1]

[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

Upstream-Status: Backport [74fc3a991f974095644897d18d43846b5f359dae]
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
v2: Do not override basename wholesale

 libopkg/opkg_archive.c | 6 ++++--
 libopkg/opkg_remove.c  | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/libopkg/opkg_archive.c b/libopkg/opkg_archive.c
index 03a4afb..b099f5b 100644
--- a/libopkg/opkg_archive.c
+++ b/libopkg/opkg_archive.c
@@ -20,6 +20,7 @@
 
 #include <archive.h>
 #include <archive_entry.h>
+#include <libgen.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -797,8 +798,9 @@ int gz_write_archive(const char *filename, const char *gz_filename)
     }
 
     /* Remove path hierarchy, as we are only compressing a single file */
-    archive_entry_set_pathname(entry, basename(filename));
-
+    char* tmp = xstrdup(filename);
+    archive_entry_set_pathname(entry, basename(tmp));
+    free(tmp);
     r = archive_write_header(a, entry);
     if (r != ARCHIVE_OK) {
         opkg_msg(ERROR, "Failed to create compressed file: '%s' : %s (errno=%d)",
diff --git a/libopkg/opkg_remove.c b/libopkg/opkg_remove.c
index 889c672..5254388 100644
--- a/libopkg/opkg_remove.c
+++ b/libopkg/opkg_remove.c
@@ -20,6 +20,7 @@
 
 #include "config.h"
 
+#include <libgen.h>
 #include <stdio.h>
 #include <glob.h>
 #include <unistd.h>
-- 
2.43.0

