Skip to content

Commit 860c9cd

Browse files
authored
Merge pull request #82 from hsule/mkfs_macos
Support mkfs.simplefs on macOS
2 parents 17e6c30 + 1889a19 commit 860c9cd

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

.ci/test-mkfs-macos.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
IMAGE=test.img
6+
IMAGESIZE=50
7+
MKFS=mkfs.simplefs
8+
9+
function build_mkfs()
10+
{
11+
make $MKFS
12+
}
13+
14+
function test_mkfs()
15+
{
16+
dd if=/dev/zero of=$IMAGE bs=1M count=$IMAGESIZE 2>/dev/null
17+
./$MKFS $IMAGE
18+
}
19+
20+
build_mkfs
21+
test_mkfs

.github/workflows/main.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ jobs:
2626
.ci/check-newline.sh
2727
.ci/check-format.sh
2828
shell: bash
29+
30+
mkfs_macos:
31+
runs-on: macos-latest
32+
steps:
33+
- name: checkout code
34+
uses: actions/checkout@v4
35+
- name: test mkfs.simplefs on macOS
36+
run: |
37+
.ci/test-mkfs-macos.sh
38+
shell: bash

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)