···11+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22+From: Randy Eckenrode <randy@largeandhighquality.com>
33+Date: Wed, 30 Oct 2024 20:19:20 -0400
44+Subject: [PATCH 20/20] Fall back to readlink on Linux
55+66+---
77+ xar/lib/archive.c | 21 +++++++++++++++++++++
88+ 1 file changed, 21 insertions(+)
99+1010+diff --git a/xar/lib/archive.c b/xar/lib/archive.c
1111+index b7f9cbf..3a79c74 100644
1212+--- a/xar/lib/archive.c
1313++++ b/xar/lib/archive.c
1414+@@ -507,10 +507,31 @@ xar_t xar_fdopen_digest_verify(int fd, int32_t flags, void *expected_toc_digest,
1515+ // If there are hardlinks, the path we pick is the most recently opened by
1616+ // the filesystem; which is effectively random.
1717+ char path_buff[PATH_MAX];
1818++#if defined(__APPLE__)
1919+ if (fcntl(fd, F_GETPATH, path_buff) < 0) {
2020+ close(fd);
2121+ return NULL;
2222+ }
2323++#elif defined(__linux__)
2424++ // Linux has to get the path to the file via `/proc`.
2525++ char proc_buff[PATH_MAX];
2626++ if (snprintf(proc_buff, PATH_MAX, "/proc/self/fd/%i", fd) < 0) {
2727++ close(fd);
2828++ return NULL;
2929++ }
3030++ if (readlink(proc_buff, &path_buff, PATH_MAX) < 0) {
3131++ close(fd);
3232++ return NULL;
3333++ }
3434++ // The filename is the file’s when the fd was created. Check to make sure it still exists.
3535++ struct stat stat_buff;
3636++ if (stat(path_buff, &stat_buff) < 0) {
3737++ close(fd);
3838++ return NULL;
3939++ }
4040++#else
4141++#error "Unknown platform. Please update with an implementation of `F_GETPATH`."
4242++#endif
4343+4444+ // Don't trust the position of the descriptor we were given, reset it back to 0
4545+ if (lseek(fd, 0, SEEK_SET) != 0) {
4646+--
4747+2.47.0
4848+