From ba4811b1d2ac5a9d363e6000c83098f2eb885119 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 27 Sep 2019 22:54:44 +0200 Subject: Add a header for platform compatibillity fluff - We don't have "endian.h" everywhere. On some BSDs its in sys and on some BSDs the macros have different names. - We definitely don't have sysmacros.h on non-Unix-like systems. - Likewise for sys/types.h, sys/stat.h and their contents. Signed-off-by: David Oberhollenzer --- include/compat.h | 95 +++++++++++++++++++++++++++++++++++++ include/fstree.h | 3 +- include/highlevel.h | 3 +- include/sqfs/predef.h | 1 - include/tar.h | 1 - include/util.h | 3 +- lib/fstree/fstree_from_dir.c | 1 - lib/fstree/fstree_from_file.c | 2 - lib/sqfs/dir_writer.c | 2 - lib/sqfs/id_table.c | 1 + lib/sqfs/read_inode.c | 1 - lib/sqfs/read_super.c | 1 - lib/sqfs/read_table.c | 1 - lib/sqfs/readdir.c | 1 + lib/sqfs/super.c | 1 - lib/sqfs/write_inode.c | 1 + lib/sqfs/write_table.c | 1 - lib/sqfshelper/tree_node_to_inode.c | 1 - lib/tar/internal.h | 1 - lib/util/Makemodule.am | 2 +- lib/util/dirstack.c | 2 - lib/util/mkdir_p.c | 2 - tar/tar2sqfs.c | 1 - tests/fstree_from_file.c | 1 - unpack/rdsquashfs.h | 4 -- 25 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 include/compat.h diff --git a/include/compat.h b/include/compat.h new file mode 100644 index 0000000..74d0b6f --- /dev/null +++ b/include/compat.h @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * util.h + * + * Copyright (C) 2019 David Oberhollenzer + */ +#ifndef COMPAT_H +#define COMPAT_H + +#if defined(__APPLE__) +#include + +#define htole16(x) OSSwapHostToLittleInt16(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define htole64(x) OSSwapHostToLittleInt64(x) + +#define le32toh(x) OSSwapLittleToHostInt32(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#elif defined(__OpenBSD__) +#include +#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) +#include + +#define le16toh(x) letoh16(x) +#define le32toh(x) letoh32(x) +#define le64toh(x) letoh64(x) +#elif defined(_WIN32) || defined(__WINDOWS__) +#define htole16(x) (x) +#define htole32(x) (x) +#define htole64(x) (x) + +#define le16toh(x) (x) +#define le32toh(x) (x) +#define le64toh(x) (x) +#else +#include +#endif + +#if defined(_WIN32) || defined(__WINDOWS__) +#define S_IFSOCK SQFS_INODE_MODE_SOCK +#define S_IFLNK SQFS_INODE_MODE_LNK +#define S_IFREG SQFS_INODE_MODE_REG +#define S_IFBLK SQFS_INODE_MODE_BLK +#define S_IFDIR SQFS_INODE_MODE_DIR +#define S_IFCHR SQFS_INODE_MODE_CHR +#define S_IFIFO SQFS_INODE_MODE_FIFO +#define S_IFMT SQFS_INODE_MODE_MASK + +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) +#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) +#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) +#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) +#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) + +#define S_ISUID SQFS_INODE_SET_UID +#define S_ISGID SQFS_INODE_SET_GID +#define S_ISVTX SQFS_INODE_STICKY + +#define S_IRWXU SQFS_INODE_OWNER_MASK +#define S_IRUSR SQFS_INODE_OWNER_R +#define S_IWUSR SQFS_INODE_OWNER_W +#define S_IXUSR SQFS_INODE_OWNER_X + +#define S_IRWXG SQFS_INODE_GROUP_MASK +#define S_IRGRP SQFS_INODE_GROUP_R +#define S_IWGRP SQFS_INODE_GROUP_W +#define S_IXGRP SQFS_INODE_GROUP_X + +#define S_IRWXO SQFS_INODE_OTHERS_MASK +#define S_IROTH SQFS_INODE_OTHERS_R +#define S_IWOTH SQFS_INODE_OTHERS_W +#define S_IXOTH SQFS_INODE_OTHERS_X + +/* lifted from musl libc */ +#define major(x) \ + ((unsigned)( (((x)>>31>>1) & 0xfffff000) | (((x)>>8) & 0x00000fff) )) + +#define minor(x) \ + ((unsigned)( (((x)>>12) & 0xffffff00) | ((x) & 0x000000ff) )) + +#define makedev(x,y) ( \ + (((x)&0xfffff000ULL) << 32) | \ + (((x)&0x00000fffULL) << 8) | \ + (((y)&0xffffff00ULL) << 12) | \ + (((y)&0x000000ffULL)) ) +#else +#include +#include +#include +#endif + +#endif /* COMPAT_H */ diff --git a/include/fstree.h b/include/fstree.h index dbd2cd6..337b598 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -9,14 +9,13 @@ #include "config.h" -#include -#include #include #include #include #include #include "str_table.h" +#include "compat.h" #define FSTREE_XATTR_KEY_BUCKETS 31 #define FSTREE_XATTR_VALUE_BUCKETS 511 diff --git a/include/highlevel.h b/include/highlevel.h index 13c85a7..6e4ad5e 100644 --- a/include/highlevel.h +++ b/include/highlevel.h @@ -22,12 +22,11 @@ #include "sqfs/xattr.h" #include "sqfs/dir.h" #include "sqfs/io.h" +#include "compat.h" #include "fstree.h" #include "util.h" #include "tar.h" -#include -#include #include typedef struct { diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h index 2e835f3..13e49f3 100644 --- a/include/sqfs/predef.h +++ b/include/sqfs/predef.h @@ -27,7 +27,6 @@ * macros and integer types. */ -#include #include #include #include diff --git a/include/tar.h b/include/tar.h index d4e72ec..d0a483c 100644 --- a/include/tar.h +++ b/include/tar.h @@ -10,7 +10,6 @@ #include "config.h" #include "util.h" -#include #include #include diff --git a/include/util.h b/include/util.h index 61754ed..e7bc431 100644 --- a/include/util.h +++ b/include/util.h @@ -10,10 +10,11 @@ #include "config.h" #include "sqfs/predef.h" -#include #include #include +#include "compat.h" + #if defined(__GNUC__) || defined(__clang__) #define UI_ADD_OV __builtin_uadd_overflow #define UL_ADD_OV __builtin_uaddl_overflow diff --git a/lib/fstree/fstree_from_dir.c b/lib/fstree/fstree_from_dir.c index c31b1a0..d68fafa 100644 --- a/lib/fstree/fstree_from_dir.c +++ b/lib/fstree/fstree_from_dir.c @@ -9,7 +9,6 @@ #include "fstree.h" #include "util.h" -#include #ifdef HAVE_SYS_XATTR_H #include #endif diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c index 619e83f..ec2f941 100644 --- a/lib/fstree/fstree_from_file.c +++ b/lib/fstree/fstree_from_file.c @@ -9,8 +9,6 @@ #include "fstree.h" #include "util.h" -#include -#include #include #include #include diff --git a/lib/sqfs/dir_writer.c b/lib/sqfs/dir_writer.c index 2efc10d..3598dd2 100644 --- a/lib/sqfs/dir_writer.c +++ b/lib/sqfs/dir_writer.c @@ -15,8 +15,6 @@ #include "sqfs/dir.h" #include "util.h" -#include -#include #include #include diff --git a/lib/sqfs/id_table.c b/lib/sqfs/id_table.c index 78a5d26..a320eca 100644 --- a/lib/sqfs/id_table.c +++ b/lib/sqfs/id_table.c @@ -11,6 +11,7 @@ #include "sqfs/super.h" #include "sqfs/table.h" #include "sqfs/error.h" +#include "compat.h" #include #include diff --git a/lib/sqfs/read_inode.c b/lib/sqfs/read_inode.c index 1916c96..51662eb 100644 --- a/lib/sqfs/read_inode.c +++ b/lib/sqfs/read_inode.c @@ -13,7 +13,6 @@ #include "sqfs/inode.h" #include "util.h" -#include #include #include diff --git a/lib/sqfs/read_super.c b/lib/sqfs/read_super.c index b2f363a..6ac2ac4 100644 --- a/lib/sqfs/read_super.c +++ b/lib/sqfs/read_super.c @@ -12,7 +12,6 @@ #include "sqfs/io.h" #include "util.h" -#include #include int sqfs_super_read(sqfs_super_t *super, sqfs_file_t *file) diff --git a/lib/sqfs/read_table.c b/lib/sqfs/read_table.c index 4c5a5cc..096ac1a 100644 --- a/lib/sqfs/read_table.c +++ b/lib/sqfs/read_table.c @@ -14,7 +14,6 @@ #include "sqfs/io.h" #include "util.h" -#include #include int sqfs_read_table(sqfs_file_t *file, sqfs_compressor_t *cmp, diff --git a/lib/sqfs/readdir.c b/lib/sqfs/readdir.c index 6b549b7..8899475 100644 --- a/lib/sqfs/readdir.c +++ b/lib/sqfs/readdir.c @@ -10,6 +10,7 @@ #include "sqfs/meta_reader.h" #include "sqfs/error.h" #include "sqfs/dir.h" +#include "compat.h" #include #include diff --git a/lib/sqfs/super.c b/lib/sqfs/super.c index 5cd5a87..8e39b55 100644 --- a/lib/sqfs/super.c +++ b/lib/sqfs/super.c @@ -12,7 +12,6 @@ #include "sqfs/io.h" #include "util.h" -#include #include #include diff --git a/lib/sqfs/write_inode.c b/lib/sqfs/write_inode.c index b16ef65..debaa00 100644 --- a/lib/sqfs/write_inode.c +++ b/lib/sqfs/write_inode.c @@ -10,6 +10,7 @@ #include "sqfs/meta_writer.h" #include "sqfs/error.h" #include "sqfs/inode.h" +#include "compat.h" static int write_block_sizes(sqfs_meta_writer_t *ir, const sqfs_inode_generic_t *n) diff --git a/lib/sqfs/write_table.c b/lib/sqfs/write_table.c index c50cf06..1931b83 100644 --- a/lib/sqfs/write_table.c +++ b/lib/sqfs/write_table.c @@ -15,7 +15,6 @@ #include "sqfs/io.h" #include "util.h" -#include #include int sqfs_write_table(sqfs_file_t *file, sqfs_compressor_t *cmp, diff --git a/lib/sqfshelper/tree_node_to_inode.c b/lib/sqfshelper/tree_node_to_inode.c index 62a804d..d75e959 100644 --- a/lib/sqfshelper/tree_node_to_inode.c +++ b/lib/sqfshelper/tree_node_to_inode.c @@ -12,7 +12,6 @@ #include "util.h" #include -#include #include #include diff --git a/lib/tar/internal.h b/lib/tar/internal.h index f30de29..435cb73 100644 --- a/lib/tar/internal.h +++ b/lib/tar/internal.h @@ -12,7 +12,6 @@ #include "util.h" #include "tar.h" -#include #include #include #include diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 1d77fd5..58333ac 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -1,6 +1,6 @@ libutil_la_SOURCES = lib/util/write_data.c libutil_la_SOURCES += lib/util/read_data.c include/util.h -libutil_la_SOURCES += lib/util/mkdir_p.c +libutil_la_SOURCES += lib/util/mkdir_p.c include/compat.h libutil_la_SOURCES += lib/util/str_table.c include/str_table.h libutil_la_SOURCES += lib/util/dirstack.c lib/util/padd_file.c libutil_la_SOURCES += lib/util/alloc.c diff --git a/lib/util/dirstack.c b/lib/util/dirstack.c index 8f7baf1..20a89cc 100644 --- a/lib/util/dirstack.c +++ b/lib/util/dirstack.c @@ -6,8 +6,6 @@ */ #include "config.h" -#include -#include #include #include #include diff --git a/lib/util/mkdir_p.c b/lib/util/mkdir_p.c index f9d7730..93d0f59 100644 --- a/lib/util/mkdir_p.c +++ b/lib/util/mkdir_p.c @@ -6,8 +6,6 @@ */ #include "config.h" -#include -#include #include #include #include diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c index 1175273..5a404f4 100644 --- a/tar/tar2sqfs.c +++ b/tar/tar2sqfs.c @@ -17,7 +17,6 @@ #include "util.h" #include "tar.h" -#include #include #include #include diff --git a/tests/fstree_from_file.c b/tests/fstree_from_file.c index a8e05c3..76e378a 100644 --- a/tests/fstree_from_file.c +++ b/tests/fstree_from_file.c @@ -8,7 +8,6 @@ #include "fstree.h" -#include #include #include #include diff --git a/unpack/rdsquashfs.h b/unpack/rdsquashfs.h index 90e94fe..26c5d08 100644 --- a/unpack/rdsquashfs.h +++ b/unpack/rdsquashfs.h @@ -20,13 +20,9 @@ #include "fstree.h" #include "util.h" -#include -#include #ifdef HAVE_SYS_XATTR_H #include #endif -#include -#include #include #include #include -- cgit v1.2.3