Parsing the Command Line and Initializing FUSE

You actually have to do very little command-line parsing: FUSE understands a large number of command-line arguments, and parses them itself. Most importantly, FUSE expects the mountpoint to be one of the members of the argv[] array.

I'm simplifying things for myself by assuming that the root directory and mount point will be the last two arguments on the command line, with the root directory preceding the mount point. So, my command line parsing happens in two short sections. First, I do some sanity checking: I make sure I've got enough arguments on the command line, and that the last two arguments aren't options.

if ((argc < 3) || (argv[argc-2][0] == '-') || (argv[argc-1][0] == '-'))

A few lines later, I pull the root directory out of the argument list, use the C library realpath() function to translate it to a canonicalized absolute pathname, and save it in my private data (I'll be describing private data in the next section)

bb_data->rootdir = realpath(argv[argc-2], NULL);
argv[argc-2] = argv[argc-1];
argv[argc-1] = NULL;

The last thing I do before turning control over to the FUSE library is to open the log file, and save its stream pointer in my private data as well.

Once I'm ready to start the filesystem, I call fuse_main(). Its parameters are argc and argv (as modified by my main() function), the bb_oper struct containing pointers to my re-implementations of the POSIX file operations, and a struct bb_data, used for storing private data.

fuse_main() parses the rest of the command line, mounts the directory specified on the command line, and performs other initializations. Then, it calls an initialization function to perform any initialization defined by my code. bb_oper->init points to my bb_init() function, so it is what gets called. My bb_init() function is really small; all it does is log the fact that it has been called.

Next: Private Data

Last modified: Thu Jun 12 18:13:36 MDT 2014