Re: [Libguestfs] [PATCH 1/3] macosx: Includes/defines for byteswap operations

Pino Toscano
Thu, 12 Feb 2015 18:28:19 -0000

On Thursday 12 February 2015 17:28:46 Margaret Lewicka
wrote:
> ---
>  src/inspect-apps.c       | 13 ++++++++++++-
>  src/inspect-fs-windows.c |  6 ++++++
>  src/journal.c            |  5 +++++
>  3 files changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/src/inspect-apps.c b/src/inspect-apps.c
> index 20cf00a..8fbae9c 100644
> --- a/src/inspect-apps.c
> +++ b/src/inspect-apps.c
> @@ -35,11 +35,22 @@
>  #include <sys/endian.h>
>  #endif
>
> -/* be32toh is usually a macro definend in <endian.h>, but it might
be
> +/* be32toh is usually a macro defined in <endian.h>, but it might be
>   * a function in some system so check both, and if neither is defined
>   * then define be32toh for RHEL 5.
>   */
>  #if !defined(HAVE_BE32TOH) && !defined(be32toh)
> +
> +#if defined __APPLE__ && defined __MACH__
> +/* Define/include necessary items on MacOS X */
> +#include <machine/endian.h>
> +#define __BIG_ENDIAN    BIG_ENDIAN
> +#define __LITTLE_ENDIAN   LITTLE_ENDIAN
> +#define __BYTE_ORDER    BYTE_ORDER
> +#include <libkern/OSByteOrder.h>
> +#define __bswap_32      OSSwapConstInt32
> +#endif /* __APPLE__ */
> +
>  #if __BYTE_ORDER == __LITTLE_ENDIAN
>  #define be32toh(x) __bswap_32 (x)
>  #else
> diff --git a/src/inspect-fs-windows.c b/src/inspect-fs-windows.c
> index 682157a..421a5b1 100644
> --- a/src/inspect-fs-windows.c
> +++ b/src/inspect-fs-windows.c
> @@ -36,6 +36,12 @@
>  #include <sys/endian.h>
>  #endif
>
> +#if defined __APPLE__ && defined __MACH__
> +#include <libkern/OSByteOrder.h>
> +#define le32toh(x) OSSwapLittleToHostInt32(x)
> +#define le64toh(x) OSSwapLittleToHostInt64(x)
> +#endif
> +
>  #include <pcre.h>
>
>  #include "c-ctype.h"
> diff --git a/src/journal.c b/src/journal.c
> index 1070067..c563b7f 100644
> --- a/src/journal.c
> +++ b/src/journal.c
> @@ -35,6 +35,11 @@
>  #include <sys/endian.h>
>  #endif
>
> +#if defined __APPLE__ && defined __MACH__
> +#include <libkern/OSByteOrder.h>
> +#define be64toh(x) OSSwapBigToHostInt64(x)
> +#endif
> +
>  #include "full-read.h"
>
>  #include "guestfs.h"

This code clearly need some refactoring, before adding more copy/pasted
parts to it.  I'd say to do this in two steps:
a) moving the definitions of the current be32toh/etc to a new
   src/guestfs-byteswap.h (internal), making use of it
b) add the proper definitions needed on Mac OS X

Thanks,
--
Pino Toscano