From bb22e387cb963e36f163315a9c628775a6e4bd8d Mon Sep 17 00:00:00 2001 From: Kostia Balytskyi Date: Tue, 18 Jul 2017 03:21:28 -0700 Subject: [PATCH] cdatapack: add some windows portability Summary: This is a first set of changes to help `cdatapack` compile on Windows. Second set will include adding some way of using `mman` on Windows. Test Plan: - `make local` on Linux, `rt` - with some intermediary solution for `mman` this also builds on Windows 10, I was able to produce `cdatapack_get.exe` and `cdatapack_dump.exe`. Here's an example: ``` PS C:\Code\fb-hg-rpms\fb-hgext\cdatapack> .\cdatapack_get.exe 3ba0b10b8d251743a2692e042b114c1204b19d74 88dadb363234ec4fec3df85810810d6073288350 xplat/third-party/yarn/offline-mirror/smoothscroll-polyfill-0.3.5.tgz Node Delta Base Delta SHA1 Delta Length 88dadb363234ec4fec3df85810810d6073288350 0000000000000000000000000000000000000000 466e6039b51cb525d70e1a5077ef81e064678eae 26057 ``` Reviewers: durham, #fbhgext Differential Revision: https://phab.mercurial-scm.org/D106 --- cdatapack/cdatapack.c | 18 ++++++------------ clib/portability/inet.h | 13 +++++++++++++ clib/portability/portability.h | 7 +++++++ clib/portability/unistd.h | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 clib/portability/inet.h create mode 100644 clib/portability/unistd.h diff --git a/cdatapack/cdatapack.c b/cdatapack/cdatapack.c index 44e4482722..c5462c84de 100644 --- a/cdatapack/cdatapack.c +++ b/cdatapack/cdatapack.c @@ -18,8 +18,6 @@ #include #include #include -#include -#include #include #if defined(__linux__) @@ -38,6 +36,8 @@ #endif /* #if defined(__APPLE__) */ #include +#include "portability/inet.h" +#include "portability/unistd.h" #include "cdatapack.h" #include "buffer.h" @@ -47,11 +47,8 @@ /** * This is an exact representation of an index entry on disk. Do not consume * the fields directly, as they may need processing. - * - * NOTE: this uses gcc's __attribute__((packed)) syntax to indicate a packed - * data structure, which obviously has potential portability issues. */ -typedef struct _disk_index_entry_t { +PACKEDSTRUCT(typedef struct _disk_index_entry_t { uint8_t node[NODE_SZ]; // offset of the next element in the delta chain in the index file @@ -61,7 +58,7 @@ typedef struct _disk_index_entry_t { // file. data_offset_t data_offset; data_offset_t data_sz; -} __attribute__((packed)) disk_index_entry_t; +}) disk_index_entry_t; /** * This represents offsets into the index indicating the range of a fanout @@ -91,17 +88,14 @@ typedef struct _pack_chain_t { /** * This is an exact representation of an index file's header on disk. Do not * consume the fields directly, as they may need processing. - * - * NOTE: this uses gcc's __attribute__((packed)) syntax to indicate a packed - * data structure, which obviously has potential portability issues. */ -typedef struct _disk_index_header_t { +PACKEDSTRUCT(typedef struct _disk_index_header_t { #define VERSION 0 uint8_t version; #define LARGE_FANOUT 0x80 uint8_t config; -} __attribute__((packed)) disk_index_header_t; +}) disk_index_header_t; static void unpack_disk_deltachunk( const disk_index_entry_t *disk_deltachunk, diff --git a/clib/portability/inet.h b/clib/portability/inet.h new file mode 100644 index 0000000000..ed8666c17e --- /dev/null +++ b/clib/portability/inet.h @@ -0,0 +1,13 @@ +#ifndef PORTABILITY_INET_H +#define PORTABILITY_INET_H + +#if defined(_MSC_VER) + #include + #pragma comment(lib, "Ws2_32.lib") + /* See https://fburl.com/7hd350j8 for more details about Ws2_32.lib */ +#else + #include +#endif + +#endif /* PORTABILITY_INET_H */ + diff --git a/clib/portability/portability.h b/clib/portability/portability.h index 7069e936cd..4f723138a8 100644 --- a/clib/portability/portability.h +++ b/clib/portability/portability.h @@ -13,4 +13,11 @@ #define COMPOUND_LITERAL(typename_) (typename_) #endif /* #if defined(_MSC_VER) */ +#if defined(_MSC_VER) + #define PACKEDSTRUCT(__Declaration__) __pragma(pack(push, 1)) \ + __Declaration__ __pragma(pack(pop)) +#else + #define PACKEDSTRUCT(__Declaration__) __Declaration__ __attribute__((packed)) +#endif + #endif /* #ifndef PORTABILITY_PORTABILITY_H */ diff --git a/clib/portability/unistd.h b/clib/portability/unistd.h new file mode 100644 index 0000000000..9f73134ac8 --- /dev/null +++ b/clib/portability/unistd.h @@ -0,0 +1,16 @@ +#ifndef PORTABILITY_UNISTD_H +#define PORTABILITY_UNISTD_H + +#if defined(_MSC_VER) + #include + /* MSVC's io.h header shows deprecation + warnings on these without underscore */ + #define lseek _lseek + #define open _open + #define close _close +#else + #include +#endif + +#endif /* PORTABILITY_UNISTD_H */ +