From 906c0186bc0e47f897a2912d3941940a8bec58a5 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Thu, 25 Feb 2016 22:40:48 -0800 Subject: [PATCH] Get path size with stat or ioctl If we're making a file system in a real device then we need to get the device size with an ioctl. --- utils/src/dev.c | 42 ++++++++++++++++++++++++++++++++++++++++++ utils/src/dev.h | 6 ++++++ utils/src/mkfs.c | 9 +++++---- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 utils/src/dev.c create mode 100644 utils/src/dev.h diff --git a/utils/src/dev.c b/utils/src/dev.c new file mode 100644 index 00000000..f1fdaada --- /dev/null +++ b/utils/src/dev.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sparse.h" +#include "dev.h" + +int device_size(char *path, int fd, u64 *size) +{ + struct stat st; + int ret; + + if (fstat(fd, &st)) { + ret = -errno; + fprintf(stderr, "failed to stat '%s': %s (%d)\n", + path, strerror(errno), errno); + return ret; + } + + if (S_ISREG(st.st_mode)) { + *size = st.st_size; + } else if (S_ISBLK(st.st_mode)) { + if (ioctl(fd, BLKGETSIZE64, size)) { + ret = -errno; + fprintf(stderr, "BLKGETSIZE64 failed '%s': %s (%d)\n", + path, strerror(errno), errno); + return ret; + } + } else { + fprintf(stderr, "path isn't regular or device file '%s'\n", + path); + return -EINVAL; + } + + return 0; +} + diff --git a/utils/src/dev.h b/utils/src/dev.h new file mode 100644 index 00000000..1fcf92fa --- /dev/null +++ b/utils/src/dev.h @@ -0,0 +1,6 @@ +#ifndef _DEV_H_ +#define _DEV_H_ + +int device_size(char *path, int fd, u64 *size); + +#endif diff --git a/utils/src/mkfs.c b/utils/src/mkfs.c index 36909acb..f42fbe04 100644 --- a/utils/src/mkfs.c +++ b/utils/src/mkfs.c @@ -16,6 +16,7 @@ #include "format.h" #include "crc.h" #include "rand.h" +#include "dev.h" /* * Update the block's header and write it out. @@ -52,8 +53,8 @@ static int write_new_fs(char *path, int fd) struct scoutfs_key root_key; struct timeval tv; char uuid_str[37]; - struct stat st; unsigned int i; + u64 size; u64 total_chunks; u64 blkno; void *buf; @@ -73,14 +74,14 @@ static int write_new_fs(char *path, int fd) goto out; } - if (fstat(fd, &st)) { - ret = -errno; + ret = device_size(path, fd, &size); + if (ret) { fprintf(stderr, "failed to stat '%s': %s (%d)\n", path, strerror(errno), errno); goto out; } - total_chunks = st.st_size >> SCOUTFS_CHUNK_SHIFT; + total_chunks = size >> SCOUTFS_CHUNK_SHIFT; root_key.inode = cpu_to_le64(SCOUTFS_ROOT_INO); root_key.type = SCOUTFS_INODE_KEY;