Skip to content

Commit 02a82a6

Browse files
committed
Support mkfs.simplefs on macOS
Signed-off-by: hsule <leann9001@gmail.com>
1 parent 17e6c30 commit 02a82a6

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

mkfs.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include <fcntl.h>
2-
#include <linux/fs.h>
2+
#ifdef __linux__
3+
#include <linux/fs.h> /* BLKGETSIZE64 */
4+
#elif defined(__APPLE__)
5+
#include <libkern/OSByteOrder.h>
6+
#include <sys/disk.h> /* DKIOCGETBLOCKCOUNT and DKIOCGETBLOCKSIZE */
7+
#define htole32(x) OSSwapHostToLittleInt32(x)
8+
#define le32toh(x) OSSwapLittleToHostInt32(x)
9+
#define htole64(x) OSSwapHostToLittleInt64(x)
10+
#define le64toh(x) OSSwapLittleToHostInt64(x)
11+
#endif
312
#include <stdint.h>
413
#include <stdio.h>
514
#include <stdlib.h>
@@ -47,7 +56,7 @@ static struct superblock *write_superblock(int fd, struct stat *fstats)
4756
nr_blocks - nr_istore_blocks - nr_ifree_blocks - nr_bfree_blocks;
4857

4958
memset(sb, 0, sizeof(struct superblock));
50-
sb->info = (struct simplefs_sb_info){
59+
sb->info = (struct simplefs_sb_info) {
5160
.magic = htole32(SIMPLEFS_MAGIC),
5261
.nr_blocks = htole32(nr_blocks),
5362
.nr_inodes = htole32(nr_inodes),
@@ -278,12 +287,31 @@ int main(int argc, char **argv)
278287
/* Get block device size */
279288
if ((stat_buf.st_mode & S_IFMT) == S_IFBLK) {
280289
long int blk_size = 0;
290+
#ifdef __linux__
281291
ret = ioctl(fd, BLKGETSIZE64, &blk_size);
282292
if (ret != 0) {
283293
perror("BLKGETSIZE64:");
284294
ret = EXIT_FAILURE;
285295
goto fclose;
286296
}
297+
#elif defined(__APPLE__)
298+
uint64_t block_count = 0;
299+
uint32_t sector_size = 0;
300+
301+
ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &block_count);
302+
if (ret != 0) {
303+
perror("DKIOCGETBLOCKCOUNT");
304+
ret = EXIT_FAILURE;
305+
goto fclose;
306+
}
307+
ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size);
308+
if (ret != 0) {
309+
perror("DKIOCGETBLOCKSIZE");
310+
ret = EXIT_FAILURE;
311+
goto fclose;
312+
}
313+
blk_size = block_count * sector_size;
314+
#endif
287315
stat_buf.st_size = blk_size;
288316
}
289317

0 commit comments

Comments
 (0)