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
This commit is contained in:
Kostia Balytskyi 2017-07-18 03:21:28 -07:00
parent 9d389170da
commit bb22e387cb
4 changed files with 42 additions and 12 deletions

View File

@ -18,8 +18,6 @@
#include <errno.h>
#include <fcntl.h>
#include <memory.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#if defined(__linux__)
@ -38,6 +36,8 @@
#endif /* #if defined(__APPLE__) */
#include <lz4.h>
#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,

13
clib/portability/inet.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef PORTABILITY_INET_H
#define PORTABILITY_INET_H
#if defined(_MSC_VER)
#include <winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
/* See https://fburl.com/7hd350j8 for more details about Ws2_32.lib */
#else
#include <arpa/inet.h>
#endif
#endif /* PORTABILITY_INET_H */

View File

@ -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 */

16
clib/portability/unistd.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef PORTABILITY_UNISTD_H
#define PORTABILITY_UNISTD_H
#if defined(_MSC_VER)
#include <io.h>
/* MSVC's io.h header shows deprecation
warnings on these without underscore */
#define lseek _lseek
#define open _open
#define close _close
#else
#include <unistd.h>
#endif
#endif /* PORTABILITY_UNISTD_H */