3 ms·
A statx(2) Linux system Python wrapper
- chriskaneen 6y agoAt my previous job, I needed to access the birth time attribute of a file on Linux. If I am not mistaken, the standard library doesn't support getting it on Linux (st_birthtime - time of file creation is supported on other unix systems, such as freebsd, see: https://docs.python.org/2/library/os.html#os.stat https://docs.python.org/2/library/os.html#os.stat). So, I created this wrapper for getting the file status (birth time, etc.) using the statx(2) linux system call.
- cmeacham98 6y agoWhy not use the statx wrapper in glibc, seeing as you already seem to be using the syscall wrapper via ctypes.CDLL(None).syscall? Possible self-answer: that wrapper only exists on glibc >= 2.28.
- LukeShu 6y agoFor reference for other readers: The statx(2) system call was added in Linux 4.11, released on 2017-04-30. The statx(3) libc wrapper was added in GNU libc 2.28, released on 2018-08-01.
- ddevault 6y agoI don't really understand what this is doing here. It definitely does not pique the hacker interest. This is just a simple syscall wrapper. The most interesting thing about it is that it somehow got upvoted 14 times. Maybe it's just general ignorance of the statx syscall's existence or utility on Linux? https://man7.org/linux/man-pages/man2/statx.2.html https://man7.org/linux/man-pages/man2/statx.2.html It's just stat but it accepts a mask for what info you want and returns a couple of extra fields.
- tyingq 6y agoNeat, though the if/then tree to get the syscall number is a bit clunky and only works on x86, x64, and ARM. I get that options are limited, but it might be nice to make that list more complete.
- chriskaneen 6y agoYou are right.
- jwilk 6y agoNo test suite. Also no error handling. I think it's a bit too early to show this to HN…
- andy_threos_io 6y agoThere are other more portable way to do OS wrapper, and maybe faster (but it's not an issue). Just write the wrapper as a C extension module, and in the module make the system call with proper library call. like in the following code: https://github.com/torvalds/linux/blob/master/samples/vfs/test-statx.c https://github.com/torvalds/linux/blob/master/samples/vfs/te... This way it will be portable! A couple of weeks ago, I just made for our OS's system calls. It's only a first draft, but works fine. You could also parse the C header for constant values. (Actually, I wrote a python script to generate the C extension for the system calls also, not just for the constants)
- miguelmurca 6y agoI'm positive I've done this before with the built-in os module
- unmole 6y ago[Shameless Plug] A blog post I wrote about file creation time in Linux: https://www.anmolsarma.in/post/linux-file-creation-time/ https://www.anmolsarma.in/post/linux-file-creation-time/
- chriskaneen 6y agoInteresting summary. I also like your renders.