Skip to content

Commit

Permalink
Update libmagic to version 5.45 (TileDB-Inc#4673)
Browse files Browse the repository at this point in the history
[SC-38521](https://app.shortcut.com/tiledb-inc/story/38521/update-libmagic-and-use-the-upstream-vcpkg-port)

Split from TileDB-Inc#4553.

This PR updates libmagic to version 5.45 and switches to using a vcpkg
port closer to the upstream one, which we can easily consume with
find_package(unofficial-libmagic) since microsoft/vcpkg#35274.

One complication is that the upstream port builds libmagic with its
official autotools-based build system, which is significantly slower on
Windows (on Linux it builds pretty fast). I tried to upstream the
CMake-based port I had added in TileDB-Inc#4119, but the PR was rejected.

Apart from binary caching, there is unfortunately nothing we can do
about the build performance regression. We could maintain the
CMake-based port for our own use, but it will split what we build and
what a future user of TileDB from vcpkg will build, and that port is not
without its problems (it failed for example when I tried cross-compiling
to arm64-windows, because it tried to execute the arm64 file.exe on my
x64 machine).

---
TYPE: BUILD
DESC: Update libmagic to version 5.45
  • Loading branch information
teo-tsirpanis authored Mar 12, 2024
1 parent 607d5ad commit 7753f33
Show file tree
Hide file tree
Showing 32 changed files with 356 additions and 1,239 deletions.
11 changes: 11 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ build:
os: "ubuntu-22.04"
tools:
python: "3.8"
apt_packages:
- autoconf
- autoconf-archive
- automake
- curl
- git
- libtool
- make
- pkg-config
- unzip
- zip

python:
install:
Expand Down
1 change: 1 addition & 0 deletions doc/dev/BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ title: Building TileDB from source
* Apple Clang 14
* Git (required by vcpkg)
* curl (required by vcpkg on non-Windows)
* autoconf (required by building libmagic on non-Windows)

## Downloading the source code

Expand Down
3 changes: 1 addition & 2 deletions ports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ After copying the port, add an entry to the table below. You should also contrib

|Port|Reason|
|----|------|
|`libmagic`|Updating to the upstream port deferred due to failures.|
|`openssl`|Pinning to OpenSSL 1.1 until we can move to 3.0 in January 2024.|
|`pcre2`|To be removed alongside libmagic.|
|`azure-storage-common-cpp`|Patching to disable default features on libxml2 (https://github.com/Azure/azure-sdk-for-cpp/pull/5221).|
|`libmagic`|Patching to add features explicitly enabling compression support.|
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ index c548e97..299ac0c 100644
#include <errno.h>
#include <fcntl.h> /* For open and flags */
-#include <regex.h>
+#include <pcre2posix.h>
+#include <tre/regex.h>
#include <time.h>
#include <sys/types.h>
#ifndef WIN32
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
From 2fa43ece9ec7564e1fbb9867bb5852b834643aa4 Mon Sep 17 00:00:00 2001
From: Long Nguyen <[email protected]>
Date: Sat, 8 May 2021 19:36:11 +0700
Subject: [PATCH 02/14] Change zlib lib name to match CMake output

---
configure.ac | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index b05c334..dd4063c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -173,7 +173,7 @@ AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat getline ctime_r

dnl Checks for libraries
if test "$enable_zlib" != "no"; then
- AC_CHECK_LIB(z, gzopen)
+ AC_SEARCH_LIBS(gzopen, [z zlib zlibd], have_zlib = "yes", have_zlib = "no")
fi
if test "$enable_bzlib" != "no"; then
AC_CHECK_LIB(bz2, BZ2_bzCompressInit)
@@ -193,11 +193,11 @@ AM_CONDITIONAL(IS_CROSS_COMPILE, test "$cross_compiling" = yes)

dnl Final sanity checks
if test "$enable_zlib" = "yes"; then
- if test "$ac_cv_header_zlib_h$ac_cv_lib_z_gzopen" != "yesyes"; then
+ if test "$ac_cv_header_zlib_h$have_zlib" != "yesyes"; then
AC_MSG_ERROR([zlib support requested but not found])
fi
fi
-if test "$ac_cv_header_zlib_h$ac_cv_lib_z_gzopen" = "yesyes"; then
+if test "$ac_cv_header_zlib_h$have_zlib" = "yesyes"; then
AC_DEFINE([ZLIBSUPPORT], 1, [Enable zlib compression support])
fi
if test "$enable_bzlib" = "yes"; then
--
2.29.2.windows.2

10 changes: 5 additions & 5 deletions ports/libmagic/0003-Fix-WIN32-macro-checks.patch
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ index 299ac0c..2c365a6 100644
--- a/src/file.h
+++ b/src/file.h
@@ -82,7 +82,7 @@
#include <pcre2posix.h>
#include <tre/regex.h>
#include <time.h>
#include <sys/types.h>
-#ifndef WIN32
Expand All @@ -31,13 +31,13 @@ index 299ac0c..2c365a6 100644
#define PATHSEP ':'
@@ -103,7 +103,7 @@

#define private static
#define file_private static

-#if HAVE_VISIBILITY && !defined(WIN32)
+#if HAVE_VISIBILITY && !defined(_WIN32)
#define public __attribute__ ((__visibility__("default")))
#ifndef protected
#define protected __attribute__ ((__visibility__("hidden")))
#define file_public __attribute__ ((__visibility__("default")))
#ifndef file_protected
#define file_protected __attribute__ ((__visibility__("hidden")))
--
2.29.2.windows.2

Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ diff --git a/src/file.h b/src/file.h
index 0332506..4aa9f60 100644
--- a/src/file.h
+++ b/src/file.h
@@ -88,6 +88,7 @@
@@ -88,7 +88,8 @@
/* Do this here and now, because struct stat gets re-defined on solaris */
#include <sys/stat.h>
#include <stdarg.h>
+#include <dirent.h>

#define ENABLE_CONDITIONALS

#include <locale.h>
#if defined(HAVE_XLOCALE_H)
#include <xlocale.h>
#endif
--
2.29.2.windows.2

2 changes: 1 addition & 1 deletion ports/libmagic/0006-Remove-Wrap-POSIX-headers.patch
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ index 3ab52d1..fc48d84 100644
--- a/src/pread.c
+++ b/src/pread.c
@@ -3,7 +3,9 @@
FILE_RCSID("@(#)$File: pread.c,v 1.2 2013/04/02 16:23:07 christos Exp $")
FILE_RCSID("@(#)$File: pread.c,v 1.5 2022/09/24 20:30:13 christos Exp $")
#endif /* lint */
#include <fcntl.h>
+#ifdef HAVE_UNISTD_H
Expand Down
25 changes: 0 additions & 25 deletions ports/libmagic/0009-No-fcntl-in-magic.c.patch

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ index 5204f20..7244841 100644
#undef HAVE_MAJOR
-#ifdef S_IFLNK
+#if S_IFLNK != 0
private int
file_private int
bad_link(struct magic_set *ms, int err, char *buf)
{
@@ -108,7 +108,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ diff --git a/src/funcs.c b/src/funcs.c
index b926625..b585486 100644
--- a/src/funcs.c
+++ b/src/funcs.c
@@ -809,6 +809,7 @@ file_print_guid(char *str, size_t len, const uint64_t *guid)
g->data4[6], g->data4[7]);
@@ -888,5 +888,6 @@
}

+#ifndef _WIN32
protected int
file_protected int
file_pipe_closexec(int *fds)
{
@@ -827,6 +828,7 @@ protected int
file_clear_closexec(int fd) {
return fcntl(fd, F_SETFD, 0);
@@ -914,5 +915,6 @@
#endif
}
+#endif

protected char *
file_protected char *
file_strtrim(char *str)
--
2.29.2.windows.2
Expand Down
24 changes: 24 additions & 0 deletions ports/libmagic/0012-Convert-MSYS2-paths-to-Windows-paths.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
From f0139ced57c1579450f3d09b6e3ae0159aae031b Mon Sep 17 00:00:00 2001
From: Long Nguyen <[email protected]>
Date: Mon, 10 May 2021 08:43:28 +0700
Subject: [PATCH 12/14] Convert MSYS2 paths to Windows paths

---
src/Makefile.am | 3 +++
1 file changed, 3 insertions(+)

diff --git a/src/Makefile.am b/src/Makefile.am
index 34781b9..26f853f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,4 +1,7 @@
MAGIC = $(pkgdatadir)/magic
+if MINGW
+MAGIC != cygpath -m -a -l '$(MAGIC)'
+endif
lib_LTLIBRARIES = libmagic.la
nodist_include_HEADERS = magic.h

--
2.29.2.windows.2

42 changes: 42 additions & 0 deletions ports/libmagic/0013-Check-for-backslash-in-argv-0-on-Windows.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
From 3f10c7d31627b64b068b84ba72e706991f672560 Mon Sep 17 00:00:00 2001
From: Long Nguyen <[email protected]>
Date: Fri, 14 May 2021 08:14:05 +0700
Subject: [PATCH 13/14] Check for backslash in argv[0] on Windows

---
magic/Makefile.am | 2 +-
src/file.c | 5 +++++
2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/magic/Makefile.am b/magic/Makefile.am
index 0eb5865..170bbab 100644
--- a/magic/Makefile.am
+++ b/magic/Makefile.am
@@ -353,7 +353,7 @@ ${MAGIC}: $(EXTRA_DIST) $(FILE_COMPILE_DEP)
@(if expr "${FILE_COMPILE}" : '.*/.*' > /dev/null; then \
echo "Using ${FILE_COMPILE} to generate ${MAGIC}" > /dev/null; \
else \
- v=$$(${FILE_COMPILE} --version | sed -e s/file-// -e q); \
+ v=$$(${FILE_COMPILE} --version | sed -e s/file${EXEEXT}-// -e q); \
if [ "$$v" != "${PACKAGE_VERSION}" ]; then \
echo "Cannot use the installed version of file ($$v) to"; \
echo "cross-compile file ${PACKAGE_VERSION}"; \
diff --git a/src/file.c b/src/file.c
index 2889f8a..12a604b 100644
--- a/src/file.c
+++ b/src/file.c
@@ -200,6 +200,11 @@ main(int argc, char *argv[])
_wildcard(&argc, &argv);
#endif

+#ifdef _WIN32
+ if ((progname = strrchr(argv[0], '\\')) != NULL)
+ progname++;
+ else
+#endif
if ((progname = strrchr(argv[0], '/')) != NULL)
progname++;
else
--
2.29.2.windows.2

38 changes: 0 additions & 38 deletions ports/libmagic/0014-Define-POSIX-macros-if-missing.patch

This file was deleted.

8 changes: 4 additions & 4 deletions ports/libmagic/0015-MSYS2-Remove-ioctl-call.patch
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ diff --git a/src/compress.c b/src/compress.c
index 33ce2bc..f172eda 100644
--- a/src/compress.c
+++ b/src/compress.c
@@ -378,7 +378,7 @@
@@ -407,7 +407,7 @@
sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
{
ssize_t rv;
-#ifdef FIONREAD
-#if defined(FIONREAD) && !defined(__MINGW32__)
+#if defined(FIONREAD) && !defined(__MINGW32__) && !defined(WIN32)
int t = 0;
#endif
size_t rn = n;
@@ -386,7 +386,7 @@
@@ -418,7 +418,7 @@
if (fd == STDIN_FILENO)
goto nocheck;

-#ifdef FIONREAD
-#if defined(FIONREAD) && !defined(__MINGW32__)
+#if defined(FIONREAD) && !defined(__MINGW32__) && !defined(WIN32)
if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1 || t == 0)) {
#ifdef FD_ZERO
Expand Down
40 changes: 40 additions & 0 deletions ports/libmagic/0016-Fix-file_famagic-function.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
diff --git a/src/fsmagic.c b/src/fsmagic.c
index 7244841..2c553c1 100644
--- a/src/fsmagic.c
+++ b/src/fsmagic.c
@@ -66,7 +66,7 @@ # define major(dev) (((dev) >> 8) & 0xff)
# define minor(dev) ((dev) & 0xff)
#endif
#undef HAVE_MAJOR
-#if S_IFLNK != 0
+#if S_IFLNK != 0 && ! defined(_WIN32)
file_private int
bad_link(struct magic_set *ms, int err, char *buf)
{
@@ -108,7 +108,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
int ret, did = 0;
int mime = ms->flags & MAGIC_MIME;
int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
-#if S_IFLNK != 0
+#if S_IFLNK != 0 && ! defined(_WIN32)
char buf[BUFSIZ+4];
ssize_t nch;
struct stat tstatbuf;
@@ -122,7 +122,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
* Fstat is cheaper but fails for files you don't have read perms on.
* On 4.2BSD and similar systems, use lstat() to identify symlinks.
*/
-#if S_IFLNK != 0
+#if S_IFLNK != 0 && ! defined(_WIN32)
if ((ms->flags & MAGIC_SYMLINK) == 0)
ret = lstat(fn, sb);
else
@@ -290,7 +290,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
return -1;
break;
#endif
-#if S_IFLNK != 0
+#if S_IFLNK != 0 && ! defined(_WIN32)
case S_IFLNK:
if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
if (ms->flags & MAGIC_ERROR) {
Loading

0 comments on commit 7753f33

Please sign in to comment.