> I try to write a new xlator by using glupy xlator
> I try to translate gf_dirent_t structure to python class
> it looks like
> ------------------------------------------------------------
> c code structure:
> struct _gf_dirent_t {
> union {
> struct list_head list;
> struct {
> struct _gf_dirent_t *next;
> struct _gf_dirent_t *prev;
> };
> };
> uint64_t d_ino;
> uint64_t d_off;
> uint32_t d_len;
> uint32_t d_type;
> struct iatt d_stat;
> dict_t *dict;
> inode_t *inode;
> char d_name[];
> };
> -------------------------------------------------------------------
> python code class:
> class gf_dirent_t (Structure):
> pass
> class dirent_struct (Structure):
> _fields_ = [
> ("next", POINTER(gf_dirent_t)),
> ("prev", POINTER(gf_dirent_t))
> ]
> class dirent_union (Union):
> _fields_ = [
> ("list", list_head),
> ("dirents", dirent_struct)
> ]
> gf_dirent_t._fields_ = [
> ("dirent_list", dirent_union),
> ("d_ino",c_uint64),
> ("d_off",c_uint64),
> ("d_len",c_uint32),
> ("d_type", c_uint32),
> ("d_stat", iatt_t),
> ("dict", POINTER(dict_t)),
> ("inode", POINTER(inode_t)),
> ("d_name", c_char_p)
> ]
> -----------------------------------------------------------------------
> then I print the d_len variable of gf_dirent_t class (in python)
> but it is different with the d_len variable of _gf_dirent_t (in c code)
> the value in python is 3386792520
> the value in c is 1
> anyone can tell me the python class I write is correct or not?
> what's the problem?
At first glance, your declarations look correct, except that the char
pointer at the end should probably be a char array. However, there are
all sorts of hidden pitfalls around structure padding and int sizes.
The first thing I'd do is look at whether d_off and d_ino also look
wrong. If they are, the problem's somewhere in dirent_union; if they're
not, it's something else. Either way, you should carefully check how
that structure is laid out *for your platform and compile flags*. One
way to do this would be to create one with recognizable contents in a C
module, load that module into Python using ctypes, and then look at the
structure both *as* a structure and as raw bytes from within Python.
Good luck!