Disclaimer: I haven't tested any of the assumptions made in the following answer
As per @user6269400, the main issue of using C++ class methods for the FUSE operations, is what is the this
pointer pointing to - or more to the point, how do we get class methods implementing FUSE operations to be called in the correct context, i.e. as method calls on the C++ class instance.
An easy answer to this is to use the FUSE context's private_data
field to store a pointer to the instance and then use some glue code to cast it to your class type.
A "library" (and I use the term very loosely here) implementing (some of) the glue code for a C++ FUSE implementation can be found here: https://github.com/jachappell/Fusepp . The way it works is by hooking correctly named non-static class methods to the FUSE operations table, then initializing FUSE with the class instance address as the "private_data
". As @syam alluded to, doing this will have FUSE invoke your class methods with incorrect (possibly null
) this
address and the result will be a very unhappy C++ code. Fusepp tries to solve this by providing a this_()
method that does the above mentioned casting for you, so your code might look like this:
const bool ExFuse::resolve_path(const std::string &path, struct stat* stbuf) {
// ...
}
int ExFuse::getattr(const char *path, struct stat *stbuf, struct fuse_file_info *)
{
memset(stbuf, 0, sizeof(struct stat));
if (!this_()->resolve_path(stbuf))
return -ENOENT;
return 0;
}
So you must use this_()
to access your instance members and there is no implicit this
- if you try to call resolve_path()
directly (without dereferncing through this_()
) you'd get undefined behavior (which at best will be a segmentation fault).
Just so we are clear - I don't recommend using FusePP: the resulting code will not be C++ but will look similar enough to C++ to fool compilers and humans with possibly catastrophic results. But taking the same approach and using a bit more glue code to intercept a FUSE operations call, cast the context private_data
to a class instance pointer, then call the C++ method through the instance pointer would be very workable, I believe.