summaryrefslogtreecommitdiffstats
path: root/src/fuse/cowMerger/src/merger.h
blob: c665df7ebe1f7a2fb512c16275eaeaee6d947fdf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdbool.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libgen.h>
#include <stdint.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

bool merger(char *imageFilePath, char *cowFilePath);
bool loadFiles(char *imageFilePath, char *cowFilePath);
bool merge();

typedef struct {
	uint32_t version;
	uint64_t imageSize;
	uint32_t nameLenght;
	uint32_t pageSize;
}cow_metadata;

#if defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
static const uint16_t dnbd3_packet_magic = (0x73 << 8) | (0x72);
// Flip bytes around on big endian when putting stuff on the net
#define net_order_64(a) ((uint64_t)((((a) & 0xFFull) << 56) | (((a) & 0xFF00ull) << 40) | (((a) & 0xFF0000ull) << 24) | (((a) & 0xFF000000ull) << 8) | (((a) & 0xFF00000000ull) >> 8) | (((a) & 0xFF0000000000ull) >> 24) | (((a) & 0xFF000000000000ull) >> 40) | (((a) & 0xFF00000000000000ull) >> 56)))
#define net_order_32(a) ((uint32_t)((((a) & (uint32_t)0xFF) << 24) | (((a) & (uint32_t)0xFF00) << 8) | (((a) & (uint32_t)0xFF0000) >> 8) | (((a) & (uint32_t)0xFF000000) >> 24)))
#define net_order_16(a) ((uint16_t)((((a) & (uint16_t)0xFF) << 8) | (((a) & (uint16_t)0xFF00) >> 8)))
#define fixup_cow_metadata(a) do { \
	(a).version = net_order_32((a).version); \
	(a).imageSize = net_order_64((a).imageSize); \
	(a).nameLenght = net_order_32((a).nameLenght); \
	(a).pageSize = net_order_32((a).pageSize); \
} while (0)
#define ENDIAN_MODE "Big Endian"
#ifndef BIG_ENDIAN
#define BIG_ENDIAN
#endif
#elif defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(__i386__) || defined(__i386) || defined(__x86_64)
// Make little endian our network byte order as probably 99.999% of machines this will be used on are LE
#define net_order_64(a) (a)
#define net_order_32(a) (a)
#define net_order_16(a) (a)
#define fixup_cow_metadata(a)   while(0)
#define ENDIAN_MODE "Little Endian"
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN
#endif
#else
#error "Unknown Endianness"
#endif