|
1 | 1 | #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 |
3 | 12 | #include <stdint.h> |
4 | 13 | #include <stdio.h> |
5 | 14 | #include <stdlib.h> |
@@ -47,7 +56,7 @@ static struct superblock *write_superblock(int fd, struct stat *fstats) |
47 | 56 | nr_blocks - nr_istore_blocks - nr_ifree_blocks - nr_bfree_blocks; |
48 | 57 |
|
49 | 58 | memset(sb, 0, sizeof(struct superblock)); |
50 | | - sb->info = (struct simplefs_sb_info){ |
| 59 | + sb->info = (struct simplefs_sb_info) { |
51 | 60 | .magic = htole32(SIMPLEFS_MAGIC), |
52 | 61 | .nr_blocks = htole32(nr_blocks), |
53 | 62 | .nr_inodes = htole32(nr_inodes), |
@@ -278,12 +287,31 @@ int main(int argc, char **argv) |
278 | 287 | /* Get block device size */ |
279 | 288 | if ((stat_buf.st_mode & S_IFMT) == S_IFBLK) { |
280 | 289 | long int blk_size = 0; |
| 290 | +#ifdef __linux__ |
281 | 291 | ret = ioctl(fd, BLKGETSIZE64, &blk_size); |
282 | 292 | if (ret != 0) { |
283 | 293 | perror("BLKGETSIZE64:"); |
284 | 294 | ret = EXIT_FAILURE; |
285 | 295 | goto fclose; |
286 | 296 | } |
| 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, §or_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 |
287 | 315 | stat_buf.st_size = blk_size; |
288 | 316 | } |
289 | 317 |
|
|
0 commit comments