Skip to content

Commit a2fd214

Browse files
committed
Add macOS/BSD support
Signed-off-by: CatMe0w <[email protected]>
1 parent ff7da92 commit a2fd214

File tree

7 files changed

+112
-1
lines changed

7 files changed

+112
-1
lines changed

config.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <errno.h>
2222
#include <getopt.h>
2323
#include <unistd.h>
24+
#include <limits.h>
2425

2526
#include "genimage.h"
2627

@@ -279,7 +280,18 @@ static char *abspath(const char *path)
279280
if (*path == '/')
280281
return strdup(path);
281282

283+
#ifdef __GLIBC__
284+
/* Use GNU extension on Linux */
282285
xasprintf(&p, "%s/%s", get_current_dir_name(), path);
286+
#else
287+
/* Use POSIX standard for macOS/BSD */
288+
char cwd[PATH_MAX];
289+
if (getcwd(cwd, sizeof(cwd)) == NULL) {
290+
fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
291+
return NULL;
292+
}
293+
xasprintf(&p, "%s/%s", cwd, path);
294+
#endif
283295

284296
return p;
285297
}

genimage.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <confuse.h>
66
#include "list.h"
77

8+
#ifndef __GLIBC__
9+
#define strdupa(s) strdup(s)
10+
#endif
11+
812
struct image_handler;
913

1014
struct image *image_get(const char *filename);

image-android-sparse.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
*/
1616

1717
#include <confuse.h>
18+
#ifdef __APPLE__
19+
#include <libkern/OSByteOrder.h>
20+
#define htole16(x) OSSwapHostToLittleInt16(x)
21+
#define htole32(x) OSSwapHostToLittleInt32(x)
22+
#define le16toh(x) OSSwapLittleToHostInt16(x)
23+
#define le32toh(x) OSSwapLittleToHostInt32(x)
24+
#else
1825
#include <endian.h>
26+
#endif
1927
#include <errno.h>
2028
#include <fcntl.h>
2129
#include <stdlib.h>

image-hd.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@
2222
#include <stdlib.h>
2323
#include <errno.h>
2424
#include <inttypes.h>
25+
#ifdef __APPLE__
26+
#include <libkern/OSByteOrder.h>
27+
#define htole16(x) OSSwapHostToLittleInt16(x)
28+
#define htole32(x) OSSwapHostToLittleInt32(x)
29+
#define htole64(x) OSSwapHostToLittleInt64(x)
30+
#define le16toh(x) OSSwapLittleToHostInt16(x)
31+
#define le32toh(x) OSSwapLittleToHostInt32(x)
32+
#define le64toh(x) OSSwapLittleToHostInt64(x)
33+
#else
2534
#include <endian.h>
35+
#endif
2636
#include <stdbool.h>
2737
#include <unistd.h>
2838
#include <sys/types.h>

image-mdraid.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@
3434
#include <string.h>
3535
#include <time.h>
3636
#include <errno.h>
37+
38+
#ifdef __linux__
3739
#include <linux/raid/md_p.h>
40+
#endif
3841

3942
#include "genimage.h"
4043

44+
#ifdef __linux__
45+
4146
#define DATA_OFFSET_SECTORS (2048)
4247
#define DATA_OFFSET_BYTES (DATA_OFFSET_SECTORS * 512)
4348
#define BITMAP_SECTORS_MAX 256
@@ -486,3 +491,48 @@ struct image_handler mdraid_handler = {
486491
.generate = mdraid_generate,
487492
.opts = mdraid_opts,
488493
};
494+
495+
#else /* !__linux__ */
496+
497+
/* Stub implementation for non-Linux systems */
498+
static int mdraid_parse(struct image *image, cfg_t *cfg)
499+
{
500+
image_error(image, "MDRAID is only supported on Linux\n");
501+
return -1;
502+
}
503+
504+
static int mdraid_generate(struct image *image)
505+
{
506+
image_error(image, "MDRAID is only supported on Linux\n");
507+
return -1;
508+
}
509+
510+
static int mdraid_setup(struct image *image, cfg_t *cfg)
511+
{
512+
image_error(image, "MDRAID is only supported on Linux\n");
513+
return -1;
514+
}
515+
516+
static cfg_opt_t mdraid_opts[] = {
517+
CFG_STR("label", "any:42", CFGF_NONE),
518+
CFG_INT("level", 1, CFGF_NONE),
519+
CFG_INT("devices", 1, CFGF_NONE),
520+
CFG_INT("role", -1, CFGF_NONE),
521+
CFG_INT("timestamp", -1, CFGF_NONE),
522+
CFG_STR("raid-uuid", NULL, CFGF_NONE),
523+
CFG_STR("disk-uuid", NULL, CFGF_NONE),
524+
CFG_STR("image", NULL, CFGF_NONE),
525+
CFG_STR("parent", NULL, CFGF_NONE),
526+
CFG_END()
527+
};
528+
529+
struct image_handler mdraid_handler = {
530+
.type = "mdraid",
531+
.no_rootpath = cfg_true,
532+
.parse = mdraid_parse,
533+
.setup = mdraid_setup,
534+
.generate = mdraid_generate,
535+
.opts = mdraid_opts,
536+
};
537+
538+
#endif /* __linux__ */

image-verity.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include <string.h>
2323
#include <stdlib.h>
2424
#include <unistd.h>
25+
#ifdef __linux__
2526
#include <sys/sendfile.h>
27+
#endif
2628
#include <sys/stat.h>
2729

2830
#include "genimage.h"
@@ -337,13 +339,23 @@ static int verity_generate(struct image *image)
337339
return -errno;
338340

339341
if (image->size && image->size < (unsigned long)sb.st_size) {
342+
#ifdef __APPLE__
343+
image_error(image,
344+
"Specified image size (%llu) is too small, generated %lld bytes\n",
345+
image->size, sb.st_size);
346+
#else
340347
image_error(image,
341348
"Specified image size (%llu) is too small, generated %ld bytes\n",
342349
image->size, sb.st_size);
350+
#endif
343351
return -E2BIG;
344352
}
345353

354+
#ifdef __APPLE__
355+
image_debug(image, "generated %lld bytes\n", sb.st_size);
356+
#else
346357
image_debug(image, "generated %ld bytes\n", sb.st_size);
358+
#endif
347359
image->size = sb.st_size;
348360
return 0;
349361
}

util.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,11 @@ int parse_holes(struct image *image, cfg_t *cfg)
952952
image->holes = xzalloc(image->n_holes * sizeof(*image->holes));
953953
for (i = 0; i < image->n_holes; i++) {
954954
const char *s = cfg_getnstr(cfg, "holes", i);
955-
char *start, *end;
956955
int len;
957956

957+
#ifdef __GLIBC__
958+
/* Use GNU extension %m for automatic memory allocation */
959+
char *start, *end;
958960
if (sscanf(s, " ( %m[0-9skKMG] ; %m[0-9skKMG] ) %n", &start, &end, &len) != 2 ||
959961
len != (int)strlen(s)) {
960962
image_error(image, "invalid hole specification '%s', use '(<start>;<end>)'\n",
@@ -966,6 +968,19 @@ int parse_holes(struct image *image, cfg_t *cfg)
966968
image->holes[i].end = strtoul_suffix(end, NULL, NULL);
967969
free(start);
968970
free(end);
971+
#else
972+
/* Use fixed-size buffers for non-glibc systems (macOS/BSD) */
973+
char start[64], end[64];
974+
if (sscanf(s, " ( %63[0-9skKMG] ; %63[0-9skKMG] ) %n", start, end, &len) != 2 ||
975+
len != (int)strlen(s)) {
976+
image_error(image, "invalid hole specification '%s', use '(<start>;<end>)'\n",
977+
s);
978+
return -EINVAL;
979+
}
980+
981+
image->holes[i].start = strtoul_suffix(start, NULL, NULL);
982+
image->holes[i].end = strtoul_suffix(end, NULL, NULL);
983+
#endif
969984
image_debug(image, "added hole (%llu, %llu)\n", image->holes[i].start, image->holes[i].end);
970985
}
971986
return 0;

0 commit comments

Comments
 (0)