Skip to content

Commit

Permalink
Do a better fix than 7dba4c0 (based on
Browse files Browse the repository at this point in the history
feedback from @metagn).  The value of this narrowing compatibility shim
vs. just making all use-sites use Statx seems suspect.
  • Loading branch information
c-blake committed Sep 7, 2024
1 parent fc13443 commit 976d459
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions cligen/statx.nim
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,21 @@ proc stat2statx(dst: var Statx, src: Stat) {.inline.} =
dst.stx_dev_major = src.st_dev.st_major.uint32
dst.stx_dev_minor = src.st_dev.st_minor.uint32

template tOr(expr, defl): untyped = # Nim compiles & mostly works on odd OSes,
try: expr # but range checks on OS API types still
except: defl # often causes trouble; -d:release helps.
proc st_blksize*(st: Statx): Blksize = tOr st.stx_blksize.Blksize, 0.Blksize
proc st_nlink*(st: Statx): Nlink = tOr st.stx_nlink.Nlink, 0.Nlink
proc st_uid*(st: Statx): Uid = tOr st.stx_uid.Uid, 0.Uid
proc st_gid*(st: Statx): Gid = tOr st.stx_gid.Gid, 0.Gid
proc st_mode*(st: Statx): Mode = tOr st.stx_mode.Mode, 0.Mode
proc st_ino*(st: Statx): Ino = tOr st.stx_ino.Ino, 0.Ino
proc st_size*(st: Statx): Off = tOr st.stx_size.Off, 0.Off
proc st_blocks*(st: Statx): Blkcnt = tOr st.stx_blocks.Blkcnt, 0.Blkcnt
proc st_blksize*(st: Statx): Blksize = cast[Blksize](st.stx_blksize)
proc st_nlink*(st: Statx): Nlink = cast[Nlink](st.stx_nlink)
proc st_uid*(st: Statx): Uid = cast[Uid](st.stx_uid)
proc st_gid*(st: Statx): Gid = cast[Gid](st.stx_gid)
proc st_mode*(st: Statx): Mode = cast[Mode](st.stx_mode)
proc st_ino*(st: Statx): Ino = cast[Ino](st.stx_ino)
proc st_size*(st: Statx): Off = cast[Off](st.stx_size)
proc st_blocks*(st: Statx): Blkcnt = cast[Blkcnt](st.stx_blocks)
proc st_atim*(st: Statx): Timespec = st.stx_atime.toTimespec
proc st_ctim*(st: Statx): Timespec = st.stx_ctime.toTimespec
proc st_mtim*(st: Statx): Timespec = st.stx_mtime.toTimespec
proc st_rmaj*(st: Statx): Dev = tOr st.stx_rdev_major.Dev, 0.Dev
proc st_rmin*(st: Statx): Dev = tOr st.stx_rdev_minor.Dev, 0.Dev
proc st_dev*(st: Statx): Dev =
tOr (st.stx_dev_major shl 32 or st.stx_dev_minor).Dev, 0.Dev
proc st_rmaj*(st: Statx): Dev = cast[Dev](st.stx_rdev_major)
proc st_rmin*(st: Statx): Dev = cast[Dev](st.stx_rdev_minor)
proc st_dev*(st: Statx): Dev = # Best effort: this likely narrow-casts away info
cast[Dev](st.stx_dev_major.uint64 shl 32 or st.stx_dev_minor.uint64)
proc `st_nlink=`*(st: var Statx, n: Nlink) {.inline.} = st.stx_nlink = uint32(n)

proc st_btim*(st: Statx): Timespec {.inline.} = st.stx_btime.toTimespec
Expand Down

0 comments on commit 976d459

Please sign in to comment.