From 199063ab53cbae3b3be199ff902f9f2d001d1058 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 24 Jul 2018 19:16:27 -0400 Subject: stat: Don't assume blocks are 512 bytes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POSIX says > The unit for the st_blocks member of the stat structure is not defined > within POSIX.1‐2008. and recommends using DEV_BSIZE from if available. Also, for -printf '%S', print 1 instead of NaN for empty files with no blocks. --- stat.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'stat.h') diff --git a/stat.h b/stat.h index d47d248..95c61ec 100644 --- a/stat.h +++ b/stat.h @@ -17,7 +17,9 @@ #ifndef BFS_STAT_H #define BFS_STAT_H +#include #include +#include #include /** @@ -47,6 +49,14 @@ enum bfs_stat_flag { BFS_STAT_BROKEN_OK = 1 << 0, }; +#ifdef DEV_BSIZE +# define BFS_STAT_BLKSIZE DEV_BSIZE +#elif defined(S_BLKSIZE) +# define BFS_STAT_BLKSIZE S_BLKSIZE +#else +# define BFS_STAT_BLKSIZE 512 +#endif + /** * Facade over struct stat. */ @@ -68,7 +78,7 @@ struct bfs_stat { uid_t uid; /** File size in bytes. */ off_t size; - /** Number of 512B blocks allocated. */ + /** Number of disk blocks allocated (of size BFS_STAT_BLKSIZE). */ blkcnt_t blocks; /** Access time. */ -- cgit v1.2.3