Apologies if this is a bit incoherent or unnecessarily verbose, I started this from 0 a couple days ago and my head is spinning a little bit trying to sort out everything I’m learning, and filter out the parts where I confused myself
tl;dr, I want to know if std.os.linux.statx follows symlinks, and if not, how do I get around using realpathfor the path argument of statx?
I have been looking at a moderately sized, ~15 year old C program and seeing how I might accomplish what it does in Zig as a fun learning project. At a particular part in this program, it does a few things:
- Uses
realpathto get the path of a device file (so as to remove symlinks, which isn’t an uncommon occurrence for this program) - Runs
staton the device file to confirm that it is either a character device or block device - Gets the major ID from the
rdevfield of thestat, which will be switched upon later - Opens the file, and runs
ioctl(depending on the switch branch)
Most of these are not issues. My plan was to do the following:
- Open the file with
std.fs.Dir.openFile, (from what I understand, this will follow symlinks, and so should be the actual device file?) - Use
std.os.linux.statxto get therdevof the file, because it isn’t returned with a normalstd.fs.Dir.stat - Use
std.fs.File.Stat.fromLinux()and inspectStat.Kindto determine if it is a character/block device - Use
File.handleforioctl
but the statx part isn’t so easy, as that requires the path to the device file, but we may have a symlink. Thus, my question: does statx follow symlinks? If not, I haven’t been able to work out a solution that doesn’t use realpath, but every discussion that includes realpath as a topic highly discourages its use, so I feel a bit stuck.