vlang/vscode-vlang

Why os module defines a limited set of fields of C struct stat (Linux has an issue)?

jetX11 opened this issue · 2 comments

// Uncommenting the following statement line reveals the issue (importing the os module has an
// unpleasant effect in Linux when I try to use the full feature of C struct stat).
// It works fine when os module is not imported.
// Why the designers (of os module) assumed that the other fields of C struct stat are not required: e.g, st_ino, st_uid, ...

// import os

#include <sys/stat.h>

struct C.stat {
st_dev i64
st_ino i64
st_mode i64
st_uid i64
st_gid i64
st_mtime i64
st_size i64
}

fn main() {
data := C.stat{}

// I tried a presumably existing /dev/tty0 device file to test it on the playground:
code := unsafe { C.stat(c'/dev/tty0', &data) }
println('code: $code')
println('data: $data')

}

Because that's what's used by os.

You can submit a simple PR with missing fields.

Thank you for the reply. That's true. In GNU C library, these fields are available (https://man7.org/linux/man-pages/man2/stat.2.html) :

struct stat {
dev_t st_dev; /* ID of device containing file /
ino_t st_ino; /
Inode number /
mode_t st_mode; /
File type and mode /
nlink_t st_nlink; /
Number of hard links /
uid_t st_uid; /
User ID of owner /
gid_t st_gid; /
Group ID of owner /
dev_t st_rdev; /
Device ID (if special file) /
off_t st_size; /
Total size, in bytes /
blksize_t st_blksize; /
Block size for filesystem I/O /
blkcnt_t st_blocks; /
Number of 512B blocks allocated */

   /* Since Linux 2.6, the kernel supports nanosecond
      precision for the following timestamp fields.
      For the details before Linux 2.6, see NOTES. */

   struct timespec st_atim;  /* Time of last access */
   struct timespec st_mtim;  /* Time of last modification */
   struct timespec st_ctim;  /* Time of last status change */

#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

I am using many of these fields in python (os and pathlib modules) quite often. In vlib (install-path-base/vlib/os/os.c.v), most of these fields are absent:

...
struct C.stat {
st_size u64
st_mode u32
st_mtime int
}

and only these three fields are mentioned (why? that was my question). Anyway, I noticed that this issue is specific to my system, and in V playground, if I redefine struct C.stat with the matching field types (for example u64 instead of i64 for st_size) and mentioning the other fields (like st_uid, st_gid, ...), I can access the other fields as well. For some reason, it didn't work on my instance of Ubuntu (18.04 on a very old computer) and my program sticks to the original definition of struct C.stat (with those three fields) and gives compile error for the other fields; that is, ignoring my redefinition of struct C.stat.

I am still verifying the circumstance under which it could produce an issue anywhere including the V playground (spotting the issue). I could temporarily fix the issue by modifying the os.c.v to find out that it is where the issue in my system happens. But, I couldn't find yet why it sometimes works and sometimes it doesn't work. The definition of struct C.stat in os module is the reason for sure. Again thank you.