Skip to content

Commit 04a801a

Browse files
committed
Add macOS support for mkfs.simplefs
mkfs.simplefs depended on Linux-only headers and ioctl calls, so the rv32emu CI could not run simplefs tests on macOS runners. Add macOS-specific endian helpers and detect block device size via DKIOCGETBLOCKCOUNT/DKIOCGETBLOCKSIZE while keeping the Linux path (BLKGETSIZE64) unchanged. Fixes: #78 Signed-off-by: hsule <leann9001@gmail.com>
1 parent 17e6c30 commit 04a801a

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

mkfs.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
#if !defined(__linux__) && !defined(__APPLE__)
2+
#error \
3+
"Do not manage to build this file unless your platform is Linux or macOS."
4+
#endif
5+
16
#include <fcntl.h>
2-
#include <linux/fs.h>
7+
#if defined(__linux__)
8+
#include <linux/fs.h> /* BLKGETSIZE64 */
9+
#elif defined(__APPLE__)
10+
#include <libkern/OSByteOrder.h>
11+
#include <sys/disk.h> /* DKIOCGETBLOCKCOUNT and DKIOCGETBLOCKSIZE */
12+
#define htole32(x) OSSwapHostToLittleInt32(x)
13+
#define le32toh(x) OSSwapLittleToHostInt32(x)
14+
#define htole64(x) OSSwapHostToLittleInt64(x)
15+
#define le64toh(x) OSSwapLittleToHostInt64(x)
16+
#endif
317
#include <stdint.h>
418
#include <stdio.h>
519
#include <stdlib.h>
@@ -278,12 +292,31 @@ int main(int argc, char **argv)
278292
/* Get block device size */
279293
if ((stat_buf.st_mode & S_IFMT) == S_IFBLK) {
280294
long int blk_size = 0;
295+
#if defined(__linux__)
281296
ret = ioctl(fd, BLKGETSIZE64, &blk_size);
282297
if (ret != 0) {
283298
perror("BLKGETSIZE64:");
284299
ret = EXIT_FAILURE;
285300
goto fclose;
286301
}
302+
#elif defined(__APPLE__)
303+
uint64_t block_count = 0;
304+
uint32_t sector_size = 0;
305+
306+
ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &block_count);
307+
if (ret) {
308+
perror("DKIOCGETBLOCKCOUNT");
309+
ret = EXIT_FAILURE;
310+
goto fclose;
311+
}
312+
ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size);
313+
if (ret) {
314+
perror("DKIOCGETBLOCKSIZE");
315+
ret = EXIT_FAILURE;
316+
goto fclose;
317+
}
318+
blk_size = block_count * sector_size;
319+
#endif
287320
stat_buf.st_size = blk_size;
288321
}
289322

0 commit comments

Comments
 (0)