I noticed that most file operations in hw/9pfs/virtio-9p-local.c
are modelled like this:

    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
       /* MAPPED version */
    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
       /* MAPPED_FILE version */
    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
               (fs_ctx->export_flags & V9FS_SM_NONE)) {
       /* PASSTHROUGH or NONE version */
    } else {
       /* this is my addition to show the "default" case */
       err = -1;
    }

I've 2 questions about this.

First, why we always have the "else" case, is it really
possible to have export_flags set in such a way so that
none of the 4 conditions are true?  If there's exactly
4 possible variants without the "else", it is possible
to simplify most of these functions greatly.

And second, why this code does not use a switch, why all
these ifs?  switch statement is good because the compiler
can check possible additional cases which are added later,
if that's the reason why we have this "else" to start with.
Something like this:

 switch (fs_ctx->export_flags & V9FS_SEC_MASK) {
   case V9FS_SM_MAPPED_FILE:
   ...
 }

(it is not that easy for the compiler to check this in
this form, so maybe security model should be moved from
export_flags to an additional field?)

Thanks,

/mjt

Reply via email to