Skip to content

Commit edd6b33

Browse files
committed
Add accessors for st_blocks and st_blksize
1 parent 064a287 commit edd6b33

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

System/Posix/Files.hsc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module System.Posix.Files (
5959
isBlockDevice, isCharacterDevice, isNamedPipe, isRegularFile,
6060
isDirectory, isSymbolicLink, isSocket,
6161

62+
fileBlockSize,
63+
fileBlocks,
64+
6265
-- * Creation
6366
createNamedPipe,
6467
createDevice,

System/Posix/Files/ByteString.hsc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module System.Posix.Files.ByteString (
5959
isBlockDevice, isCharacterDevice, isNamedPipe, isRegularFile,
6060
isDirectory, isSymbolicLink, isSocket,
6161

62+
fileBlockSize,
63+
fileBlocks,
64+
6265
-- * Creation
6366
createNamedPipe,
6467
createDevice,

System/Posix/Files/Common.hsc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ module System.Posix.Files.Common (
5656
isBlockDevice, isCharacterDevice, isNamedPipe, isRegularFile,
5757
isDirectory, isSymbolicLink, isSocket,
5858

59+
fileBlockSize,
60+
fileBlocks,
61+
5962
-- * Setting file sizes
6063
setFdSize,
6164

@@ -255,6 +258,14 @@ specialDeviceID :: FileStatus -> DeviceID
255258
-- | Size of the file in bytes. If this file is a symbolic link the size is
256259
-- the length of the pathname it contains.
257260
fileSize :: FileStatus -> FileOffset
261+
-- | Number of blocks allocated for this file, in units of
262+
-- 512-bytes. Returns @Nothing@ if @st_blocks@ is not supported on this
263+
-- platform.
264+
fileBlocks :: FileStatus -> Maybe CBlkCnt
265+
-- | Gives the preferred block size for efficient filesystem I/O in
266+
-- bytes. Returns @Nothing@ if @st_blocksize@ is not supported on this
267+
-- platform.
268+
fileBlockSize :: FileStatus -> Maybe CBlkSize
258269
-- | Time of last access.
259270
accessTime :: FileStatus -> EpochTime
260271
-- | Time of last access in sub-second resolution. Depends on the timestamp resolution of the
@@ -294,6 +305,19 @@ modificationTime (FileStatus stat) =
294305
statusChangeTime (FileStatus stat) =
295306
unsafePerformIO $ withForeignPtr stat $ (#peek struct stat, st_ctime)
296307

308+
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
309+
fileBlocks (FileStatus stat) =
310+
Just $ unsafePerformIO $ withForeignPtr stat $ (#peek struct stat, st_blocks)
311+
#else
312+
fileBlocks _ = Nothing
313+
#endif
314+
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
315+
fileBlockSize (FileStatus stat) =
316+
Just $ unsafePerformIO $ withForeignPtr stat $ (#peek struct stat, st_blksize)
317+
#else
318+
fileBlockSize _ = Nothing
319+
#endif
320+
297321
accessTimeHiRes (FileStatus stat) =
298322
unsafePerformIO $ withForeignPtr stat $ \stat_ptr -> do
299323
sec <- (#peek struct stat, st_atime) stat_ptr :: IO EpochTime

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ AC_CHECK_MEMBERS([struct stat.st_uatime])
6666
AC_CHECK_MEMBERS([struct stat.st_umtime])
6767
AC_CHECK_MEMBERS([struct stat.st_uctime])
6868

69+
AC_CHECK_MEMBERS([struct stat.st_blocks])
70+
AC_CHECK_MEMBERS([struct stat.st_blksize])
71+
6972
AC_CHECK_MEMBER([struct passwd.pw_gecos], [], [AC_DEFINE([HAVE_NO_PASSWD_PW_GECOS],[],[Ignore the pw_gecos member of passwd where it does not exist])], [[#include <pwd.h>]])
7073

7174
# Functions for changing file timestamps

0 commit comments

Comments
 (0)