diff --git a/.ci/test-mkfs-macos.sh b/.ci/test-mkfs-macos.sh new file mode 100755 index 0000000..cc90850 --- /dev/null +++ b/.ci/test-mkfs-macos.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -e + +IMAGE=test.img +IMAGESIZE=50 +MKFS=mkfs.simplefs + +function build_mkfs() +{ + make $MKFS +} + +function test_mkfs() +{ + dd if=/dev/zero of=$IMAGE bs=1M count=$IMAGESIZE 2>/dev/null + ./$MKFS $IMAGE +} + +build_mkfs +test_mkfs diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 8edb577..33687c8 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -26,3 +26,13 @@ jobs: .ci/check-newline.sh .ci/check-format.sh shell: bash + + mkfs_macos: + runs-on: macos-latest + steps: + - name: checkout code + uses: actions/checkout@v4 + - name: test mkfs.simplefs on macOS + run: | + .ci/test-mkfs-macos.sh + shell: bash diff --git a/mkfs.c b/mkfs.c index 59dd967..b50a009 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1,5 +1,19 @@ +#if !defined(__linux__) && !defined(__APPLE__) +#error \ + "Do not manage to build this file unless your platform is Linux or macOS." +#endif + #include -#include +#if defined(__linux__) +#include /* BLKGETSIZE64 */ +#elif defined(__APPLE__) +#include +#include /* DKIOCGETBLOCKCOUNT and DKIOCGETBLOCKSIZE */ +#define htole32(x) OSSwapHostToLittleInt32(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#endif #include #include #include @@ -278,12 +292,31 @@ int main(int argc, char **argv) /* Get block device size */ if ((stat_buf.st_mode & S_IFMT) == S_IFBLK) { long int blk_size = 0; +#if defined(__linux__) ret = ioctl(fd, BLKGETSIZE64, &blk_size); if (ret != 0) { perror("BLKGETSIZE64:"); ret = EXIT_FAILURE; goto fclose; } +#elif defined(__APPLE__) + uint64_t block_count = 0; + uint32_t sector_size = 0; + + ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &block_count); + if (ret) { + perror("DKIOCGETBLOCKCOUNT"); + ret = EXIT_FAILURE; + goto fclose; + } + ret = ioctl(fd, DKIOCGETBLOCKSIZE, §or_size); + if (ret) { + perror("DKIOCGETBLOCKSIZE"); + ret = EXIT_FAILURE; + goto fclose; + } + blk_size = block_count * sector_size; +#endif stat_buf.st_size = blk_size; }